Enhancement: Set the sensor class to MONETARY
whi-tw opened this issue · 7 comments
Is your feature request related to a problem? Please describe.
Currently, the sensors are 'simple' floating point values. This has the effect of:
- Preventing Hass from understanding it's a monetary thing, meaning it can't be autodiscovered by other entities looking for this kind of sensor
- Not displaying with a currency symbol in dashboards
- 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
- Manually setting the display precision (solves 3, not 1/2)
- Creating a template sensor to mutate the values created by the integration (solves all problems, tedious, miserable and manual to execute)
Additional context
The ha_hildebrand_glow_idh_mqtt
integration has some prior art for this:
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.
Updated, data all migrated over nicely. One issue: currency detection seems to have given up:
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?
I think I know what went. I'll check it in the evening.
Star, thank you! No rush: fortunately this isn't mission critical info, and at least it's not actually doing a currency conversion :D
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.
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)
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