Carpe Diem:15 September, 2020

Forwarding Persistent Notifications

This post may contain affiliate links. Please see our disclaimer for more info.

Greetings readers! As a father of a 2 yr old and a 1 month old, I’m finding it difficult to find free time these days. This is in addition to trying to keep my sanity. As a way to still contribute regularly, I’m adding a new section to the techdout.dev blog to cover Home Assistant quick tips/enhancements. The scope of these quick tips will be much smaller than the traditional project posts.

First up – forwarding persistent notifications to another notification service via Node Red.

Persistent Notifications – I have 3 of them!

Home Assistant has a persistent notification service that is used to communicate various alerts to the front end. You’ve seen these before in the main front end – they pop up in the corner and notify you about various things. While these are great, I’m not always looking at my front end and often miss these. Most of my home automation is, well… automated, requiring little input from me. To solve for missing these, I’ve created a simple automation to forward these to my phone.

This automation works by listening for certain events from the event bus, then filtering out everything but persistent notification service calls and then forwarding those details to a notify service. In my case, I’m using the iOS companion to send to my iPhone, but any notification platform could work.

I’m using Node Red for this automation (and 99% of all my automations – I highly reccomend), however you can achieve this using automations.yaml. Shoot me a comment if you need some help figuring this out.

Download the node red template here. Or see below for the function code to copy in yourself.

Below is the javascript with the function node:

if (msg.payload.event.domain == 'persistent_notification' &&
    msg.payload.event.service == 'create')
    // this is a persistent notification
    {
    return msg.payload;
} else {
    // ignore
    return null;
}

And the JSON within the notify service call node:

{
    "title": "Persistent: {{event.service_data.title}}",
    "message": "{{event.service_data.message}}"
}

Pretty simple right? It took a little finesse to figure out how to extract the correct message details in Node Red, but I’m pleased with the solution. To test that it’s working correctly, head to the developer tools > services and call persistent_notification.create with service data message: persistent test!

Leave a Reply

Your email address will not be published. Required fields are marked *