Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rmarskell/2ff65fca409e7252e47fb1bc86ad11eb to your computer and use it in GitHub Desktop.
Save rmarskell/2ff65fca409e7252e47fb1bc86ad11eb 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
period:
name: How often to test
description: Test is run on a periodic basis, every x minutes
default: 30
selector:
number:
min: 1
max: 59
unit_of_measurement: minutes
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/rmarskell/2ff65fca409e7252e47fb1bc86ad11eb
variables:
period: !input 'period'
period_repeat: '/{{period}}'
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_pattern
minutes: '{{ period_repeat }}'
condition:
- '{{ sensors != '''' }}'
action:
- choose: []
default: !input 'actions'
mode: single
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment