Clearing Android Notifications when an entity's state is changed by something other than the notification action

Hi all - I have a working automation that sends me and my wife a notification if the garage door is left open for a few mins, and we can use that notification to close the door. If one of us does it, it dismisses the notification for us both.

However the garage door can be closed in other ways after the notification has sent (My wife uses the Hue Dimmer Switch in the garage which triggers the door to close, or asks Google to do it).

How can I improve the below automation to dismiss the notification for us both when either the notification is used to close the door or the garage door state returns to closed?

All help hugely appreciated!

alias: Garage Door Left Open - Actionable Notification
description: ""
trigger:
  - type: opened
    platform: device
    device_id: 8188f35ee988d0f3055299bb6c5b5ad4
    entity_id: binary_sensor.external_garage_door_sensor_contact
    domain: binary_sensor
    for:
      hours: 0
      minutes: 2
      seconds: 0
condition: []
action:
  - service: notify.notify
    data:
      title: The Garage Door is Open
      message: >-
        The Garage Door was left open at
        {{now().hour}}:{{now().minute}}:{{now().second}}
      data:
        actions:
          - action: CLOSE_DOOR
            title: Close Door
        tag: garage-door
  - wait_for_trigger:
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: CLOSE_DOOR
  - service: switch.turn_on
    data: {}
    target:
      device_id: 9b8f790a0d43d36ef9dabd89e75f1378
  - service: notify.notify
    data:
      message: clear_notification
      data:
        tag: garage-door
mode: single

I think this could work.
I added a trigger when the door closes and this clears any notifications.
(I think)

alias: Garage Door Left Open - Actionable Notification
description: ""
trigger:
  - type: opened
    platform: device
    device_id: 8188f35ee988d0f3055299bb6c5b5ad4
    entity_id: binary_sensor.external_garage_door_sensor_contact
    domain: binary_sensor
    for:
      hours: 0
      minutes: 2
      seconds: 0
    id: is_open
  - platform: device
    type: turned_on
    device_id: 9b8f790a0d43d36ef9dabd89e75f1378
    domain: switch
    id: closed
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: is_open
        sequence: 
          - service: notify.notify
            data:
              title: The Garage Door is Open
              message: >-
               The Garage Door was left open at
               {{now().hour}}:{{now().minute}}:{{now().second}}
              data:
                actions:
                  - action: CLOSE_DOOR
                    title: Close Door
                tag: garage-door
          - wait_for_trigger:
            - platform: event
              event_type: mobile_app_notification_action
              event_data:
                action: CLOSE_DOOR
          - service: switch.turn_on
            data: {}
            target:
              device_id: 9b8f790a0d43d36ef9dabd89e75f1378
          - service: notify.notify
            data:
              message: clear_notification
              data:
                tag: garage-door
    default: []
  - choose:
      - conditions:
          - condition: trigger
            id: closed
        sequence: 
          - service: notify.notify
            data:
              message: clear_notification
              data:
                tag: garage-door
    default: []
mode: single

Edit forgot to remove the original part.
I think I had indentation errors.

I had to add in the entity_id for the 2nd trigger as it was throwing an error before that - but not working unfortunately!

Can you use the state instead of device trigger of the switch?
Device triggers are sometimes not working.

It could be that I have more indentation errors.
I posted this on my phone so it was rather hard to keep the indentations correctly.

Do you get anything in the trace? Does it even trigger?
If not then it’s probably the device trigger that is the cause.

Thanks - tried with the state and no luck.

The automation triggers, the notification is sent, and can be closed as per the original. But the notification doesn’t clear if the garage door is shit any other way.

Looking at the trace it seems this won’t work because only the trigger that activates it is being carried through (is_open) - the other trigger is greyed out in the trace; given ‘is_open’ triggers the automation it seems like the 2nd choose option would never activate in this scenario.

Any other ideas? Think I’ll need to have a deeper dive into this!

What kind of sensors and states do you have to the garage?

I think you can use the same entity as you have for open.
Instead of the closed id posted before replace it with:

  - type: closed
    platform: device
    device_id: 8188f35ee988d0f3055299bb6c5b5ad4
    entity_id: binary_sensor.external_garage_door_sensor_contact
    domain: binary_sensor
    id: closed

We have the Aqara contact sensor on the door - so simple on/off for open/closed. Then we have a Shelly wired in to the garage door opener which is controlling the opening/closing of the door.

So the first part works as t did previously. But using the garage door sensor being closed in the Triggers section sets off the automation outside the intended use (when it’s open for a few minutes - triggers when the door gets closed).

You mean it triggers when the door has already been closed with the notification?
Yes that is a thing that is hard to remove.
I don’t know if there is a way to find out if the notification is still present on the phone by using the notification sensor.
It’s probably hard to get that working.

But what happens when the clear notification message is sent and the notification is already cleared?

The companion apps support clearing of notifications - the key link is to assign a “tag” to the noficiation when you trigger it and then reference this “tag” when you clear - documentation is here: Introduction | Home Assistant Companion Docs

So when the door closes you would need an automation to clear the notification tag.

Thanks @Hellis81 - I definitely learnt a lot more about how to build automations, and really appreciate the time you took to help.

And thanks @BlackCatPeanut - I kept my original automation, minus the clear_notification part and set this up in a new automation triggered by the garage door closing.

2 Likes