Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save salimkayabasi/0f07cda4392247df8817a4e2cb0bfc4f to your computer and use it in GitHub Desktop.
Save salimkayabasi/0f07cda4392247df8817a4e2cb0bfc4f to your computer and use it in GitHub Desktop.
blueprint:
name: Low battery level detection & notification (Interval Check) v2.1
description: Regularly test all sensors with 'battery' device-class for crossing
a certain battery level threshold and if so execute an action. The check runs at a configurable
interval and sends a notification via a selected service.
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
interval_days:
name: Check Interval (Days)
description: How often to run the check, in days (e.g., 1, 7). Leave at 0 to ignore.
default: 0
selector:
text: {}
interval_hours:
name: Check Interval (Hours)
description: How often to run the check, in hours (e.g., 2, 12). Leave at 0 to ignore.
default: 6
selector:
text: {}
interval_minutes:
name: Check Interval (Minutes)
description: How often to run the check, in minutes (e.g., 30). Use sparingly. Leave at 0 to ignore.
default: 0
selector:
text: {}
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
notification_entity:
name: Notification Service Entity (Optional)
description: The notification entity to use for alerts (e.g., notify.mobile_app_your_phone, notify.telegram). Leave blank to skip notification.
default: ""
selector:
entity:
domain: notify
source_url: https://gist.github.com/salimkayabasi/0f07cda4392247df8817a4e2cb0bfc4f
variables:
threshold: !input 'threshold'
exclude: !input 'exclude'
notify_ent: !input 'notification_entity'
days: !input 'interval_days'
hours: !input 'interval_hours'
minutes: !input 'interval_minutes'
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_pattern
minutes: '/1'
condition:
- '{{ sensors != '''' }}'
- or:
- condition: template
value_template: >
{% set days_int = days | int(0) %}
{{ days_int > 0 and now().hour == 0 and now().minute == 0 and now().day % days_int == 0 }}
- condition: template
value_template: >
{% set hours_int = hours | int(0) %}
{{ hours_int > 0 and now().minute == 0 and now().hour % hours_int == 0 }}
- condition: template
value_template: >
{% set minutes_int = minutes | int(0) %}
{{ minutes_int > 0 and now().minute % minutes_int == 0 }}
action:
- choose:
# Action 1: Execute the notification if an entity was provided
- conditions:
- '{{ notify_ent != '''' }}'
sequence:
# --- FINAL FIX: Call the GENERIC notify.notify service and specify the target entity ID in the data ---
- service: notify.notify
data:
# Use the full entity ID as the target name in the data payload
target: !input notification_entity
title: '⚠️ Low Battery Alert! ⚠️'
message: >
Hey there! 👋 The following devices are running low on battery (below {{ threshold }}%):
🔋 **{{ sensors }}**
Time to grab some fresh batteries! ⚡
mode: single
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment