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.
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
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:
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.
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.
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
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:
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.