Hi!
I’ve been getting into home assistant templating, and one of the issues I’ve run into has been that sensors report their units with inconsist dimensionality, e.g. my mains power meter reports consumption in W, while the Zigbee meter I have installed reports it in kW. I honestly can’t really keep track of which does which, and unfortunately I found that when converting to float to do calculations, the values aren’t normalized, so 1.5 kW becomes 1.5 rather than 1500.
I’m sure I’m a noob and there’s a better way of handling this, what would you suggest the best approach would be, so I don’t have to care which sensor has decided to prefix a ‘kilo’ or ‘milli’.
This should be under another category than Development. Maybe configuration instead.
If you compare your sensor visually, then you should make templates that convert the values to a common one.
If you make automatic calculations, then you can convert in the formulas used.
I get that, but imo when doing math with all sensors of type ‘Power’ there should be a way to make sure you can take the values no matter what prefix is set for the sensor.
I’m not using templates to change the units btw, I’m just consuming the output of the sensor, and noticed that the zigbee power meter gives the unit in kW, while the utility meter I have gives it in W. I shouldn’t have to keep track of what unit prefix the sensor’s designers have picked, I’m sure there’s functionality that can correctly add ‘kW’ units to ‘W’
Your case might be that like that.
Other users want it in kW and the other want it in mW.
Somenwant a mix of it, because the input power for a house makes sense in kW or W, but the input power for the smart watch charger only makes sense in mW.
And then there is the sensors that report 1500 KW, but should have been 1500W, which is something the vendors of those devices should fix, but because they are from Chinese week-life shops, then no support exist.
A correctly configured sensor has unit of measure a d possibly device class and state class in the attributes of the sensor itself. So you don’t have to ‘keep track of anything’ just query the semsor, confirm the unit_of_measure, do your math…
See the sensor in developer tools to see the attributes.
It feels a bit unfortunate that HA doesn’t have standard units for storage and then converts what the sensor reports to that, and also a separate unit for display.
Granted, that would be a big change – what does states() show? Would automations always need to work in the base unit, for example? (It’s what we do for timestamps, of course.)
If I change from ºF to ºC it looks like my deck was really hot lately…