Only trigger automation if restart time is above 5 minutes

I have one sensor that tends to restart due to power loss.
There is automation triggering due to the reboot.
I’d like to set up a condition in the automation, telling it to NOT fire in case the last reboot time is below 5 minutes from current time.
I can see there are conditions in the automation, one of them (Value of a date/time helper or timestamp-class sensor) seems to be like the thing I am looking for, but I cannot formulate it properly.
The current automation (without the time condition) looks like this:

alias: Garage door opened notification
description: ""
trigger:
  - type: turned_on
    platform: device
    device_id: XXXXXX
    entity_id: binary_sensor.switch1
    domain: binary_sensor
condition: []
action:
  - service: telegram_bot.send_message
    data:
      message: Garage doors opened at {{ now().strftime('%H:%M') }}
      target: XXXXXX
      disable_web_page_preview: true
mode: single

condition:
  - "{{ (now() - as_datetime(states('sensor.uptime'))).total_seconds() > 300 }}"

Thanks for the tip, but it does not seem to work. For some reason, even if the sensor restarted, it still reports ~5 minutes ago - seems like it counts restart time of home assistant, not of the sensor.

Oh! sorry, didn’t read your post carefully enough. My fault.

As I doubt you have any entities recording the sensor reboot time, take a look at the sensor history and the automation trace. Is it being incorrectly triggered by a change from unavailable or unknown to on? If so, rewrite your trigger like this, getting rid of all the device cruft:

trigger:
  - platform: state
    entity_id: binary_sensor.switch1
    from: 'off'
    to: 'on'

(and remove my HA restart condition).

That way, it will only trigger if it goes on from a prior state of off.

Here’s an automation trace from my system, showing a binary sensor going from off to on:

1 Like