Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andyblac/35b4164355dbef4f998f358353278236 to your computer and use it in GitHub Desktop.
Save andyblac/35b4164355dbef4f998f358353278236 to your computer and use it in GitHub Desktop.
Home Assistant Blueprint: Low battery level detection & notification for all battery sensors
blueprint:
# Version: 1.3.1
name: Low Battery and Device Unavailable Check
description:
Regularly test all sensors with 'battery' device-class for crossing
a certain battery level threshold as well as if it is unavailable.
domain: automation
input:
threshold:
name: Battery warning level threshold
description:
Battery sensors below threshold are assumed to be low-battery (as
well as binary battery sensors with value 'on').
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: 0
selector:
number:
min: 0.0
max: 7.0
mode: slider
step: 1.0
exclude:
name: Excluded Sensors. (Optional)
description: This will ingore these entities in the auto detection list,
battery sensors only are supported (e.g. smartphone).
!! Only entities are supported, devices must be expanded !!
default:
entity_id: []
selector:
target:
entity:
device_class: battery
include:
name: Included Only Sensors. (Optional)
description:
This bypasses the auto detection and only checks against included entities,
battery sensors only are supported (e.g. motion sensor) to included in the check.
!! Only entities are supported, devices must be expanded !!
default:
entity_id: []
selector:
target:
entity:
device_class: battery
unavailable:
name: Report if a device is unavailable. (Optional)
description:
List any devices that are unavialble, possibly due to offline device, or a flat battery,
some devices even though they will show like 40% battery could very well die the next day.
default: "battery"
selector:
select:
options:
- label: Include all unavailable devices (only works when not using Include Only).
value: "all"
- label: Include battery only unavailable devices.
value: "battery"
- label: Don't include unavailable devices.
value: "disabled"
notify_types:
name: Notification type.
description: Choose what type of notification you require.
default: mobile
selector:
select:
options:
- label: Mobile
value: mobile
- label: Persistent Notification
value: persistant
- label: Telegram
value: telegram
sort: false
custom_value: false
multiple: true
notify_device:
name: Devices To Notify
description: Select the devices to be notified when devices are found..
default: []
selector:
device:
filter:
- integration: mobile_app
multiple: true
telegram_name:
name: Telegram name
description: The name used when setting up Telegram integration.
default: ""
selector:
text:
title:
name: Title
description: The notification title of the low battery message.
default: "Low Battery"
selector:
text:
message:
name: Message
description: The notification message {{sensors}} is replaced with
the names of sensors being low on battery.
default: "{{sensors|default('')}}"
selector:
text:
source_url: https://gist.github.com/andyblac/35b4164355dbef4f998f358353278236
variables:
day: !input day
threshold: !input threshold
exclude: !input exclude
include: !input include
unavailable: !input unavailable
notify_types: !input notify_types
notify_device: !input notify_device
telegram_name: !input telegram_name
title: !input title
message: !input message
sensor_list: >-
{% set result = namespace(sensors=[]) %}
{% if include.entity_id | count > 0 %}
{% for entity in states | selectattr('attributes.device_class', 'eq', 'battery') | selectattr('entity_id', 'in', include.entity_id) %}
{% if 0 <= entity.state | int(-1) < threshold | int %}
{% set result.sensors = result.sensors + [ entity.name ~ ' (' ~ entity.state ~ '%)'] %}
{% endif %}
{% endfor %}
{% if unavailable in ["all","battery"] %}
{% for entity in states | selectattr('state','eq','unavailable') | rejectattr('entity_id', 'in', exclude.entity_id) %}
{% set result.sensors = result.sensors + [ entity.name ~ ' (' ~ entity.state ~ ')'] %}
{% endfor %}
{% endif %}
{% else %}
{% for entity in states | selectattr('attributes.device_class', 'eq', 'battery') | rejectattr('entity_id', 'in', exclude.entity_id) %}
{% if 0 <= entity.state | int(-1) < threshold | int %}
{% set result.sensors = result.sensors + [ entity.name ~ ' (' ~ entity.state ~ '%)'] %}
{% endif %}
{% endfor %}
{% if unavailable in ["all","battery"] %}
{% if unavailable == "all" %}
{% for entity in states | selectattr('state','eq','unavailable') | rejectattr('entity_id', 'in', exclude.entity_id) %}
{% set result.sensors = result.sensors + [ entity.name ~ ' (' ~ entity.state ~ ')'] %}
{% endfor %}
{% elif unavailable == "battery" %}
{% for entity in states | selectattr('state','eq','unavailable') | selectattr('attributes.device_class', 'eq', 'battery') | rejectattr('entity_id', 'in', exclude.entity_id) %}
{% set result.sensors = result.sensors + [ entity.name ~ ' (' ~ entity.state ~ ')'] %}
{% endfor %}
{% endif %}
{% endif %}
{% endif %}
{{result.sensors}}
sensors: "{{sensor_list|join(', ')|replace(', ','\n')}}"
trigger:
- platform: time
at: !input time
condition:
- "{{ sensor_list | length > 0 and (day | int == 0 or day | int == now().isoweekday()) }}"
action:
- choose:
- conditions:
- condition: template
value_template: "{{ 'mobile' in notify_types }}"
sequence:
repeat:
for_each: !input notify_device
sequence:
- service: "notify.mobile_app_{{ device_attr(repeat.item, 'name') | slugify }}"
data:
title: !input title
message: !input message
- choose:
- conditions:
- condition: template
value_template: "{{ 'persistant' in notify_types }}"
sequence:
- service: notify.persistent_notification
metadata: {}
data:
title: !input title
message: !input message
- choose:
- conditions:
- condition: template
value_template: "{{ 'telegram' in notify_types }}"
sequence:
- service: notify.{{ telegram_name }}
metadata: {}
data:
message: !input message
mode: single
@Konstantin-Kuzin
Copy link

