When adding entities in lovelace Home Assistant automatically formats them as localized strings with the unit of measurement based on the device class etc. To name a few examples:
on/off states in switches are localized
Numeric values are shown as 5% or 20°C based on their unit_of_measurement
DateTimes in the past are shown as “X minutes ago”
DateTimes in the future are shown as “In X minutes”
Enum values (even from 3rd party integrations) are shown with their corresponding friendly names
This is super amazing, because it prevents you from formatting every sensor again and again. How can this be achieved in templates? Can it be achieved? I really hope so.
Currently, when I have a sensor and I display it in a template, or a custom card:
the following is shown as 20: {{ states('sensor.room_temperature') }}
Ofcourse I could fix this by doing: {{ states('sensor.room_temperature') }}°C
Or I could even go as far as trying to retrieve the unit_of_measurement and append that.
It would be a lot easier if I could get the values as they’re represented by Home Assistant itself. Is there something to format a sensor so the output becomes like that? I’ve searched around in the documentation and on the forum, but i can’t find any examples that do this. (So I fear the worst).
Especially with Enum values from 3rd party integration, it is a hassle to create large if statements in order to format the correct value.
So I continued my search and found: {{ states.sensor.room_temperature.state_with_unit }} which kinda gives me what I want. Two caveats:
It does not localize. So in my situation it shows as 24,59 °C instead of 24,59 °C, which for this unit of measurement isn’t the worst but for others would be more annoying.
According to the docs, using states. isn’t recommended, but states() and state_attr() are recommended instead. It seems there is not states_with_unit() equivelant.
The unit should be available as an attribute in all sensors, that have set a unit. Doesn’t matter which unit, it is always set. But I’m not sure what you mean with localization. °C or m for meter or % for humidity should be independent from a localization. If you use °F in your setup, temperature should be shown in Fahrenheit.
So to use the state_attr: {{ state_attr('sensor.temperature', 'unit_of_measurement') }}
Yeah i know about unit_of_measurement, but some sensors don’t have it. Also, I would like to prevent from using this large line of code everywhere in your example just to get 24,59 °C: {{ states('sensor.temperature') }} {{ state_attr('sensor.temperature', 'unit_of_measurement') }}
Simple example of a state that does not use unit of measurement: {{ states('person.me') }} returns home, but I like it to return “Thuis” (which is Dutch for home) like the UI itself does.
Meanwhile I’m searching in the source code, it seems that hass.localize() does the trick. I just can’t find a template function that exposes this method.
Edit: also tried {{ states('person.me') | localize }} but is also doesn’t seem to be a filter.