Home-Assistant Battery Inventory Management
I wanted to have a report allowing me to have an overview of the types of batteries used with the quantity used by equipment, in order to help me in planning my battery orders.
With this report, I can easily plan my battery purchases. Unfortunately, at the moment, in Home-Assistant, there is no way to know the type of battery and the quantity used by a device.
But Home-Assistant allows to customize the entities, so I added the type of battery and the quantity used per device and for all my devices.
I use customize.yaml to store all my customizations. If you don't have a customize.yaml file, you can create one.
In your configuration.yaml
set customize with a file named customize.yaml
like this:
homeassistant:
customize: !include customize.yaml # customize Home Assistant
Inside the customize.yaml
file use the following code as base template:
sensor.first_floor_landing_motion_sensor_battery_level:
battery_type: CR123A
sensor.first_floor_landing_smoke_sensor_battery_level:
battery_type: CR123A
sensor.first_floor_landing_lcd_battery:
battery_type: LR03/AAA
quantity: 2
sensor.aqara_opple_switch_2x2_battery:
battery_type: CR2032
sensor.anemometer_battery:
battery_type: LR06/AA
sensor.attic_staircase_motion_sensor_battery:
battery_type: CR2450
sensor.garden_south_irrigation_controller_battery:
battery_type: LR06/AA
quantity: 4
You need to adapt the entity_id
to your needs. the entity_id represents your stack level value for a sensor.
The battery_type
attributes is mandatory.
For the battery_type
attribute you can use any text representation of a battery type.
Example:
- CR123A
- LR03/AAA
- LR06/AA
- CR2450
The quantity
attribute is optional, the template will assume a value of 1
if is not specified. The quantity
must be a valid number.
Just copy the yaml code and paste it into a empty Manual card
inside of your Dashboard.
type: markdown
content: >-
{%- set ns = namespace(sensors=[], battery_type = '', quantity = 0, report=[], info=[]) -%}
{%- for state in states.sensor
| selectattr('attributes.device_class', 'defined')
| selectattr('attributes.device_class', 'eq', 'battery')
| selectattr('attributes.battery_type', 'defined') -%}
{%- if not state_attr(state.entity_id, 'battery_type') in ['SolarPanel','DC', 'Battery', 'APCRBC110'] -%}
{% set ns.sensors = ns.sensors + [dict(name= state.name | lower | replace(':', '') | replace(' battery level', '') | replace(' battery', '') | capitalize() , battery_type=state_attr(state.entity_id, 'battery_type'), quantity=state_attr(state.entity_id, 'quantity') | int(1), level=state.state |int(0) )] %}
{%- endif -%}
{%- endfor -%} {%- set battery_list = ns.sensors | sort(attribute='battery_type') %} {%- for battery in battery_list -%}
{% if ns.battery_type == battery.battery_type %}
{% set ns.quantity=ns.quantity + battery.quantity %}
{% set ns.info=ns.info+[dict(name=battery.name,quantity=battery.quantity,level=battery.level)] %}
{% else %}
{% if ns.battery_type != '' %}
{% set ns.report = ns.report+[dict(battery_type=ns.battery_type, quantity=ns.quantity, info=ns.info | sort(attribute='level')) ] %}
{% endif %}
{% set ns.battery_type= battery.battery_type %}
{% set ns.quantity=battery.quantity %}
{% set ns.info=[dict(name=battery.name,quantity=battery.quantity,level=battery.level)] %}
{% endif %}
{%- endfor -%}
<table>
<thead>
<tr><th>Type</th><th>Quantity</th><th>Details</th></tr>
</thead>
<tbody>
{%- for state in ns.report -%}
<tr>
<td VALIGN=TOP>{{ state.battery_type }}</td><td VALIGN=TOP><b>{{ state.quantity }}</b></td> <td VALIGN=TOP> {%- for info in state.info -%} {{ info.quantity }}x {{ info.name }} <font color={{ (info.level < states.input_number.low_battery_report_threshold.state | int) | iif("OrangeRed", "White") }}><b>{{ info.level }}%</b></font><br> {%- endfor -%}<br></td>
</tr>
{%- endfor -%}
</tbody>
</table>
title: Battery Inventory Management
You will also need to create a Numeric helper to set the low_battery_report_threshold
value as show:
Base on Lovelace: Battery state card
You can build this battery level reports:
You need to install the Lovelace: Battery state card, check official documentation Lovelace: Battery state card
Then you can use the following code to build the report:
type: custom:battery-state-card
title: Battery levels
sort_by_level: asc
tap_action: more-info
filter:
include:
- name: attributes.device_class
value: battery
exclude:
- name: state
value: Unknown
operator: '='
- name: state
value: 'On'
operator: '='
- name: state
value: 'Off'
operator: '='
- name: state
value: 60
operator: '>'
- name: state
value: Unavailable
operator: '='
- name: attributes.attribution
value: 'Data provided by Apple iCloud'
- name: entity_id
value: sensor.*apple_watch*
- name: entity_id
value: sensor.*iphone*
- name: entity_id
value: sensor.oregon_cm180i*
secondary_info: battery_type
round: 0
bulk_rename:
- from: Battery Level
to: ''
- from: ': Battery level'
to: ''
- from: sensor_battery
to: ''
- from: Battery level
to: ''
- from: battery level
to: ''
- from: Battery
to: ''
- from: battery
to: ''
Is my last battery report. Is helpful to discover if a device stop reporting the battery level. The battery level can be good, but the device didn't send anymore report. Can be a problem with the device or the battery is dead!
Also base on Lovelace: Battery state card
You can use the following code to build the report:
type: custom:battery-state-card
title: Battery levels date report
sort_by_level: asc
tap_action: more-info
collapse: 10
filter:
include:
- name: entity_id
value: '*_battery_level'
- name: attributes.device_class
value: battery
exclude:
- name: state
value: 'On'
operator: '='
- name: attributes.attribution
value: Data provided by Apple iCloud
- name: entity_id
value: sensor.*apple_watch*
- name: entity_id
value: sensor.*iphone*
- name: state
value: 'Off'
operator: '='
secondary_info: last_updated
bulk_rename:
- from: Battery Level
to: ''
- from: ': Battery level'
to: ''
- from: sensor_battery
to: ''
- from: Battery level
to: ''
- from: battery level
to: ''
- from: Battery
to: ''
- from: battery
to: ''
round: 0