Low battery level detection & notification for all battery sensors

How can I manually check which sensors are reported as “low battery level” state?

I used the blueprint and add an action according to Low battery level detection & notification for all battery sensors but the notification just tells "Low battery warning for: "

I don´t know if the variable for {{sensors}} doesn´t work and which devices actually are affected.

How to check this, maybe with dev tools?

Normally this shouldn’t be empty since that is one of the automation conditions.

If you want to try to debug what’s going on you can go to Developer Tools -> Template and paste the template code from the blueprint: https://gist.github.com/sbyx/1f6f434f0903b872b84c4302637d0890#file-low-battery-level-detection-notification-for-all-battery-sensors-yaml-L118
Lines 118-130.

Set exclude to an empty list [ ] (e.g. remove exclude0 … exclude9) and replace threshold with some number, e.g. 20 or something higher. 101 should give you all sensors.

Hello,

I just got the same problem: I receive a notification (on my phone from the android application), but no sensor name in it.

I’ve tried with debug template, seems like results are correct : nothing in my case for 20%, and 2 “Battery_123xyz” for 30% but testing script with a 30% threshold still give no sensors (tried replacing underscores and without).

Thanks for sharing your script!

Could you please in the Automation Settings for the automation from this blueprint click on the 3 dots and select Edit as YAML and post the results here?

I’ve just dropped the device_id:

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:
    day: '0'
    actions:
      - device_id: theid
        domain: mobile_app
        type: notify
        message: 'The battery of the sensor(s) {{sensors}} is low.'
        title: Low battery
    threshold: '30'
    time: '10:00:00'

Can you try instead of a device action instead using “Call Service” with service notify.mobile_app_… and then putting the message there with the {{sensors}} variable?

In the data section you can put:
message: 'The battery of the sensor(s) {{sensors}} is low.'

Got: The battery of the sensor(s) is low. notified on mobile

Weird, no idea why that doesn’t work for you. Do you see any template or other related errors in the log?

Hum… No, nothing in logs unfortunately :confused:
Since I’m discovering HA since a few days only; I cannot exclude I have setup issues, most of my existing sensors has been discovered but not set up at all yet (mainly because I’m not able to differentiate them all :smiley:).

Let me know if I can provide extra information; I’ll certainly take another look later. Thank you very much anyways, and merry x-mas!

Instead of listing out 10 sensor inputs to exclude, couldn’t you just use a single target selector since that collects a list of entities of a specific type. Or I guess two target selectors, one for sensors to exclude and one for binary sensors to exclude.

1 Like

I tried to but couldn´t get it to work. Do I need do copy&paste the whole raw code?
Maybe you can provide the code I have to paste filled with all the variables.

At least it´s not just me so maybe there´s a bug in the blueprint.

Please assist, I´ll try to help debug.

You need to copy the pointed code but adapt it a bit. Try with this, change threshold variable according to your needs:

    {% set result = namespace(sensors=[]) %}
    {% set exclude = [] %}
    {% set threshold = 20 %}
    {% 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(', ')}}

About the issue itself, I’ve worked on my sensors configuration, names I see in debug seems to be the correct ones, but notification still miss them.
Also, with a 20% threshold, I have no results (I can confirm with debug results) but notification is sent.

That worked.

I need to apologize: the blueprint is working as intended:

  • When I set the treshold to e. g. 90 % and trigger the automation, the devices/sensors below that treshold are reported in the notification message.
  • BUT when I set treshold to 20 % (or a level lower than current devices/sensors states) and trigger the automation manually I get the notification with empty text "Low battery warning for: " as reported in Low battery level detection & notification for all battery sensors

So the question is: is this a normal behaviour of automations triggered manually --> do they always run even the conditions are not met? I think so because I did not receive any notification during the last days when the automation was run at set time daily.

So I guess everything´s fine, thank you @Sbyx.

2 Likes

No problem, thanks for the reply. Yes, when manually triggering through the UI, conditions are ignored by default. You can trigger automations manually with conditions in Developer Tools - Services using skip_condition: false.

6 Likes

Love it, just had to change to get the battery level also.

{% set result.sensors = result.sensors + [state.name] + [state.state] %}

1 Like

You are actually correct. I wasn’t sure if it was possible to “abuse” the target selector but I managed to find a way. Therefore last version removes the 10 input selectors again and instead adds a target selector for exclusion instead.

If you are updating the blueprint and had excluded sensors before please add them again using the “Pick Entities” button.

Great idea, added it to the latest version as well. Thanks!

Also, one other small suggestion. The int filter accepts a parameter of a default number. This number is what it returns when asked to convert a non-numeric value to an integer.

So with that you can change this

if 0 < state.state | int < threshold | int

To this

if 0 <= state.state | int(-1) < threshold | int

This way it will only skip alerting you non-numeric sensors with type battery and not for battery sensors with a dead battery

1 Like

Great, now even the percentage of sensors is shown in notification message.

I´m wondering in general how to update blueprints or be aware of updates available. Not sure if the HA devs have a roadmap for this.

For this blueprint I just deleted it and imported it again, the automation was still available and referenced the new version of the blueprint.

Yeah, that seems to be the way. May need to reload automations as well or at least that’s what I am doing when I change blueprints on disk.

@CentralCommand changed, thanks again!