MrLinux
September 1, 2023, 9:53am
1
I have an auto-entities card creating a list of battery devices, sorted by battery level. Is there a way to click to sort the same card by name without having to create another card?
type: custom:auto-entities
show_empty: false
card:
show_header_toggle: false
type: entities
title: Battery Levels
filter:
include:
- attributes:
device_class: battery
sort:
method: friendly_name
numeric: false
MrLinux
September 1, 2023, 4:17pm
3
Thanks for the tip/trick. I’m curious if I can use a declutter card to incorporate the sort-toggle button seamlessly into the auto-entities card.
jeffcrum
(Jeff Crum)
September 1, 2023, 11:59pm
4
Here is a quick example.
I created an input_select called battery_sort. Set options to name and level.
Remove the sort from your auto_entities. It is done in the template.
Here is the card.
- type: vertical-stack
cards:
- type: entities
entities:
- input_select.battery_sort
- type: custom:auto-entities
card:
type: entities
filter:
template: >
{%- set ns = namespace(batteries=[]) -%}
{%- for battery in states.sensor | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', 'eq', 'battery') -%}
{%- set ns.batteries = ns.batteries + [{"id": battery.entity_id, "name": state_attr(battery.entity_id, 'friendly_name'), "level": battery.state | int(default=0) }] -%}
{%- endfor -%}
{%- for battery in ns.batteries | sort(attribute=states.input_select.battery_sort.state) -%}
{{ battery.id }},
{%- endfor -%}
options: null
You can certainly pretty it up. Use two buttons to change the input_select.battery_sort value with scripts and not put the input_sort in the card. But, should be a start.
1 Like
Remove “options: null
” line, it cannot be used with the “template
” option.
jeffcrum
(Jeff Crum)
September 2, 2023, 12:50am
6
Thanks for that. But, it seems to be ignored if in there.
Matter of perfection, this option should not be here.
Also, I would add “battery.state | int(default=0)
” to handle “unavailable
” states…
1 Like
jeffcrum
(Jeff Crum)
September 2, 2023, 12:57am
8
I am absolutely good with perfection and clean code.
Thanks for your help!
Btw, there is a great flex-table-card.
Can be used to list batteries & sort:
Unfortunately, so far it does not support re-sorting by clicking on a header.
jeffcrum
(Jeff Crum)
September 7, 2023, 2:06am
10
Need a default on the conversion to int in my code above in case something is unavailable. I have edited the above post.
Update - this functionality is added yesterday.
1 Like
jeffcrum
(Jeff Crum)
September 8, 2023, 9:13pm
12
Not actually added yet. But, there is a pull request for it.