Presence detection condition door opened/closed during last 5 min

Hi,
Trying to set up an automation to turn off things when I leave home. I’m using iPhone Device Tracker integration to ping phones, but from time to time the phones go into deep sleep and HA will turn off everything.

I would like the automation to trigger only if the door was also used during the last 5 minutes (door sensor went from on to off during the last 5 minutes?).
Unfortunately, I found no examples for this scenario…
Is there a way to achieve this?
Thanks

 alias: Turn off things when I leave home
 trigger:
  - platform: state
    entity_id: device_tracker.hostname2
    from: home
    to: not_home

  condition: 
  - platform: state
    entity_id: binary_sensor.sonoff_1000fd4184
    from: 'on'
    to: 'off'
      for:           ???????
        minutes: 5  ???????

Make a binary sensor with an off delay

2 Likes

Oh, you’re right!
I should start the other way around, with the door sensor.
Thank you