Low battery level detection & notification for all battery sensors

could someone give me some advice on this please.

I have previously had this setup with a telegram bot to notify me but i have had to do a reinstall and lost the setup as it was not in the backup.

my telegram bot notifier is working ok but when i execute the automation it doesnt include any of the device names at all. it has the message but no device

alias: Low Battery Telegram
description: ""
use_blueprint:
  path: sbyx/low-battery-level-detection-notification-for-all-battery-sensors.yaml
  input:
    actions:
      - service: notify.anthony
        data:
          message: "low battery on {{devices}}"
    threshold: 50
    time: "01:51:00"
    exclude:
      entity_id:
        - sensor.pixel_battery_level

could anyone give any advice please

Hi,
I’ve slightly modified @josephquigley code to include the battery type, based on the HACS integration Battery Notes (GitHub - andrew-codechimp/HA-Battery-Notes: A Home Assistant integration to provide battery notes of devices)

blueprint:
  name: Low battery level detection for all battery sensors
  description:
    Regularly test all sensors with 'battery' device-class for crossing
    a certain battery level threshold and if so execute an action.
  domain: automation
  input:
    threshold:
      name: Battery warning level threshold
      description:
        Battery sensors below threshold are assumed to be low-battery (as
        well as binary battery sensors with value 'on').
      default: 20
      selector:
        number:
          min: 5.0
          max: 100.0
          unit_of_measurement: "%"
          mode: slider
          step: 5.0
    time:
      name: Time to test at
      description: Test is run at the configured time
      default: "10:00:00"
      selector:
        time: {}
    day:
      name: Weekday to test on
      description:
        "Test is run at configured time either every day (0) or on a given
        weekday (1: Monday ... 7: Sunday)"
      default: 0
      selector:
        number:
          min: 0.0
          max: 7.0
          mode: slider
          step: 1.0
    exclude:
      name: Excluded Sensors
      description: Battery sensors (e.g. smartphone) to exclude from detection.
      default:
        exclude_entities: []
      selector:
        target:
          entity:
            device_class: battery
    actions:
      name: Actions
      description:
        Send a notification or perform some other Action. `{{names}}` is
        replaced with the names of the sensors low on battery.
      selector:
        action: {}
  source_url: https://gist.github.com/josephquigley/03f5de47827f798539cebbe179e15bc9#file-jq-home-assistant-low-battery-blueprint-v1-yaml
variables:
  day: !input day
  threshold: !input threshold
  exclude: !input exclude
  names: "{% set ns = namespace(devices = [], device_ids = [], exclude=[]) %}
    {% for entity_id in exclude.exclude_entities %}
    {% set is_entity_only = device_id(entity_id) == None %}
    {% set id = (entity_id if is_entity_only else device_id(entity_id))|string %}
    {% set ns.exclude = ns.exclude + [id] %}
    {% endfor %}
    {% for platform in [states.binary_sensor, states.sensor] %}
    {% for item in platform %}
    {% set entity_id = item.entity_id|string %}
    {% if is_state_attr(entity_id, 'device_class', 'battery') and is_state_attr(entity_id, 'state_class', 'measurement') %}
    {% if is_state(entity_id, 'on') or (is_number(states(entity_id)) and states(entity_id)|int < threshold) %}
    {% set is_entity_only = device_id(entity_id) == None %}
    {% set id = (entity_id if is_entity_only else device_id(entity_id))|string %}
    {% if not id in ns.device_ids and not id in ns.exclude %}
    {% set device_name = device_attr(id|string, 'name') if device_attr(id|string, 'name_by_user') == None else device_attr(id|string, 'name_by_user') %}
    {% set entity_name = state_attr(entity_id, 'name') if state_attr(entity_id, 'friendly_name') == None else state_attr(entity_id, 'friendly_name') %}
    {% set name = entity_name if is_entity_only else device_name %}
    {% set battery_type = states(entity_id~'_type')|string %}
    {% set ns.device_ids = ns.device_ids + [id] %}
    {% set battery_level_entity = entity_id if is_entity_only else device_entities(id)|first %}
    {% set ns.devices = ns.devices + [{'id': id, 'name': name|trim ~ '\nLevel: ' ~ states(entity_id)|int ~ '% \nType: ' ~ battery_type  ~ '\n' , 'battery_level': states(entity_id)|int, 'is_entity': is_entity_only}] %}
    {% endif %}
    {% endif %}
    {% endif %}
    {% endfor %}
    {% endfor %}
    {{ ns.devices|sort(attribute='battery_level')|map(attribute='name')|join('\n') }}"
