Low battery level detection & notification for all battery sensors

In fact there is an error in the log in that regard:

  • Error sending message: Can’t parse entities: can’t find end of the entity starting at byte offset 202. Args: (1347055346, “Batterie schwach - Xiaomi Batterie, Xiaomi Batterie, K iPhone Battery Level, Katjas iPhone Battery Level, R iPhone Battery Level, Xiaomi Batterie, Schalte Batterie, Xiaomi Batterie, us_ws_ikea_motion Battery Level”), kwargs: {‘parse_mode’: ‘Markdown’, ‘disable_notification’: False, ‘disable_web_page_preview’: None, ‘reply_to_message_id’: None, ‘reply_markup’: None, ‘timeout’: None, ‘message_tag’: None}

Sensors in use:

  • Ikea 2 Button Switch
  • Ikea Motion Sensor
  • Hue Switch
  • Hue Motion Sensor
  • Xiaomi Aqara Temp/Humidity Sensors
  • 2 iPhones

I have the same trouble. If i use service notify.mobile_app, all work fine, but if i use service notify.telegram_chat message dont left and in logs i see this:

Error sending message: Can't parse entities: can't find end of the entity starting at byte offset 95. Args: (201732582, 'Battery low - MI PAD 4 Battery Level, iPhone Irina Battery Level, T-H-Balcony_battery'), kwargs: {'parse_mode': 'Markdown', 'disable_notification': False, 'disable_web_page_preview': None, 'reply_to_message_id': None, 'reply_markup': None, 'timeout': None, 'message_tag': None}

Thank you

It seems that Telegram expects the message to be valid markdown and I suppose if there are e.g. an odd number of _ characters it believes this to be invalid markdown. Not sure if the underscores are the only problem here, but you could first try {{sensors|replace("_"," ")}} instead of just {{sensors}} maybe that does the trick already.

1 Like

Thanks, that did it! Much appreciated. SchĂśne Weihnachten! :slight_smile:

1 Like

Good to know it worked. I will make a note in the original post.

Und danke ebenfalls! :wink:

1 Like

Works great. Many thanks :slight_smile:

I use the Action:

service: notify.google_cloud_notify
data:
  message: 'Low battery warning for: {{sensors}}'
1 Like

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