Automatically generated Low Battery devices Alert, using auto-entities (mainly)

Hey guys,
I finally managed to make it work so i thought i shared.
(I’m sure there are plenty of other ways to do it but here it goes:)

First step was to create an automatic card that would show all the devices that report a battery level using auto-entities and secondaryinfo-entity-row, like this:

card:
  show_header_toggle: false
  title: Devices with battery
  type: entities
filter:
  exclude: #excluding some devices that are reported twice
    - entity_id: switch.plug_
    - entity_id: sensor.humidity_
  include:
    - attributes:
        battery_level: <101 #make sure all your devices report battery level like this or include any different attributes accordingly
      options: #added this so that the battery level is reported as secondary_info
        type: 'custom:secondaryinfo-entity-row'
        secondary_info: '[[ {entity}.attributes.battery_level ]]%'
sort:
  method: attribute
  attribute: battery_level
type: 'custom:auto-entities'

This card shows all my devices but its pretty big so i hid it on some of the last tabs of my config.

I created this card (which only appears if populated) and placed it first on my main page.

card:
  show_header_toggle: false
  title: Device Low Battery Alert
  type: entities
filter:
  exclude:
    - entity_id: switch.plug_
    - entity_id: sensor.humidity_
    - entity_id: sensor.humidity_
  include:
    - attributes:
        battery_level: <10
      options:
        type: 'custom:secondaryinfo-entity-row'
        secondary_info: '[[ {entity}.attributes.battery_level ]]%'
show_empty: false
sort:
  method: attribute
  attribute: battery_level
type: 'custom:auto-entities'

There, hope i helped.

Now if i can find a way that shows the battery level in the state area instead of secondary-info and works with the other plugins, that would be great.

Note: In order to make secondaryinfo thingy to work i had to declare it in the raw config as type:module although documentation reads type:null

8 Likes

This is great. Works well on 0.118. Now trying to figure out how to get a notification if an entity meets the battery_level criteria.

Thanks for sharing, the code didn’t work exactly how I expected, here is mine.

type: 'custom:auto-entities'
card:
  show_header_toggle: false
  title: Battery low
  type: entities
filter:
  include:
    - attributes:
        device_class: battery
      state: "<= 10"
  exclude:
    - name: /[Ll]ow/
    - name: /[Ss]tate/
sort:
  method: state
  numeric: true
19 Likes

Like these filters, didn’t know about them :slight_smile:

Thanks for this! I tried several posts and blogs, and all of them had something wrong with the code. Yours worked straight away.

just as a reference to make this dynamic, using config-template-card (might be even heavier in the frontend than auto-entities alone (evaluating battery sensors constantly), but there it is:

  - type: custom:config-template-card
    entities:
      - input_number.battery_alert_level
    variables:
      threshold: states['input_number.battery_alert_level'].state
    card:
      type: custom:auto-entities
      card:
        type: entities
        state_color: true
        card_mod:
          style: |
            ha-card {
              box-shadow: none;
              margin: 0px -16px;
            }
      filter:
        include:
          - attributes:
              device_class: battery
            state: "${ '<=' + threshold}"
        exclude:
          - name: /[Ll]ow/
          - name: /[Ss]tate/

which is a replacement or alternative to:

    filter:
      template: >
        {% set threshold = states('input_number.battery_alert_level')|float(0) %}
        {% set batteries =
             states.sensor
              |selectattr('attributes.device_class','defined')
              |selectattr('attributes.device_class','eq','battery')
              |map(attribute='entity_id')|list %}
        {% for s in expand(batteries)
           if s.state|is_number and s.state|float(0) < threshold %}
          {{s.entity_id}}
        {%- endfor %}

Using this in an Auto-Entities card, works just as expected but, does anyone know how to add a replace to this code to replace parts of entity names.

I want to try and clean up the code a bit, and seeing battery or battery_level against each entity name is bit messy and I want to remove those parts of the strings.

you might want to use template-entity-row which allows you to do anything on those fields with straightforward jinja templates

                type: custom:template-entity-row
                secondary: >
                  {{relative_time(states[config.entity].last_changed)}} ago
                name: >
                  {{state_attr(config.entity,'friendly_name').split(' Dur')[0]}}
                state: >
                  {{states(config.entity)}} min - {{states(config.entity|replace('duration','distance'))|int(default=0)}} km
1 Like

That looks like an interesting possibility and I’ve installed the plug-in and tried to figure out where I could insert a replace in the existing code but I haven’t got a clue.

This is my existing code which has been extended to include additional code already:

EDIT: Just to add, I have tried adding this to my code below, without success so I’m probably way off in my understanding of this plug-in.

type: custom:template-entity-row
                name: {{replace( "Battery Level", "") }}

Existing Card Code

type: custom:auto-entities
card:
  show_header_toggle: false
  title: Average
  state_color: false
  type: entities
  card_mod:
    style: |
      ha-card .card-header {
        font-size: 14px;
        color: white;
        font-weight: 600
      }
      ha-card {
          #states > div {
            margin: -15px 0px !important;
          }
        .card-content hui-sensor-entity-row:
          $:
            hui-generic-entity-row:
              $: |
                .info {
                  margin-left: 0px !important;
                 }
        --mdc-icon-size: 10px !important;
        --paper-item-icon-color: yellow;
        font-size: 12px !important;
        padding: 0px 0px 0px !important;
        width: 400px !important;
        font-weight: 200;
        background: none;
        .card-content div {
          margin-top: 0px !important;
          margin-bottom: 0px  !important;
        }
      }
