Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save NateEaton/287caef748ad6d3bd8cf173afe649bdc to your computer and use it in GitHub Desktop.
Save NateEaton/287caef748ad6d3bd8cf173afe649bdc 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.
Forked from version by Schm1tz1 which was forked by original by sbyx.
- changed results list delimiter from comma to newlines
- added persistent notification action (but still support additional actions)
- excludes mobile device battery sensors (adapted from blueprint by Marck)
For any battery sensors that are not detected, use Customize to set
device_class = battery.
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/NateEaton/287caef748ad6d3bd8cf173afe649bdc
variables:
day: !input day
threshold: !input threshold
exclude: !input exclude
sensors: >-
{% set result = namespace(sensors=[]) %}
{% for state in states.sensor
| rejectattr('attributes.device_class', 'undefined')
| selectattr('attributes.device_class', '==', 'battery') %}
{% if 0 <= state.state | int(-1) < threshold | int
and not state.entity_id in exclude.entity_id
and state.entity_id not in integration_entities("mobile_app") %}
{% set result.sensors = result.sensors + [state.name ~ ' ' ~ state.attributes.battery_type ~ ' (' ~ state.state ~ ' %)'] %}
{% endif %}
{% endfor %}
{% for state in states.binary_sensor
| rejectattr('attributes.device_class', 'undefined')
| 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('\n')}}
trigger:
- platform: time
at: !input time
condition:
- '{{ sensors != '''' and (day | int == 0 or day | int == now().isoweekday()) }}'
action:
- choose: []
default: !input actions
- service: persistent_notification.create
data:
title: Low Battery Level Warning
notification_id: LowBatteryLevelWarning
message: |-
**Battery level below {{threshold}}%:**
{{sensors}}
mode: single
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment