Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Low battery sensor and notification.
#######################################################################################################################
## Package - Low Battery Alert
## credit to @MrNick4B for the idea of putting the battery level in the message
#######################################################################################################################
## - Nest Protect battery states: Ok, Replace
binary_sensor:
- platform: template
sensors:
low_battery:
icon_template: "{{ 'mdi:battery-alert' if is_state('binary_sensor.low_battery','on') else 'mdi:battery-90' }}"
friendly_name: Battery Status
device_class: battery
entity_id: sensor.time # update every minute
delay_on: # prevent false alerts, stagger push notifications on restart
seconds: 60
value_template: >
{% if is_state('input_boolean.enable_alerts','off') or is_state('var.startup_complete','false') %}
false
{% else %}
{{ states.sensor|select('in', expand('group.nest_batteries'))|selectattr('state','eq', 'Replace' )|list|length > 0
or states.zwave|selectattr('attributes.battery_level', 'defined')
|selectattr('attributes.battery_level','<', states('input_number.battery_alert_threshold')|int )|list|length > 0 }}
{% endif %}
input_number:
battery_alert_threshold:
name: Battery Alert Threshold
icon: mdi:battery-alert
min: 0
max: 100
step: 1
unit_of_measurement: '%'
group:
nest_batteries:
entities:
- sensor.downstairs_nest_protect_battery_health
- sensor.upstairs_nest_protect_battery_health
alert:
low_battery:
name: "Low Battery"
title: "Low Battery Alert"
message: >
{%- set battery_alert_threshold = states('input_number.battery_alert_threshold') | int -%}
{%- set ns = namespace(message='') -%}
{%- set low_batteries = states.zwave|selectattr('attributes.battery_level', 'defined') | rejectattr('state','!=','ready') | selectattr('attributes.battery_level','<=', battery_alert_threshold ) -%}
{%- for battery in low_batteries -%}
{%- set ns.message = ns.message ~ '- ' ~ battery.name ~ ' (' ~ '%0.0f'|format(battery.attributes.battery_level | float) | string ~ '%)\n' -%}
{%- endfor -%}
{%- set nest_batteries = states.sensor | select('in', expand('group.nest_batteries')) | selectattr('state','eq', 'Replace' ) -%}
{%- for battery in nest_batteries -%}
{%- set ns.message = ns.message ~ '- ' ~ battery.name ~ '\n' -%}
{%- endfor -%}
{{ ns.message }}
entity_id: binary_sensor.low_battery
state: 'on'
repeat: 1440 # 24 hours
can_acknowledge: true
skip_first: false
notifiers: jason
data:
actions:
- action: pause_low_battery_alert
title: Pause
- action: close_low_battery_alert
title: Close
tag: low_battery
timestamp: "{{ as_timestamp(now()) }}" #push
priority: normal
renotify: true #push
ttl: 3600
silent: true #push
requireInteraction: false #push
sticky: false #app
url: /lovelace/system #push
clickAction: !secret SYSTEM_URL #app
color: '#BE7D00' #app
image: !secret BATTERY_IMAGE
icon: !secret BATTERY_ICON #push
badge: !secret BATTERY_BADGE #push
automation:
- id: system_low_battery_alert
alias: "[System] Low Battery Alert"
description: "Create persistent notification when alert turns on."
initial_state: true
mode: single
trigger:
# run every 15 minutes
- platform: time_pattern
minutes: "/15"
# run whenever the battery alert threshold is changed
- platform: state
entity_id: input_number.battery_alert_threshold
condition:
# only run if system automation is enabled
- condition: state
entity_id: input_boolean.system_automation
state: 'on'
# only run if alerts are enabled
- condition: state
entity_id: input_boolean.enable_alerts
state: 'on'
# only run if low battery sensor is on
- condition: state
entity_id: binary_sensor.low_battery
state: 'on'
action:
# create persistent notification
- service: persistent_notification.create
data_template:
title: "Low Battery Alert"
message: >
{%- set battery_alert_threshold = states('input_number.battery_alert_threshold') | int -%}
{%- set ns = namespace(message='The battery level of the following device(s) is low:\n') -%}
{%- set low_batteries = states.zwave|selectattr('attributes.battery_level', 'defined') | rejectattr('state','!=','ready') | selectattr('attributes.battery_level','<=', battery_alert_threshold ) -%}
{%- for battery in low_batteries -%}
{%- set ns.message = ns.message ~ '- ' ~ battery.name ~ ' (' ~ '%0.0f'|format(battery.attributes.battery_level | float) | string ~ '%)\n' -%}
{%- endfor -%}
{%- set nest_batteries = states.sensor | select('in', expand('group.nest_batteries')) | selectattr('state','eq', 'Replace' ) -%}
{%- for battery in nest_batteries -%}
{%- set ns.message = ns.message ~ '- ' ~ battery.name ~ '\n' -%}
{%- endfor -%}
{{ ns.message }}
notification_id: low_battery
- id: system_close_low_battery_notifications
alias: "[System] Close Low Battery Notifications"
description: "Dismiss notifications on all devices."
initial_state: true
mode: single
trigger:
# run when alert turns off
- platform: state
entity_id: alert.low_battery
to: 'idle'
for:
minutes: 5 # keep notification open for 5 minutes
# run when alert is paused
- platform: state
entity_id: alert.low_battery
to: 'off'
#ISSUE cannot determine the from state of a persistent notification
#BUGFIX trigger for all state changes, check for unknown (closed) in conditions
# run when persistent notification state changes
- platform: event
event_type: state_changed
event_data:
entity_id: persistent_notification.low_battery
# run when push notification closed
- platform: event
event_type: html5_notification.closed
event_data:
tag: low_battery
# run when push notification clicked
- platform: event
event_type: html5_notification.clicked
event_data:
tag: low_battery
#ISSUE no clicked/closed event for mobile app
#BUGFIX use close/pause action buttons
# run when mobile app notification action button clicked
- platform: event
event_type: mobile_app_notification_action
event_data:
action: close_low_battery_alert
# run when mobile app notification action button clicked
- platform: event
event_type: mobile_app_notification_action
event_data:
action: pause_low_battery_alert
condition:
# only run if system automation is enabled
- condition: state
entity_id: input_boolean.system_automation
state: 'on'
# only run if alerts are enabled
- condition: state
entity_id: input_boolean.enable_alerts
state: 'on'
- condition: or
conditions:
#ISSUE cannot determine from state of a persistent notification
#BUGFIX can run if persistent notification state is unknown (notification was closed)
- condition: template
value_template: "{{ states('persistent_notification.low_battery') == 'unknown' }}"
#BUGFIX required due to perisitent notification condition
# can run if alert turns off (state is idle)
- condition: state
entity_id: alert.low_battery
state: 'idle'
#BUGFIX required due to perisitent notification condition
# can run if alert is manually turned off
- condition: state
entity_id: alert.low_battery
state: 'off'
#BUGFIX required due to perisitent notification condition
# can run if the notification event is close notification (check was done in trigger)
- condition: template
value_template: >
{% if trigger.event is defined %}
{{ trigger.event.event_type in ['mobile_app_notification_action', 'html5_notification.clicked', 'html5_notification.closed'] }}
{% endif %}
action:
# turn off automation to avoid recursive calls
- service: automation.turn_off
entity_id: automation.system_close_low_battery_notifications
# if triggered from an event, turn the alert off
- service_template: >
{% if trigger.event is defined %}
{% if trigger.event.data['action'] == 'pause_low_battery_alert' %} alert.turn_off
{% else %} script.null_script
{% endif %}
{% else %} script.null_script
{% endif %}
entity_id: alert.low_battery
# dismiss persistent notification
- service: persistent_notification.dismiss
data:
notification_id: low_battery
# dismiss push notifications on all devices
- service: html5.dismiss
data:
data:
tag: low_battery
# dismiss app notifications
- service: notify.mobile_app_jphone
data:
message: clear_notification
data:
tag: low_battery
# wait for notification to close on all devices
- delay:
seconds: 60
# turn automation back on
- service: automation.turn_on
entity_id: automation.system_close_low_battery_notifications
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment