How Long since something changed
Home Assistant Template
Sometimes you want to check how long a door or window has been opened/closed or something has been off/on.
{{ ((as_timestamp(now()) - as_timestamp(states.binary_sensor.patio_doors_on_off.last_updated)) / 60) | round(0) }}
In the example above it will return the number of minutes an item has been at a given state.
The following could be used to create a template sensor, to return the number of minutes since a door was opened, or 0 if the door is currently opened.
{% if states('binary_sensor.patio_doors_on_off') == 'on' %} 0 {% else %} {{ ((as_timestamp(now()) - as_timestamp(states.binary_sensor.patio_doors_on_off.last_updated)) / 60) | round(0) }} {% endif %}