Low Battery Alerts: One automation for all battery sensors
Important Edit
See this comment for a Blueprint that implements battery discovery and monitoring significantly more elegant than what I have below.
Context
My deployment of battery-powered IoT devices is growing, and as such, so it my need to monitor and micromanage their battery levels. For the longest time, I had a dashboard with a list of all of the battery entities and I would just glance at it every once in a while. This worked because I didn't have very many devices. Now that my deployment is growing into leak sensors, plant soil monitors, door locks, buttons, etc., I need some form of alerting. The dashboard was about to get messy, quick.
Here's what I came up with using a single automation + trigger IDs. The alternative is to create one automation for every device and label/group them accordingly. I don't love that, so here's the single automation configuration.
Automation
YAML implementation follows the GUI implementation.
Pseudocode / Logic Process
- Set up one trigger for every battery entity. Set that trigger to occur every time that battery entity changes values.
- When the trigger occurs, if the entity's value is now below a certain threshold, produce an alert that provides the name of the entity noting that its battery should be changed.
Using the GUI
Create a new automation from scratch.
Add a Trigger under the "When" heading.
- Select
Entity
- Select
State
- Select your desired battery entity in the Entity field. This entity should contain the current battery percentage of the device.
- Do not fill anything into the Attribute, From, To, or For fields.
- Assign this Trigger a Trigger ID. Click on the three dots in the top-right corner of the Trigger and select "Edit ID". Assign it a nickname directly affiliated with the device that you're monitoring. For example, for a plant sensor named "Cactus", you probably want to name your Trigger ID "Cactus".
- Repeat this step for every battery you want to monitor.
- Select
Do not create any conditionals under the optional "And if" heading
For your Actions under the "Then do" heading, create one "If-then" action.
- Click "Add Action"
- Click "Building Blocks" (or search for If-then)
- Click "If-then"
Create a nested Or statement within the If-then action.
- Click "Add Condition"
- Click "Building Blocks" (or search for Or)
- Click "Or"
Create one nested And statement for every device to monitor.
- Click "Add Condition"
- Click "Building Blocks" (or search for And)
- Click "And"
- Repeat this step for the number of battery sensors you will be monitoring. This will be the same number as Triggers created in Step 2.
Create two conditions within the And statement.
- Create a condition to determine which trigger was caught.
- Click "Add Condition"
- Click "Other" (or search for Triggered by)
- Click "Triggered by"
- Select one of the triggers from Step 2.
- Create a condition to determine if this entity's battery is below a certain threshold.
- Click "Add Condition"
- Click "Entity"
- Select the same battery Entity from the Trigger select as it relates to Step 2.
- Under the "Upper Limit" heading, you can take one of two actions here:
- Specify the integer value for the percentage in which you want to deem a battery "Low". I specified 20 in my case.
- You can create a Helper "Number" entity, call it "Low Battery", and set this to a "Low" battery value threshold of your choosing, and select this with the "Numeric value of another entity" radio button instead. This way, you can refer to the same single value for all of your alerts.
- Repeat this for all triggers.
- Create a condition to determine which trigger was caught.
Under the "Then*" heading, create an Action to take. This is where we're going to generate the Home Assistant notification.
- Click "Add Action"
- Click "Notifications"
- Click "Send a persistent notification". This doesn't send a notification to any of your devices, but rather into the Home Assistant notification system in the sidebar.
- Click the three dots in the top right of the Action, and select "Edit in YAML". This automation dynamically detects which entity triggered the automation, and this functionality is only available via YAML.
Insert the following code:
action: persistent_notification.create
metadata: {}
data:
title: Low Battery Alert
message: Battery of {{ trigger.entity_id }} is below x%. Please replace it.
Replace "x%" with your desired percentage.
YAML
alias: Battery Low Notification
description: Notify when any sensor's battery is below 20%.
triggers:
- entity_id:
- sensor.deviceA_battery
trigger: state
id: deviceA
- entity_id:
- sensor.deviceB_battery
trigger: state
id: deviceB
- entity_id:
- sensor.deviceC_battery
trigger: state
id: deviceC
conditions: []
actions:
- if:
- condition: or
conditions:
- condition: and
conditions:
- condition: trigger
id:
- deviceA
- condition: numeric_state
entity_id: sensor.deviceA_battery
below: 20
- condition: and
conditions:
- condition: trigger
id:
- deviceB
- condition: numeric_state
entity_id: sensor.deviceB_battery
below: 20
- condition: and
conditions:
- condition: trigger
id:
- deviceC
- condition: numeric_state
entity_id: sensor.deviceC_battery
below: 20
then:
- action: persistent_notification.create
metadata: {}
data:
title: Low Battery Alert
message: Battery of {{ trigger.entity_id }} is below 20%. Please replace it.