I’ve combined the suggestions provided by Petro and Mariusthvdb then added a few of my own ideas to create the following sensor and automation. I’ve tested them and confirmed they work.
This Template Sensor’s state
reports the number of sensors with low batteries. The names of the corresponding sensors are reported in its battery_low
attribute and are stripped of the words “battery level” in order to make them more compact.
The Template Sensor is refreshed by sensor.date
(in other words, once a day at midnight). If that is insufficient, there are other ways to make it refresh more frequently.
sensor:
- platform: template
sensors:
battery_alert:
entity_id: sensor.date
value_template: >
{% set ns = namespace(below=[]) %}
{% for s in states.sensor
if s.entity_id.endswith('battery_level') and s.state != 'unknown' and s.state|int < 50 %}
{% set ns.below = ns.below + [ s ] %}
{% endfor %}
{{ ns.below | count }}
attribute_templates:
battery_low: >
{% set ns = namespace(below=[]) %}
{% for s in states.sensor
if 'battery_level' in s.entity_id and s.state != 'unknown' and s.state|int < 50 %}
{% set ns.below = ns.below + [ s.name[:-14] ~ ' (' ~ s.state ~ '%)'] %}
{% endfor %}
{{ ns.below | join(', ') }}
Here is a sample automation that triggers at 08:30
. If sensor.battery_level
is greater than 0
it proceeds to produce a notification containing the string in the battery_low
attribute. It uses the quantity of low-battery sensors to determine if the message should use the word “is” or “are”.
Be advised that, if there are any low batteries, this automation will produce a notification every morning. The advantage is that if the following day an additional sensor battery becomes low, the notification will include it as well. You will be reminded every morning until the batteries are finally replaced (which, depending on your mood, can be perceived to be either an advantage or disadvantage).
- alias: Report when batteries are low
trigger:
platform: time
at: '08:30:00'
condition:
condition: template
value_template: "{{states('sensor.battery_alert')|int > 0}}"
action:
service: notify.notify
data_template:
title: 'Low Battery Alert'
message: >
{% set phrase = 's are ' if states('sensor.battery_alert')|int > 1 else ' is ' %}
The following sensor{{ phrase }} low: {{ state_attr('sensor.battery_alert', 'battery_low') }}
If you prefer to be notified the instant a battery becomes low, the following automation will do that. However, because of the way a Template Trigger works, it will only notify you once, the moment the battery_alert sensor reports more than 0
. It won’t trigger again if a second or third sensor’s battery becomes low. Before it can trigger again, the battery_alert sensor must first return to 0
.
- alias: Instantly report when a battery is low
trigger:
platform: template
value_template: "{{states('sensor.battery_alert')|int > 0}}"
action:
service: notify.notify
data_template:
title: 'Low Battery Alert'
message: >
{% set phrase = 's are ' if states('sensor.battery_alert')|int > 1 else ' is ' %}
The following sensor{{ phrase }} low: {{ state_attr('sensor.battery_alert', 'battery_low') }}
Here’s an example of the output after changing:
service: notify.notify
to:
service: persistent_notification.create