🔋 Lovelace: Battery state card

Hi,
I have some Lorawan Sensors with very long battery live, and they only have battery status 0, 1, 2, 3. So my ideas was to multiply this value by 33, to get 0, 33, 66, 99 which would drop into more useful values but I do not understand how to use the multiply.

type: custom:battery-state-card
secondary_info: "{last_changed}"
filter:
  include:
    - name: entity_id
      value: sensor.dragino*bat*
      options:
        name: "{state|multiply(33)}"
sort:
  by: state
collapse: 8
bulk_rename:
  - from: " Battery"
  - from: " level"
colors:
  steps:
    - value: 0
      color: "#ff0000"
    - value: 1
      color: "#ffff00"
    - value: 2
      color: "#00ff00"
  gradient: true

leads to

The values of the sensory are like e.g.

sensor.dragino_275a08_018905e3_bat_stat_2

can anyone enlighten me?

Many thanks
Juergen

HI,
this:

type: custom:battery-state-card
secondary_info: "{last_changed}"
multiplier: 33
entities:
  - entity: sensor.dragino_275a08_018905e3_bat_stat_2
    name: Liebherr Fach 4
    multiplier: 33
  - entity: sensor.dragino_275a08_018905e4_bat_stat
    name: Liebherr Fach 2
    multiplier: 33
  - entity: sensor.dragino_275a08_018905e6_bat_stat_3
    name: Garage Dragino
    multiplier: 33
  - entity: sensor.dragino_275a08_0189ee98_bat_stat_4
    name: Hauswirtschaftsraum Dragino
    multiplier: 33
sort:
  by: state
colors:
  steps:
    - value: 0
      color: "#ff0000"
    - value: 1
      color: "#ffff00"
    - value: 2
      color: "#00ff00"
  gradient: true

works as I hoped. Is it possible, that the multiplier only works on single entities? Which is not a real problem, as I only have thos “special” four, but if there is an easier way to use multiplier on more it would be great.

thanks
Juergen

Couldn’t get the ha-icon to change colour, so added in ha-alert’s instead and added in condition to only show the card when >1 is below my threshold.

type: conditional
conditions:
  - entity: sensor.low_battery_count
    state_not: "0"
card:
  type: custom:vertical-stack-in-card
  cards:
    - type: markdown
      content: |
        ## Low Battery Alerts
    - type: markdown
      title: null
      content: |
        {%- set friendly_names = {
          "sensor.XX_battery_level": "XX Phone",
          "sensor.iXX_watch_battery_level": "XX Watch",
          "sensor.XX_battery_level": "Kiosk Battery",
          "sensor.iXXmacbook_pro_internal_battery_level": "MacBook",
          "sensor.XXX_iphone_2_battery_level": "XX Phone"
        } -%}
        {%- set all_batteries = states.sensor
          | selectattr('attributes.device_class', 'defined')
          | selectattr('attributes.device_class', 'eq', 'battery')
          | sort(attribute='state', reverse=False) -%}  

        {%- for battery in all_batteries -%}
          {%- set raw_state = battery.state -%}
          {%- set clean_state = raw_state | trim -%}
          {%- set state_int = clean_state | int(default=None) -%}
          {%- if state_int is not none and state_int <= 20 %}
            {%- set state_int = battery.state | int %}
            {%- if state_int <= 5 %}
              <ha-alert alert-type="error" style="display: flex; align-items: center;">
                <ha-icon 
                  icon="mdi:battery-{{ (state_int / 10) | int * 10 }}" 
                  style="margin-right: 8px;">
                </ha-icon>
                {{ friendly_names.get(battery.entity_id, battery.entity_id) }}: {{ state_int }}%
              </ha-alert>
            {%- elif state_int <= 13 %}
              <ha-alert alert-type="warning" style="display: flex; align-items: center;">
                <ha-icon 
                  icon="mdi:battery-{{ (state_int / 10) | int * 10 }}" 
                  style="margin-right: 8px;">
                </ha-icon>
                {{ friendly_names.get(battery.entity_id, battery.entity_id) }}: {{ state_int }}%
              </ha-alert>
            {%- elif state_int <= 20 %}
              <ha-alert alert-type="info" style="display: flex; align-items: center;">
                <ha-icon 
                  icon="mdi:battery-{{ (state_int / 10) | int * 10 }}" 
                  style="margin-right: 8px;">
                </ha-icon>
                {{ friendly_names.get(battery.entity_id, battery.entity_id) }}: {{ state_int }}%
              </ha-alert>
            {%- endif %}
          {%- endif %}
        {%- endfor %}

I did this:

card_mod:
  style: |
    ha-card .entity-spacing {
      margin: -10px 0px;
    }

is there a way to get the last known status or value of an entity which is offline ?

Brilliant!

Is there a possibility to show the replace timestamp with a relative time and in a second step
to add a color which depends on the years which are ago since the last battery replacement.
0-1 year green
1-2 years yellow …

I managed to get the list, but I have no idea how to show a relaitive time.

type: custom:battery-state-card
title: Replaced at
debug: ftrue
colors:
steps:
- “#ff0000”
- “#ffff00”
- “#00ff00”
gradient: true
filter:
include:
- name: entity_id
value: “*_battery_last_replaced”
options: null
exclude:
- name: state
value: Unknown
bulk_rename:

  • from: Batterie ersetzt
    Changed:
    state|reltime(): null
    sort:
    by: name
    desc: true

