Github Gist: https://gist.github.com/sbyx/1f6f434f0903b872b84c4302637d0890
Explanation
This is a different take on the whole low battery-level detection use case. Instead of manually listing one or more sensors this blueprint checks all sensors that have the attribute device_class set to ‘battery’ for either crossing below a threshold or being ‘on’ (low-battery binary sensors with class ‘battery’). This means whenever a sensor entity with device_class battery is added to HA it is automatically considered in the next check. Since this is a bit costly action and would probably cause reevaluation often if we were to use this as a trigger and I’m assuming we are dealing with battery lifetimes in the weeks or months range we are only testing this every day or every week at a given time.
Actions to run can be chosen freely and the names of the sensors are available through a template variable {{sensors}} e.g. you can use the following as data section for your notification action:
message: 'Low battery warning for: {{sensors}}'
Some notification providers (e.g. Telegram) expect messages to be valid Markdown. It may help to repace underscore possible underscore characters e.g. using {{sensors|replace("_"," ")}}
instead of just {{sensors}}
.
There is a target input for sensors you want to manually exclude from low-battery detection. It will only work for entities. If you select a device, you need to expand it so the underlying sensor is selected.
Given the nature of this automation this approach has a few gotchas too:
- You may get a notification up to 1 day (or 1 week, if configured) after a sensor actually crossed the threshold.
- You can only apply one threshold for all sensors of class battery, exclusions of sensors are possible based on the exclusion input or by using the customization feature of HA and defining a different device_class for them.
- Similarly if your sensor does not have a device_class of ‘battery’ set it is not considered here. Again using HA’s customization you can manually set a device_class attribute to ‘battery’
- Since the companion app also registers the battery level of the phone you may be default get a notification of your phone being low-battery at the configured time. To avoid this either disable the sensor in the companion app or use manual exclusion or customizations to override the device_class to something other than ‘battery’.
- This currently only works if your battery-level or low-battery state is the actual state of a sensor entity. If your entities just expose the battery level as an attribute this is currently not considered but could be extended to do this.
Blueprint Code
Click the badge to import this Blueprint: (needs Home Assistant Core 2021.3 or higher)
Or import this Blueprint by using the forum topic / Gist URL:
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:
- condition: template
value_template: '{{ sensors != '''' and (day | int == 0 or day | int == now().isoweekday()) }}'
action:
- choose: []
default: !input 'actions'
mode: single
Changelog
- 2020-12-26: Replace exclusion inputs with target selector and report battery level as well where available. Thanks to @CentralCommand @KlausDK
- 2020-12-23: Added 10 exclusion inputs
- 2020-12-22: Initial version