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
  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…

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

  - trigger:
      - platform: state
          - sensor.persistent_list
          - unavailable
          - unknown
        id: list
          message: "{{ ((states.persistent_notification | list)[-1].attributes).message }}"
          title: "{{ ((states.persistent_notification | list)[-1].attributes).title }}"
      - platform: state
          - persistent_notification.invalid_config
          - unavailable
          - unknown
          message: "{{ trigger.to_state.attributes.message }}"
          title: "{{ trigger.to_state.attributes.title }}"
      - name: Persistent Notification
        state: "{{ message }}"
          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

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 != [] }}"
          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
      - platform: time
        # This trigger will fire at 9:00
        at: "09:00:00"
        #- alias: Repeat Dismiss action for all
            for_each: >-
             {{ states.persistent_notification | selectattr('attributes.title', 'eq', 'New BirdNET-Pi Detection') | map(attribute='object_id') | list }}
              - service: persistent_notification.dismiss
                  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