Send telegrams with important hassio 'persistent notifications'

Posting here in case somebody needs something similar:

Having this in hassio…

How to get informed on your phone?:

First I created a log file that saves every persistent notification that creates home assistatn to a log file. This is taken from VDRainer from here:

That is not needed. I did that to have a record with all notification events, which is always nice to have.

I use splitted configuration files to keep codes organized so, adapt for your case:

###/notify/notify_files.yaml
- name: persistent_notification_to_log
  platform: file
  filename: persistent_notify.log

…and for the automation that saves the file:

###/automations/automations_global.yaml
### PERSISTENT NOTIFICATION TO FILE:
- id: 'save_my_persistent_notifications_to_file_0937700372861'
  alias: Save my persistent notification to file
  trigger:
    - platform: event
      event_type: call_service
      event_data:
        domain: persistent_notification
        service: create
  action:
    - service: notify.persistent_notification_to_log
      data_template:
        message: >
          {{ now().strftime('%Y-%m-%d %H:%M:%S') }}
          {{ trigger.event.data.service_data.title }} /
          {{ trigger.event.data.service_data.message }}

I added an automated process that trims the log file daily to a maximun of 200 lines of text:

###shell_commands/shell_commands_global.yaml
trim_to_200_lines_the_persistent_notifications_log: ssh [email protected] -p 22 -o StrictHostKeyChecking=no -i /my_keyfile 'MYTMP=$(tail -n 5 /usr/share/hassio/homeassistant/persistent_notify.log 2>/dev/null) && echo "${MYTMP}" > /usr/share/hassio/homeassistant/persistent_notify.log'

(note: for my shell commands from hassio, I use to do an ssh out to self linux to take adventages of my already configured paths, commands, etc that’s because I always have had many problems when using the hassio internal shell directly. Not sure if it is a good way of executing commands, but that’s what works for me. Of course you have to check that you can ssh from hassio terminal to your native shell to execute the command (accept keys, etc, of first login).

…now the automation to trim the log daily a t 13:39:43 :crazy_face:

###/automations/automations_global.yaml
- id: 'trim_to_200_lines_daily_the_persistent_notifications_log_2336478374673'
  alias: Trim to 200 lines daily the persistent notifications log
  trigger:
    platform: time
    at: '13:39:43'
  action:
    service: shell_command.trim_to_200_lines_the_persistent_notifications_log

(You can start here for only the telegram part)…now the automation that sends a telegram when a persistent notification, containing a matching string, is received:

###/automations/automations_assistants.yaml
- id: 'send_telegram_when_alexa_authorization_is_needed_3562223673560'
  alias: Send telegram when Alexa authorization is needed
  trigger:
    - platform: event
      event_type: call_service
      event_data:
        domain: persistent_notification
        service: create
  condition:
    ### CHECKS IF THE NOTIFICATION IS FOR AUTHORIZING AN ALEXA DEVICE:
    condition: template
    value_template: >
      {{ 'Reauthenticate' in trigger.event.data.service_data.message }}
  action:
    - service: notify.telegrams_to_myself
      data:
        message: "Alert!. Notification at hassio to re-authorize Alexa, today at {{ now().strftime('%H:%M') }}"

Important thing to have in mind is that the text to be triggered from the persistent notification (‘Reauthenticate’ text in my case for alexa notification) MUST be part of the body of the notification (the small characters), not the bigger font of the title. That’s important because if you search for a string in the title, it will never trigger the alert. It doesn’t have to be just one word… it could be more like ‘Reauthenticate on the Integrations page’.

…of course, you need the notify service for telegram already configured, something like:

###/configuration.yaml
telegram_bot:
  - platform: polling
    parse_mode: html
    api_key: 9876543210987654321abcdefghijklmnopqrstuvw
    allowed_chat_ids:
      - 123456789

and the notify entry:

###/notify/notify_telegram.yaml
- name: telegrams_to_myself
  platform: telegram
  chat_id: 123456789

Configuration → server_controls → Check_configuration button and → if green ok, then → restart_server.

You don’t have to wait for an Alexa authorize message to see if your automation triggers the text string you configured… you could just test with:

Developer Tools → Services → Service: persistent_notification.create → and paste this code to simulate an alexa authorization notification request:

###paste it in the service data field:
{
  "title": "Sample notification",
  "message": "test... Reauthenticate on the Integrations"
}

…and press CALL SERVICE button.

If everything went well, you will see that you phone receives a telegram notification of the alexa addon event.
Hope it helps somebody.

1 Like

This is really great. I moving from Homey to HA and what I am really missing is a notification timeline with only items that you find important to notify. Is there also may be a nice Lovelace card to present this?