Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dskindell/c44b2abf9fcb3fb80715f0f507bd7249 to your computer and use it in GitHub Desktop.
Save dskindell/c44b2abf9fcb3fb80715f0f507bd7249 to your computer and use it in GitHub Desktop.
Low battery level detection & notification for all HA-Battery-Notes battery sensors
blueprint:
name: Low battery level detection & notification for all HA-Battery-Notes battery sensors
description:
Regularly test all ha-battery-notes (https://github.com/andrew-codechimp/HA-Battery-Notes)
sensors with 'battery' device-class for crossing the configured battery level threshold
and if so, execute an action.
domain: automation
input:
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
a list of battery entities being low on battery that include properties {{name}}
{{description}} (which lists the current battery entity name & percentage) and
{{battery_type}} (if supported).
selector:
action: {}
source_url: https://gist.github.com/dskindell/5d19c2c8a833827c1a2d727f7667a3ea
variables:
day: !input day
exclude: !input exclude
sensors: >-
{% set result = namespace(sensors=[]) %}
{% for state in states.sensor
| selectattr('attributes.device_class', '==', 'battery')
| rejectattr('entity_id', 'in', exclude) %}
{% if state.state|int(-1) != -1 and
not state.entity_id in exclude.entity_id %}
{% set low_battery_entity_id = 'binary_' ~ state.entity_id|replace('_battery', '_battery_low') %}
{% set low_battery_plus_entity_id = 'binary_' ~ state.entity_id|replace('_battery', '_battery_plus_low') %}
{% set low_battery_state = states(low_battery_plus_entity_id) if states(low_battery_plus_entity_id) != 'unknown' else states(low_battery_entity_id) %}
{% if low_battery_state == 'on' %}
{% set battery_type = states(state.entity_id ~ '_type')
| replace('_ind', '(industrial)')
| replace('_Li', '(Li-ion)') %}
{% set result.sensors = result.sensors + [{
'name': state.name,
'description': state.name|replace(' Battery', '') ~ ' (' ~ ((battery_type ~ ' at ') if battery_type != 'unknown') ~ state.state ~ '%)',
'battery_type': battery_type
}] %}
{% endif %}
{% endif %}
{% endfor %}
{{result.sensors}}
trigger:
- platform: time
at: !input time
condition:
- "{{ sensors|length > 0 and (day | int == 0 or day | int == now().isoweekday()) }}"
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