Low battery level detection & notification for all battery sensors

Hi and many thanks for sharing this blue print. :+1:
I tested it and it works very well with the usual devices with a corresponding battery display in %.
But now Iā€™m facing a problem because I also have devices that donā€™t use the device_class ā€œbatteryā€ but ā€œvoltageā€ā€¦ and they are devices with different voltage levels.
This means, for example, some devices work with a 1.5V battery and others with 3V or 9V and therefore all of these device/battery types reach their alarm value at a different voltage level.
For 1.5V devices this could be 1.2V, for 3V devices it could be 2.5V etcā€¦

Now Iā€™m asking myself whether I may have overlooked something that I could use to implement this with this blue print or whether it would be possible to integrate this function into this template?

Or are there other approaches for this?
I would be very happy about any help.
regards,

@c64 my suggestion would be to find a way to update those devicesā€™ integrations so that they do use the device_class ā€œbatteryā€. I donā€™t see a reason why they should not do this. If the designer of the device or maintainer of the integration knows what the full battery voltage is, then they can define some logic to work out the percentage battery remaining.

Without this you will probably end up with some guesswork, but you could create a template sensor, set the device class to battery, the unit of measurement to %, then use the following template, to linearly create a % battery sensor based on the voltage reading.

{% set vmax = 9.0 %}
{% set vmin = 8.0 %}
{% set v = states('sensor.your_sensor_here') | float %}
{% if v > vmax -%}
  100.0
{%- else -%}
  {{(v-vmin)/(vmax-vmin)*100}}
{%- endif %}

Youā€™ll need to define vmax, vmin and the sensor entity to suit your sensor.
This should generate a new sensor that the blueprint can see.

I should add, youā€™ll need to do this for each sensor that you want to generate a battery % for.

1 Like

Hello @dave_t_uk
Thanks for answering here and for the tip with example code.
As a workaround solution, this could effectively work, but it would be a hassle for every single sensor and wouldnā€™t be a particularly smart solution :slight_smile:
Letā€™s see how many sensors are affected in the end and whether I will solve it with a template sensor.
A battery control panel in HA or an add-on that would be dedicated to battery monitoring would be a great and certainly useful thing, because there are likely to be a lot of battery-operated devices in every smart home.
best regards

I came across this post almost by accident and compared the template I had written some time ago for battery control. Instead of individually excluding entities from automation (generally mobile devices with companion app installed) I directly excluded the ā€˜mobile_appā€™ integration.

