Click to sort entities in a list

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
  show_header_toggle: false
  type: entities
  title: Battery Levels
    - attributes:
        device_class: battery
  method: friendly_name
  numeric: false

There is a workaround.

  1. Create input_boolean helper like “sort_by_name” or whatever.
  2. In auto-entities use “template” option for filtering instead of “include” - go to corr. auto-entities thread for examples.
  3. In this “template” option generate a list with a conditional sorting depending on a value of that “input_boolean”.
  4. Add a button or a toggle to toggle this flag in a neighbouring card.

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.

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
          - type: entities
              - input_select.battery_sort
          - type: custom:auto-entities
              type: entities
              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) -%}
                  {{ }},
                {%- 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.

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

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.

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

Not actually added yet. But, there is a pull request for it.

The update is there now.