filter:
  include:
    - attributes:
        device_class: battery
      state: < 70
  exclude:
    - name: /[Ll]ow/
    - name: /[Ss]tate/
    - name: iPad M Battery Level
    - name: M MACBook Battery
    - name: iPhone M App Battery Level
    - name: iPhone R App Battery Level
    - state: < 30
sort:
  method: state
  numeric: true
show_empty: false

fr the template to work, you got to provide a full working template. you need to feed it the name of the config entity you are replacing the string in

like:

              type: custom:template-entity-row
              state: >
                {{states(config.entity)}} dB
              secondary: >
                Level: {{states(config.entity.replace('rssi_level','discrete_rssi_level'))}}

and then ofc point to the name (or friendly_name) attribute of the config.entity

or the example I posted above :wink:

I appreciate your replies to this, Marius, but I’m just not getting this at all.

I’ve tried for much of the day, using a simplified entity card to ‘practice’ and I can get some of this to work for a single entity but I can’t see how to make it work for a device_class list, as I don’t want to have to input each battery entity separately as I would have to edit it each time I add a new battery powered device.

And templates are not my strong point…but I’m trying to learn :grimacing:

the main idea here is to have auto-entities select the entities (what’s in a name)

and then under options of the filter (in your case a device_class) have the custom card listed with the specific templates you want.

if you use config.entity in that template auto-entities then replaces that with the actual entity it finds to list in your card

break things down:

did you manage to have the correct entities listed, without template, in a core entities card? Always the first step to test.

I’m playing around with the same, I’ve almost got it working, there’s a couple of gotchas:

  • The way batteries are exposed between Zigbee2MQTT (or maybe just MQTT) is fairly different to ZHA.
  • Filter is difficult due to the differences.

Does anyone know if there’s a way to create a “virtual list” behind the scenes, and then simply use entity list to pull from that?
If it was just the lovelace card, it could be made to work inside auto-entities if I kept plowing away at it, but what about wanting to create a kind of notify service?
E.g. if a battery has continuously been under the threshold for the last 24 hours (to avoid the unreliability of most zigbee devices voltage sensors), send a telegram message or something similar, does anyone know a way?
In such a way the same “virtual list” could be used to pull from the card UI as well as notify.

Here’s what I have hacked away with for the auto-entities approach based on various examples:

card:
  show_header_toggle: false
  title: Low Battery Devices
  type: entities
filter:
  exclude: null
  include:
    - attributes:
        battery: < 45
      options:
        type: custom:template-entity-row
        state: |
          {{state_attr('this.entity_id','battery')}} %
    - attributes:
        device_class: battery
        battery_voltage: < 2.7
      options:
        type: custom:template-entity-row
        state: |
          {{state_attr('this.entity_id','battery_level')}} 
show_empty: true
type: custom:auto-entities

While this works for both ZHA and Zigbee2MQTT (Zigbee2MQTT being battery: < 45 and ZHA being battery_voltage: < 2.7) battery_voltage is really awful, it seems to vary from device to device and there’s other aesthetic stuff like % missing.

life-saver, thank you very much!!!