Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andyblac/35b4164355dbef4f998f358353278236 to your computer and use it in GitHub Desktop.
Save andyblac/35b4164355dbef4f998f358353278236 to your computer and use it in GitHub Desktop.
Home Assistant Blueprint: Low battery level detection & notification for all battery sensors
blueprint:
# Version: 1.3.1
name: Low Battery and Device Unavailable Check
description:
Regularly test all sensors with 'battery' device-class for crossing
a certain battery level threshold as well as if it is unavailable.
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. (Optional)
description: This will ingore these entities in the auto detection list,
battery sensors only are supported (e.g. smartphone).
!! Only entities are supported, devices must be expanded !!
default:
entity_id: []
selector:
target:
entity:
device_class: battery
include:
name: Included Only Sensors. (Optional)
description:
This bypasses the auto detection and only checks against included entities,
battery sensors only are supported (e.g. motion sensor) to included in the check.
!! Only entities are supported, devices must be expanded !!
default:
entity_id: []
selector:
target:
entity:
device_class: battery
unavailable:
name: Report if a device is unavailable. (Optional)
description:
List any devices that are unavialble, possibly due to offline device, or a flat battery,
some devices even though they will show like 40% battery could very well die the next day.
default: "battery"
selector:
select:
options:
- label: Include all unavailable devices (only works when not using Include Only).
value: "all"
- label: Include battery only unavailable devices.
value: "battery"
- label: Don't include unavailable devices.
value: "disabled"
notify_types:
name: Notification type.
description: Choose what type of notification you require.
default: mobile
selector:
select:
options:
- label: Mobile
value: mobile
- label: Persistent Notification
value: persistant
- label: Telegram
value: telegram
sort: false
custom_value: false
multiple: true
notify_device:
name: Devices To Notify
description: Select the devices to be notified when devices are found..
default: []
selector:
device:
filter:
- integration: mobile_app
multiple: true
telegram_name:
name: Telegram name
description: The name used when setting up Telegram integration.
default: ""
selector:
text:
title:
name: Title
description: The notification title of the low battery message.
default: "Low Battery"
selector:
text:
message:
name: Message
description: The notification message {{sensors}} is replaced with
the names of sensors being low on battery.
default: "{{sensors|default('')}}"
selector:
text:
source_url: https://gist.github.com/andyblac/35b4164355dbef4f998f358353278236
variables:
day: !input day
threshold: !input threshold
exclude: !input exclude
include: !input include
unavailable: !input unavailable
notify_types: !input notify_types
notify_device: !input notify_device
telegram_name: !input telegram_name
title: !input title
message: !input message
sensor_list: >-
{% set result = namespace(sensors=[]) %}
{% if include.entity_id | count > 0 %}
{% for entity in states | selectattr('attributes.device_class', 'eq', 'battery') | selectattr('entity_id', 'in', include.entity_id) %}
{% if 0 <= entity.state | int(-1) < threshold | int %}
{% set result.sensors = result.sensors + [ entity.name ~ ' (' ~ entity.state ~ '%)'] %}
{% endif %}
{% endfor %}
{% if unavailable in ["all","battery"] %}
{% for entity in states | selectattr('state','eq','unavailable') | rejectattr('entity_id', 'in', exclude.entity_id) %}
{% set result.sensors = result.sensors + [ entity.name ~ ' (' ~ entity.state ~ ')'] %}
{% endfor %}
{% endif %}
{% else %}
{% for entity in states | selectattr('attributes.device_class', 'eq', 'battery') | rejectattr('entity_id', 'in', exclude.entity_id) %}
{% if 0 <= entity.state | int(-1) < threshold | int %}
{% set result.sensors = result.sensors + [ entity.name ~ ' (' ~ entity.state ~ '%)'] %}
{% endif %}
{% endfor %}
{% if unavailable in ["all","battery"] %}
{% if unavailable == "all" %}
{% for entity in states | selectattr('state','eq','unavailable') | rejectattr('entity_id', 'in', exclude.entity_id) %}
{% set result.sensors = result.sensors + [ entity.name ~ ' (' ~ entity.state ~ ')'] %}
{% endfor %}
{% elif unavailable == "battery" %}
{% for entity in states | selectattr('state','eq','unavailable') | selectattr('attributes.device_class', 'eq', 'battery') | rejectattr('entity_id', 'in', exclude.entity_id) %}
{% set result.sensors = result.sensors + [ entity.name ~ ' (' ~ entity.state ~ ')'] %}
{% endfor %}
{% endif %}
{% endif %}
{% endif %}
{{result.sensors}}
sensors: "{{sensor_list|join(', ')|replace(', ','\n')}}"
trigger:
- platform: time
at: !input time
condition:
- "{{ sensor_list | length > 0 and (day | int == 0 or day | int == now().isoweekday()) }}"
action:
- choose:
- conditions:
- condition: template
value_template: "{{ 'mobile' in notify_types }}"
sequence:
repeat:
for_each: !input notify_device
sequence:
- service: "notify.mobile_app_{{ device_attr(repeat.item, 'name') | slugify }}"
data:
title: !input title
message: !input message
- choose:
- conditions:
- condition: template
value_template: "{{ 'persistant' in notify_types }}"
sequence:
- service: notify.persistent_notification
metadata: {}
data:
title: !input title
message: !input message
- choose:
- conditions:
- condition: template
value_template: "{{ 'telegram' in notify_types }}"
sequence:
- service: notify.{{ telegram_name }}
metadata: {}
data:
message: !input message
mode: single
@romanpet1
Copy link

line 142 and 153 - can you pls fix typo. replace bettery with battery.

done, sorry about.

thank you for the fast response. battery only mode works now.

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