The card mentioned above is fine if it’s only about visualising battery levels. If you want to send notifications and such it gets more complicated.
I’ll share my solution, based on tips and things I gathered from the forum. Copy and pasting on mobile, so please excuse typos and the code/config posted without trimming it.
You can actually add custom attributes to sensors, e.g. note the monitor attribute here. You can also use these attribute values to filter on in the battery state card.
sensor.main_bedroom_ht_battery:
friendly_name: Main Bedroom H&T Battery
monitor: True
In my case, I wanted to do two things: At a set time daily, notify me about batteries below a certain threshold, but also notify me when any battery completely runs flat at that moment in time.
For the latter, I needed this. Note the use of the custom attribute.
- sensor:
# https://community.home-assistant.io/t/trigger-an-automation-based-on-a-groups-individual-entity-state-change/383560/2
# https://community.home-assistant.io/t/single-automation-for-all-lights/375028/7
- name: Number of Flat Batteries
icon: mdi:battery-alert-variant-outline
state: >
{% set level = 0 %}
{% set flat_batteries =
states.sensor
| selectattr('attributes.monitor', 'defined')
| selectattr('attributes.monitor', 'eq', True)
| rejectattr('state', 'in', ['unavailable', 'unknown', 'none'])
| selectattr('attributes.device_class', 'eq', 'battery')
| map(attribute='state')
| map('int')
| select('eq', level)
| list
%}
{{ flat_batteries | count }}
Here are my automations.
- alias: "Check For Low Batteries"
initial_state: true
variables:
level: 5
# https://community.home-assistant.io/t/variable-in-automation-not-working-as-expected/431591/2
monitored_batteries: >-
{{
states.sensor
| selectattr('attributes.monitor', 'defined')
| selectattr('attributes.monitor', 'eq', True)
| rejectattr('state', 'in', ['unavailable', 'unknown', 'none'])
| selectattr('attributes.device_class', 'eq', 'battery')
| map(attribute='entity_id')
| list
}}
trigger:
# what if the server is down at this time? for now, we don't care: batteries might last till the next day and there's a flat battery check
platform: time
at: "09:00:00"
condition: >-
{{
expand(monitored_batteries)
| map(attribute='state')
| map('int')
| select('lt', level)
| list
| count
> 0
}}
action:
- service: notify.mobile_app_ceres
data:
# icon: https://companion.home-assistant.io/docs/notifications/notifications-basic/#notification-icon
# https://companion.home-assistant.io/docs/notifications/actionable-notifications/#icon-values
title: "Batteries"
# https://community.home-assistant.io/t/recommended-ways-to-manage-devices-and-entities-names/243815/12
message: >
The following devices have less than {{ level }}% charge:
{%- for b in monitored_batteries %}
{%- if states(b) | int < level and not is_state(b, 'unavailable') %}
- {{ state_attr(b, 'friendly_name') | replace(' Battery', '') }}: {{ states(b) | int }}%
{%- endif -%}
{%- endfor %}
data:
group: "batteries"
url: homeassistant://navigate/lovelace/devices
# https://companion.home-assistant.io/docs/notifications/notifications-basic/#notification-icon
# https://community.home-assistant.io/t/mobile-notification-icon-not-showing-up/408050
# icon_url: "https://github.com/home-assistant/assets/blob/9b782fe562cbd4e6139f9be17d8e7befafa5f945/logo/logo-small.png?raw=true"
# https://community.home-assistant.io/t/why-isnt-there-a-groups-entities-state-trigger/467179
# https://community.home-assistant.io/t/unleash-the-power-of-expand-for-template-sensors/136941/22
# https://community.home-assistant.io/t/trigger-an-automation-based-on-a-groups-individual-entity-state-change/383560
# https://community.home-assistant.io/t/single-automation-for-all-lights/375028/8
# todo: alert when battery at 1%
- alias: "Check For Flat Batteries"
initial_state: true
variables:
# fix test value
level: 0
monitored_batteries: >-
{{
states.sensor
| selectattr('attributes.monitor', 'defined')
| selectattr('attributes.monitor', 'eq', True)
| rejectattr('state', 'in', ['unavailable', 'unknown', 'none'])
| selectattr('attributes.device_class', 'eq', 'battery')
| map(attribute='entity_id')
| list
}}
trigger:
- platform: state
entity_id: sensor.number_of_flat_batteries
condition: >-
{{
expand(monitored_batteries)
| map(attribute='state')
| map('int')
| select('eq', level)
| list
| count
> 0
}}
action:
- service: notify.mobile_app_ceres
data:
title: "Batteries"
message: >
The following devices have no charge:
{%- for b in monitored_batteries %}
{%- if states(b) | int == level and not is_state(b, 'unavailable') %}
- {{ state_attr(b, 'friendly_name') | replace(' Battery', '') }}
{%- endif -%}
{%- endfor %}
data:
group: "batteries"
url: homeassistant://navigate/lovelace/devices
EDIT:
- I also want aggregated notifications for the daily notification; not one per battery.
- I don’t want notifications for all batteries.
- I don’t want to maintain a group.