Checking state of persistent notifications

This snippet of code used to work:

  - condition: not
    conditions:
      - condition: state
        entity_id: persistent_notification.mailbox_flag
        state: notifying

If a specific persistent notification wasn’t “notifying” it would pass this test.

Now I know this was probably a “feature” as if the notification isn’t active the entity doesn’t exist - thus this test is invalid. But it used to work - so probably the way automations handle non-existent entities has changed - now the whole automation just fails of this condition is left in.

So - any ideas how I can test if a specific notification is displaying that won’t crash the automations?

This needs to be a condition - not a trigger.

1 Like

Came up with a solution which I will post in case this comes up in a search.

Created an input_select and two new automations:

input_select:
  mail_notification_active:
    options:
      - true
      - false
    initial: false

- alias: mail_notification_created
  trigger:
  - platform: event
    event_type: call_service
    event_data:
      domain: persistent_notification
      service: create
  condition:
  - condition: template
    value_template: '{{ trigger.event.data.service_data.notification_id == "mailbox_flag" }}'  
  action:
  - service: input_select.select_option
    target:
      entity_id: input_select.mail_notification_active
    data:
      option: true
      
- alias: mail_notification_dismissed
  trigger:
  - platform: event
    event_type: call_service
    event_data:
      domain: persistent_notification
      service: dismiss
  condition:
  - condition: template
    value_template: '{{ trigger.event.data.service_data.notification_id == "mailbox_flag" }}'  
  action:
  - service: input_select.select_option
    target:
      entity_id: input_select.mail_notification_active
    data:
      option: false

Now my condition looks like:

  - condition: state
    entity_id: input_select.mail_notification_active
    state: 'False'

The uppercase F is not a typo - that appears to be something the input_select integration does - but apart from that glitch this works as I need and will only run my automation if the notification is not active.

If you have a more elegant solution please post it.

Edit: this stopped working with the first June 2023 update of Home Assistant because of Complete persistent notifications migration by bdraco · Pull Request #92828 · home-assistant/core · GitHub, state is always unknown now :frowning_face:

Edit 2: the fix is in Disable or hide notifications? - #11 by VDRainer

Checking if persistent notification exists works for me with the following:

condition: template
value_template: '{{ states(''persistent_notification.my_notification_id'') != ''unknown'' }}'

When notification is not present:

When notification is present:

Inspired from How to test whether an entity exists? - #2 by VDRainer