Presence detection condition door opened/closed during last 5 min

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?

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

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

Make a binary sensor with an off delay


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