Can you add Telegram notification - https://www.home-assistant.io/integrations/telegram/?

@taggy99
Copy link

taggy99 commented Feb 20, 2024

Hi great blueprint, how could I amend to get a persistent notification ie add to HA Notifications.
Regards.

@andyblac
Copy link
Author

@Konstantin-Kuzin @taggy99 ok try to update the blueprint from HA, it should now support Telegram and Persistent notifications.

@taggy99
Copy link

taggy99 commented Feb 21, 2024

Brilliant that works treat, thanks. 🤩

@romanpet1
Copy link

romanpet1 commented Mar 1, 2024

line 142 and 153 - can you pls fix typo. replace bettery with battery.

@andyblac
Copy link
Author

andyblac commented Mar 1, 2024

line 142 and 153 - can you pls fix typo. replace bettery with battery.

done, sorry about.

@romanpet1
Copy link

line 142 and 153 - can you pls fix typo. replace bettery with battery.

done, sorry about.

thank you for the fast response. battery only mode works now.

@robertofabrizi
Copy link

I must have misconfigured it, i used Included Only Sensors. (Optional) setting it to a specific thermostat entity, and Include battery only unavailable devices. Yet I got hundreds of entities in the notification body. Thank you very much for this!

@andyblac
Copy link
Author

andyblac commented Mar 1, 2025

I must have misconfigured it, i used Included Only Sensors. (Optional) setting it to a specific thermostat entity, and Include battery only unavailable devices. Yet I got hundreds of entities in the notification body. Thank you very much for this!

did you choose the battery thermostat entity and NOT the thermostat device as devices are NOT supported.

@robertofabrizi
Copy link

I must have misconfigured it, i used Included Only Sensors. (Optional) setting it to a specific thermostat entity, and Include battery only unavailable devices. Yet I got hundreds of entities in the notification body. Thank you very much for this!

did you choose the battery thermostat entity and NOT the thermostat device as devices are NOT supported.

I did

@andyblac
Copy link
Author

andyblac commented Mar 1, 2025

hmm works ok here.

Screenshot 2025-03-01 at 11 18 35

@robertofabrizi
Copy link

robertofabrizi commented Mar 1, 2025

This is how I configured it, to test it out I wanted to receive a notification just for that entity, but it had to include unavailable because it's currently dead battery:
Immagine 2025-03-01 132053

But I get this (that entity was among the ones notified, it's not right now as i did change its batteries)

Immagine 2025-03-01 132318

@andyblac
Copy link
Author

andyblac commented Mar 1, 2025

you need to enable "Don't include unavailable devices", as this is separate to battery entities.

@robertofabrizi
Copy link

you need to enable "Don't include unavailable devices", as this is separate to battery entities.

If i set it to "Don't include unavailable devices", the notification for the thermostat entity targeted doesn't arrive (it's currently unavailable since the batteries have died), actually if i set that the automation doesn't trigger at all. If i switch back to include battery only unavailable devices, it triggers and i get all those extra entities too.

@andyblac
Copy link
Author

andyblac commented Mar 1, 2025

thats correct behaviour, as the automation only checks ENTITIES for low batteries, the unavailable devices check is NOT linked to entities they are 2 separate functions, ie the automation does NOT look at entities only for unavailable.

as your device batteries are dead the battery entity can not be checked for low battery.

@robertofabrizi
Copy link

So to get notified if a specific battery device has died (some go from 40% battery to dead the day after) i must get all those other drvices too? :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment