Skip to content

Instantly share code, notes, and snippets.

@davet2001
Last active May 12, 2023 00:55
Show Gist options
  • Save davet2001/5c671f5dda1194758e40e3586f9e5198 to your computer and use it in GitHub Desktop.
Save davet2001/5c671f5dda1194758e40e3586f9e5198 to your computer and use it in GitHub Desktop.
blueprint tutorial
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.
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: 6
selector:
number:
min: 0.0
max: 7.0
mode: slider
step: 1.0
notify_device:
name: Device to notify
description: Device needs to run the official Home Assistant app to receive notifications
selector:
device:
integration: mobile_app
variables:
day: !input 'day'
threshold: !input 'threshold'
counttotal: "{{ states.sensor \
| selectattr('attributes.device_class','eq','battery') \
| sort(attribute='state') | rejectattr('state', 'in', ['0', '100']) \
| list | count }}"
countlow: "{{ states.sensor \
| selectattr('attributes.device_class','eq','battery') \
| sort(attribute='state') | rejectattr('state', 'in', ['0', '100']) \
| selectattr('state', 'lessthan', threshold|string) | list | count }}"
listtextlow: "{% for batt in states.sensor \
| selectattr('attributes.device_class','eq','battery') \
| sort(attribute='state') \
| rejectattr('state', 'in', ['0', '100']) \
| selectattr('state', 'lessthan', threshold|string) \
| list -%}
{{ batt.attributes.entity_id }}
{{ batt.attributes.friendly_name }} is {{ batt.state }}%
{% endfor %}"
notification_message: "{% if countlow > 0 -%}{{countlow}} out of \
{{ counttotal }} device batteries are low:
{{listtextlow}}
{%- else -%}
All {{counttotal}} device batteries are ok!
{% endif %}"
trigger:
- platform: time
at: !input 'time'
condition:
- condition: template
value_template: '{{ sensors != '''' and (day | int == 0 or day | int == now().isoweekday())
}}'
action:
- device_id: !input 'notify_device'
domain: mobile_app
type: notify
title: 'Battery Check'
message: '{{ notification_message }}'
- service: persistent_notification.create
data:
title: 'Battery Check'
message: '{{ notification_message }}'
notification_id: battery-check
@Erickclee
Copy link

Hi @davet2001, thanks for this modified blueprint. It helps. There is some small bugs when we select threshold at level5% and 100%. when select 5%, system see it as 50% and report battery that is <50%, when select 100%, system report all battery OK. Could you help to see if this can be fixed? Thanks for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment