Counting number of devices with low battery

Hey guys,

Iam trying to create a status page where I can see with one quick glance the status of my devices.
iam struggling with the count of devices (per device_class, motion, temperature, window, door) which have a low battery. I dont have to know which devices have a low battery level, just how many.

I came across several solutions for instance:
a blueprint https://community.home-assistant.io/t/low-battery-level-detection-notification-for-all-battery-sensors/258664

or:
https://community.home-assistant.io/t/howto-create-battery-alert-without-creating-a-template-for-every-device/30576

but I was more looking for a one-liner something like:

{{ states | selectattr('attributes.device_class', 'eq', 'motion') | selectattr('attributes.device_class', 'eq', 'battery') | selectattr('state', '<=','20') | list | count}}

I tried several several combinations from several one-liner examples, But I lack the skills to figure this out, if even possible.

Somebody?
Thanks in advance
Frank

I am using this sensor provided by a user to count any battery devices below 10%. You can dissect what you need from it.

  - platform: template
    sensors:
      devices_with_low_battery:
        friendly_name: 'Devices with low battery'
        unit_of_measurement: devices
        value_template: >-
          {% set ignore_entities = ['sensor.id_battery_level', 'sensor.hallway_door_battery',  'sensor.cr_curtains_battery', 'sensor.lr_curtains_battery'] %}
          {{ states.sensor
            | selectattr('attributes.device_class', 'eq', 'battery')
            | rejectattr('entity_id', 'in', ignore_entities)
            | map(attribute='state')
            | reject('in', ['unknown', 'unavailable'])
            | map('int', -1) | select('le', 10)
            |select('ge', 0)
            | list | count
          }}
        icon_template: >-
          {% if is_state('sensor.devices_with_low_battery', '0') %}
            mdi:check-circle
          {% else %}
            mdi:battery-alert
          {% endif %}

2 Likes

I use Auto Entities with this code.

card:
  type: entities
  state_color: true
  title: Batteries
filter:
  include:
    - attributes:
        device_class: battery
      state: < 30
  exclude:
    - entity_id: sensor.mancave_door_blind_power
sort:
  method: state
  reverse: true
show_empty: true
1 Like

Thank you both, will give it a try…

I am not sure what I am doing wrong, but I keep getting a message when I test the template that the attribute device class is not available for the domain “sensor”

Any thoughts what I am doing wrong here!?

UndefinedError: ‘homeassistant.util.read_only_dict.ReadOnlyDict object’ has no attribute ‘device_class’

This is the exact error I am getting using this in the template editor:

       {{ states.sensor
            | selectattr('attributes.device_class', 'eq', 'battery')
            | map(attribute='state')
            | reject('in', ['unknown', 'unavailable'])
            | map('int', -1) | select('le', 10)
            |select('ge', 0)
            | list | count
          }}

Add the first selectattr shown below.

        {{ states.sensor
            | selectattr('attributes.device_class', 'defined')
            | selectattr('attributes.device_class', 'eq', 'battery')
            | map(attribute='state')
            | reject('in', ['unknown', 'unavailable'])
            | map('int', -1) | select('le', 10)
            |select('ge', 0)
            | list | count
        }}
3 Likes

nice… that’s solved it

Thank you very much for your help :slight_smile:

1 Like

Hi Is there a way to exclude all sensor from a particular integration, to be precise I want the result of number of low battery but I don’t want any sensor from all mobile APPs, I don’t need to if my iPhone is part of number of low batteries?

It’s possible to exclude all sensor entities from a particular integration.

For example, this rejects anything that part of the Tasmota integration.

{{ states.sensor
            | selectattr('attributes.device_class', 'defined')
            | selectattr('attributes.device_class', 'eq', 'battery')
            | rejectattr('entity_id', 'in', integration_entities('tasmota'))
            | map(attribute='state')
            | reject('in', ['unknown', 'unavailable'])
            | map('int', -1) | select('le', 10)
            | select('ge', 0)
            | list | count
        }}

I don’t use the mobile app so I don’t the name of the integration that represents entities from the app.

1 Like

Hi
Sorry for resurrecting this old thread. But I try to replace the fixed limit of 10, to a value of a sensor.

        | map('int', -1) | select('le', {{ (states('sensor.any_sensor')) }})

My suggestion is not working, gives an error.

For anyone wondering you can use the above with the integration name as ‘mobile_app’ excludes all battery sensors from devices that use the homeassistant app.