How to delay an automation after it triggers the first time

Good Evening, I have made automation for my unifi protect doorbell, when motion is detected it sends an announcement to amazon Alexa. The only issue I am having is that the automation is triggering multiply times. Alexa says “someone is at the front door” three times on one person at the front door. Is there a way to set a delay from the first time it triggers to the next time it triggers? Here is my current code.

  alias: Someone is at the front door
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_1_doorbell
    for:
      hours: 0
      minutes: 0
      seconds: 0
  condition:
  - condition: trigger
    id: ''
  action:
  - service: notify.alexa_media_dinning_rm_echo_dot
    data:
      message: Someone is at the front door.
      target: notify.alexa_media_dinning_rm_echo_dot
      data:
        type: tts
  mode: single

Thanks,
Justin

I’d add a delay at the end for 1 minute, so that the automation doesn’t end immediately add max_exceeded: silent under mode so that it won’t spam the log with warnings that the automation is already running.

Alternatively something like this:

{{ (now()|as_timestamp - states.automation.my_automation_name.attributes.last_triggered|as_timestamp)|int > 60 }}

as a condition (template) of the automation, replace my_automation_name with the actual name of the your automation, and 60 with the number of seconds you want to delay the automation for. This will prevent the automation from running, if the time it was last triggered is LESS than the number of seconds you replace 60 with.

2 Likes

As of 2/8/2024 this would be written as:

{{ (now()|as_timestamp - state_attr(‘automation.000’, ‘last_triggered’)|as_timestamp)|int > 60 }}

replacing 000 your automation name.

I’d argue that just adding a wait at the end of the automation is a bit more obvious and self documenting.

Alternatively you can keep everything as a datetime object. I think it makes the code easier to read:

{{ now() - state_attr('automation.xxx', 'last_triggered') > timedelta(minutes=1)  }}

This method makes more sense than adding a delay at the end if you have long wait times (hours or more). This way the automation isn’t left running the entire time and isn’t affected by HA restarts. You can also do more complicated things with the code like “only run once per day”.