🔹 Auto-entities - Automatically fill cards with entities

Same was for me.
Only after clearing the browser’s cache the disappeared content displayed.

Indeed:

12:38:58.063 AUTO-ENTITIES 1.8.3 IS INSTALLED auto-entities.js:194:5516

Unfortunately no luck!

Deleted all cards using auto entities. Removed and reinstalled auto-entities. After start-up, following message was recorded: 12:38:58.063 AUTO-ENTITIES 1.8.3 IS INSTALLED auto-entities.js:194:5516. No errors found. Using auto-entities again without embedded custom:fold-entity-row, functioned properly. Including custom:fold-entity-row back again made lovelace card disappear and resulted in error message: 13:17:40.477 TypeError: r is undefined auto-entities.js:1:27664.
Using the auto-entities.js hyperlink as shown in console delivered the following highlighted pointer:
function t(t,e,i,n){var s,o=arguments.length,r=o<3?e:null===n?n=Object.getOwnPropertyDescriptor(e,i):n;if(“object”==typeof Reflect&&“function”==typeof …etc

Thank you. Try 1.8.4

Bingo! :wink: Thank you so much!

Need some help.
For this I gonna make a simple case.

There is a list of batteries.
The card contains:

But the batteries must be sorted by “charge level”.
This is an easy task if using “filter / include / …” method.
But here I gonna use a “filter / template” method:

type: 'custom:auto-entities'
card:
  type: entities
  title: Batteries
entities:
  - type: section
    label: Some title
unique: entity
show_empty: true
filter:
  template: |
    {% for state in states.sensor -%}
      {%- if state.entity_id | regex_match('sensor.battery_',ignorecase=False) -%}
        {{
          { 'entity': state.entity_id,
            'secondary_info': 'last-changed'
          } }},
      {%- endif -%} 
    {%- endfor %}
sort:
  method: state
  reverse: false
  ignore_case: false
  attribute: null
  first: 0
  count: 1000
  numeric: true

Now the question is - how to sort this list?
The code provided above gives this picture:


Note that the “section” row is sorted with other “battery” rows - and it comes to the bottom.
So how can I sort only “templated” rows?

Of course, there is a workaround "reverse: true" - but this is not a solution.

I am not sure what has happened. But, today my sensors are sorting in reverse order on my browser and the correct/normal order on my tablet. Using 1.8.4. Any ideas?

https://jinja.palletsprojects.com/en/2.11.x/templates/#sort

Thank you for the guidance!

Made this:

  template: |
    {% for state in states.sensor|sort(reverse=false,attribute="state") -%}
      {%- if state.entity_id | regex_match('sensor.battery_',ignorecase=False) -%}
        {{
          { 'entity': state.entity_id,
            'secondary_info': 'last-changed'
          } }},
      {%- endif -%} 
    {%- endfor %}

But:
Sort is made for “strings”, so the result is “100%, 14%, 20%, 90%”.
Is it possible to sort it is numbers?

P.S.
A bit simpler way (w/o "secondary_info") - filter by “device_class” (may not be 100% useful in some particular setup) - with "sort", and it does not sort properly too 'cause it sorts strings instead of numbers:

      template: |
        {{ states.sensor
           | selectattr('attributes.device_class','eq','battery')
           | sort(reverse=false,attribute='state')
           | map(attribute='entity_id')
           | list }}

of course ymmv, but, depending on the states and auto-entities cards you have, continuous evaluating of states in the states machine might be very costly. especially since these are done in the back-end and not in the browser?

suggestion: auto create a group at startup with the same template and have the auto-entities card use the group for filter?

automation:
  - alias: Create battery group
    trigger:
      platform: homeassistant
      event: start
    action:
      service: group.set
      data:
        object_id: battery_sensors
        entities: >-
          {%- for s in states.sensor
            if ('battery_level' in s.entity_id or
                'motion_sensor_battery' in s.entity_id)%}
            {{s.entity_id}}{% if not loop.last %}, {% endif %}
          {%- endfor %}
1 Like

This is a very good solution. I am still learning…

But how to sort as numbers?

What I found - two separate lists, and these lists may be placed as two separate auto-entities cards:

      template: |
        {{ states.sensor
           | selectattr('attributes.device_class','eq','battery')
           | rejectattr('state','eq','100')
           | sort(reverse=false,attribute='state')
           | map(attribute='entity_id')
           | list }}
      template: |
        {{ states.sensor
           | selectattr('attributes.device_class','eq','battery')
           | selectattr('state','eq','100')
           | map(attribute='entity_id')
           | list }}

Or is it possible to concatenate these lists in one template?

Update: I was wrong, this is not enough, we need three sets - “<10”, “10…99”, “100”
Details are in the posts below

I guess:

{% set reject = states.sensor
           | selectattr('attributes.device_class','eq','battery')
           | rejectattr('state','eq','100')
           | sort(reverse=false,attribute='state')
           | map(attribute='entity_id')
           |join(', \n') %}
{% set select =  states.sensor
           | selectattr('attributes.device_class','eq','battery')
           | selectattr('state','eq','100')
           | map(attribute='entity_id')
           |join(',\n') %}
{{reject ~ select}}
1 Like

That was a quite brain-cracking challenge for me))
Thank you!
For auto-entities I chose this version:

      template: |
        {{ states.sensor
           | selectattr('attributes.device_class','eq','battery')
           | rejectattr('state','eq','100')
           | sort(reverse=false,attribute='state')
           | map(attribute='entity_id')
           | join(',')
           ~
           ','
           ~
           states.sensor
           | selectattr('attributes.device_class','eq','battery')
           | selectattr('state','eq','100')
           | map(attribute='entity_id')
           | join(',')
           }}

