thomasloven / lovelace-auto-entities

🔹Automatically populate the entities-list of lovelace cards
MIT License
1.19k stars 111 forks source link

Feature Request - Sort by a 2nd value #367

Closed daloki83 closed 1 year ago

daloki83 commented 1 year ago

I created this config with all of my sensors for power meassurements including a switch to turn them on/off. But what ever i try, i'm not able to sort by the "power"-entitie/value. It always uses the state from the switch. Is there any way to do this or if not to implement?

type: custom:auto-entities
card:
  type: entities
  state_color: false
sort:
  method: state
  reverse: true
  numeric: false
  ignore_case: false
  ip: false
entities:
  - entity: switch.arbeit
    type: custom:multiple-entity-row
    toggle: true
    state_color: true
    entities:
      - entity: sensor.arbeit_energy_power
        name: ' '
  - entity: switch.fritz_box7590ax
    type: custom:multiple-entity-row
    toggle: true
    state_color: true
    entities:
      - entity: sensor.fritz_box7590ax_energy_power
        name: ' '

Screenshot 2023-06-09 234048

ildar170975 commented 1 year ago

Probably this is same as https://github.com/thomasloven/lovelace-auto-entities/issues/363 - i.e. about 2nd sort method. You may use more than 1 sorting method when using a template option.

daloki83 commented 1 year ago

Okay, but I cannot find and example or explanation for that. Could you provide a sample please?

ildar170975 commented 1 year ago

For instance:

    filter:
      template: >-
        {{ states.binary_sensor |
           selectattr('entity_id','search','battery_charging') |
           sort(reverse=false,attribute='entity_id') |
           sort(reverse=false,attribute='state') |
           map(attribute='entity_id') | list }}
daloki83 commented 1 year ago

Hmm not really working for me. I added:

filter:
  template: >-
    {{ states.sensor | selectattr('entity_id','search','_power') |
    sort(reverse=true,attribute='entity_id') |
    sort(reverse=false,attribute='state') | map(attribute='entity_id') | list }}

and he added every sensor with a power attribute to the rest of my sensors. I think i could exclude unwanted sensors, but how could i than bring back the Power-Switch button

Screenshot 2023-06-10 105000

daloki83 commented 1 year ago

Made it now this way for all devices so....can be closed here

type: custom:auto-entities
card:
  type: entities
  state_color: false
entities:
  - entity: sensor.arbeit_energy_power
    name: Arbeit
    type: custom:multiple-entity-row
    entities:
      - entity: switch.arbeit
        name: ' '
        toggle: true
        state_color: true

Screenshot 2023-06-11 091538

prankousky commented 6 months ago

