Use binary Sensor Device Class Specific State in Template

I’m trying to create what I thought would be a simple automation to notify me when a garage door is left open. I’ve created the two garage door sensors as shown below and they show Open/Closed in the UI, but On/Off in the states. I wanted to know if there is some method to query the state in the context of the device class.

I feel like {{states('binary_sensor.k_garage')}} should respect the device class and return Open/Closed, but instead I get On/Off. Is there a method to use the same state that the UI is using?

Sensors are setup like this:

binary_sensor:
  - platform: template
    sensors: 
      k_garage:
        friendly_name: "K Garage"
        device_class: garage_door
        value_template: "{{ is_state('sensor.garage_door_alarm_level_k', '255') }}"
      r_garage:
        friendly_name: "R Garage"
        device_class: garage_door
        value_template: "{{ is_state('sensor.garage_door_alarm_level_r', '255') }}"

Not that I know, the state of binary sensor is always on/off.
What are you trying to achieve?

I was trying to use the string in a notification and was hoping there was an easier way than to make an if statement for every condition. I wanted to use:

message: K garage is {{states('binary_sensor.k_garage')}}

in the notifier data, but of course that would say “K garage is on”

Specifying a device_class determines how the binary_sensor is depicted in the Lovelace UI. It controls the icon and the terminology used to describe the state values.

Regardless of a binary_sensor’s device_class, its state values are on/off. So if you make a template that checks the binary_sensor’s state, it should refer to values on and off (not Open/Closed, Detected/Clear, Wet/Dry, Unsafe/Safe, etc).

Yes, that is understood. I was asking for a method to query whatever Lovelace is looking at when displaying device-class specific states. I was trying to get to “open/closed” without writing convoluted logic to give me the string I want.

message: K garage is {{ 'Open' if is_state('binary_sensor.k_garage', 'on') else 'Closed' }}

That should get you the wording you want. :slight_smile:

1 Like

Thanks. I don’t think there’s a way to do what I want to do, this will have to do.

I was also looking for this and you can have a look at state_translate

1 Like