Is there a way to return the state’s description, rather than “off” or “on” for binary sensors?
I’m trying to return my smoke detector’s status as “Detected” or “Clear” (as shown in the UI, based on the sensor’s device_class) rather than “on” or “off” as {{state('sensor.nest_protect_smoke_status')}} returns.
I realize I could create template sensors that store the text of each sensor’s state, but I would prefer a solution that is simpler than creating such a template for every sensor I’d like to do this for.
The backend (the State Machine) stores a binary_sensor’s states as on/off.
The frontend (the UI) uses the value of the binary_sensor’s device_class (if it exists) to display the on/off states differently (like Detected/Clear).
Depending on the binary_sensor’s integration, it may be possible to change the value of its device_class (thereby influencing the appearance of its reported states in the UI) but you cannot change the binary_sensor’s on/off states.
That’s what you’ll need to do if want the entity’s actual state values to be Detected/Clear.
That’s what I feared. I was hoping there was a function/feature I could use in a template that translated a state’s value (on/off) to the values the UI uses based on the device_class.
The translation of an entity’s state value to a display value, based on device_class and chosen language, is performed by the front-end whereas Jinja2 functions operate in the backend.
In other words, even if you create a Template Sensor it won’t have access to the conversion/translation maps that the front-end uses. You’ll have to hard-code the maps yourself.
Maybe there’s a way do it with a custom card that supports Javascript templating … but I’m just speculating (I have no experience with that).