Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save elboletaire/f8acdca8f89ac140894e54e3356d0eae to your computer and use it in GitHub Desktop.

Select an option

Save elboletaire/f8acdca8f89ac140894e54e3356d0eae 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 and Device Unavailable Check (with All-Clear Control)
description:
Regularly test all sensors with 'battery' device-class for crossing
a certain battery level threshold as well as if it is unavailable. Optionally
sends an "all clear" notification.
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:
Removed Battery only selection to select any entity like switches.
Only entities are supported, devices must be expanded!
default:
entity_id: []
selector:
target: {}
suppress_all_clear:
name: Do not send notification when everything's good
description: If checked, the automation will take no action when all sensors are OK.
default: false
selector:
boolean: {}
actions:
name: Actions
description:
Notifications or similar to be run. The `sensors` variable is available
for your message template. It will be empty if everything is fine.
selector:
action: {}
source_url: https://gist.github.com/elboletaire/f8acdca8f89ac140894e54e3356d0eae
variables:
day: !input day
threshold: !input threshold
exclude: !input exclude
suppress_all_clear: !input suppress_all_clear
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.sensor | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', '==', 'battery') %}
{% if "unavailable" in state | string 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 %}
{% for state in states.binary_sensor | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', '==', 'battery') %}
{% if "unavailable" in state | string and not state.entity_id in exclude.entity_id %}
{% set result.sensors = result.sensors + [ state.name ~ ' (' ~ state.state ~ ')'] %}
{% endif %}
{% endfor %}
{% for state in states.switch | selectattr('state','eq','unavailable') %}
{% if state.entity_id not in exclude.entity_id %}
{% set result.sensors = result.sensors + [ state.name ~ ' (' ~ state.state ~ ')'] %}
{% endif %}
{% endfor %}
{{result.sensors|join(', ')}}
trigger:
- platform: time
at: !input time
condition:
- condition: template
value_template: "{{ (day | int == 0 or day | int == now().isoweekday()) }}"
action:
- choose:
# Case 1: There are problematic sensors. Always run the action.
- conditions:
- condition: template
value_template: "{{ sensors != '' }}"
sequence: !input actions
# Case 2: No problematic sensors, AND the user wants an "all clear" notification.
- conditions:
- condition: template
value_template: "{{ sensors == '' }}"
- condition: template
value_template: "{{ not suppress_all_clear }}"
sequence: !input actions
# Default case: No problematic sensors, AND the user has checked the box to
# suppress the "all clear" notification. Do nothing.
default: []
mode: single
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment