Automation: Wait for trigger and then only continue if not true

I have an automation that unlocks the door when someone arrives home. As a security precaution I would like the door to lock again, if it hasn’t been opened within 3 minutes. I have that working just fine in Node red, but am trying to use the built-in automations instead. What troubles me is the 3 minutes delay, which I have implemented with a “wait for” automation with continue on time out. That works if I do not open the door, then HA waits for 3 minutes and then locks the door again. But if I open the door then the door tries to lock itself immediately. So what I do need is that the automation only continues if “wait for” timeout and if it doesn’t time out (because the door has then been opened) then the automation should stop. Any ideas to accomplish that?

alias: Dørlås - Åben
description: ""
trigger:
  - platform: state
    entity_id:
      - person.morten
    from: not_home
    to: home
    id: morten_hjemme
  - platform: state
    entity_id:
      - person.trine
    from: not_home
    to: home
    id: trine_hjemme
  - platform: state
    entity_id:
      - input_boolean.house_mode
    from: "off"
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 15
condition:
  - condition: state
    entity_id: lock.danalock
    state: locked
action:
  - service: lock.unlock
    data: {}
    target:
      entity_id: lock.danalock
  - service: notify.all_devices
    data:
      title: Dørlås
      message: Hoveddøren er låst op automatisk
  - wait_for_trigger:
      - platform: state
        entity_id:
          - binary_sensor.openclose_12
        from: "off"
        to: "on"
    timeout:
      hours: 0
      minutes: 3
      seconds: 0
      milliseconds: 0
    continue_on_timeout: true
  - service: lock.lock
    data: {}
    target:
      entity_id: lock.danalock
  - service: notify.all_devices
    data:
      message: Hoveddøren er låst igen
      title: Dørlås
mode: single

Using a separate automation may be easier. Here’s mine:

- id: 6efafd55-221c-4227-b0f8-75a550510df2
  alias: 'Lock Front Door if Closed but Unlocked'
  trigger:
  - platform: state
    entity_id: lock.front_door
    to: 'unlocked'
    for:
      minutes: 15
  - platform: state
    entity_id: binary_sensor.front_door
    to: 'off'
    for:
      minutes: 10
  condition:
  - condition: state
    entity_id: binary_sensor.front_door
    state: 'off'
  - condition: state
    entity_id: lock.front_door
    state: 'unlocked'
  action:
  - service: lock.lock
    entity_id: lock.front_door
  - service: notify.telegram_general
    data:
      title: '🔒 <b>Front door locked</b>'
      message: "Front door automatically locked"

Thanks - the issue I have by putting it in a seperate automation is that I would like to have it triggered by this automation only, not in case we unlock the door ourselves in the morning without opening it. I think I can figure out a work-around where my automation triggers an input boolean and then this one is used in a seperate automation, but it feels a bit like a setback compared to the one flow in Node-Red.

What about a simple delay? Or a wait_for_trigger from on to off?

How would the “wait” work - I can’t be absolutely sure that the door will be still be open after exactly 3 minutes, but just during that 3 minutes. But perhaps I misinterpreted the “wait” condition?

Flipping the condition from on to off → off to on will help a bit as the door will then lock once the door is indeed closed again, but often we don’t lock the door when at home, so it will still be a bit inferior to the current implementation in Node Red.


action:
  - service: lock.unlock
    target:
      entity_id: …

  - wait_for_trigger:
      - platform: state
        entity_id:
          - binary_sensor.…
        to: "on"
    timeout:
      minutes: 3

  - if:
      - condition: state
        entity_id: binary_sensor.…
        state: "off"
    then:
      - service: lock.lock
        …
      - service: notify…



1 Like

Thanks a lot, that was the solution, so in the end it looks like this:

alias: Dørlås - Åben
description: ""
trigger:
  - platform: state
    entity_id:
      - person.morten
    from: not_home
    to: home
    id: morten_hjemme
  - platform: state
    entity_id:
      - person.trine
    from: not_home
    to: home
    id: trine_hjemme
  - platform: state
    entity_id:
      - input_boolean.house_mode
    from: "off"
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 15
condition:
  - condition: state
    entity_id: lock.danalock
    state: locked
action:
  - service: lock.unlock
    data: {}
    target:
      entity_id: lock.danalock
  - service: notify.all_devices
    data:
      title: Dørlås
      message: Hoveddøren er låst op automatisk
  - wait_for_trigger:
      - platform: state
        entity_id:
          - binary_sensor.openclose_12
        to: "on"
        from: "off"
    timeout:
      hours: 0
      minutes: 3
      seconds: 0
      milliseconds: 0
  - if:
      - condition: state
        entity_id: binary_sensor.openclose_12
        state: "off"
        for:
          hours: 0
          minutes: 3
          seconds: 0
    then:
      - service: lock.lock
        data: {}
        target:
          entity_id: lock.danalock
      - service: notify.all_devices
        data:
          title: Dørlås
          message: Hoveddør er låst igen
mode: single