I am trying to achieve this as well. I would like to sort my sensors first by state (`display "on" at the top), then by either friendly_name or last_changed (so "on" still on top, but sorted by 2nd sort, then "off" underneath, also sorted by 2nd sort).

This is my code; what would I have to add / change to get the results I need?

type: vertical-stack
cards:
  - type: custom:auto-entities
    card:
      type: entities
      state_color: true
    filter:
      include:
        - entity_id: binary_sensor.draussen_einfahrt_motion_3
          options:
            secondary_info: last-changed
        - entity_id: binary_sensor.draussen_einfahrt_motion_2
          options:
            secondary_info: last-changed
        - entity_id: binary_sensor.oben_bad_occupancy_aqara_presence
          options:
            secondary_info: last-changed
        - entity_id: binary_sensor.*_presence_presence
          options:
            secondary_info: last-changed
        - entity_id: '*_occupancy'
          options:
            secondary_info: last-changed
        - entity_id: '*motion'
          options:
            secondary_info: last-changed
        - entity_id: binary_sensor.blink_keller_motion_detected
          options:
            secondary_info: last-changed
      exclude:
        - entity_id: '*global*'
        - entity_id: switch.*
        - state: unavailable
        - entity_id: automation.*
    sort:
      method: state
      reverse: true
  - type: custom:auto-entities
    card:
      type: entities
      state_color: true
    filter:
      include:
        - entity_id: '*tuer_contact'
          state: 'on'
          options:
            secondary_info: last-changed
        - entity_id: '*fenster_contact'
          state: 'on'
          options:
            secondary_info: last-changed
      exclude: []

Screenshot

As you can see, these are sorted by state. But other than that, it seems randomly. They are neither sorted by alphabet, nor by last_changed.

grafik

ildar170975 commented 6 months ago

As you can see, these are sorted by state. But other than that, it seems randomly

No, they ARE sorted by state: first on, then off. What you can see is a side effect of your chosen device_class.

prankousky commented 6 months ago

No, they ARE sorted by state: first on, then off. What you can see is a side effect of your chosen device_class.

Yes. I wrote that.

As you can see, these are sorted by state.

But... I would like them to be sorted by state and then by last_change; so the correct order from the screenshot that I am trying to generate would be like this:

grafik

Minuten = minutes, Stunde = hour; so smallest value minute to highest value minute, then smallest value hour to highest value hour.

Is this possible? And if so, could you please explain to me, how I can achieve this? Ideally based on the yaml I pasted in my initial post? Thanks :)

ildar170975 commented 6 months ago

be sorted by state and then by last_change

Let's go step by step.

  1. Since you defined
    sort:
      method: state

    your rows are properly sorted by state "on" -> "off". As expected.

  2. You need to have the 2nd sorting criteria. There are 2 ways of sorting: using the "sort" option & using a jinja sort() function inside a "template" option (example). The 1st way does not allow you to define >1 sorting criteria; the 2nd way - does.
  3. If you gonna ask for adding a possibility of multi-criteria sort by that "sort" option - then this would be a Feature request which you need to register.
  4. This particular FR was created by OP - and then closed; he said that he found a way - but looking at the code he posted as a solution I do not see that it is somehow related to the issue & to the auto-entities in general.
  5. There is one more issue related to 2nd sorting criteria - but it is made in a rather sloppy way, and it's author simply does not care. But this issue is at least open))). Then I would suggest you either to create a 2nd duplicate (not good) or continue proposing your ideas in that open issue.
prankousky commented 6 months ago

Ok, I don't know if this has anything to do with this not working, but...

{{ states.binary_sensor.gruppe_arbeitszimmer_fenster.last_changed }}> 2024-01-06 07:08:22.594679+00:00 {{ state_attr("binary_sensor.gruppe_arbeitszimmer_fenster", "last_changed") }}> None

grafik

I tried changing the template you linked using what I know. Nothing. Eventually I just messed around with random values. Of course, nothing.

Then I realized that there is no last_changed attribute. I had troubles with that before (community.home-assistant.io).

Do you know if there is a way to utilize this in order to sort my entities the way I need them to be sorted?

ildar170975 commented 6 months ago

Check this example:

{% set OBJECTS = states.input_boolean |
                  selectattr('entity_id','search','test.*boolean') |
                  sort(reverse=false,attribute='last_changed') |
                  sort(reverse=false,attribute='state') %}

{% for object in OBJECTS %}
entity_id: {{object.entity_id}}
state: {{object.state}}
state: {{object.last_changed|as_local}}
{% endfor %}

image

prankousky commented 6 months ago

Thank you.

I changed it to this, which seems to present the desired output in developer tools.

        {% set OBJECTS = states.binary_sensor |
           selectattr('entity_id','search','.*presence*|.*occupancy*') |
           sort(reverse=false,attribute='state') |
           sort(reverse=false,attribute='last_changed') 
        %}

        {% for object in OBJECTS %}
          entity_id: {{object.entity_id}}
          state: {{object.state}}
          state: {{object.last_changed|as_local}}
        {% endfor %}

But this won't display in auto-entities:

grafik

ildar170975 commented 6 months ago

Because the template is wrong. It must represent a list of dictionaries. Suggest to go to the Community auto-entities thread for examples of using "template".

IMPORTANT: compare my

                  sort(reverse=false,attribute='last_changed') |
                  sort(reverse=false,attribute='state') %}

with yours, order of filters is crucial.