I’m currently using the battery-state-card to visualize a range of temperatures in my Home Assistant setup. While I’m quite satisfied with the card overall, I’ve encountered an issue where it seems unable to display negative temperature values. Could you please advise on how I can configure the card to correctly show negative temperatures?

Is it possible to filter entities in this card based on room or floor?
I tried using floor_id but it doesn’t seem to work properly.

Show what you’ve tried.

I have 3 rooms: room 1, room 2, room 3.
Floor 1 has room 1 and 2.
Floor 2 has room 3.
Devices are associated with one of the 3 rooms.

This is my config:

type: custom:battery-state-card
secondary_info: "{last_changed}"
filter:
  include:
    - name: attributes.device_class
      value: battery
    - name: attributes.floor_id
      value: "floor_1"
  exclude:
    - name: entity_id
      value: device_tracker.*
    - name: entity_id
      value: binary_sensor.*_battery_low
    - name: entity_id
      value: sensor.iphone*
    - name: entity_id
      value: sensor.ipad*
sort:
  by: state
collapse: 20
bulk_rename:
  - from: " Battery"
  - from: " level"
colors:
  steps:
    - "#ff0000"
    - "#ffff00"
    - "#00ff00"
  gradient: true

My second attempt is to change to:

filter:
  include:
    - attributes:
        device_class: battery
        floor_id: "floor_1"

Other attempt:

filter:
  include:
    - domain: "floor_1"

I am missing how to make the link between a device and its room+floor.
And I didn’t find any examples on the github.

I also tried with a working template which give me all entities with a battery from a specified floor:

{{ floor_areas("floor_1") 
| map('area_entities')| sum(start=[])  
| select('match', 'sensor.*_battery') 
| list }}

However if I try to integrate it, I get no data. I tried with:

filter:
  include:
    - name: entity_id
        value: |-
          {{ floor_areas("floor_1") 
           | map('area_entities')| sum(start=[])  
           | select('match', 'sensor.*_battery') 
           | list }}

and with the template input:

filter:
  template: |
    {% floor_areas("floor_1") %}
    {% map('area_entities') | sum(start=[])  %}
    {% select('match', 'sensor.*_battery')  %}
    {% list %}

With different syntax:

filter:
  template: |
    value: |-
        {{ floor_areas("floor_1") 
         | map('area_entities')| sum(start=[])  
         | select('match', 'sensor.*_battery') 
         | list }}

and:

filter:
  template: "{{ floor_areas('floor_1') | map('area_entities') | sum(start=[]) | select('match', 'sensor.*_battery') | list }}"

What am I missing?

What is expected by the “filter: include” ?
A list of entity ?
I tested those in the template tool:

I have the auto-entities card working, but I am missing the nice presentation of this card.

EDIT:
Sorry for the late spamming.
I finally got it working using the auto-entities !!

type: custom:auto-entities
card:
  type: custom:battery-state-card
  title: Battery warning
  secondary_info: "{last_changed}"
  sort:
    by: state
  collapse: 5
  bulk_rename:
    - from: " Battery"
    - from: " level"
  colors:
    steps:
      - "#ff0000"
      - "#ffff00"
      - "#00ff00"
    gradient: true

filter:
  template: |
      {{ floor_areas("floor_1") 
       | map('area_entities')| sum(start=[])  
       | select('match', 'sensor.*_battery') 
       | list }}

Which gives me:
image

I don’t see that the card support templates. It’s not stated in the docs anywhere. This is tangentially related too.

The floor, area or label isn’t available as a property/attribute on entities.

I think your luck is out, unfortunately.

What I do is to add a custom attribute via my customize.yaml to indicate it must be monitored.

    sensor.kitchen_smoke_battery:
      friendly_name: Kitchen Smoke Detector Battery
      monitor: True
        - type: custom:battery-state-card
          title: Battery Charge Levels
          filter:
            include:
              - name: "attributes.monitor"
                value: True
          bulk_rename:
            rules:
              - from: "Battery"
          sort:
            - "state"
            - "name"

This is an option as well, but I don’t want to configure the area or room twice. HA already provides this.

The last script provided above works as intended. (it displays all entities from “floor_1” in this example).

Right, I see: The auto entities card (as an alternative) indeed supports templates.

Hi to All. Is there an option to use template in state_map section ?

My goal is to mark in color the entities which are up-to-date with latest ESPHome version and which are not. Different icon would also be a solution.

Unfortunately, Code below does not work :confused:

type: custom:battery-state-card
secondary_info: "{last_changed}"
title: Version
state_map:
  - from: "{{ state_attr('sensor.esphome_esphome_latest_release','tag') }}"
    to: 1
  - from: "off" #have no idea how to select other states
    to: 2
filter:
  include:
    - name: entity_id
      value: "*_esphome_version"
    - name: entity_id
      value: sensor.esphome_esphome_latest_release
  exclude:
    - name: state
      value: Unavailable
sort:
  by: state
  desc: false
bulk_rename:
  - from: ESPHome Version
icon: mdi:information-box-outline
colors:
  steps:
    - value: 1
      color: "#00ff00"
    - value: 2
      color: "#ffff00"


I have exact same issue as @OrangePrize, as soon as I add anything to the YAML (eg. title: “foo”) the card stops displaying anything. I have pretty much default and up to date install of both HA and the Battery State Card. I don’t see any warnings or errors in the browser’s java script console. Any clues?

Hi did you manage to solve this as Im having the same issue