Binary_sensor template not updating

binary_sensor.garagedoorpi state changes from on to off successfully. However, my binary_sensor.garage_door state remains “Closed” and doesn’t change when garagedoorpi does.
Any suggestions?
thanks

binary_sensor:
  - platform: remote_rpi_gpio
    host: xxxx
    #pull_mode: UP
    invert_logic: true
    ports:
      17: garagedoorpi
  - platform: template
    sensors:
      garage_door_status:
        value_template: "{%if states.binary_sensor.garagedoorpi.state == 'Off' %}Closed{% elif states.binary_sensor.garagedoorpi.state == 'On' %}Open{% endif %}"
        friendly_name: 'Garage Door'
        device_class: garage_door

image

Try "{% if
That is with a space between % and if

The trick with all these things is to use the template editor to troubleshoot every time…

Alternatively this should work. Yours may not. Binary sensor templates expect true/false results.

  - platform: template
    sensors:
      garage_door_status:
        entity_id: binary_sensor.garagedoorpi.state
        value_template: "{{ is_state('binary_sensor.garagedoorpi.state', 'on') }}"
        friendly_name: 'Garage Door'
        device_class: garage_door

This template will evaluate to true/false:

"{{ is_state('binary_sensor.garagedoorpi.state', 'on') }}"

The device class will convert it to open/closed for display in the front end.

1 Like

Personally, I’d go with @tom_l’s solution. But the reason your template isn’t updating is because you’re checking against the states 'On' and 'Off'. Removing the capital O’s and replacing them with lowercase will make your template work. The second reason your template isn’t working is because binary sensors only accept true/false as an answer. You’re returning ‘closed’ and ‘open’. Both of those in the jinja and python world resolve to TRUE. So even if you fix the states, the template still won’t work as expected. Which is why you should go with @tom_l’s solution.

Whenever you are writing a template, you need to look at the states page. The states page will display the state of any sensor. What you see in the UI is a translated state. That typically means that the UI state isn’t the real state, it’s just a pretty version of the real state or a pretty translated version of the real state.

Is there an expected delay for the template to update? I made the suggested change and when I change my gpio state, binary_sensor.garagedoorpi changes instantly in Lovelace. However, the template doesn’t change states. It remains as “Closed”.

I also noticed that remote_rpi_gpio platform doesn’t support device_class. Seems like a possible enhancement.

binary_sensor:
  - platform: remote_rpi_gpio
    host: xxxxx
    #pull_mode: UP
    invert_logic: true
    ports:
      17: garagedoorpi
  - platform: template
    sensors:
      garage_door_status:
        value_template: "{{ is_state('binary_sensor.garagedoorpi.state', 'on') }}"
        friendly_name: 'Garage Door'
        device_class: garage_door

the template should be

"{{ is_state('binary_sensor.garagedoorpi', 'on') }}"

There was a typo in @tom_l’s template that we all overlooked

1 Like

yup, that works great. thanks