Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sbyx/1f6f434f0903b872b84c4302637d0890 to your computer and use it in GitHub Desktop.
Save sbyx/1f6f434f0903b872b84c4302637d0890 to your computer and use it in GitHub Desktop.
Home Assistant Blueprint: Low battery level detection & notification for all battery sensors
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 (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
description: Battery sensors (e.g. smartphone) to exclude from detection. Only entities are supported, devices must be expanded!
default: {entity_id: []}
selector:
target:
entity:
device_class: battery
actions:
name: Actions
description: Notifications or similar to be run. {{sensors}} is replaced with
the names of sensors being low on battery.
selector:
action: {}
source_url: https://gist.github.com/sbyx/1f6f434f0903b872b84c4302637d0890
variables:
day: !input 'day'
threshold: !input 'threshold'
exclude: !input 'exclude'
sensors: >-
{% set result = namespace(sensors=[]) %}
{% for state in states.sensor | selectattr('attributes.device_class', '==', 'battery') %}
{% if 0 <= state.state | int(-1) < threshold | int and not state.entity_id in exclude.entity_id %}
{% set result.sensors = result.sensors + [state.name ~ ' (' ~ state.state ~ ' %)'] %}
{% endif %}
{% endfor %}
{% for state in states.binary_sensor | selectattr('attributes.device_class', '==', 'battery') | selectattr('state', '==', 'on') %}
{% if not state.entity_id in exclude.entity_id %}
{% set result.sensors = result.sensors + [state.name] %}
{% endif %}
{% endfor %}
{{result.sensors|join(', ')}}
trigger:
- platform: time
at: !input 'time'
condition:
- '{{ sensors != '''' and (day | int == 0 or day | int == now().isoweekday()) }}'
action:
- choose: []
default: !input 'actions'
mode: single
@KVinS
Copy link

KVinS commented Jul 14, 2024

Did I do something wrong? I had to add the following code to avoid receiving notifications with an empty list:

action:
- choose:
    - conditions: "{{ result|length > 0 }}"
      sequence: !input actions

@korpx
Copy link

korpx commented Aug 27, 2024

Great blueprint @sbyx !

It would be awesome if there were exclude options other than just entities. Labels would be very useful!

I am jusing RFXCom for some devices I have around the house and the only way to painlessly add devices to it is to use "Enable automatic add" but this means that I have 4792 entities(!) from the whole block added with that integration. Needless to say I do not want to receive low battery alarms for all of them :-) And excluding them one by one is not going to work.

If it would be possible to exclude certain labels it would be great. Then I could add a label to all but the entities that actually are mine and get low battery alerts only for those.

Even better would be to have the option to pick a label to include.

@HummelsTech
Copy link

Maybe I'm stupid but is it possible to add the battery type from the battery notes integration to the notification and todo list, so I can immediately
can order the correct batteries.
Thanks in advance for any help

@Memnom
Copy link

Memnom commented Dec 28, 2024

it would be great if these can be include for reference , location and label as batterij info
{{labels(state.entity_id)}}
{{area_name(state.entity_id) }},
{{floor_name(state.entity_id)}}

@WernerModenbach
Copy link

Hi sbyx,

thanks for this great work.
I'd like to use your script for visualizing battery status for all my devices in a battery-state-card or lovelace-auto-entities-card.
Have you ever considered doing that? Or maybe someone else has done it already.
Werner

@saschaludwig
Copy link

Like @Memnom already stated: It would be nice to have at least the area name in the notification so you don't have to rename every battery entity to know where this device is.

@bcutter
Copy link

bcutter commented Jan 27, 2025

Can we please finally - after this was raised already in October 2022 - have an INCLUDE setting?

https://community.home-assistant.io/t/low-battery-level-detection-notification-for-all-battery-sensors/258664/227

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