Dismiss all persistent notifications script

Hello!

I can’t make working the script which can dismiss all existing persistent notification (I occasionally created a lot of them).

The script:

script:
  dismiss_all_persistent_notifications:
    alias: Persistent notifications
    sequence:
      - service_template: >
          {% if states.persistent_notification | count > 0 %}
            script.turn_on
          {% else %}
            script.turn_off
          {% endif %}
        data_template:
          entity_id: script.dismiss_all_persistent_notifications_loop
          message: >
            {% for item in states.persistent_notification %}
              {% if loop.first %}
                {{ item.name | lower | replace("persistent_notification.", "") }}
              {% endif %}
            {% endfor %}

  dismiss_all_persistent_notifications_loop:
    alias: Persistent notifications loop
    sequence:
      - service: persistent_notification.dismiss
        data:
          notification_id: "{{ message }}"
      - service: script.turn_on
        data:
          entity_id: script.dismiss_all_persistent_notifications
1 Like

Hello! :slight_smile:

Problems I can see:

  1. Instead of trying to turn off the second script if there aren’t any remaining notifications, the first script should just abort via a condition step.
  2. You’re not passing the data to the second script correctly. See this doc page for more details.
  3. The second script needs to use data_template instead of data in the first step.
  4. Looping with scripts is tricky at best. You have to be absolutely sure the other script is no longer running when you try to invoke it.

Also, getting the notification_id (AKA object_id) of one of the existing notifications is much easier than what you have.

Try this:

script:
  dismiss_all_persistent_notifications:
    alias: Persistent notifications
    sequence:
      - condition: template
        value_template: "{{ states.persistent_notification|count > 0 }}"
      - wait_template: >
          {{ is_state('script.dismiss_all_persistent_notifications_loop', 'off') }}
      - service: script.dismiss_all_persistent_notifications_loop
        data_template:
          message: >
            {{ (states.persistent_notification|list)[0].object_id }}

  dismiss_all_persistent_notifications_loop:
    alias: Persistent notifications loop
    sequence:
      - service: persistent_notification.dismiss
        data_template:
          notification_id: "{{ message }}"
      - wait_template: >
          {{ is_state('script.dismiss_all_persistent_notifications', 'off') }}
      - service: script.dismiss_all_persistent_notifications

Great thanks for your detailed and useful impact! Will change the script.

So actually, there is a much better way to do this. That is, using a python_script as opposed to “regular” scripts.

First, add the following line to your configuration.yaml file (if you don’t already have it):

python_script:

Then create a directory in your <config> directory (i.e., the directory that contains configuration.yaml) named python_scripts. Then add a file to the <config>/python_scripts directory named dismiss_notifications.py and enter the following code into it:

for notif in hass.states.entity_ids('persistent_notification'):
    hass.services.call(
        'persistent_notification', 'dismiss',
        {"notification_id": notif.split('.', 1)[-1]})

Then instead of calling:

service: script.dismiss_all_persistent_notifications

call:

service: python_script.dismiss_notifications
6 Likes

This python script does not remove my notification for “Login attempt or request with invalid authentication from localhost (127.0.0.1). See the log for details.”

That’s probably because that post is five years old, and HA has changed quite a bit in that time.

Have you tried the persistent_notification.dismiss_all service?

Super! Not sure why something like this wasn’t at the top of all my searches as to how to accomplish this. Thanks.

Probably because that service did not use to exist (and there wasn’t even a “dismiss all” option in the notifications area), so it took work to write scripts to get rid of all existing notifications, hence these forum topics. But now that there is a service to do the job, nobody is asking anymore (because it says how to do it in the docs), so no new topics on the subject, just these stale, old topics that are out of date. :joy:

Great!

What automation?