Forked from sbyx/low-battery-level-detection-notification-for-all-battery-sensors.yaml
Last active
August 24, 2021 17:52
-
-
Save jeanmi-git/f2e4e6863f1a6d63d311cc71b5885b38 to your computer and use it in GitHub Desktop.
Home Assistant Blueprint: Low battery level detection & notification for all battery sensors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/jeanmi-git/f2e4e6863f1a6d63d311cc71b5885b38/edit | |
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 state.state | int < threshold and not state.state == 'unavailable' %} | |
{% set result.sensors = result.sensors + [state.name ~ ' (' ~ state.state ~ '%)'] %} | |
{% endif %} | |
{% endfor %} | |
{% for sensor in states.binary_sensor %} | |
{% if 'battery_level' in sensor.attributes %} | |
{% if ((state_attr(sensor.entity_id, 'battery_level')-2) |float/0.01)|round(1) | int < threshold and not sensor.state == 'unavailable' %} | |
{% set result.sensors = result.sensors + [sensor.name ~ ' (' ~ ((state_attr(sensor.entity_id, 'battery_level')-2)|float/0.01)|round(1) ~ '%)'] %} | |
{% endif %} | |
{% endif %} | |
{% endfor %} | |
{% for sensor in states.camera %} | |
{% if 'battery_level' in sensor.attributes %} | |
{% if state_attr(sensor.entity_id, 'battery_level') | int < threshold and not sensor.state == 'unavailable' %} | |
{% set result.sensors = result.sensors + [sensor.name ~ ' (' ~ state_attr(sensor.entity_id, 'battery_level')~ '%)'] %} | |
{% endif %} | |
{% 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' | |
mode: single |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment