I just realized that my motion detector in the kitchen was dead - I happen to be on my maintenance tab of HA that has a group of battery levels and kitchen has been at 0% for 12 days. I’ve been meaning to write a notification automation and finally got around to it.
The notification runs every 24 hours and notifies me if my battery reporting devices are low. I have a group setup that contains all the battery sensors and my automation iterates over that group.
I overlooked selectattr when reading the documentation, didn’t know we could use that as filter. An example like this should be added into the HA documentation because it shows the power of combining multiple filters in Jinja2.
I have one sensor which has a battery and is wired powered. While wire powered the batter_level value is a constant 4. How do I dismiss of that sensor?
Based on the submission of @jwelter, I tried something for myself.
This automation reports all battery levels in a single notification. I use this automation for Telegram notifications, so that’s why all entities in a single notification is fine for me.
I configured this automation in the Home Assistant frontend.
Trigger
Trigger type: Time
At: 19:00
Condition
Condition type: Template
Value template:
{%- set battery_alert_threshold = 10 | int -%}
{{ states | selectattr('attributes.battery_level', 'defined') | selectattr('attributes.battery_level','<=', battery_alert_threshold ) | list | count >= 1 }}
Action
Action type: Call service
Service: notify.telegram_mychatbot
Service data:
message: >-
{%- set ns = namespace(message = 'The battery level of the following device(s) is low:\n') -%}
{%- set battery_alert_threshold = 10 | int -%}
{%- set low_batteries = states |
selectattr('attributes.battery_level', 'defined') |
selectattr('attributes.battery_level','<=', battery_alert_threshold ) -%}
{%- for battery in low_batteries -%}
{%- set ns.message = ns.message + '- ' + battery.name + ' (' + (battery.attributes.battery_level | round(0) | string) + '%)\n' -%}
{%- endfor -%}
{{ ns.message }}
title: '*Home Assistant - Battery level report*'
You can set the value of battery_alert_threshold in the condition and in the action to the percentage of your liking.
The notification will look something like this:
Home Assistant - Battery level report
The battery level of the following device(s) is low:
– Bedroom Cube (4%)
– Bedroom Temperature Sensor (9%)
I hope that this helps someone who wants to achieve the same
Hi @MrNick4B - that looks awesome (and working in part for me!) - newbie here and learning from the community posts and lots and lots of trail and error, perhaps you can point me in the right way.
Cut and pasted your automation in and got it running fine, using SMTP email notification and they’re coming through. I have three battery devices, one through the mobile integration and the other two through deconz integration.
For the notification I can only get it to output the battery from the mobile as a line entry on the body of the text, I can trigger the automation from the other devices (by charging the mobile and tweaking the parameters thus ensuring the condition is checking the others), but for the life of me can’t see why they won’t spring up on the notification!!
Any words of wisdom happy to receive!!! (any appropriate config I’m equally happy to post!)
Thanks
Edit: so I can get them out on a message - trying to learn a bit more each time
{% for state in states.sensor -%}
{{ state.name | lower }} is {{state.state_with_unit}}
{%- endfor %}
The batteries are coming out - back to unpicking the original message
So I’ve had to comprise for now, using the original automation posted the condition works fine but can’t get the ‘conditionality’ in the message to work and the devices to pop up. In the end gone with they’ll all be listed but had to use state.sensor and battery device_class to get them but this has the effect of not being able to do the test of the battery_level.
if anyone stumbles across this and has some ideas - all welcome!
- id: '1596277325558'
alias: Auto - Low Battery Warning
description: ''
trigger:
- at: '20:30'
platform: time
condition:
- condition: template
value_template: '{%- set battery_alert_threshold = 20 | int -%}
{{ states | selectattr(''attributes.battery_level'', ''defined'') | selectattr(''attributes.battery_level'',''<='',
battery_alert_threshold ) | list | count >= 1 }}'
action:
- data:
message: '{%- set ns = namespace(message = ''Some devices have low battery:\n'')
-%} {%- set battery_alert_threshold = 20 | int -%} {%- set low_batteries =
states.sensor | selectattr(''attributes.device_class'', ''eq'', ''battery'')
-%} {%- for battery in low_batteries -%} {%- set ns.message = ns.message
+ ''- '' + battery.name + '' '' + battery.state + '' \n'' -%} {%- endfor -%}
{{ ns.message }}'
title: '** Home Assistant - Battery Level Report **'
service: notify.email
mode: single
So, again some more headaches and resolution. My challenge with this (and I was misled by the other posts because I didn’t understand it) was my devices didn’t have a battery level attribute but infact had an entity of battery level.
In the end I’ve looked for the word “battery” in the sensor name (given the only ones with battery in the name are battery level entities) and worked around that. I’m sure there’s a better way to do it (always open to suggestions) but for the moment this working out okay for me…
- id: '1596277325558'
alias: System - Low Battery Warning
description: ''
trigger:
- at: '19:00'
platform: time
condition:
- condition: template
value_template: "{%- set ns = namespace(message = false) -%}\n{%- set threshold\
\ = 20 -%}\n{%- for item in states.sensor -%}\n{%- if \"battery\" in item.name\
\ | lower and item.state | int < threshold and\n item.state|int != 0 -%}\n\
{%- set ns.message = (true) -%}\n{%- endif -%} \n{%- endfor -%} \n{{ ns.message\
\ }}"
action:
- data:
message: '{%- set ns = namespace(message = ''The battery level of the following
device(s) is low:\n'') -%} {%- set threshold = 20 -%} {%- for item in states.sensor
-%} {%- if "battery" in item.name | lower and item.state | int < threshold
and item.state|int != 0 -%} {%- set ns.message = ns.message + '' - '' + item.name
+ '' ('' + item.state + ''%)\n'' -%} {%- endif -%} {%- endfor -%} {{ ns.message
}}'
title: '** Home Assistant - Battery Level Warning **'
service: notify.email
- data:
message: '{%- set ns = namespace(message = ''The battery level of the following
device(s) is low:\n'') -%} {%- set threshold = 20 -%} {%- for item in states.sensor
-%} {%- if "battery" in item.name | lower and item.state | int < threshold
and item.state|int != 0 -%} {%- set ns.message = ns.message + '' - '' + item.name
+ '' ('' + item.state + ''%)\n'' -%} {%- endif -%} {%- endfor -%} {{ ns.message
}}'
service: notify.mobile_app_mike_wood_iphonex
mode: single
Note: I created these through the UI which is why the formal isn’t as clean as hand crafted!