Automation using wait_template continues if trigger happens again while waiting

I have the following automation:

- id: '12'
  alias: Simulate Presence Living Room
  initial_state: True
  trigger:
    - platform: event
      event_type: 'event_sp_main'
  condition:
    condition: and
    conditions:
      - condition: sun
        after: sunset
      - condition: time
        after: '15:00:00'
        before: '21:00:00'
      - condition: state
        entity_id: 'sensor.someone_home'
        state: 'no'
  action:
    - delay: '00:{{ (range(15, 45) | random) }}:00'
    - service: notify.telegram
      data:
        message: "Turning light on"
    - service: light.turn_on
      data:
        entity_id: light.fibaro_system_fgd212_dimmer_2_level_5
        brightness: 128
    - wait_template: "{{ is_state('sensor.time', '22:00') }}"
    - delay: '00:0{{ (range(5, 9) | random) }}:00'
    - service: notify.telegram
      data:
        message: "Turning light off"
    - service: light.turn_off
      data:
        entity_id: light.fibaro_system_fgd212_dimmer_2_level_5

I trigger event event_sp_main at different particular times and situations when we are not at home. One is on sunset and other one at different times.

I can see that at just after it triggers again much earlier than 22:00, the automation is in the wait_template step I receive the message on my phone that the lights are going to be turned off and when I arrive home they certainly are.

I cannot find anything in the documentation of what happens to an automation that triggers multiple times while it is running.

May someone help me understand if this is a bug or something I’m doing wrong?

Thank you in advance.

You can disable the automation as long it’s in the wait state.

1 Like

Or you can add a condition that the automation doesn’t run again if it ran in the last xx sec

  condition:
    condition: template
    value_template: '{{ as_timestamp(now()) - as_timestamp(states.automation.Simulate_Presence_Living_Room.attributes.last_triggered) | int > xx }}'
1 Like

Cool! So you reckon that if the event triggers again while a running instance is in the wait_template it unblocks it and let it continue.

It certainly looks like that is what is happening to me. I guess if that is the way it works, it could be exploited to write some interesting automations…

I’ll try disabling the automation and report if that fixes it. Thanks for the suggestion.

I think in this case it is more convenient to disable it as I don’t know for how long it will trigger. (although probably for this automation I could work out a number that works)

Thanks for the suggestion. I’ll try this if the other suggestion doesn’t work.

Disabling the automation works! :slight_smile:

But it doesn’t actually show as disabled in the dashboard. But I don’t mind.

Thank you a lot for your suggestions and your help.