Edit (27th February): Please update to the latest version below
I found an issue with my original version of this script which causes a problem with persistent notifications with ids containing hyphens which, unfortunately, includes the “Login attempt failed” notification. The updated version replaces the hyphen with an underscore when determining the entity id of the notification to fix this.
I’ve starting using persistent notifications a lot, and I needed a way to make them persist if Home Assistant restarts. I created some automations for this, as a package:
persistent_notification_restoration.yaml (2.0 KB)
There are three automations:
- save_notification_create - saves notifications when they are created by posting a retained message to an MQTT topic
- save_notification_dismiss - removes the retained message when a notification is dismissed
- restore_notifications - subscribes to the topic and creates persistent notifications if a notification doesn’t already exist for the notification id or the message has changed (I missed this bit in my first attempt and ended up with the notification being sent repeatedly to the MQTT topic, because it was re-triggering the save automation!)
To add this to Home Assistant:
1) Configure MQTT if you don’t already have it configured.
2) Create a packages folder in your config folder and add the following under homeassistant: in your config file:
packages: !include_dir_named packages
3) add the persistent_notification_restoration.yaml file in your packages folder.
Alternative, you can copy the automations from the file and add them to your config file if you don’t want to use packages.
Note: The first automation has a condition to exclude the notification on new devices being discovered (id config_entry_discovery), Without that, these notifications were appearing on every restart (only with the automations enabled). I suspect that something in the restart process causes it to erroneously detect that there are new devices, but this is not normally noticed because the notification was previously lost once it had restarted.