{% set class = states.sensor | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', 'eq', 'battery') | rejectattr('state','in',['unavailable','unknown']) %}
{% set low_battery_sensors = namespace(sensors=[]) %}
{% for battery in class %}
  {% if not battery.entity_id in integration_entities('mobile_app') and battery.state | int (0) <= states('input_number.low_battery') | int and battery.state | int (0) != 0 %}
    {% set low_battery_sensors.sensors = low_battery_sensors.sensors + [ battery.name + ' ' + battery.state + '%' ] %}
  {% endif %}
{% endfor %}
{{ low_battery_sensors.sensors }}

this the template I use with helper input_number.low_battery. I hope it can be useful

1 Like

@Sbyx I saw that you deleted a comment in response to what I had written. I hope I didnā€™t annoy or worse yet annoy you. Should I delete the post?

Really no problem at all

This automation is all I wanted!!

Now that To-Do List are a thing I want to add all the batteries below 20% to my shopping cart but the problem is that ii will keep adding the same item over and over everyday, also, Itā€™ll be nice to be able to add each battery as an individual item to the list.

Any ideas on this?

1 Like

How do I know which version I have installed of this Blueprint, it doesnā€™t say anywhere and I donā€™t know how to update it. The Github page doesnā€™t show it either in the code.

Also not sure what the ACTION section is used for? I get the notification on my mobile with correct batteries listed, but I have nothing in my ACTION section.

Iā€™m new to this, so please gently.

Is there a way to get a list of devices shown in the dashboard?
I rather see it in the dashboard, then getting notified every week (to much notifications already).

1 Like

I use the auto-entities card for this:

type: custom:auto-entities
card:
  type: entities
filter:
  include:
    - attributes:
        device_class: battery
      state: < 15
  exclude: []
2 Likes

There is a special card for batteries that is useful here as well:

  - type: custom:auto-entities
    card:
      type: custom:battery-state-card
    filter:
      include:
        - attributes:
            device_class: battery
          state: "< 50"  # Changed for the example
      exclude:
        - integration: mobile_app
    sort:
      method: state
      reverse: true
    card_mod:
      style: |
        ha-card {
          border: none !important
        }

Here is my extended setup, showing all batteries:
image

Full config:

type: custom:vertical-stack-in-card
title: Batteries
cards:
  - type: custom:auto-entities
    title: Batteries needing attention
    card:
      type: custom:battery-state-card
    filter:
      include:
        - attributes:
            device_class: battery
          state: < 25
      exclude:
        - integration: mobile_app
        - name: Kostal-*
    sort:
      method: state
      numeric: true
    card_mod:
      style: |
        ha-card {
          border: none !important
        }
  - type: custom:auto-entities
    card:
      type: custom:battery-state-card
    filter:
      include:
        - attributes:
            device_class: battery
          state: unknown
        - attributes:
            device_class: battery
          state: unavailable
      exclude:
        - integration: mobile_app
        - name: Kostal-*
    sort:
      method: state
      numeric: true
    card_mod:
      style: |
        ha-card {
          border: none !important
        }
  - type: custom:auto-entities
    card:
      type: custom:battery-state-card
    filter:
      include:
        - attributes:
            device_class: battery
          state: '>= 25'
      exclude:
        - integration: mobile_app
        - name: Kostal-*
    sort:
      method: state
      numeric: true
    card_mod:
      style: |
        ha-card {
          border: none !important
        }
5 Likes

Awesome Blueprint! Works great, TYVM!

I have a set of sensors - plenty of them, all packed in one automation based on this great blueprint. Three of them need a lower Battery warning level threshold, as the one used in the automation is a bit too high for those - triggering the notification unnecessarily.

Trying to avoid excluding those 3 and creating another automation for them. Is there a way in the Actions section to handle such cases? Maybe by using and rejecting the specific entities in the sensor with a bit of templating?

Like ā€œif only sensors a, b or c are low, do nothing. Otherwise, do the regular stuff (send notification etc.)ā€:

An easy way to not trigger a notification if sensors is empty:

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:
      - choose:
          - conditions: "{{ sensors | length > 0 }}"
            sequence:
              - service: notify.notify
                data:
                  message: The battery of the sensor(s) {{sensors}} is low.
    time: "12:00:00"
2 Likes

Thanks for a great blueprint! I had originally looked at doing something similar with a binary sensor with attribute list of the battery sensors below the threshold (with an automation triggered by that binary sensor) but I like the idea of only checking on a regular schedule instead of having code evaluated every time any of dozens of battery sensors change state.

But the reason I am posting here is that Iā€™m cleaning up my HA configuration and I realized that I forked your code almost a year ago and created my own custom blueprint with various modifications but I never shared that with you. You can see my code in this git.

Here are the mods I made:
- changed results list delimiter from comma to newlines
- added persistent notification action (but still support additional actions)
- excludes mobile device battery sensors (adapted from blueprint by Marck)

@Sbyx Iā€™d like to suggest an update to add the battery_size attribute.

If you change the below section of your Blueprint from thisā€¦

And update it to thisā€¦

  {% if 0 <= state.state | int(-1) < threshold | int and not state.entity_id in exclude.entity_id %}
    {% if state.attributes.battery_size != null %}
      {% set result.sensors = result.sensors + [state.name ~ ' (' ~ state.state ~ ' % / Type: ' ~ state.attributes.battery_size ~ ') '] %}
    {% else %}
      {% set result.sensors = result.sensors + [state.name ~ ' (' ~ state.state ~ ' %)'] %}
    {% endif %}
  {% endif %}

It would have this as the output:
iPad Battery Level (15 %), Plant Sensor 1EC0 Battery (0 %), Deep Freeze Environment Battery (16 % / Type: CR2032)

Adding that battery_size attribute to the notification would make it more usable!

**Note, for sensors without the battery_size attribute, as seen below, and it showed up just fine.

3 Likes

Not sure how you added those attributes but itā€™s not Battery Notes.
The integration adds one new entity to a device called battery_type

@codechimp Thank you for jumping in! I did not realize that may of my devices had battery_size as an attribute before installing Battery Notes! Iā€™ll update my previous post.

The first request

is easy to solve in the yaml code.

message: "{{sensors | replace('Battery', '')}}"

Or.

message: "{{sensors | replace('Battery-level', '')}}"

Just change text depending what you want to remove.

Full yaml when creating blueprint:

alias: Auto - Battery Low (TEST UDEN BATTERY)
description: ""
use_blueprint:
  path: itn3rd77/low-battery-detection-notification.yaml
  input:
    threshold: 40
    exclude:
      - sensor.x1234
      - sensor.x12345
    actions:
      - service: notify.mobile_app_xxxxxxx
        data:
          title: Battery Low!
          message: "{{sensors | replace('Battery', '')}}"
    message: "[% bullet %] [% sensor %] ([% state %]%)"
1 Like

This is OUTSTANDING. I use UI-Minimalist, and have a screen for battery levels which hase them grouped by room and animated icons for cell phones that are charging and the like. It is very convenient, and I am sure to add to it whenever I get any new sensors that is battery-based, but it is tedious - and i only have to do it once however. So I love the idea of automating it and only showing items that have low battery levels, but then you always think in the back of your mind what about the other ones (even if offline)ā€¦ iā€™d really like to always see all of them et ceteraā€¦ Keep up the great work however this works best for me. It would be great to automate building a ui minimalist screen like what I am doing but once itā€™s set up then you donā€™t need to bother signing it up again so the idea of automating it is getting diminishing returns for your effortsā€¦

@Sbyx Is there a way how to send you a coffee or something? This blueprint is amazing!