Your a bit corrected version (will not work otherwise):

      template: |
        {% set reject = states.sensor
             | selectattr('attributes.device_class','eq','battery')
             | rejectattr('state','eq','100')
             | sort(reverse=false,attribute='state')
             | map(attribute='entity_id')
             | join(',') %}
        {% set select =  states.sensor
             | selectattr('attributes.device_class','eq','battery')
             | selectattr('state','eq','100')
             | map(attribute='entity_id')
             | join(',') %}
        {{reject ~ ',' ~ select}}

Update:
Using "group.battery_sensors" + displaying "secondary_info":

      template: |
        {% set batteries = expand('group.battery_sensors')
                           | rejectattr('state','eq','100')
                           | sort(reverse=false,attribute='state') -%}
        {%- for battery in batteries -%}
          {{
            { 'entity': battery.entity_id,
              'secondary_info': 'last-changed'
            } }},
        {%- endfor -%}
        {%- set batteries_full = expand('group.battery_sensors')
                                | selectattr('state','eq','100') -%}
        {%- for battery in batteries_full -%}
          {{
            { 'entity': battery.entity_id,
              'secondary_info': 'last-changed'
            } }},
        {%- endfor %}

Update: no, it does not work - see post below

1 Like

sorry I missed the concatenating comma, easily overlooked… have another card-mod one for you, Ill post it there :wink:

No, this method does not work.
It does not sort items numerically:


The idea was “let’s sort values <100 separately and then add values ==100”.
Now we need to sort items “<10” separately too.
But - to do this we cannot use "selectattr()" & "rejectattr()" - these filters work with strings, not numbers…

So - using "template" does not give us easy methods to sort numeric states.
Of course we can use "sort" options (here on GitHub) - but these options work for all entities on the card, not only provided by the "template".

yes, that could be an issue. using |map('int') wont probably help here, because then it won’t list the entity_id’s anymore:

        {% set alert_level = states('input_number.battery_alert_level')|int %}
        {{expand('group.battery_sensors')
                 |map(attribute='state')|map('int')
                 |select('<',alert_level)
                 |list|count}}

to give you an idea of how to force that. Must have another look at this, but will be tomorrow…

I have been looking at the same direction, thank you!

For now I found a way:

      template: |
        {% set batteries = expand('group.battery_sensors')
                           |rejectattr('state','eq','100') -%}
        {%- for battery in batteries|sort(reverse=false,attribute='state') -%}
          {%- if battery.state|int < 10|int -%}
          {{
            { 'entity': battery.entity_id,
              'secondary_info': 'last-changed'
            } }},
          {%- endif -%}
        {%- endfor -%}
        {% set batteries = expand('group.battery_sensors')
                           |rejectattr('state','eq','100') -%}
        {%- for battery in batteries|sort(reverse=false,attribute='state') -%}
          {%- if battery.state|int >= 10|int -%}
          {{
            { 'entity': battery.entity_id,
              'secondary_info': 'last-changed'
            } }},
          {%- endif -%}
        {%- endfor -%}
        {% set batteries = expand('group.battery_sensors')
                           |selectattr('state','eq','100') -%}
        {%- for battery in batteries|sort(reverse=false,attribute='entity_id') -%}
          {{
            { 'entity': battery.entity_id,
              'secondary_info': 'last-changed'
            } }},
        {%- endfor -%}

I tried to find a way without using "if .. endif".
Finally I only managed to print a list of states, not entity_ids:

