Home Assistant Notifications (with Sync in Dashboard and Mobile)
I’ve been struggling with Home Assistant notifications. From some searching, it seems other people are also struggling.
A lot of videos online cover basic “How to send a notification to your phone”, but I wanted a whole lot more life-cycle than that. I want to clear them. And I don’t want to have to write a thousand action conditions, either.
I sat down and spent far more time than I’d like to admit to tackling the problem. This might not fit every use-case for every person, but this was exactly the functionality I needed.
Table of Contents
- Changes
- Goals
- Make Notifications
- Push/Sync Notifications with Android Phones
- Dashboards
- Neat Tricks
- Caveats and Possible Improvements
- Super Helpful Tips
- Downloads
- Thanks
Changes
- 2023-01-31: Added a Table of Contents, and added a few layers of nested headers to help organize.
Goals
My basic list of goals were as follows:
-
Notifications that can be displayed in Home Assistant dashboard.
I’m installing wall-mounted tablets. Notifications must be visible and apparent when walking past.
-
Notifications that are also relayed to Mobile (Home Assistant app on Android).
I may not be walking past a tablet (or even home) when a notification occurs.
-
Notifications can be dismissed via automation
My theory of notifications are that they are states that need to be addressed, rather than notices to swipe away. Rather than having to dismiss the “Door Unlocked” or “Washer Finished” notification, the corrective action (Locking the door, opening the washer) should clear the notification. Both on the dashboard, and the phone.
I was solving this with a dashboard cards that lit up when triggered. However, they were always there, even if not valid – i.e., I always have an icon for a washing machine, even if it’s not helpful most of the time.
-
Not needing to update every automation to adjust mobile notification rules.
If I add/change/remove/upgrade my phone, I don’t want to review rules. Ditto for adding my wife to notifications, etc.
-
Do all of the above with NO HACS ADDONS, or at minimum, as few as possible
I don’t have anything against HACS (and I use it). But I also don’t like building a house of cards and weird one-off special purpose cards. Core HA-included functionality should cope with HA upgrades, without issue.
Spoiler alert: No HACS addons are needed for most of this, including the mobile notifications. However, I am using one HACS addon for the Dashboard. It technically isn’t required, but it significantly improves the experience. I’ll detail the dashboard options below.
And as always, there are numerous ways to do anything in Home Assistant. This isn’t “correct” or “incorrect” – merely “solves the intended scenario”.
Note, there are downloads available at the end of this post.
Make Notifications
Notifications from Automations
-
Automations create notifications
Using the
persistent_notification.create
service to create notifications allows you to create notifications that are handled by Home Assistant itself. Setting anotification_id
is very, very important. If you callnotify.persistent_notification
instead, you don’t get to set that.For example, via automation, when my Washing Machine power drops to 0.0W this Action is performed:
- service: persistent_notification.create data: title: Washing Machine message: Load Complete! notification_id: Washing Machine
-
Automations dismiss notifications
Use
persistent_notification.dismiss
to dismiss notifications when the original status has been resolved. As I said earlier, I want notifications to be dismissed by resolving the condition that set it. This requiresnotification_id
, so we can clear specific notifications.When I open my washing machine door, for example, this action is performed:
- service: persistent_notification.dismiss data: notification_id: Washing Machine
You can set up those notifications however you want: one automation that creates/dismisses based on an if/else, multiple automations with complex conditions. You may create a dozen automations creating/dismissing 100 notifications. That’s up to you. The key thing is that you’re creating/destroying them with the persistent_notifications
service.
Push/Sync Notifications with Android Phones
Forward Notifications to Mobile Phones
Here we can create a new automation to forward notifications to the Home Assistant app, installed on my device.
We can trigger an automation whenever the persistent_notification.create
service is used. From there, we can create a mobile notification, and re-use the message, title, and notification_id
from the original notification:
Below is the entire automation used to relay all notifications to my phone:
alias: Notifications - Send To Mobile
description: ""
trigger:
- platform: event
event_type: call_service
event_data:
domain: persistent_notification
service: create
condition: []
action:
- service: notify.mobile_app_pixel_6a
data:
message: "{{trigger.event.data.service_data.message}}"
title: "{{trigger.event.data.service_data.title}}"
data:
tag: "{{trigger.event.data.service_data.notification_id}}"
channel: "{{trigger.event.data.service_data.notification_id}}"
mode: single
You’ll need to add each mobile device you want to send notifications to (eg. Me, wife, etc). However, you don’t have to also do that on the dozens/etc automations for the Washer, Front Door lock, etc.
These don’t need to be mobile notifications, of course. This could relay to any notification channel (XMPP, etc).
Dismiss notifications on Mobile Phones
The Home Assistant Companion App Notification Docs mention that you can actually dismiss notifications as well, by sending a specially-crafted message (clear_notification
) along with, you guessed it, the notification_id
.
One last automation to dismiss the notification from our phones whenever the persistent_notification is dismissed:
alias: Notifications - Dismiss to Mobile
description: ""
trigger:
- platform: event
event_type: call_service
event_data:
domain: persistent_notification
service: dismiss
condition: []
action:
- service: notify.mobile_app_pixel_6a
data:
message: clear_notification
data:
tag: "{{trigger.event.data.service_data.notification_id}}"
mode: single
Again, you’ll have to duplicate the action for additional devices.
Now, while the notifications themselves could be sent to any notification provider, these clear_notification
messages are specific to the companion app on Android – The docs mention this doesn’t work on iOS.
Dashboards
Notifications in Dashboard
The built-in persistent notifications are not very noticeable in Home Assistant (little bell icon in the side bar gets a coloured dot on it). This was insufficient for me.
However, we can put notifications right in the dashboards with two options:
Without HACS
Without HACS, we can use an entity-filter
card. This has a significant downside in that you need to pre-enumerate every notification_id
you use. If you want this widget on multiple dashboards, adding a new notification is a significant chore.
I don’t suggest actually using this, except as an example that it can be done
- type: entity-filter
entities:
- persistent_notification.abc
secondary_info: last-changed
type: attribute
attribute: message
- persistent_notification.xyz
secondary_info: last-changed
type: attribute
attribute: message
- persistent_notification.zzz
secondary_info: last-changed
type: attribute
attribute: message
state_filter:
- notifying
card:
type: entities
title: Manual Filter Entities w/ Secondary
You must use the entity-filter card with filter based for ’notifying’ state. This will exclude notifications that don’t currently exist. If we used a plain entities
card, you’ll get errors for every notification not currently active.
With HACS
The much better option is to use Auto-Entities via HACS. It provides some significant benefits:
-
filter includes, not just excludes. Additionally, we can use a wildcard. This makes this a generic card now. You can drop it on many dashboards without having to touch it again in the future.
-
It has a
show_empty
value, which makes the whole widget disappear if empty. -
It has a
empty
value, which lets you select an alternate card to display if empty (vsshow_empty
, above) -
It lets you use any container-type card (grid, vertical-stack, etc), combined with any card for the entities. This gives you much more style flexibility.
You can see why I compromised on my “No HACS” goal for this point.
And this, much simpler yaml:
- type: custom:auto-entities
card:
type: vertical-stack
title: Notifications in Vertical Stack of Cards
card_param: cards
filter:
include:
- entity_id: persistent_notification.*
options:
type: entity
attribute: message
sort:
method: last_triggered
Gives us this much, much nicer list. Also note my Washing Machine notification is here. It wasn’t in the previous example because I hadn’t hard-coded it.
Home Feed Card
There’s also a Home Feed Card in HACS specifically to display persistent_notifications. However, it’s style didn’t match my dashboard. I may have grown to like it, but I’m very happy with the above solution.
My Dashboard (Vertical)
Here’s a screenshot of my (still in progress) main dashboard, with notifications displayed in a vertical -stack of cards on the left. This gives me very visible notifications when present:
When there are no notifications, that space is used instead for a weather forecast.
I am using two frontend addons from HACS on this dashboard, however.
-
layout-card to gridify my dashboard but with grid layout and placement control.
-
Canary, allowing some basic css styles (
canary_style
)Canary is not necessarily the best option for styles, but it is the one I was experimenting with.
My Dashboard (Horizontal)
An alternative dashboard I’m considering is top-notifications that disappear entirely when clear:
When there are no notifications, that space is reclaimed by the rest of the dashboard.
Neat Tricks
-
Since we set a
channel
on the Mobile Notification, we can actually long-press on the notification and adjust notification settings (on/off, silent, pop-up on screen) on your phone itself. These are configured per-channel.This also means your
notification_id
values should be user-readable, since we re-used these for Channel IDs. Don’t usexyz3
like my test data, because these channels never go away once seen, and your notification settings will look like this:This may also be Android-only. Check the docs.
-
Since we set a
tag
on the Mobile Notification, re-sending a notification replaces the old one with the sametag
, rather than giving you a second notification. I’m re-usingnotification_id
here as well.
Caveats and Possible Improvements
-
It sucks to have to list every mobile device in the two forwarding automations. It would be nice to have some logic that can do a wildcard match and loop. Since I have only two phones to configure, but many notifications, this was good enough.
-
You could add key words to message/title/notification_id (ex: “Urgent:” or “- Mobile”), then add a condition to only forward notifications to phones if that key word is there.
I may do this in the future. There’s some notifications that will be irrelevant unless you’re actually home.
-
If you manually dismiss a notification in Home Assistant, the notification is dismissed on mobile.
However, if you “Dismiss All” from the Home Assistant sidebar, only one notification is actually dismissed on the phone. This may be a fixable problem, but that’s not a problem I’m concerned about since the side bar will probably be hidden on my wall tablets.
Super Helpful Tips
There are a bunch of docs and tools I used to accomplish this:
-
Home Assistant’s Developer Tools > Events Tab
Helped clue me in that I could trap the service call when creating/dismissing persistent notifications
-
Logbook
Many log items are just notices that something updated with no further data. However, if you click on an ‘Automation’ in Logbook, theres a whole lot of detail on what the automation did/didn’t do, as well as the data available to it at each stage.
Once I had the “Send to Mobile” notification triggering correctly (with test content only), I had to figure out which notification actually triggered the automation. I clicked on the Automation in the log, and inspected the first step (Executed).
There’s a tab in the bottom pane called “Changed Variables”. This identifies all of the data available for you that was set when the automation was triggered. This is how I was able to identify how to reference the message/title/notification_id data that was passed to the service call.
-
Notification documentation for the Companion App (Explained tags/channels, and a ton of other features)
https://companion.home-assistant.io/docs/notifications/notifications-basic/
Downloads
Here’s some examples you can download and shove into your HA to test.
-
The dashboards require some HACS addons as-is, but no actual customization is needed for them to display notifications. (Obviously, the buttons won’t work for you)
-
automation-send-to-mobile.yaml
You’ll need to adjust the service line to reference your phone’s notification service. And/or add additional actions for additional phones.
-
automation-discard-to-mobile.yaml
You’ll need to adjust the service line to reference your phone’s notification service. And/or add additional actions for additional phones.
Thanks
I hope this will be helpful to others.