How to terminate an action if condition changes?

Please help me to add a condition that will stop the action when the state changes. My knowledge is too little, and I am hopelessly tinkering over this for the second day. Here is my automation to trigger a buzzer when the door is left open for longer than the time set. How do I make the buzzer go off if the door is closed and not to wait for the action to be fully executed?

alias: Door left open alert
description: Notification if the main door is left open
trigger:
  - platform: state
    entity_id: binary_sensor.ikea_door_sensor_opening
    to: "on"
    from: "off"
    for: "00:00:10"
    id: open
    variables:
      long_enough: true
  - platform: state
    entity_id: binary_sensor.ikea_door_sensor_opening
    to: "off"
    from: "on"
    id: closed
    variables:
      long_enough: "{{ now() - trigger.from_state.last_changed >= timedelta(seconds=10)}}"
condition:
  - condition: template
    value_template: "{{ long_enough }}"
action:
  - repeat:
      sequence:
        - service: switch.toggle
          target:
            entity_id: switch.sonoff_100090791d
          data: {}
        - delay:
            hours: 0
            minutes: 0
            seconds: 2
            milliseconds: 0
      count: 9
  - service: switch.turn_off
    target:
      entity_id: switch.sonoff_100090791d
    data: {}
mode: single

The wait time is set to 10 seconds for testing purposes. The variable long_enough is added so that the automation does not record every opening.

mode: restart will do it, along with an if action:

alias: Door left open alert
description: Notification if the main door is left open
trigger:
  - platform: state
    entity_id: binary_sensor.ikea_door_sensor_opening
    to: "on"
    from: "off"
    for: "00:00:10"
    id: open
  - platform: state
    entity_id: binary_sensor.ikea_door_sensor_opening
    to: "off"
    from: "on"
    id: closed
action:
  - if:
      - condition: trigger
        id: "closed"
    then:
      - service: switch.turn_off
        target:
          entity_id: switch.sonoff_100090791d
        data: {}
    else:
      - repeat:
          sequence:
            - service: switch.toggle
              target:
                entity_id: switch.sonoff_100090791d
              data: {}
            - delay:
                hours: 0
                minutes: 0
                seconds: 2
                milliseconds: 0
          count: 9
      - service: switch.turn_off
        target:
          entity_id: switch.sonoff_100090791d
        data: {}
mode: restart

Thank you :+1: The underrated restart mode comes to the rescue :tipping_hand_man:
How do I prevent logging “normal” door open events in the automation? Or are they just not logged?

Logged?

If you mean triggered the door has to be open for 10 seconds before it will trigger.

I referred to the door opening records in the logbook. So that they don’t multiply unnecessarily, and only record when alarm conditions are met. I’m probably worrying about this unnecessarily, right?

You can either log all state changes of the door or none. See: https://www.home-assistant.io/integrations/recorder/#exclude

1 Like

This clarifies everything and ends the topic :tipping_hand_man: