martinarva / dynamic_energy_cost

35 stars 9 forks source link

Enhancement: Set the sensor class to MONETARY #13

Closed whi-tw closed 1 month ago

whi-tw commented 2 months ago

Is your feature request related to a problem? Please describe.

Currently, the sensors are 'simple' floating point values. This has the effect of:

  1. Preventing Hass from understanding it's a monetary thing, meaning it can't be autodiscovered by other entities looking for this kind of sensor
  2. Not displaying with a currency symbol in dashboards
  3. Displaying with 15 decimal places

Describe the solution you'd like

It'd be grand if the integration created the sensor entities as SensorDeviceClass.MONETARY. This would fix all of the above issues.

Describe alternatives you've considered

Additional context

The ha_hildebrand_glow_idh_mqtt integration has some prior art for this:

https://github.com/megakid/ha_hildebrand_glow_ihd_mqtt/blob/main/custom_components/hildebrand_glow_ihd_mqtt/sensor.py#L153-L161

This integration: image

Rendering of the other integration I mentioned: image

martinarva commented 2 months ago

Thanks for the great idea. It's done in newest release: https://github.com/martinarva/dynamic_energy_cost/releases/tag/v0.3.0

Also did a reset service which was missing.

whi-tw commented 2 months ago

Updated, data all migrated over nicely. One issue: currency detection seems to have given up:

image

I haven't looked into how you've implemented the new sensor class (and it's been a hot minute since I last played around with hass addons), but I wonder: Is it possible to directly pull the currency from the 'source' cost/unit sensor? It may be that you're already doing that, but if not, it would probably resolve any potential 'weird' locale issues (eg. system currency = $, energy billed in £)

Edit: Thinking about it, this could be a migration issue for me, perhaps recreating the devices would work to reset the currency. maybe?

martinarva commented 2 months ago

I think I know what went. I'll check it in the evening.

whi-tw commented 2 months ago

Star, thank you! No rush: fortunately this isn't mission critical info, and at least it's not actually doing a currency conversion :D

martinarva commented 2 months ago

Actually it should take the unit of measurement from the price_sensor

    def get_currency(self):
        """Extract the currency from the unit of measurement of the price sensor."""
        price_entity = self.hass.states.get(self._price_sensor_id)
        if price_entity and price_entity.attributes.get('unit_of_measurement'):
            currency = price_entity.attributes['unit_of_measurement'].split('/')[0].strip()
            _LOGGER.debug(f"Extracted currency '{currency}' from unit of measurement '{price_entity.attributes['unit_of_measurement']}'.")
            return currency
        else:
            _LOGGER.warning(f"Unit of measurement not available or invalid for sensor {self._price_sensor_id}, defaulting to 'EUR'.")
        return 'EUR'  # Default to EUR if not found

Please turn debug logs on and see if it gets the currency

EDIT: Are you using it with energy (kwh) sensor? I actually haven't done the currency part in power (w) based sensors.

Actually I plan do discontinue the power based sensors logic, because maintaining both is time consuming and 99% cases there is a kwh sensor and it's more precise.

whi-tw commented 2 months ago

Eeenteresting.

So, I just enabled debug logs, reloaded the integration and the currency has updated:

core-1  | 2024-05-09 11:43:45.682 DEBUG (MainThread) [custom_components.dynamic_energy_cost.energy_based_sensors] Extracted currency 'GBP' from unit of measurement 'GBP/kWh'.

Unsure why it didn't work properly the first time around. I wonder if your integration started up before mqtt / my energy supplier integration, so the sensor currency hadn't properly been initialised? It'd make sense, as it would likely have defaulted to EUR in that case.

To your edit: yeah, using a kWh sensor (esphome energy monitor component on a sonoff POW iirc)

martinarva commented 2 months ago

I wonder if your integration started up before mqtt / my energy supplier integration, so the sensor currency hadn't properly been initialised? It'd make sense, as it would likely have defaulted to EUR in that case.

yes, this could be the case