Automation based on order of events

I have created an automation that notifies me if the garage was left open for 4 min. Using Choices and triggers, the same automation also notifies me when it closes. However, today it notifies on close every time it closes. Instead I would like it to only notify me on close if the 4min trigger preceded it. Thoughts?

 alias: Garage door open alert
  description: Sends a notification if the Garage Door is open over 4min
  trigger:
  - type: opened
    platform: device
    device_id: e7d69ba0dfdf89334ff9596912f6c97d
    entity_id: e85f5889496d6c967a7a67cb0fa4da31
    domain: binary_sensor
    for:
      hours: 0
      minutes: 4
      seconds: 0
    id: Garage left open 4min
  - type: not_opened
    platform: device
    device_id: e7d69ba0dfdf89334ff9596912f6c97d
    entity_id: e85f5889496d6c967a7a67cb0fa4da31
    domain: binary_sensor
    id: Garage Closed
  condition: []
  action:
  - choose:
    - conditions:
      - condition: trigger
        id:
        - Garage left open 4min
      sequence:
      - service: notify.mobile_app_phone
        data:
          message: Garage open for 4 min
    - conditions:
      - condition: trigger
        id:
        - Garage Closed
      sequence:
      - service: notify.mobile_app_phone
        data:
          message: Garage closed
  mode: single

If you put the close notification in a separate automation, then you could enable that automation with the 4 min warning and disable it when you issue the close notification.

1 Like

There are a few ways to do this. You could do it using a Wait for trigger following the original opening message instead of using a Choose action:

alias: Garage door open alert
description: Sends a notification if the Garage Door is open over 4min
trigger:
  - type: opened
    platform: device
    device_id: e7d69ba0dfdf89334ff9596912f6c97d
    entity_id: e85f5889496d6c967a7a67cb0fa4da31
    domain: binary_sensor
    for:
      hours: 0
      minutes: 4
      seconds: 0
condition: []
action:
  - service: notify.mobile_app_phone
    data:
      message: Garage open for 4 min
  - wait_for_trigger:
    timeout: "00:10:00"
    continue_on_timeout: false
      - type: not_opened
        platform: device
        device_id: e7d69ba0dfdf89334ff9596912f6c97d
        entity_id: e85f5889496d6c967a7a67cb0fa4da31
        domain: binary_sensor
  - service: notify.mobile_app_phone
    data:
      message: Garage closed
mode: restart

Make sure to review the Wait for Trigger doc and assign an appropriate timeout for your wait so the automation isn’t left in a waiting state for an excessive amount of time.

Another option is to use a template to check the state object of the door sensor. While this could be done in the condition of the Choose option, in the automation that follows it is moved to the Condition block. This should help keep the automation’s debug traces from being filled with door closing events unrelated to prolonged opening.

alias: Garage door open alert
description: Sends a notification if the Garage Door is open over 4min
trigger:
  - platform: state
    entity_id: e85f5889496d6c967a7a67cb0fa4da31
    to: 'on'
    from: 'off'
    for: "00:04:00"
    id: Garage open for 4 min
    variables:
      long_enough: true
  - platform: state
    entity_id: e85f5889496d6c967a7a67cb0fa4da31
    to: 'off'
    from: 'on'
    id: Garage Closed
    variables:
      long_enough: "{{ now() - trigger.from_state.last_changed >= timedelta(minutes=4)}}"
condition:
  - "{{ long_enough }}"
action:
  - service: notify.mobile_app_phone
    data:
      message: "{{ trigger.id }}"
mode: single
1 Like