Restoring persistent notifications on restart

Replace the original condition.

That indicates that only notifications created with notification_id that matches the first 5 characters (myha_) are saved and restored.

      - service: persistent_notification.create
        data:
          title: "Bresser 3CH"
          message: "Sensor Timeout"
          notification_id: "myha_bresser_notify"

All right I think I understand… but it isn’t working. The notifications do publish to the mqtt broker but when I restart home assistant they do not restore. In my logs I am seeing the following error:

Error during template condition: UndefinedError: 'dict object' has no attribute 'event'

when I run the template thru the template editor I get this:

Error rendering template: UndefinedError: 'trigger' is undefined

When I restart I get the following message with no content on my mqtt broker:

Message 19 received on home-assistant/notifications/config_entry_discovery at 8:36 PM: QoS: 0 - Retain: false

This is my full config:


automation:
  - id: save_notification_create
    alias: Save Notifications on Create
    initial_state: 'on'
    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[:5] == "kyle_" }}          
    action:
      - service: mqtt.publish
        data_template:
          topic: "home-assistant/notifications/{{ trigger.event.data.service_data.notification_id }}"
          payload: >-
            {
              "notification_id" : "{{ trigger.event.data.service_data.notification_id }}",
              "title" : "{{ trigger.event.data.service_data.title }}",
              "message" : "{{ trigger.event.data.service_data.message }}"
            }
          retain: true
  
  - id: save_notification_dismiss
    alias: Removed Saved Notifications on Dismiss
    initial_state: 'on'
    trigger:
      - platform: event
        event_type: call_service
        event_data:
          domain: persistent_notification
          service: dismiss
    action:
      - service: mqtt.publish
        data_template:
          topic: "home-assistant/notifications/{{ trigger.event.data.service_data.notification_id }}"
          retain: true
  
  - id: restore_notifications
    alias: Restore Notifications
    initial_state: 'on'
    trigger:
      - platform: mqtt
        topic: home-assistant/notifications/+
    condition:
      - condition: template
        value_template: >
          {{ trigger.event.data.service_data.notification_id[:5] == "kyle_" }}          
    action:
      - service: persistent_notification.create
        data_template:
          title: >-
            {{ trigger.payload_json.title }}
          message: >-
            {{ trigger.payload_json.message }}
          notification_id: >-
            {{ trigger.payload_json.notification_id }}

EDIT: This works if I use your condition for the ā€˜create’ automation but use the original condition for the ā€˜restore’ automation. Weird.

EDIT 2: Oh wow I am stupid. I just re-read your post and you never said to use it in the restore automation. Jesus. Thanks for your help!!!

@Steven_Rollason Hi and thanks for sharing this. I think i made everything to add this to HA, but my persistent notifications are still gone after restarting HA. Do i have to do anything else, which is not written? Can someone please help me?

Will be hard as you don’t give any information about logs, error or other things.

What is written into the mentioned MQTT-Messages, what do you see on Broker side? What happens when you push messages manually? What is included in logs, traces, … any errors?