trigger:
  - platform: time
    at: !input time
condition:
  - condition: template
    value_template:
      "{{ names != '' and (day | int == 0 or day | int == now().isoweekday())
      }}"
action:
  - choose: []
    default: !input actions
mode: single

I’ve also added some formatting with line breaks to the ‘names’ variable. This helps me see the information more clearly.
That’s how the message appears on Telegram:
image

And the code for the automatization (in case you want to use the same format):

alias: low-battery-level-detection-notification
description: ""
use_blueprint:
  path: sbyx/low-battery-level-detection-notification-for-all-battery-sensors.yaml
  input:
    time: "19:30:00"
    threshold: 50
    actions:
      - service: notify.javier_telegram
        data:
          title: "*🪫 Low battery level detection (< 50%)*"
          message: |-
            ⠀
            {{names}}

Things to consider:

  1. The entity name for the battery type must be exactly the same as the battery sensor’s, but followed by ‘_type’. ie: sensor.philipspir1_battery → sensor.philipspir1_battery_type
  2. If there’s no declared battery type entity or it doesn’t meet the rule above… it will assign ‘unknown’ for the battery type.
4 Likes

Nice blueprint, working like a charme!

How can I get rid of the space between the actual percentage and the %?
Can’t seem to find the place to delete a space in the blueprint.

Thanks!

Great job!!! Thank you

hmm… when running the blueprint I ““just”” get…

“Template variable warning: ‘sensors’ is undefined when rendering ‘{{sensors}} are running low’”

any idea what I am missing?

Thanks for this, works perfectly. Just one small adjustment as I have a few sensors which have battery_level as their entity name instead of just battery:

{% set battery_type = states(entity_id|replace('_level','')~'_type')|string %}
1 Like

A future version of Battery Notes will stop you having to do all this messing about crossing entities, still in early dev but a sneak peak here

5 Likes

Is there a template that would do todolist from the devices with low battery and maybe even delete entries if / when the baterry is replaced?

Or that would be impossible to do?

Most similar thing I found is this Listing sensors with battery state lower than input number helper - Configuration - Home Assistant Community (home-assistant.io)

You’ll be able to create automations to both of those things with some new stuff coming in V2 in a few weeks.

1 Like

Hey.
Great blueprint :slight_smile:
Love it.

But i have Battery type for my devices in attributes, i added that with customize-glob.

Is there away to add that info in the message?

oooh. This is exciting, Thank you!
And yes, are you referring to mostly Z-Wave devices that have the binary sensor for “low battery” ?

I love it! This new version of Battery Notes replaced most automations, blueprints and convoluted lovelace cards in my setups :slight_smile:

How did you add the batteries as secondary info? Got battery notes installed.

2 Likes

Honestly I have not studied your code yet, but is it possible to notify which one is the low battery?

Hi,
I have created an automation using this blueprint quite a while ago. I am now getting an error for the automation as one of the device to be notified no longer valid due to an intentional change on the device. Hence I am trying to update or delete the automation but I am not able to edit the automation in the GUI nor find the automation in yaml.

I have the following lines in my configuration.yaml.
automation manual: !include_dir_merge_list automations/
automation ui: !include automations.yaml

I went through automations.yaml and the yaml files in automations/ but the automation is not listed. Any idea where it is stored ?
Thanks,

This blueprint would be stored in a file in config/blueprints/automation/SgtBatten

To be generic blueprints are installed at config/blueprints/<blueprint_type>/<author>/<blueprint> in this cause blueprint_type is automation

Hi @Patrick1610 , maybe this blueprint does what you need?

Have you checked in the UI?
For a while now, automations are created changed in the UI. Configuration - Automation

Thanks for sharing!