Template help, x minutes since door opened

I have a motion sensor group and want to add my door sensors to it - so the behaviour I’m looking for is to have a motion group on if a motion sensor is activated, or if any door is opened. That of course requires a time period - so I’m looking to create a template binary sensor that is on if a door is opened/closed and then stays on for X minutes before switching off if the door hasn’t opened/closed again in the meantime. Is it possible to have a time cutoff like this in a template? Thanks!

Template binary sensors have delay_on and delay_off options. That seems to be what you want.

1 Like

Thanks @tom_l . I don’t think delay_off will work for me because I want the sensor to switch off 2 minutes after the door state changes, rather than after it is closed / opened. Auto_off might work though. Maybe auto_off would work? What I’m looking for is to have the binary sensor switch on if the door state is changed and then switch off after 2 mins. But that 2 mins should reset if the door state changes again before the 2 min timer has finished.

Maybe taras’ approach comes in consideration for you? Your sensor could look like this:

template:
  - binary_sensor:
      - name: 'last door'
        unique_id: 202209302036
        state: |-
          {%- set group = expand('group.YOUR_GROUP')
            |sort(attribute='last_changed', reverse=true) %}
          {%- set x = group |selectattr('state','eq','on') |list %}

          {% if x|count != 0 %}
          {%- set time = x[0].last_changed %}
          {{ 'on' if (now() - time) <= timedelta(minutes=2) else 'off' }}
          {% else %}
          {% endif %}

Thanks, I’ll give it a go!