[SOLVED]How to handle a sensor that returns STR instead of INT/ FLOAT?

Hey everyone,

I have several xiaomi Zigbee plugs that also report consumption.

Unfortunately the values reported are handled as string and not integers or floats.

This results in history showing as value bars instead of graphs:

I can of course create a template sensor to change it into a float and work with that. I tried it and it does work but, is there some way to manipulate the original sensor and have HA treat it as a number, in order to avoid having multiple ?

What’s the correct course of action here?

I also tried customizing the entity and adding a unit of measurement (W) and a device class (Energy) in hopes HA would treat it as a float without luck.

Thanks in advance

All states are strings, strictly. Adding a unit_of_measurement should do the job, although I don’t know if that works via customisation. Can you paste a screenshot of the Developer Tools / States screen for one of these sensors, and the result of this in Developer Tools / Templates?

{{ states('sensor.YOUR_SENSOR_NAME') }}
{{ states('sensor.YOUR_SENSOR_NAME')|float }}

It might just be easier to create a template sensor if that does what you want. Depends how much you value your time.

1 Like

First thing i did, let me restart.

That was it, I was just eager enough to post for help before restarting my instance.
Thanks a ton :slight_smile:

Went back and customized all my meters, device_class: power, unit_of_measurement: W and icon: mdi:flash. Now they are all nice and tidy.

1 Like