Automatically add 'battery' entities to dashboard

I have a tab on lovelace in which I display all my battery entities using the ‘gauge’ widget, so I can check it and see at a glance if there are any batteries which need replacing. I have to manually update this, but that got me to thinking - is there a way that I could have anything with ‘battery’ in it automatically added to that screen?

I did this using two custom cards:

type: custom:auto-entities
card:
  type: grid
  columns: 4
card_param: cards
filter:
  include:
    - entity_id: sensor.*battery*
      options:
        type: custom:config-template-card
        entities:
          - this.entity_id
        card:
          type: gauge
          entity: this.entity_id

Auto entities from Thomas is awesome and gets a list of entities that meets the defined criteria (in this case with battery in the name although you need to change that to meet your configuration) and passes these into the card defined. However it works best with the entities card as it passes a list of entities in; by using the config-template-card you can get round that challenge.

I’m sure others have found smarter ways to do it but this works quite well for my use case. In all honestly I can’t fully remember how I got this working fully but it would have been through lots of reading on the forums and loads of trial and error!

4 Likes

Thanks - that’s exactly what I needed! Actually got it to work really nicely without a template (I might have missed what extras that would bring me though?).

This is my config for it:

type: custom:auto-entities
card:
  type: grid
  columns: 6
card_param: cards
filter:
  include:
    - entity_id: sensor.*battery*
      options:
        type: gauge
        min: 0
        max: 100
        severity:
          green: 50
          yellow: 30
          red: 15
        entities:
          - this.entity_id
4 Likes

If you’re open to a totally different solution this highly configurable custom battery card works great.

4 Likes

I have a bunch of entities that are ‘battery’ related but aren’t actual percentages. So this code only shows things with ‘battery’ and % as it’s unit. (I’ve also added an auto sorting thing so that the lowest percentages are the top)

type: custom:auto-entities
card:
  type: grid
  columns: 4
card_param: cards
filter:
  include:
    - entity_id: sensor.*battery*
      attributes:
        unit_of_measurement: '%'
      options:
        type: custom:config-template-card
        entities:
          - this.entity_id
        card:
          type: gauge
          entity: this.entity_id
sort:
  method: state
  numeric: true
1 Like

As I spent nearly 3 hours in building my battery status card, I would like to share the solution as this might help others too.

My goal was to combine the following things:

  • Automatically populate the card for all desired battery powered devices
  • Display the result with the gauge card
  • Remove “Batterie” or “Battery” from the friendly_name
  • Reduce the font size to limit card size

There are multiple solutions to achive 2-3 of the above items but i wanted all 4 :grin:
To get this up and running I played arround in formatting json in a way to work together with the filter-template-option of the lovelace-auto-entites card.

type: custom:auto-entities
card:
  type: grid
  title: Batteriestatus
  columns: 4
  square: false
card_param: cards
filter:
  template: |-
    {% for state in states.sensor -%}
      {%- if (state.entity_id | regex_match("sensor.*battery*", ignorecase=True)) -%}
        {{
          {
            'type' : "gauge",
            'entity' : state.entity_id,
            'name': state.attributes.friendly_name|regex_replace(find='(:|)(Batterie|)(Battery|)(level|numeric|)', replace='', ignorecase=True),
            'card_mod' : {
              'style': "ha-card > div {\n   font-size: 10px !important;\n}"
            },
            'options' : {
              'entities': [
                state.entity_id,
              ], 
            }
          }
        }},
      {%- endif -%}
    {%- endfor %}
  exclude:
    - entity_id: sensor.total_battery_energy_charged
    - entity_id: sensor.total_battery_energy_discharged
    - entity_id: sensor.battery_power_charging
    - entity_id: sensor.battery_power_discharging
    - device_manufacturer: BYD
sort:
  method: attribute
  attribute: battery
  reverse: false
  numeric: true

1 Like

Hi

If I wanted to add severity in this card how do I do it

What would the code look like -

Sorry not so good with coding still learning

Vas

I am using integration Battery Notes and Battery State Card;
Is there a possibility to get Devices considered in that card
which doesn’t have a Battery status with % reflected but status LOW_BAT ?

Try this:

type: custom:auto-entities
card:
  type: entities
filter:
  include:
    - integration: battery_notes
      domain: binary_sensor
      state: on
  exclude: []

And you can add in your Low Batteries

type: custom:auto-entities
card:
  type: entities
filter:
  include:
    - integration: battery_notes
      domain: binary_sensor
      state: on
    - integration: battery_notes
      domain: sensor
      attributes:
        device_class: battery
      state: <= 25
  exclude: []
