Help! Set template binary sensor true if another sensor is on for x minutes

I know I can trigger an automation when a sensor is on for some period of time. But I would like to create a template binary sensor that is resolves to true if another sensor has been in an on state for x minutes.

Here is pseudo code, with the intention of turning on My Sensor when some other sensor has been on for 5 minutes. Can this be done? Easily?

    my_sensor:
      friendly_name: "My Sensor"
      device_class: power
      value_template: >-
        {{ states('sensor.some_other_sensor') == 'on' for '00:05:00'  }}

You can’t use for in templates, but you can add a time comparison between now and the last time the sensor changed:

      value_template: >-
        {{ states('sensor.some_other_sensor') == 'on' and
        now() >=  states.sensor.some_other_sensor.last_changed + timedelta(minutes = 5) }}
2 Likes

This looks promising. I will give it a try. Thank you!