template: |
  {{ 
     expand('group.battery_sensors')
     |map(attribute='state')|map('int')|select('<',10|int)
     |sort()
     |join(',')
     ~
     ','
     ~
     expand('group.battery_sensors')
     |map(attribute='state')|map('int')|select('<',100|int)|select('>=',10|int)
     |sort()
     |join(',')
     ~
     ','
     ~
     expand('group.battery_sensors')
     |selectattr('state','eq','100')
     |sort(reverse=false,attribute='name')
     |map(attribute='state')
     |join(',')
     }}
template: |
  6,12,26,36,42,47,76,90,100,100,100,100

But cannot create a list of entity_ids from here…


OK, that was an investigation, very useful for me.
But in practice the easiest ways are:

  1. Do not use the “template” - use “include” etc.
  2. If the “template” is have to be used - then try to use “sort” options (note that they work for all entities on the card).
1 Like

Sorry for bothering people again - but I got a new erratic problem.
Please help me…

The card contains:

  1. vertical-stack
  2. Two auto-entities cards with sensors with states:
  • unavailable
  • unknown

The code:

type: vertical-stack
cards:
  - type: 'custom:auto-entities'
    card:
      type: entities
      title: Сенсоры 'unknown'
    unique: true
    show_empty: true
    filter:
      template: >-
        {{states | selectattr('state', 'eq', 'unknown') |
        map(attribute='entity_id') | list }}
    sort:
      method: name
      reverse: false
      ignore_case: false
      attribute: null
      first: 0
      count: 100
      numeric: false
  - type: 'custom:auto-entities'
    card:
      type: entities
      title: Сенсоры 'unavailable'
    unique: true
    show_empty: true
    filter:
      template: >-
        {{states | selectattr('state', 'eq', 'unavailable') |
        map(attribute='entity_id') | list }}
    sort:
      method: name
      reverse: false
      ignore_case: false
      attribute: null
      first: 0
      count: 100
      numeric: false

If unavailable sensors are present, I see this:
изображение

If unavailable sensors are NOT present, I expect to see this:
изображение

But I see this instead:
изображение
The second auto-entity list disappeared - although there is "show_empty: true" option.

If I put the 2nd auto-entities card into a separate card (without vertical stack), I can see this - the card is displayed:
изображение

Moreover, if I open the initial card in the editor, I see this - the 2nd list is displayed again:

Browser Chrome, Win10x64.
Same in Firefox.

Tried to:

  • clean cache;
  • restart browser.

First it helped: when I opened the page - everything was OK.
After refresh (F5) - same problem, no “unavailable” card.

Moreover - sometimes after refresh I see the card again - and then after next refresh (or even after switching to/from editor mode) - the card disappear.
So, the problem is erratic.


Rewrote the card without “template” - same problem, the 2nd list not displayed.

type: vertical-stack
cards:
  - type: 'custom:auto-entities'
    card:
      type: entities
      title: Сенсоры 'unknown'
    unique: true
    show_empty: true
    filter:
      include:
        - state: unknown
    sort:
      method: name
      reverse: false
      ignore_case: false
      attribute: null
      first: 0
      count: 100
      numeric: false
  - type: 'custom:auto-entities'
    card:
      type: entities
      title: Сенсоры 'unavailable'
    unique: true
    show_empty: true
    filter:
      include:
        - state: unavailable
    sort:
      method: name
      reverse: false
      ignore_case: false
      attribute: null
      first: 0
      count: 100
      numeric: false

Update: the 2nd list always erratically disappears if it is empty.
Made a test card with 2 lists:

  • domain: fan
  • domain: light

So far I do not have any "light" entity - so the 2nd “light” card is disappeared.

One more problem with "auto-entities" inside "vertical-stack" - list of persons on a map.

It works if "auto-entities" is NOT inside "vertical-stack" (update 27.05.22: see a Note below):

type: 'custom:auto-entities'
card:
  type: map
  title: Map Family abroad
show_empty: false
filter:
  include:
    - entity_id: person.*
      options:
        dark_mode: false
        default_zoom: 14

It does NOT work if "auto-entities" IS inside "vertical-stack" (update 27.05.22: see a Note below):

type: vertical-stack
cards:
  - type: 'custom:auto-entities'
    card:
      type: map
      title: Map Family abroad
    show_empty: false
    filter:
      include:
        - entity_id: person.*
          options:
            dark_mode: false
            default_zoom: 14


But if you try to edit smth like “title” - the card is shown again:

But after save - again not shown.

update 27.05.22 - a Note:
seems that the issue may be reproduced even without placing a map card into a vertical stack.