How to get state value translated based on device class in templates?

Hi everyone, I’m relatively new to Home Assistant and struggling to get the “friendly” state values that seem to be based on the device class of the sensor. For example, I created a sensor to show the internet status like this:

binary_sensor:
  - platform: ping
    name: Internet #(or whatever you want to name it)
    host: 8.8.8.8
    scan_interval: 60

I want to display the state using a template but this code returns, for example “on” instead of “Connected” which you would see if you went to the Info tab of the entity.

 {{ states("binary_sensor.internet") }}

Is there a way to retrieve the more “friendly” version of the state values using templates?

Thanks!

@z06Drew

  1. Please use a proper formatting for a code - triple “`”, without “>” prefixes.

  2. As for “Connected”:

Assume that you have already defined an appropriate “device_class” for this sensor (here - “connectivity”).
Cards like “history-graph”, “entities” (as well as more-info popup) show a translated “nice” labels like “Connected / Disconnected”.
But in templates you will still get “on/off”:

But if you need to get these “Connected / Disconnected” labels - currently you will have to use this extension:
Github repo
Community thread

which gives you this:

Hope one day this functionality will become a part of HA core (check here).

2 Likes

@Ildar_Gabdullin , this worked great! Thank you very much for the guidance.

1 Like

For anyone reading this post March 2024… The new way to accomplish the same is 2024.3: Drag 'n Drop it like it's hot! 🎉 - Home Assistant

# Translated
{{ state_translated("binary_sensor.movement_backyard") }}  # Shows: Detected
{{ state_translated("sun.sun") }}  # Shows: Below horizon
1 Like