Notify to persistent notification

Tags: #<Tag:0x00007f738af41848>

I just wanted to be able to use the persistent notification as an actual notification platform/service, so that it can be used in alerts or in notify groups.
I had seen complex solutions with automations detecting alert events or persistent notifications creations to then call another notification service, but I think I found a more straightforward solution. It simply uses the RESTful notification and the HA REST API:

notify:
  - name: persist_it
    platform: rest
    resource: 'http://localhost:8123/api/services/persistent_notification/create'
    method: POST_JSON
    headers:
      authorization: !secret persist_notif_token #create a long-term token in your profile page and add 'Bearer ' before it in your secrets file
      content-type: 'application/json'
    message_param_name: message
    title_param_name: title
    target_param_name: notification_id

You can then call it as another notify service, with the usual title and message parameters, and it will create the desired persistent notification.
Optionally, you can use the target parameter of the notify service to specify a persistent notification_id, if you want to be able to discard it later on with a call to the persistent_notification.dismiss service.

8 Likes

Thank you, this is very useful and exactly what I was looking for.

Just a tip: Don’t forget to add “Bearer” in front of the API token in the secrets file

Also, if you are using a certificate, don’t use localhost but the hostname from the certificate and of course you will have to set the port to the correct value.

Great tip! Thank you! :+1:

Works exactly as described and, as explained, the persistent notification can be programmatically dismissed if it’s created with a notification_id.