Get notified any time there is a new Home Assistant alert

My Home Assistant

I accidentally broke Home Assistant updating to docker 20.10 on my supervised install. That would’ve never occurred had I seen the a Home Assistant alert about 3 days earlier. So I decided to keep that from happening again by setting up HA to alert me any time a new HA alert pops up. And since we now have blueprints, seemed like a great opportunity to try them out.

:warning: Additional Dependency

In order to make this blueprint work you also must set up feedreader to watch the HA alerts feed, the events this integration fires are what triggers the automation. To do that, add the following to your configuration.yaml:

feedreader:
    urls:
    - https://alerts.home-assistant.io/feed.xml

This will check the feed once an hour by default, you can adjust the scan_interval if you prefer a quicker response time. Restart after, then you can use this blueprint.

Notes

There’s a few features missing that I hope get added in the future to make this easier to use:

  1. There’s currently no way to make a service selector to let users pick a service of type notify so you’ll have to type in the notifier name. I know a lot of other blueprints ask users to select a device of type mobile_app but I want to create a persistent notification by default. Plus I want to use this blueprint and I don’t use mobile app notifications
  2. The Additional Data option is really scrappy, you have to enter the data in JSON format in the box. I hope a dictionary type selector is added in the future so this can become YAML entry.

Blueprint

blueprint:
  name: Send Notification for Home Assistant Alert
  description: Send notification on your preferred channel when there is a new Home Assistant alert published
  domain: automation
  input:
    notifier:
      name: Notifier
      description: 'Name of integration you prefer to use for notifications (i.e. what comes after `notify.` in the service call). Defaults to `persistent_notification`'
      default: persistent_notification
    title:
      name: Notification title
      description: 'Title of notification when there is an alert. Defaults to "New Home Assistant alert"'
      default: New Home Assistant alert
    urls_supported:
      name: URLs are supported
      description: 'True if your notification platform supports clicking the notification to open a URL, false to just include the alert URL in the message body'
      selector:
        boolean:
      default: false
    additional_data:
      name: Additional data for notification
      description: 'Additional data to include in service call if your notification platform has extended functionality. Will need to write as a JSON object (ex. `{"badge":"https://alerts.home-assistant.io/images/favicon.png", "priority":"high"}`)'
      default: "{}"
    feed_url:
      name: Feed URL
      description: 'Used for testing automation by setting to a custom URL so you can create fake HA alerts and see it in action. Leave blank otherwise so it defaults to the normal HA alert feed'
      default: 'https://alerts.home-assistant.io/feed.xml'

mode: parallel

variables:
  notifier: !input notifier
  urls_supported: !input urls_supported
  additional_data: !input additional_data

trigger:
  platform: event
  event_type: feedreader
  event_data:
    feed_url: !input feed_url

action:
- service: "notify.{{ notifier }}"
  data:
    title: !input title
    message: "{{ trigger.event.data.title + ('' if urls_supported else '\n' + trigger.event.data.link) }}"
    data: "{% set data = additional_data | from_json %}{% if urls_supported %}{{ dict(data, url=trigger.event.data.link) }}{% else %}{{ data }}{% endif %}"
4 Likes

Many thanks for sharing this! I’ve experienced the same thing and boy did I feel stupid when I saw the alert. Especially since I broke a lot more trying to get home assistant back up again.

FYI, had to update the blueprint. The template I was using for setting additional data in the notify service call was getting butchered on import and no longer outputting a dictionary correctly. I fixed it and tested it and its working correctly now.

Hooray, it worked! Got the HomematicIP alert. I had tested it with a local clone of the feed a bunch but this is the first actual alert lol

Dunno how many people are using this but for anyone wondering why they never got a notification for Security Disclosure 2, its because the feed entry for the first Security Disclosure was updated without changing the date fields instead of a new one being posted. Feedreader looks at these fields and so it didn’t pick up the change to the entry.

I created an issue about it. Hopefully the HA team will change this pattern when it comes to updates to existing alerts otherwise not entirely sure what can be done here.

4 Likes

Yep. Not using the blueprint but same problem. Thanks for creating the issue.

I tried to use this blueprint and I also integrated feedreader before doing so. Unfortunately manually triggering the automation does not work, HA log shows:

Error executing script. Error for call_service at pos 1: Error rendering data template: UndefinedError: 'trigger' is undefined
and
Error while executing automation automation.notify_system_new_home_assistant_alert: Error rendering data template: UndefinedError: 'trigger' is undefined

feedreader-Settings:

feedreader:
  urls:
    - https://alerts.home-assistant.io/feed.xml
  scan_interval:
    hours: 6
  max_entries: 10

Won´t feeds be pulled initially after added to yaml and HA has been restarted? Or would it take 6 hours?

It doesn’t really work like that. On startup the feedreader integration builds a cache of the rss feed as it exists then. Then it polls that feed and any time a new event appears it creates an event of type feedreader. The automation this blueprint makes listens for those events and notifies you when it gets them.

So it really doesn’t do anything on startup or when manually triggered. It only does something when a new alert appears in the rss feed it is watching.

If you want to test it, that’s why I put in that feed_url input. You can clone the alerts RSS feed somewhere (like the www folder of your HA config for example) and then have feedreader and this blueprint watch your cloned feed there. Then you can add a fake alert to the list and see that it sends you a notification.

Well that´s strange. There was a new alert (with URL https://alerts.home-assistant.io/#rituals_perfume_genie.markdown) recently (has been removed meanwhile) - which triggered my blueprint based automation (March 4th 06:17) - but it seems like the blueprint can´t handle my notification group (group of devices) cause I did receive nothing:

That’s not good. I use notification groups for everything too so I don’t think its a problem with all groups, must be something with one of the integrations behind the one you use? Notification groups actually just make a service call to with identical data to every notifier in the group.

Is there any errors in your log that might give a hint about it doesn’t like? Or if you don’t mind sharing what integration(s) you do use for notifications I can take a look at what that particular notification service lists as allowed inputs and see if I can piece together what happened.

Well I use the Home Assistant Companion App, sometimes for a single device, sometimes for a group of devices. E. g. notify.my_smartphone or notify.all_devices.

Unfortunately I don´t have logs anymore (HA Core has been restarted meanwhile, don´t know if and where former logs are stored, running HASS OS). Did not dive into the local xml thing yet otherwise I could recreate the situation. Do you have an example XML where I just need to add the timestamp so a new feed element gets detected and the automation triggers?

Actually the easiest way to get an example URL is to just go to the current URL - https://alerts.home-assistant.io/feed.xml. It’s actually just an XML file at that endpoint so you can download it, store it somewhere locally that HA can access and then tweak away to test scenarios.

Although bear in mind this issue is still open so changing the timestamp or data of an existing entry won’t trigger a feedreader event and an alert. Adding a new one to the top should though.

[EDIT] Oh wait right, changing the timestamp of an existing entry may trigger an alert, I’m not sure. The issue with changing a timestamp is its not a realistic recreation of what alerts.homeassistant.io actually does. Currently the alerts site does not change the timestamps of entries when it updates them, it just updates the data.