Persisent notifications and critical notifications


So I figured I may as well crosspost this while it is fresh. I hope it helps someone.
I haven’t seen much on persistent_notification.create and notify.persistent_notification recently

Persistent notitifcations and Critical Notifications

What are I am talking about?
The service `persistent_notification.create’

3count

How is it different from The service 'notify.persistent_notification'

`notification_id` cannot be used with 'notify.persistent_notification'


notid


So you can send multiple messages that stack up on your notification panel.
This is true for either service.
Only persistent_notification.create accepts an ID

20count



Using notication_id ONLY the new message will arrive under that ID

Regardless of how many times it is sent.

Old messages with matching ID will be overwritten




So that’s details out of the way. Now some examples.

Example Message


service: persistent_notification.create
data:
  message: There is an update in Os-Agent!
  title: Update message

basic

Add some Markdown


Bold	<b>My bold text</b> or in the UI **My bold text**
Cursive	<i>My cursive text</i>
Link	[Link](https://home-assistant.io/)


service: persistent_notification.create
data:
  message: <b>There is</b> <i>an update in</i> [Os-Agent!](https://github.com/home-assistant/os-agent)
  title: Update message



with Spacing


service: persistent_notification.create
data:
  message: >-
    **There is** <i> an update in </i>
    [Os-Agent!](https://github.com/home-assistant/os-agent) 

    *My cursive text*


    **My bold text**
  title: Update message


compact using line breaks


service: persistent_notification.create
data:
  message: >-
    **There is** <i> an update in </i> [Os-Agent!](https://github.com/home-assistant/os-agent) </br>
    *My cursive text* <br></br>
    **My bold text**
  title: Update message


This is the output of spacing or compact

masMD

complex example with variables and sensors.

The sensor


with quote marks


service: persistent_notification.create
data:
  notification_id: os-agent
  message: >-
    OS_Agent {{ states('sensor.os_agent_version_github') }} is
    available


    [Latest](https://github.com/home-assistant/os-agent/releases/latest)


    " wget https://github.com/home-assistant/os-agent/releases/download/{{
    states('sensor.os_agent_version_github') }}/os-agent_{{
    states('sensor.os_agent_version_github') }}_linux_aarch64.deb "


    " sudo dpkg -i os-agent_{{ states('sensor.os_agent_version_github')
    }}_linux_aarch64.deb "
  title: OS_Agent New Version

quoted


Plain and spaced

service: persistent_notification.create
data:
  notification_id: os-agent
  message: >-
    OS_Agent is
    available


    [Latest](https://github.com/home-assistant/os-agent/releases/latest)


    wget https://github.com/home-assistant/os-agent/releases/download/1.4.0/os-agent_1.4.0_linux_aarch64.deb 


    sudo dpkg -i os-agent_1.4.0_linux_aarch64.deb 
  title: OS_Agent New Version


Spaced with sensor as variable


service: persistent_notification.create
data:
  notification_id: os-agent
  message: >-
    OS_Agent {{ states('sensor.os_agent_version_github') }} is
    available


    [Latest](https://github.com/home-assistant/os-agent/releases/latest)


    wget https://github.com/home-assistant/os-agent/releases/download/{{
    states('sensor.os_agent_version_github') }}/os-agent_{{
    states('sensor.os_agent_version_github') }}_linux_aarch64.deb 


    sudo dpkg -i os-agent_{{ states('sensor.os_agent_version_github')
    }}_linux_aarch64.deb
  title: OS_Agent New Version


compact with sensor variables


service: persistent_notification.create
data:
  notification_id: os-agent
  message: >-
    OS_Agent {{ states('sensor.os_agent_version_github') }} is
    available<br></br>
    [Latest](https://github.com/home-assistant/os-agent/releases/latest)<br></br>
    wget https://github.com/home-assistant/os-agent/releases/download/{{
    states('sensor.os_agent_version_github') }}/os-agent_{{
    states('sensor.os_agent_version_github') }}_linux_aarch64.deb<br></br> sudo
    dpkg -i os-agent_{{ states('sensor.os_agent_version_github')
    }}_linux_aarch64.deb
  title: OS_Agent New Version


the result of Plain, Spaced with vars, or compact with variables

complex


Related automations

One Shot notification
This will disappear once you click on it. It will not return until the sensor attribute updates again.

The sensor via github integration



The Automation with variable version number message


alias: Os-Agent Update
description: OS Agent upgrade available
trigger:
  - platform: state
    entity_id:
      - sensor.home_assistant_os_agent_latest_release
    attribute: tag                    ## <<<<<<<<< the attribute 
condition:
  - condition: template
    value_template: >-
      {{ trigger.from_state.attributes.tag != "unavailable" and
      trigger.from_state.attributes.tag != "unknown"  }}
  - condition: not
    conditions:
      - condition: state
        entity_id: sensor.home_assistant_os_agent_latest_release
        attribute: tag           ## <<<<<<<<< the attribute
        state: unavailable
      - condition: state
        entity_id: sensor.home_assistant_os_agent_latest_release
        attribute: tag          ## <<<<<<<<< the attribute
        state: unknown
 
action:
  - service: persistent_notification.create
    data:
      notification_id: os-agent
      title: "There is an update in Os-Agent! "
      message: >-
        OS_Agent {{ states('sensor.home_assistant_os_agent_latest_tag') }} is
        available<br></br> [os-agent
        latest](https://github.com/home-assistant/os-agent) <br></br> wget
        https://github.com/home-assistant/os-agent/releases/download/{{
        states('sensor.home_assistant_os_agent_latest_tag')
        }}/os-agent_1.4.0_linux_x86_64.deb<br></br> sudo dpkg -i os-agent_{{
        states('sensor.home_assistant_os_agent_latest_tag')
        }}_linux_x86_64.deb<br></br>
mode: single


Critical notification

A notification that comes back and bugs you until you complete the task. Mine checks every morning. If the github version is higher the result will be “True” and then a message will be sent.
The else can be removed, I just had it in there for testing. One day I may set it up to be a clickable action or something. I just cut and past it onto my host console. After reading the changes from the link of course…

The top two are rest sensors.
They check the installed home assistant agent version and github agent version.



template:
    - sensor:
        - name: OS_agent Updater
          unique_id: os_agent_updater_version8123
          icon: mdi:memory
          state: >
             "{{ states('sensor.os_agent_version_github' ) > states('sensor.os_agent_version_installed' ) }}"  


The Automation with variable HOST archtype and version number message


alias: os_agent version check
description: Check if the installed and released os_agent versions match
trigger:
  - platform: sun
    event: sunrise
    offset: "0:59:00"
    enabled: true
  - platform: time
    at: "10:00:00"
    enabled: false
condition: []
action:
  - service: homeassistant.update_entity
    data: {}
    target:
      entity_id: sensor.os_agent_version
  - service: homeassistant.update_entity
    data: {}
    target:
      entity_id: sensor.os_agent_version_github
  - if:
      - condition: state
        state: "\"True\""
        for:
          hours: 0
          minutes: 0
          seconds: 0
        entity_id: sensor.os_agent_updater
    then:
      - service: persistent_notification.create
        data:
          notification_id: os-agent
          message: >-
            OS_Agent {{ states('sensor.os_agent_version_github') }} is
            available<br></br>
            [Latest](https://github.com/home-assistant/os-agent/releases/latest)<br></br>
            wget https://github.com/home-assistant/os-agent/releases/download/{{
            states('sensor.os_agent_version_github') }}/os-agent_{{
            states('sensor.os_agent_version_github')
            }}_linux_{{ states('sensor.host_arch')
          }}.deb<br></br> sudo dpkg -i os-agent_{{
            states('sensor.os_agent_version_github') }}_linux_{{ states('sensor.host_arch')
          }}.deb
          title: OS_Agent New Version
      - service: notify.mobile_app_android_phone
        data:
          message: OS_Agent {{ states('sensor.os_agent_version_github') }} is available
          data:
            clickAction: https://github.com/home-assistant/os-agent/releases/latest
    else:
      - service: persistent_notification.create
        data:
          notification_id: os-agent
          title: OS_Agent was version checked
          message: "{{ now().timestamp() | timestamp_custom('%H:%M:%S %D') }}"
mode: single


Enjoy

falsetrue

edit added Host arch variable to last automation msg.

Thanks

Thanks to @Tryfos yet-another-ignore-previous-unavailable-state

Thanks to @Friedrieck 2020 notify-to-persistent-notification

1 Like

rest sensors…

image

You will need to make a long lived token via your profile button Capture in Home Assistant.
That will need to be saved to to your secrets.yaml

Don’t forget to add the Bearer addition!

rest_bearer: Bearer eyJ0eXAiOi......................

github OS-Agent version tag.
You can use the github integration if you can’t figure out the token for github. You need to :star: the repo.

sensor:                        ##  <<< sensor Only needed for top sensor entry
  - platform: rest
    name: OS Agent Version github
    unique_id: os_agent_rest_github8123
    resource: https://api.github.com/repos/home-assistant/os-agent/releases/latest
    username: !secret GITHUB_USERNAME
    password: !secret GITHUB_BEARER_TOKEN
    authentication: basic
    scan_interval: 604800     ## 1 week of seconds using Home Assistant Core Integration: Update entity
    value_template: "{{ value_json.tag_name | default(None)}}"
    headers:
      Accept: application/vnd.github.v3+json
      Content-Type: application/json
      User-Agent: Home Assistant REST sensor 


OS-Agent Installed on Host

  - platform: rest
    unique_id: os_agent_rest_installed8123
    name: OS Agent Version Installed
    resource: http://localhost:8123/api/hassio/host/info
    value_template: "{{ value_json.data.agent_version }}"
    scan_interval: 604800
    headers:
      Authorization: !secret rest_bearer
      User-Agent: Home Assistant
      Content-Type: application/json


Host Architecture


  - platform: rest
    unique_id: host_arch8123
    name: Host Arch
    resource: http://localhost:8123/api/hassio/supervisor/info
    value_template: "{{ value_json.data.arch }}"
    scan_interval: 604800
    headers:
      Authorization: !secret rest_bearer
      User-Agent: Home Assistant
      Content-Type: application/json


The template sensor

template:                  ##  <<< template Only needed for top template entry
 - sensor:
        - name: OS_agent Updater
          unique_id: os_agent_updater_version8123
          icon: mdi:memory
          state: >
             "{{ states('sensor.os_agent_version_github' ) > states('sensor.os_agent_version_installed' ) }}"

Other available HA endpoints

Regarding

as I don’t like to have that additional piece and also bind to my user account:

What about simply using the Supervisor token as e. g. @CentralCommand does (did) with

(first post, unfold Original Package for HA prior to 2022.4.0, unfold Update Notifications package):

command: 'curl http://supervisor/supervisor/info -H "Authorization: Bearer $(printenv SUPERVISOR_TOKEN)" | jq ''{"newest_version":.data.version_latest,"current_version":.data.version,"update_available":.data.update_available,"addons":[.data.addons[] | select(.update_available)]}'''

Looking at this part:

Authorization: Bearer $(printenv SUPERVISOR_TOKEN)

…which is pretty much following Authentication API | Home Assistant Developer Docs which is linked in the profile section at long lived tokens section at the bottom.

  • Can this be used together with a rest sensor?
  • Or is this specifically only for command_line sensors, as only there the SUPERVISOR_TOKEN is available in the environment the command is run?

Try it…

It’s still a long live token