-
-
Save davet2001/5c671f5dda1194758e40e3586f9e5198 to your computer and use it in GitHub Desktop.
blueprint: | |
name: Low battery level detection & notification for all battery sensors | |
description: Regularly test all sensors with 'battery' device-class for crossing | |
a certain battery level threshold and if so execute an action. | |
domain: automation | |
input: | |
threshold: | |
name: Battery warning level threshold | |
description: Battery sensors below threshold are assumed to be low-battery. | |
default: 20 | |
selector: | |
number: | |
min: 5.0 | |
max: 100.0 | |
unit_of_measurement: '%' | |
mode: slider | |
step: 5.0 | |
time: | |
name: Time to test on | |
description: Test is run at configured time | |
default: '10:00:00' | |
selector: | |
time: {} | |
day: | |
name: Weekday to test on | |
description: 'Test is run at configured time either everyday (0) or on a given | |
weekday (1: Monday ... 7: Sunday)' | |
default: 6 | |
selector: | |
number: | |
min: 0.0 | |
max: 7.0 | |
mode: slider | |
step: 1.0 | |
notify_device: | |
name: Device to notify | |
description: Device needs to run the official Home Assistant app to receive notifications | |
selector: | |
device: | |
integration: mobile_app | |
variables: | |
day: !input 'day' | |
threshold: !input 'threshold' | |
counttotal: >- | |
{% set data = namespace(battery_list=[]) %} | |
{% for batt in states.sensor | selectattr('attributes.device_class','eq','battery') -%} | |
{% set data.battery_list = data.battery_list + [{'name': batt.attributes.friendly_name, 'level_int': batt.state|int(0), 'level_str': batt.state}] %} | |
{%- endfor %} | |
{{ data.battery_list | count }} | |
countlow: >- | |
{% set data = namespace(battery_list=[]) %} | |
{% for batt in states.sensor | selectattr('attributes.device_class','eq','battery') -%} | |
{% set data.battery_list = data.battery_list + [{'name': batt.attributes.friendly_name, 'level_int': batt.state|int(0), 'level_str': batt.state}] %} | |
{%- endfor %} | |
{{ data.battery_list | |
| selectattr('level_int', 'lessthan', threshold) | |
| list | |
| count }} | |
listtextlow: >- | |
{% set data = namespace(battery_list=[]) %} | |
{% for batt in states.sensor | selectattr('attributes.device_class','eq','battery') -%} | |
{% set data.battery_list = data.battery_list + [{'name': batt.attributes.friendly_name, 'level_int': batt.state|int(0), 'level_str': batt.state}] %} | |
{%- endfor %} | |
{% for batt in data.battery_list | |
| sort(attribute='level_int') | |
| selectattr('level_int', 'lessthan', threshold) | |
%} | |
{{ batt.name }} is {% if batt.level_int != 0 -%} | |
{{ batt.level_int }}% | |
{%- else -%} | |
{{ batt.level_str }} | |
{%- endif -%} | |
{%- endfor %} | |
notification_message: "{% if countlow > 0 -%}{{countlow}} out of \ | |
{{ counttotal }} device batteries are low: | |
{{listtextlow}} | |
{%- else -%} | |
All {{counttotal}} device batteries are ok! | |
{% endif %}" | |
trigger: | |
- platform: time | |
at: !input 'time' | |
condition: | |
- condition: template | |
value_template: '{{ sensors != '''' and (day | int == 0 or day | int == now().isoweekday()) | |
}}' | |
action: | |
- device_id: !input 'notify_device' | |
domain: mobile_app | |
type: notify | |
title: 'Battery Check' | |
message: '{{ notification_message }}' | |
- service: persistent_notification.create | |
data: | |
title: 'Battery Check' | |
message: '{{ notification_message }}' | |
notification_id: battery-check |
Hi All, been a long time since I touched this. Yes, I found a problem recently as well. I think the entity_id attribute got removed from one of the structures I was using.
Also @Erickclee there were some shortcuts being taken here for sure - e.g. the list was being sorted by string version of battery percentage, meaning silly sort orders like 0, 1, 100, 12, 3, 50, 9. I've fixed that now.
Also, important: I've made it handle unavailable sensors now, which was a niggle all along. Previously, once a battery got so low it went offline, it disappeared from the list. Now it shows at the top of the list.
The end result for me and I expect many others, is that this blueprint will now show you a whole load of long since forgotten devices, and probably some devices reporting batteries that don't even have them. That prompted me to remove lots of old devices from my setup and 'un enable' bogus battery sensors in the config. Please re-import and comment back here if you have any further issues.
Thanks!
For anyone else just finding this, I don't think it works any more. Devices all report as being fine even if they fall below the threshold you set.