Fetching persistent_notification as sensor?

Im sending notifications from a RaspberryPi with Apprise to Homeassistant.

Like: ‘apprise -vvv hassios://{host}/{access_token}’

HA is receiving the notifications on the left sidebar. Everything works.

But I wanted to receive the notifications as sensor, so I could show them on the dashboard, or making math with it.

Is it possible to generate a sensor fetching the notifications?

thanks in advance!

If you create persistent_notifications with a notification_id,

service: persistent_notification.create
data:
  title: My Title
  message: My Message
  notification_id: '1234'

you can make a template senor with the attributes.

1 Like

This isn’t fully debugged, but hopefully it gives you a foundation to work from…

template:
  - sensor:
      - name: Persistent List
        state: "{{ states.persistent_notification | list }}"
        availability: "{{ states.persistent_notification | list != [] }}"

  - trigger:
      - platform: state
        entity_id: 
          - sensor.persistent_list
        not_to:
          - unavailable
          - unknown
        id: list
        variables:
          message: "{{ ((states.persistent_notification | list)[-1].attributes).message }}"
          title: "{{ ((states.persistent_notification | list)[-1].attributes).title }}"
      - platform: state
        entity_id:
          - persistent_notification.invalid_config
        not_to:
          - unavailable
          - unknown
        variables:
          message: "{{ trigger.to_state.attributes.message }}"
          title: "{{ trigger.to_state.attributes.title }}"
    sensor:
      - name: Persistent Notification
        state: "{{ message }}"
        attributes:
          history: >
            {% set current = this.attributes.get('history', []) %}
            {% set new = [{
                "title": title,
                "message": message,
                "time": now().strftime("%A, %-I:%M%p") }] %}
            {{ (new + current)[:10] }}
1 Like

hello,
Ive completed it.

Im using BirdnetPi. A acoustic bird classification on a Raspberrypi.

Every time a new bird is identified, the pi sends a persistent notification to Home Assistant via apprise.

Apprise could speak mqtt, but with the home assistant persistent notification I don’t need a mqtt broker.

Created a template sensor, I map all messages from persistent_notification containing “New BirdNET-Pi Detection”

  - sensor:
      - name: "notification_list"
        state:   >-
          {{ ((states.persistent_notification | selectattr('attributes.title', 'eq', 'New BirdNET-Pi Detection') | list)[-1].attributes).title }}
        availability: "{{ states.persistent_notification | list != [] }}"
        attributes:
          message: >-
            {{ ((states.persistent_notification | selectattr('attributes.title', 'eq', 'New BirdNET-Pi Detection') | list)[-1].attributes).message }}   

So I can show the newest detection on the dashboard.

Because there a more than 100 messages, a automation dismiss all notifications at 9:00 containing “New BirdNET-Pi Detection”

    alias: dismis
    description: ""
    mode: single
    trigger:
      - platform: time
        # This trigger will fire at 9:00
        at: "09:00:00"
    action:
        #- alias: Repeat Dismiss action for all
          repeat:
            for_each: >-
             {{ states.persistent_notification | selectattr('attributes.title', 'eq', 'New BirdNET-Pi Detection') | map(attribute='object_id') | list }}
            sequence:
              - service: persistent_notification.dismiss
                data:
                  notification_id: "{{ repeat.item }}"

Thank you!

Just a heads up, it looks like this is going to break in 2023.06…

1 Like

I tried this on HA 2023.6.3 and I don’t see the states are all when I check states in dev area