Now that I have the excellent Home Assistant monitoring ever more things around the house, I figured it'd be useful if it could pipe up and let me know when specific things need attention. Home Assistant isn't geared up for massive state-based alerting/alarming like an IT monitoring platform but it does have some building blocks that can be configured to send me a notification.
Configuring this is a little fiddly and requires diving into YAML. This page documents my first attempt at getting this working, there are quite possibly other, better, ways to achieve the same. If I find any, I'll update.
The basics
Creating a notification upon an alarm requires:
An alarm that is raised when the state of something meets a value, or a sensor value crosses a threshold.
Somewhere to send the notification, for example a pop-up notification on the mobile phone app.
An alert to respond to the alarm and send the notification.
My requirement
Home Assistant should notify me when the temperature in my fridge/freezers is too high. Perhaps I've left the door open?
Create binary sensors for alarm conditions
We need an entity that is switched on/off by a device's state matching a given value. I'll call this an alarm, though this isn't official Home Assistant nomenclature.
In Home Assistant, we use a Templated Binary Sensor, in other words, a virtual entity that has an on/off state (the binary sensor) controlled by a template. It's possible to define a binary sensor without a template but that only gives basic state comparison. Using a template allows us to define a pattern to preprocess a sensor value before the comparison. It is also possible to add a time value to stop false alerts on transient sensor readings.
We make use of the Template integration to achieve this. The Template integration is not configured by the UI (at present) so requires editing the configuration.yaml file. I use the File Editor integration for this, this blog contains a starter guide.
The following configuration.yaml statement defines:
A template integration..
that defines a binary sensor..
whose value is set to on if a monitored sensor value..
when converted to a floating point number..
is greater than a threshold of 10 (degrees)..
for more than two minutes.
template:
- binary_sensor:
- name: "Alarm: Kitchen fridge is warm"
unique_id: alarm.kitchen_fridge_warm
state: "{{ states('sensor.esp_kitchenfridge_temp_01') | float(default=0) > 10 }}"
delay_on: "00:02:00"
device_class: heat
Defining the device_class for the binary sensor means Home Assistant automatically gives this alarm a suitable icon on the GUI - see here for all the possibilities.
Set up somewhere to receive notifications
When you connect a mobile phone app to Home Assistant the Mobile App integration is automatically added. As well as a myriad of useful/interesting sensors reporting from the phone into Home Assistant, this integration also provides a straightforward Notify service which pops a message up on your phone.
To test it, check your phone is connected then use Developer Tools / Services and search for the service Notify.mobile_phone_name. In my case my phone is called giles_pixel_6_pro and the resulting notify service is notify.mobile_app_giles_pixel_6_pro.
Enter a little message and click CALL SERVICE. All being well you'll get a popup on your phone. Clicking on the notification takes you to the Home Assistant app.
In addition to the mobile app there are many other integrations that provide notification receivers. I look forward to playing with some of these in the future.
Create the alert to join the two
Now we have the alarm raising a binary sensor state, and a notification service to receive a notification, we join the two of them using the Alert integration. This is also configured in YAML in configuration.yaml. There are lots of options for the alert including settings for repeating alerts, how often to send repeats, whether to skip the first notification etc.
The following configuration.yaml statement defines:
An alert
Triggered by the binary sensor transitioning to its on state
to send the message "Garage freezer is over temperature" to my phone app
That will send a notification every 30 minutes
alert:
garage_freezer_overtemp:
name: 'Alert: Garage freezer hot'
entity_id: binary_sensor.alarm_garage_freezer_is_warm
message: 'Garage freezer is over temperature'
state: "on"
repeat: 30
can_acknowledge: true
skip_first: false
notifiers:
- mobile_app_giles_pixel_6_pro
The documentation says it's possible to acknowledge notifications which stops them from repeatedly notifying you. I haven't made that bit work yet.
You could do some of this with an Automation, and indeed hooking an Automation off the binary sensor's state change is how you'd automate some actions to execute when the alarm triggers (change your smart lights to red perhaps?). However the repeat notification feature is the special sauce that the Alert integration offers.
Time to test
That is all the configuration: Templated binary sensor, notification receiver and alert.
All three parts (the monitored sensor, the binary sensor 'alarm' and the alert) appear in the Entities list:
To test, I just warmed up the freezer sensor a little. Two minutes after the temperature crossed the threshold the alarm triggered (note the little red 'fire' icon - that amuses me):
And I got a popup message on my phone:
Hooray! Success.
Dashboard
Finally, although the Home Assistant mobile app notification popup opens the app when it's tapped, once in the app there is (seems to be) no way of seeing what the notification was. This doesn't feel quite right, I think there should be a way to see and acknowledge notifications, but so far I haven't found it, or the setting that enables it.
Instead I set up a simple alarms display to show the status of the alarms, so I have a quick at-a-glance dashboard view to check should I receive a notification. This is using the Glance card at the moment, however this feels like a good situation to use the Entity Filter card on the GUI, to have a dynamic list of entities that only shows those entities that are in the 'alarming' state
Comments