Help with Door Close Automation

Hi!

I’ve been having some trouble figuring out how to configure my door close automation.

I have a handful of doors that I set up an alert letting me know if the door has been open for more than 5 minutes.

I’m trying to figure out a way to get a notification if a door that was open for more than 5 minutes was closed.

Here is my current door open notification.

alias: Door Open Notification
  trigger:
    - platform: state
      entity_id:
        - binary_sensor.kitchen_garage_door
        - binary_sensor.back_door
        - binary_sensor.front_door
        - binary_sensor.side_garage_door
      for:
        hours: 0
        minutes: 5
        seconds: 0
      to: "on"
      from: "off"
  action:
    - service: notify.mobile_app
      data_template:
        title: Door Open Alert
        message:
          The {{ trigger.to_state.attributes.friendly_name }} has been open for
          more than 5 minutes.
        data:
          priority: high

You can use trigger variables to limit notification based on the length of time between now and that stored in the last_changed property with an offset based on the open duration. The last_changed property of the from_state will hold a datetime object reflecting when the previous state change occurred. In this case we will use trigger ids to filter it so it only checks when the binary sensor in question turned “on”, then we add a 5 minute offset to that time and compare it to the current time.

alias: Door Open Notification
description: ""
trigger:
  - platform: state
    entity_id:
        - binary_sensor.kitchen_garage_door
        - binary_sensor.back_door
        - binary_sensor.front_door
        - binary_sensor.side_garage_door
    for: "00:05:00"
    to: "on"
    from: "off"
    id: open
  - platform: state
    entity_id:
        - binary_sensor.kitchen_garage_door
        - binary_sensor.back_door
        - binary_sensor.front_door
        - binary_sensor.side_garage_door
    to: "off"
    from: "on"
    id: closed
action:
  - choose:
      - conditions:
          - condition: trigger
             id: 'open'
        sequence: 
          - service: notify.mobile_app
            data:
              title: Door Open Alert
              message:
                The {{ trigger.to_state.attributes.friendly_name }} has been open for
                more than 5 minutes.
              data:
                priority: high
      - conditions:
          - condition: template
            value_template: "{{ now() >= trigger.from_state.last_changed + timedelta(minutes=5) }}"
        sequence:
          - service: notify.mobile_app
            data:
              title: Door Open Alert Resolved
              message:
                The {{ trigger.to_state.attributes.friendly_name }} has been closed.
              data:
                priority: high

This could be condensed even further using a few more templates to eliminate the Choose action.

2 Likes

Thank you @Didgeridrew that worked perfectly. You’ve saved my sanity!