Last active
April 27, 2024 18:33
-
-
Save sbyx/1f6f434f0903b872b84c4302637d0890 to your computer and use it in GitHub Desktop.
Home Assistant Blueprint: Low battery level detection & notification for all battery sensors
This file contains 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/sbyx/1f6f434f0903b872b84c4302637d0890 | |
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 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 | |
at: !input 'time' | |
condition: | |
- '{{ sensors != '''' and (day | int == 0 or day | int == now().isoweekday()) }}' | |
action: | |
- choose: [] | |
default: !input 'actions' | |
mode: single |
Hi sbyx,
Many thanks for your cool blueprint!
🫶
I must admit: before using it, I've spent a few dark hours in toilets in my life because I only realized too late that the battery of the motion detector was almost empty...
🙈
Anyway, I tried to create a blueprint for an open window notification based on your example, but I have to admit that I'm a real templating noob and have never created a blueprint before.
Of course it doesn't work and is certainly full of bugs: could you please help me out? That would be great!
blueprint:
name: Open Window detection & notification for all contact sensors
description: Regularly test all sensors with 'contact' device-class
for being open and if so execute an action.
domain: automation
input:
time_period:
name: Time period to test on
description: Test is run at configured time period
default: '5'
selector:
number:
min: 1
max: 30
unit_of_measurement: 'min'
mode: slider
step: 1
exclude:
name: Excluded Sensors
description: Contact sensors (e.g. smartphone) to exclude from detection. Only
entities are supported, devices must be expanded!
default:
entity_id: []
selector:
target:
entity:
device_class: binary_sensor
actions:
name: Actions
description: Notifications or similar to be run. {{sensors}} is replaced with
the names of sensors being open.
selector:
action: {}
variables:
time_period: !input time_period
exclude: !input exclude
sensors: "{% set result = namespace(sensors=[]) %} {% for state in states.sensor
| selectattr('attributes.device_class', '==', 'binary_sensor') %}\n {% if 'on' <= state.state
| int and not state.entity_id in exclude.entity_id %}\n
\ {% set result.sensors = result.sensors + [state.name ~ ' (' ~ state.state
~ ' %)'] %}\n {% endif %}\n{% endfor %} {% for state in states.binary_sensor
| selectattr('attributes.device_class', '==', 'binary_sensor') | selectattr('state',
'==', 'on') %}\n {% if not state.entity_id in exclude.entity_id %}\n {% set
result.sensors = result.sensors + [state.name] %}\n {% endif %}\n{% endfor %}
{{result.sensors|join(', ')}}"
trigger:
- platform: time_pattern
minutes: /!input time_period
condition:
- '{{ is_state (sensors != '''', 'on') }}'
action:
- choose: []
default: !input actions
mode: single
Thank you in advance!
Kay
To be honest, I recommend at this point using this package for low battery alerting and sensing; it's more capable and extensible than this blueprint, and covers more of my use cases.
I cant import this blueprint
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi and many thanks for sharing this blue print.
I tested it and it works very well with the usual devices with a corresponding battery display in %
But now I'm facing a problem because I also have devices that don't use the device_class "battery" but "voltage"..... and they are devices with different voltage levels.
This means, for example, some devices work with a 1.5V battery and others with 3V or 9V and therefore all of these device/battery types reach their alarm value at a different voltage level.
For 1.5V devices this could be 1.2V, for 3V devices it could be 2.5V etc...
Now I'm asking myself whether I may have overlooked something that I could use to implement this with this blue print or whether it would be possible to integrate this function into this template?
Or are there other approaches for this?
I would be very happy about any help.
regards,