1 Like

Thanks for your help.
I have setup a card like this - it is showing nothing, but currently non of my 15 “low_bat” sensors has status ON. I need to wait till one of those batteries getting empty. Set state to on is directly overwriten again with the real status. I am going to provide feedback.

You can easily verify your code. In the card editor, just temporarily change on to off and if the card populates with the ‘on’ devices then you know it works. Undo the change and you’re good to go.

I’m not familiar with severity in HA. Can you explain what you mean?
How does severity appear in your entities??

Hi Daniel, great catch! :see_no_evil:
Unfortunateley the HmIP devices are not shown, only some Aqara and Hue stuff.


Not clear to me because they have the same device class and show up under battery_notes integration.

Your filter only includes binary_sensors.
Are the HmIP devices binary_sensor or sensor?

Here’s my include filter…

filter:
  include:
    - integration: battery_notes
      domain: sensor
      attributes:
        device_class: battery
      state: < 60

It shows me all of the Battery Notes sensor.*_battery_plus entities (if I remove the state: < 60).

Here are two of my card designs which make use of new feattures in the
Battery Notes (V2.0) Integration. The latest card design uses custom:template-entity-row which although a little heavy on templating, enabled me to call the battery_notes.set_battery_replaced service via a tap hold_action and pass it the required device_id:. The card I created looks identical to the one I made first using custom:battery-state-card where I had to create and call a script as it denied the use of the required device_id: data value, plus it does not allow a jinja template to use {{device_id('entity_id')}} nor does it even have a hold_action option in the first place (it’s been added to the v3.3 milestone now though).

image

The first one…

type: custom:auto-entities
card:
  type: entities
  title: Low Batteries
  debug: false
  card_mod:
    class: inline-card
filter:
  include:
    - integration: battery_notes
      domain: sensor
      attributes:
        device_class: battery
      state: < 60
      options:
        type: custom:template-entity-row
        entity: this.entity_id
        name: >-
          {{state_attr(config.entity,'friendly_name')}}
          ({{state_attr(config.entity,'battery_type_and_quantity')}})
        secondary: >-
          Replaced: {{
          state_attr(config.entity,'battery_last_replaced')|relative_time() }}
          ago
        state: '{{ states(config.entity)|int(0) }}%'
        hold_action: |
          {
            "action": "call-service",
            "service": "battery_notes.set_battery_replaced",
            "data": {
              "device_id": "{{ device_id(config.entity) }}",
              },
            "confirmation": {
              "text": "Update Battery Replaced Date",
              },
          }
    - integration: battery_notes
      domain: binary_sensor
      attributes:
        device_class: battery
      state: 'on'
  exclude:
    - integration: mobile_app
sort:
  method: state
  numeric: true

and the second one…

type: custom:auto-entities
card:
  type: custom:battery-state-card
  title: Low Batteries
  debug: false
  card_mod:
    class: inline-card
filter:
  include:
    - integration: battery_notes
      domain: sensor
      attributes:
        device_class: battery
      state: < 60
      options:
        name: '{attributes.friendly_name} ({attributes.battery_type_and_quantity})'
        secondary_info: 'Reported: {attributes.battery_last_replaced|reltime()}'
        hold_action:
          action: call-service
          service: script.battery_notes_battery_replaced
          service_data:
            entity_id: this.entity_id
    - integration: battery_notes
      domain: binary_sensor
      attributes:
        device_class: battery
      state: 'on'
  exclude:
    - integration: mobile_app
sort:
  method: state
  numeric: true

Really strange, they are binary_sensor but don’t show up with domain: binary_sensor

Can you show me your device info screen?
And also your current auto-entities include: filter?


yellow marked is the low_bat binary_sensor: binary_sensor.garagentor_low_bat

type: custom:auto-entities
card:
  type: entities
filter:
  include:
    - integration: battery_notes
      domain: binary_sensor
      attributes:
        device_class: battery
  exclude: []

Very strange.
My MQTT devices all show up with the following filter (see 2nd include):

filter:
  include:
    - integration: battery_notes
      domain: sensor
      attributes:
        device_class: battery
      state: < 60
      options:
        name: '{attributes.friendly_name} ({attributes.battery_type_and_quantity})'
        secondary_info: 'Reported: {attributes.battery_last_replaced|reltime()}'
    - integration: battery_notes
      domain: binary_sensor
      attributes:
        device_class: battery
      state: 'off'
  exclude:
    - integration: mobile_app