Dismiss of a persistent notification as trigger for automations

Hi all,

I try to use the dismiss of a persistent notificaiton as a trigger for an automation.
At first i start with something like this:

    - alias: 'Test.'
      trigger:
        platform: state
        entity_id: persistent_notification.test
        from: "notifying"
      action:
       test

This didn’t work because the entities state is not available anymore because after you dismiss the notitifacion the enity is gone.

So maybe I can do something with event as a trigger?
There is an event type persistent_notifications_updated but I did not know how to implement this in my automation.

Maybe someone can help me with this?

I’ve tested the following two automations and confirmed they work.

This one detects when a persistent notification is created and reports it in the system log.

- alias: 'persistent notification created'
  trigger:
    platform: event
    event_type: call_service
    event_data:
      domain: persistent_notification
      service: create
  action:
  - service: system_log.write
    data_template:
      message: "Persistent notification created. {{now()}}"
      level: warning

This one detects when a persistent notification is dismissed and reports it in the system log.

- alias: 'persistent notification dismissed'
  trigger:
    platform: event
    event_type: call_service
    event_data:
      domain: persistent_notification
      service: dismiss
  action:
  - service: system_log.write
    data_template:
      message: "Persistent notification dismissed. {{now()}}"
      level: warning

For your purpose, you just need this part:

  trigger:
    platform: event
    event_type: call_service
    event_data:
      domain: persistent_notification
      service: dismiss
2 Likes

Hi thank you for your answer!

I only need the part to see if the persistent notification ist deleted. But I need to know which persistent notification is deleted. Did you know how to archieve this? All of my persistent notifications have an id.

Trial and error. :slight_smile:

I’ll take a guess that the notification_id is accessible via trigger.event.data.notification_id so you can create a Template Condition to check if it matches the one you want.

Actually, that’s not the reason. It should work, but doesn’t because a bug was introduced a year ago when they started introducing code to track the context of events, etc. See PR #16415. The bug is on this line:

When the notification is dismissed, to_s is None, and so to_s.context causes an exception.

Here is the corresponding state_changed event (where to_s in the code corresponds to new_state in the event):

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "persistent_notification.test",
        "old_state": {
            "entity_id": "persistent_notification.test",
            "state": "notifying",
            "attributes": {
                "message": "test"
            },
            "last_changed": "2019-09-05T18:16:06.111857+00:00",
            "last_updated": "2019-09-05T18:16:06.111857+00:00",
            "context": {
                "id": "d305a43ea31843b1a45e8f0c735e206e",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": null
    },
    "origin": "LOCAL",
    "time_fired": "2019-09-05T18:16:10.736375+00:00",
    "context": {
        "id": "ff1c09780ca146deb383dae314d06082",
        "parent_id": null,
        "user_id": null
    }
}

I just tried it and got this exception:

2019-09-05 13:16:10 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=persistent_notification, service=dismiss, service_data=notification_id=test>
2019-09-05 13:16:10 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=persistent_notification.test, old_state=<state persistent_notification.test=notifying; message=test @ 2019-09-05T13:16:06.111857-05:00>, new_state=None>
2019-09-05 13:16:10 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event persistent_notifications_updated[L]>
2019-09-05 13:16:10 ERROR (MainThread) [homeassistant.core] Error doing job: Exception in callback async_track_state_change.<locals>.state_change_listener(<Event state_...ew_state=None>) at /home/phil/repos/home-assistant/homeassistant/helpers/event.py:77
Traceback (most recent call last):
  File "/usr/lib/python3.7/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/home/phil/repos/home-assistant/homeassistant/helpers/event.py", line 96, in state_change_listener
    event.data.get("new_state"),
  File "/home/phil/repos/home-assistant/homeassistant/core.py", line 372, in async_run_job
    target(*args)
  File "/home/phil/repos/home-assistant/homeassistant/components/automation/state.py", line 83, in state_automation_listener
    call_action()
  File "/home/phil/repos/home-assistant/homeassistant/components/automation/state.py", line 69, in call_action
    context=to_s.context,
AttributeError: 'NoneType' object has no attribute 'context'

So, your instinct was correct, it’s just that the bug is preventing it from working.

I think you can achieve the same thing with something like this:

  trigger:
    platform: event
    event_type: state_changed
    event_data:
      entity_id: persistent_notification.test
  condition:
    condition: template
    value_template: >
      {{ trigger.event.data.old_state and
         trigger.event.data.old_state.state == 'notifying' }}

I just gave it a try and it seems to work.

Oops, my bad. @123’s suggestions, of course, are another perfectly fine way to solve the problem. I was more commenting on the bug that prevented your first attempt from working.

I wanted to do the same thing and trigger an automation when a specific notification was dismissed.

I added the following template condition:

condition:
  - condition: template
    value_template: '{{ trigger.event.data.service_data.notification_id == "staubsauger_leeren" }}'

I ended up coming up with a slightly different take on this one. Basically the tack as @pnbruckner’s solution, I justed tested for an unknown state in the condition to differentiate the notification being created or dismissed. Seems to work well enough.

    - platform: event
      event_type: state_changed
      event_data:
        entity_id: persistent_notification.sensor_alert

    condition:
      - condition: template
        value_template: "{{ states('persistent_notification.sensor_alert') == 'unknown' }}"

A few months later I have to bump this topic:>
I habe build the following automation:

- alias: 'Unnotify Trash'
  trigger:
    platform: event
    event_type: call_service
    event_data:
      domain: persistent_notification
      service: dismiss
      notification_id: muell
  action:
      - service: counter.decrement
        entity_id: counter.iosbadgecount_tim
      - service: notify.ios_tims_devices
        data_template:
          title: "Thanks"
          message: "For beeing a good trashpanda."
          data:
           push:
            badge: '{{states.counter.iosbadgecount_tim.state}}'
            thread-id: "Muell"


But this automation gets trigered every 10-20 minutes 0o I really dont understand why:o The good thing is: Its working when the correct persistent notification gets dissmissed. But its a little bit odd to get all these messages the whole day:D

Does someone have a clue?