Is there a way to read units in a consistent manner?

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’.

What would your suggestion here be?

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.

Moved.

You don’t need templates to change the units. Just change it in the entity config.

Click on the entity, Cog icon, Unit of Measurement.

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…

And that’s what ends up in the state DB:

sqlite> select state, anything_updated from states_by_name where entity_name = 'sensor.backyard_3_temperature'  order by state_id desc limit 8;
state             anything_updated
----------------  -----------------------
16.1722222222222  2025-11-05 07:39:24.857
16.1722222222222  2025-11-05 07:38:24.851
16.1611111111111  2025-11-05 07:37:25.011
16.1277777777778  2025-11-05 07:30:14.440
61.03             2025-11-05 07:28:24.872
60.91             2025-11-05 07:19:24.942
60.88             2025-11-05 07:14:24.833
60.84             2025-11-05 07:13:24.847

No you’d have to write your own macro. FYI you can get the unit with this:

{{ states('sensor.my_power', with_unit=True) }}

You can not then do math because it has the unit string W or kW is appended to the value.

So for example a macro to convert all readings to W would have to:

  1. get the state with the unit
  2. determine if the unit contains a multiplier e.g. k (and W, if not return an error code)
  3. strip the unit from the value and multiply by the multiplier (e.g. 1000 if it had the prefix k)
  4. return the result
1 Like

This custom integration does exactly what you want: Automatic unit conversion