Skip to content

Instantly share code, notes, and snippets.

@mbainter
Last active May 31, 2022 23:20
Show Gist options
  • Save mbainter/e621073433e277f7bcb1448d94c75b36 to your computer and use it in GitHub Desktop.
Save mbainter/e621073433e277f7bcb1448d94c75b36 to your computer and use it in GitHub Desktop.
Homeassistant Blueprints
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
source_url: https://gist.github.com/mbainter/e621073433e277f7bcb1448d94c75b36#file-low_battery_alert-yaml
# Blueprint Inputs
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: {}
# In this segment the user will selected the week days that te automation will be exectuted.
monday_enabled:
name: 'Monday'
description: 'Run test on Monday'
default: True
selector:
boolean:
tuesday_enabled:
name: Tuesday
description: 'Run test on Tuesday'
default: True
selector:
boolean:
wednesday_enabled:
name: Wednesday
description: 'Run test on Wednesday'
default: True
selector:
boolean:
thursday_enabled:
name: Thursday
description: 'Run test on Thursday'
default: True
selector:
boolean:
friday_enabled:
name: Friday
description: 'Run test on Friday'
default: True
selector:
boolean:
saturday_enabled:
name: Saturday
description: 'Run test on Saturday'
default: True
selector:
boolean:
sunday_enabled:
name: Sunday
description: 'Run test on Sunday'
default: True
selector:
boolean:
exclude:
name: Excluded Sensors
description: Battery sensors (e.g. smartphone) to exclude from detection. Only entities are supported, areas and devices must be expanded!
default: {}
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:
variables:
monday_enabled: !input 'monday_enabled'
tuesday_enabled: !input 'tuesday_enabled'
wednesday_enabled: !input 'wednesday_enabled'
thursday_enabled: !input 'thursday_enabled'
friday_enabled: !input 'friday_enabled'
saturday_enabled: !input 'saturday_enabled'
sunday_enabled: !input 'sunday_enabled'
current_day: '{{ now().weekday() | int }}'
threshold: !input 'threshold'
exclude: !input 'exclude'
sensors: >-
{% set result = namespace(sensors=[]) %}
{% for state in states.sensor | selectattr('attributes.device_class', '==', 'battery') %}
{% if exclude.entity_id is defined %}
{% 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 %}
{% else %}
{% if 0 <= state.state | int(-1) < threshold | int %}
{% set result.sensors = result.sensors + [state.name ~ ' (' ~ state.state ~ ' %)'] %}
{% endif %}
{% endif %}
{% endfor %}
{% for state in states.binary_sensor | selectattr('attributes.device_class', '==', 'battery') | selectattr('state', '==', 'on') %}
{% if exclude.entity_id is defined %}
{% if not state.entity_id in exclude.entity_id %}
{% set result.sensors = result.sensors + [state.name] %}
{% endif %}
{% else %}
{% set result.sensors = result.sensors + [state.name] %}
{% endif %}
{% endfor %}
{{result.sensors|join(', ')}}
# Triggers
trigger:
- platform: time
at: !input 'time'
# Conditions used to determine if the automation should run for the specific trigger event
condition:
# Check if the automation is supposed to be executed during that specific weekday
- condition: template
value_template: >-
{{
(current_day == 0 and monday_enabled) or
(current_day == 1 and tuesday_enabled) or
(current_day == 2 and wednesday_enabled) or
(current_day == 3 and thursday_enabled) or
(current_day == 4 and friday_enabled) or
(current_day == 5 and saturday_enabled) or
(current_day == 6 and sunday_enabled)
}}
# Check if the sensors list is not empty
- condition: template
value_template: >-
{{ sensors != '''' }}
# Actions
action:
- choose: []
default: !input 'actions'
mode: single
max_exceeded: silent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment