Trouble with template binary sensor

I’m trying to create a binary sensor that’ll be “on” once a garage door has been closed for > 5 min. The state of it is always “off” although I’ve checked my template in the template dev tool.

  - platform: template
    sensors: 
      garage_closed_awhile:
        friendly_name: "Garage door closed for a while"
        value_template: '{{ is_state("binary_sensor.garage_door_sensor","off") and as_timestamp(now()) -  as_timestamp(states.binary_sensor.garage_door_sensor.last_updated) > 300}}'

If I drop the template in the dev tool, it correctly switches from false to true.

What am I missing?

1 Like

I might be dreaming it, but I think you’ll need to use sensor.time instead of now() in that template, because it will only evaluate the ‘now’ when the state of the binary sensor changes (which will be 0 seconds), whereas sensor.time will be revaluated every second (or minute, or something).

It works in the template editor because it basically reprints the template every time you do something on the page.

I think.

Grave apologies if this sends you down the wrong path and I’m chatting bubbles, but some random incomplete memory of something similar is nagging at me.

Yup! I was coming around to a similar idea to what you’re saying. I ended up adding sensor.time as an entity_id and that appears to be working.

Edit: Adding binary_sensor.garage_door_sensor as a second entity_id improved things so that the sensor would go false as soon as the door opened.

1 Like