Low battery level detection & notification for all battery sensors

Thank you! This is awesome

Hi all,

I have an esphome unit which I added a battery level attribute by hand.

Where apperently the calculation can give a negative battery level percentage :grimacing:.

But the automation ain’t giving a messages for this entity.

Anyone else experiencing the same? ( Don’t know if any one has negative battery level percentage tho :rofl:)

Sbyx,

I just came across this today and it’s exactly what I’ve been looking for and works perfectly. I’m not much of a programmer and do most things through the UI, so I just wanted to say thanks for putting this together and sharing!

2 Likes

Hi all, is it possible to include the area of the sensor to the message?
Thanks!

Many Thanks. You saved me a lot of time, just wanted to start building an automation.

Hello!
I really like this blueprint!

Is it possible that it won’t work anymore with version 2022.1? I got a warning about this:

* Template warning: 'int' got invalid input 'Not Charging' when rendering template '{% set result = namespace(sensors=[]) %} {% set exclude = [exclude0, exclude1, exclude2, exclude3, exclude4, exclude5, exclude6, exclude7, exclude8, exclude9] %} {% for state in states.sensor | selectattr('attributes.device_class', '==', 'battery') %} {% if 0 < state.state | int < threshold | int and not state.entity_id in exclude %} {% set result.sensors = result.sensors + [state.name] %} {% endif %} {% endfor %} {% for state in states.binary_sensor | selectattr('attributes.device_class', '==', 'battery') | selectattr('state', '==', 'on') %} {% if not state.entity_id in exclude %} {% set result.sensors = result.sensors + [state.name] %} {% endif %} {% endfor %} {{result.sensors|join(', ')}}' but no default was specified. Currently 'int' will return '0', however this template will fail to render in Home Assistant core 2022.1

I think the default values need to be added.

Best
Lemming

It seems that more entities of yours have device_class set to battery than before and some of them return their charging status (in addition to the entities which sport the “real” percentage).
The hint about setting default values may be misleading here.

Go to developer tools (first tab entities) and filter the attribute row for “device_class: battery” – perhaps the result is showing the culprit.

This works fine manualy, but for some reason it doesn’t run automaticaly at 07:00 ?

Any idea why ?

alias: Low battery level detection & notification for all battery sensors
description: ''
use_blueprint:
  path: sbyx/low-battery-level-detection-notification-for-all-battery-sensors.yaml
  input:
    actions:
      - service: notify.notify
        data:
          message: The battery of the sensor(s) {{sensors}} is low.
    time: '07:00:00'
    threshold: 20

@Sbyx Is that a general problem ?

Hi there,

I’m trying to modify the Blueprint to also report the battery levels for “low” or “unavailable” battery states. I tried with the below code modification but it doesn’t seem to work, any ideas?
Also is there any way to get a line break after each listed sensor so that the format of listed sensors is easier to read?

  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', 'or', 'unavailable', 'or', 'low') %}
      {% if not state.entity_id in exclude.entity_id %}
        {% set result.sensors = result.sensors + [state.name] %}
      {% endif %}
    {% endfor %}
    {{result.sensors|join(', ')}}

This is the resultant telegram message:

Thanx for any guidance.

For „low“ (refers to binary sensors whose state is on or off):


| selectattr('state', 'eq', 'on')

In whole:


sensors: >-
    {% set result = namespace(sensors=[]) %}
    {% set exclude = [] %}
    {% set threshold = 40 |int(0) %}
    {% for state in states.sensor | selectattr('attributes.device_class', '==', 'battery') %}
      {% if (0 <= state.state | int(-1) < threshold or state.state in ['unavailable', 'unknown', 'none']) 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('\n') }}

1 Like

Thanx @pedolsky!

That worked very well, and for my understanding, was it the addition of the (’\n’) that put each sensor on to a new line in the results?

So it is. The join() filter makes the output more readable and defines the line format (together with whitespace, see the hyphens in -%} .

1 Like

@ Sbyx Thank you very much for this great blueprint !

I would like to have one similar that check all the zigbee link_quality and last_seen attributes, if someone knows some blueprint that do that, it would be greatfull to let me know please, thanks!

Edit: I finally find a way to get those sensors attributes for my zigbee devices :

{% set result = namespace(sensors=[]) %}
    {% set exclude = [] %}
    {% set threshold = 200 %}
    {% for state in states.sensor | **selectattr('attributes.linkquality')** %}
      {% if 0 < state.attributes.linkquality | int < threshold | int and not state.entity_id in exclude.entity_id **and 'linkquality' in state.attributes.friendly_name** %}
        {% set result.sensors = result.sensors + [state.name ~ ' (' ~ state.**attributes.linkquality** ~ ')'] + [' **last seen at ' ~** **state.attributes.last_seen.split('T') | trim('[]')** ~ ')'] %}
      {% endif %}
    {% endfor %}
    {{result.sensors|join(', ')}}

Gives me this output:

DĂ©tecteur Aqara 01 linkquality (151), last seen at '2022-03-17', '19:19:26+01:00'), DĂ©tecteur ouverture Aqara 01 linkquality (151), last seen at '2022-03-17', '19:21:48+01:00'), Plug Tuya 01 linkquality (151), last seen at '2022-03-17', '19:51:06+01:00')

Have to format the “last seen” date to make it more freindly now but didn’t found a way to do that for now


Fantastic blueprint! I have one issue/question – it appears that it gets triggered not just when the battery threshold is reached, but also if I have any Zigbee devices that “went asleep” and momentarily aren’t reporting their battery life (“unavailable”). Is there a way to exclude any devices that show their battery percentage as unavailable?

@Sbyx I’ve found two minor bugs:

  • If only one excluded entity selected => can’t delete it
  • If set threshold to any value, can’t back to 0 with the slider, input text is OK

I am brand new to HA. Just got my RPi400 last week and successfully added all my Z-wave and Zigbee devices. I was going to sit down this morning to try programming a low battery notification for all my battery powered devices when I came across this blueprint. Learned a lot about blueprints and this one worked flawlessly for my situation. Thanks very much for sharing this. All the best.

There seems to be an improved version of this at blueprints_repo/low_battery_notification.yaml at e3945ad01d9eddc2fda920d6cec5c5c9ccf19f8e · gmlupatelli/blueprints_repo · GitHub

Hello, i have tried to make this work but i am just getting a notification on “low battery” nothing else ?
Why am i not getting what sensor that has low battery ?
regards
/Micke

What does your action text look like? Maybe you forgot to use the {{ sensors }} template.