I use notifications all of the time, and they’re very powerful. But they can also be very hard to use. Every time I create a new notification I have to look up the YAML for specifying a tag or group, and more advanced features like critical notifications, badging, or including an image are even more complex. Features vary by platform, and you often have to carefully read through the developer docs multiple times to understand all of the capabilities.
To make this easier, I created a script with as many of the notification features as possible. You can send and update notifications, remove notifications, set badge counts, and much more. The notifications also support actions, attachments, and even triggering Siri Shortcuts. The script has quite a few fields (I wish I could dynamically populate the script based on your selection), but I still think it’s far easier to use since you only have to set cross-platform features once, even if the syntax is different per-platform.
Note: I primarily use iOS, so I haven’t yet added some of the Android-only features like notification LED colors, alert once, etc. But it includes most other features so far. It also supports both mobile (iOS/Android) and HTML5 notifications.
Copy the code for the script to a new script in Home Assistant. Note: You’ll need to edit two things before it’ll work properly.
In the send_to field, open the Selector Options and modify the groups and users to match your setup. I’d recommend creating notification groups in notify.yaml to make things easier (for example, my notify.notify_family includes notify.notify_william and notify.notify_amy). The values should match the names of the notify services for each group/user/device (minus the “notify.” prefix).
The first action in the sequence is called Map browsers. It maps the same notify entities above to the HTML5 devices you want to send notifications to for that user/group. You’ll need to update this list to match your send_to options above and the device names you specified when enabling HTML5 notifications on each device.
The script should now be working!
The script is unfortunately too long to share on the forum (it’s double the max character limit), but you can download it from here.
I hope this helps a few folks! It’s my first draft, so please let me know if you have any feedback.
First, let me add a few disclaimers. I’ve only been working in Home Assistant for a few weeks and is my first time coding in python, yaml, or jinja. Secondly, I’ve never used github for anything other than downloading various projects. I could not figure out any other way for a general discussion about your project, other than creating an issue.
My questions are:
Are you assuming that this script is implemented through the Home Assistant Automation UI for editing, after creating the new yaml file?
Is your intent that this is a script block that you would call from other automations and pass values to it, or that it essentially becomes a copy and paste code block each time you wish to utilize the notification integration in an automation?
This looks like a great idea and a great start; it’s probably my own lack of real understanding to coding in Home Assistant that leads me to asking these questions.
I also apologize if this is completely the wrong approach to asking these questions.
Yes, you’ll want to create a new script via the UI, go into edit mode, and then paste in the YAML on GitHub. You can then switch back to editing via the UI.
Once you’ve configured the script, you can call it using an action. Just click + Add Action and then search for the name of your script (which is Send notification unless you changed it). You’ll see fields for all of the script options you can use. It’s a long list, but there’s no way to conditionally show script fields.
A couple of issues I’ve been working through while trying to implement your script.
I was getting a lot of errors if some of the values weren’t passed to the script as seen below. These errors were coming when sending notifications to a mix of devices including Android, html5, and tv.
Template variable warning: ‘badge’ is undefined when rendering ‘{{ badge }}’
Other template variables that were throwing this error included:
subtitle
subject
tag
The main error seemed to be the tag reference, which was causing a fatal error and stopping the script. I changed the tag variable definition as follows, which seemed to correct that issue.
tag: '{{ tag | default(null) }}'
I started correcting the other errors by adding similar variable definitions, but I wasn't sure if this was the best way to correct these errors, or if there were a better way to handle situations where you don't want/need to pass every value into the script.
Strange… I’ve been using the script extensively and never hit any errors with the current logic. I’m assuming you haven’t checked the boxes for the optional fields? If so, I can dig in more. It’s possible null is better than None.
FWIW, I recently added a new feature to ignore a specified tag for HTML5 endpoints, so I’ll update the script and ensure that’s not the issue.
So…I’m not a huge fan of using the UI to build automations or other coding functions. I’ve saved your script in config/scripts/send_notification.yaml and include that directory in my configuration.yaml with:
script manual: !include_dir_merge_named scripts
I’m then calling your script from an action in my automation, passing the notification variables I specifically want to deviate from the “default,” primarily the
notification_type
send_to
device_type
title
message
** I’ll use more once I’m more comfortable with it.
That would effectively leave the optional fields unchecked, as far as I can tell.
I’m also using a split configuration for the automations, including separate automation files in my:
config/automations/
using the following include in my configuration.yaml
There’s a very real possibility one of my other automations is negatively impacting this, …thinking the scoping of my variables, although there’s no direct overlap of the tag variable.
I just integrated your changes from yesterday, and reverted back to
tag: '{{ tag | default(none) }}'
The specific warning in the Home Assistant Core log is:
Logger: homeassistant.components.html5.notify
Source: components/html5/notify.py:393
integration: HTML5 Push Notifications (documentation, issues)
First occurred: 8:06:40 PM (1 occurrences)
Last logged: 8:06:40 PM
Callback event payload is not valid: string value is None for dictionary value @ data[‘tag’]. Got None
It’s now a warning, and not causing the script to exit, as it was before. It’s also tagging it to a known issue with the HTML5 push notification translations in HA Core.