This doesn’t feel horribly innovative, but after several hours slogging through documentation and forum posts, I thought it would be worth sharing how I managed to get the notification count in the web app (i.e. persistent notifications) synced up with notifications and badge count on mobile apps. I saw someone had a feature request for HA to do this as part of core, and I really wish the team would do that. Anyway, on to the show.
First, I created a counter helper called counter.notification_count
with an initial value of zero and a step count of 1. I also created a notify group in configuration.yaml
:
notify:
- platform: group
name: all
services:
- service: mobile_app_my_ipad
- service: mobile_app_my_iphone
Then I created an automation that triggers on the persistent_notification.create
event that increments the counter and then sends the message via notify.all
:
alias: Notify Forward
description: ""
trigger:
- platform: event
event_type: call_service
event_data:
domain: persistent_notification
service: create
condition: []
action:
- service: counter.increment
data: {}
target:
entity_id: counter.notification_count
- service: notify.all
data:
message: "{{trigger.event.data.service_data.message}}"
title: "{{trigger.event.data.service_data.title}}"
data:
push:
badge: "{{states('counter.notification_count')}}"
mode: queued
and one that triggers on the persistent_notification.dismiss
event that decrements the counter and then sends a message to notify.all
that will change the badge count on the mobile devices:
alias: Notify Delete
description: ""
trigger:
- platform: event
event_type: call_service
event_data:
domain: persistent_notification
service: dismiss
condition: []
action:
- service: counter.decrement
data: {}
target:
entity_id: counter.notification_count
- service: notify.all
data:
message: delete_alert
data:
push:
badge: "{{states('counter.notification_count')}}"
mode: queued
The mode
for the Notify Delete
automation is SUPER IMPORTANT. If you leave it in single
mode the dismiss all
button in the web app notification panel will only decrement the counter by one. In queued
mode dismiss all
will properly decrement the counter based on the number of notifications you are dismissing. Yup, that was an hour of my life I’ll never get back.
And to bring it all together, here’s an automation that sends me a notification if any of my batteries get low. The binary sensors are templated so that they turn on when the battery gets low (and have friendly names that make it easy to just use them as the basis of the message) and aren’t really in scope for this project share.
alias: Notify on Low Batteries
description: ""
trigger:
- platform: state
entity_id:
- binary_sensor.bedroom_left_alert
- binary_sensor.bedroom_center_alert
- binary_sensor.bedroom_right_alert
- binary_sensor.lanai_door_alert
- binary_sensor.lanai_left_alert
- binary_sensor.lanai_left_back_alert
- binary_sensor.lanai_right_alert
- binary_sensor.lanai_right_back_alert
- binary_sensor.dining_left_alert
- binary_sensor.dining_center_alert
- binary_sensor.dining_right_alert
- binary_sensor.study_left_alert
- binary_sensor.study_right_alert
- binary_sensor.batt_ecowitt_bedroom
- binary_sensor.batt_ecowitt_study
- binary_sensor.batt_ecowitt_outdoor
to: "on"
condition: []
action:
- service: persistent_notification.create
data:
title: Low Battery
message: "{{ trigger.to_state.name }} is low"
mode: queued
I hope someone else finds this useful.