Notify to persistent notification

Tags: #<Tag:0x00007fc3fabd3330>

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.

9 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.

Does this still work? I’ve added

  - 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

to notify and the following to secrets:

persist_notif_token: 'Bearer QencJwqaixn_iKIUjLB70aXXXX'

When I try to call the service I get


2020-10-18 17:09:13 ERROR (MainThread) [homeassistant.components.rest.switch] Got non-ok response from resource: 404
2020-10-18 17:09:15 ERROR (SyncWorker_57) [homeassistant.components.rest.notify] Client error. Response 401: Unauthorized:
NoneType: None

Is this an authentication error that has something to do with the token?

I’m a bit puzzled by the error at 09:13. Are you sure it always appears when calling that service?
For the other, if you are 100% sure of your token, then maybe try replacing localhost in the URL with the actual IP address of your instance. Same if you use SSL or have changed the port number, etc.

Possibly related :wink:

1 Like

I just changed to https using my duckdns address:

- name: persist_it
  platform: rest
  resource: "https://XXX.duckdns.org/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

I think the first error did indeed not belong there, I think it happens when I update the notify part in home assistant and reload it. I tried calling the service three times and get this:

2020-10-19 09:53:28 ERROR (SyncWorker_61) [homeassistant.components.rest.notify] Client error. Response 401: Unauthorized:
NoneType: None
2020-10-19 09:53:29 ERROR (SyncWorker_26) [homeassistant.components.rest.notify] Client error. Response 401: Unauthorized:
NoneType: None
2020-10-19 09:53:30 ERROR (SyncWorker_8) [homeassistant.components.rest.notify] Client error. Response 401: Unauthorized:
NoneType: None

When I go to https://XXX.duckdns.org/api/services/persistent_notification/create in the browser I get 405: Method Not Allowed

Are you sure you have created your long-lived token in your account profile? I am asking because my token does not look like the one you posted. In my secret file, it looks like this (truncated):
persist_notif_token: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc[…]
You will also see that I have not set any quote around the entry (but I doubt this is the issue).
Otherwise, you may just wait for the change request posted by Diogo above to be released, as this workaround should then soon become obsolete (hopefully!)

Also, you wrote

I assume you also tried after a full reboot of HA. Indeed, maybe just reloading rest and notifications services does not reload the secret file, for instance.

Ha! That was the problem, turns out I screwed up when copying the token and copied only half or something :see_no_evil: :see_no_evil:

It works now! Also you don’t have to restart home assistant appearantly, I just reloaded and it worked. Thanks for you reply.

Indeed hopefully soon this won’t be necessary anymore :slight_smile: