Trigger state if previous state for 30 minutes

Hi there,

I want to trigger an automation if the previous state of a device (in my case a bulb) is off for at least 30 minutes and now changes to on.
I’ve found the following template condition in the forum:
{{ utcnow() - timedelta(seconds=30) < trigger.from_state.last_changed < utcnow() - timedelta(seconds=20) }}
Can this explain somebody to me please?

You would use the template in a template condition as follows:

  - platform: state
    entity_id: light.your_lightbulb
    to: 'on'
    from: 'off'
  - condition: template
    value_template: >
      {{ now() - trigger.from_state.last_changed >= timedelta(minutes=30) }}

Keep in mind that last_changed does not survive restart, so there may be occasional instances when the condition fails.


Thank you very much! That made my day!