🔹 Auto-entities - Automatically fill cards with entities

Quick question: I can see back in 2019 an enhancement was made to specify a max number of entities: “Display up to a maximum number of entities (e.g. display the first 10 matches)”

I’m having trouble locating this option in the GitHub page. Does anyone know the best way to accomplish this? I have a fixed-height card that I’m populating with auto-entities. It works great, but occasionally it finds too many entities that spill over outside the card. I’d like to limit it (or maybe somehow allow scrolling within the card?). Thanks in advance!

sort - count

Thank you, and sure enough I can see it now in the doc. I must be tired.

Hi,

I’m trying to optimize a media player dashboard, and I’m hoping that I can use auto-entities to solve it. However, I have not had the luck to succeed yet; I would like to know if it’s possible to populate the card based on an attribute of an entity.

Here’s the attributes of the media_player (cleaned up):

group_members:
  - media_player.kitchen_2
  - media_player.lego_vaerelset
volume_level: 0.23

Can I make a template to show the entities provided in Group members attribute?

EDIT: Nevermind, it was very easy, must have made a typo earlier.

type: custom:auto-entities
card:
  type: entities
filter:
  template: >
    {{ state_attr("media_player.kitchen_2", "group_members") | list }}


Hi, I am working on showing scenes as chips using mushroom-chips-card:

I read this thread and I try multiple variant proposed in thread and none of them is working.

Has anybody idea, what I am doing wrong?

type: custom:auto-entities
card:
  type: custom:mushroom-chips-card
show_empty: true
card_param: chips
filter:
  include:
    - entity_id: scene.*
      area: Ložnice
      options:
        card:
          type: entity
          content_info: name
          icon_color: |-
            {% if is_state("this.entity", "on") %}
              amber
            {% else %}
              red
            {% endif %}
          tap_action:
            action: toggle

How would I go to get ONE entity from each of my areas?

I wish to use a predefined card with lots of templating specified to the area an entity is assigned, but I have trouble to get just one of each “area”

How would i use card_param: cards with a filter as a template?

What i am trying to achieve is this:

Current Code
type: custom:auto-entities
card:
  type: custom:stack-in-card
  card_mod:
    style: |
      ha-card {
        padding-top: 5px;
        padding-bottom: 5px !important;
      }
filter:
  include:
    - entity_id: sensor.office**battery
      options:
        type: custom:bar-card
        card_mod:
          style: |
            bar-card-currentbar, bar-card-backgroundbar {
              border-radius: 13px !important;
              height: 43px !important;
              margin-top: -8px !important;
            }
            bar-card-row {
              margin-bottom: 14px !important;
            }
            bar-card-name {
              color: black !important;
              margin-top: -10px !important;
            }
            bar-card-value {
              color: black !important;
              margin-top: -10px !important;
            }
            ha-icon{
              padding: 9px;
              border-radius: 100px;
              --mdc-icon-size: 28px;
              margin-top: -12px;
            }
            bar-card-row bar-card-currentbar,
            ha-icon, bar-card-backgroundbar {
              --bar-color:
              {% if states(config.entity) | int <= 10 %}
                #ff0026 !important;
                --card-mod-icon: mdi:battery-10 !important;
              {% elif states(config.entity) | int <= 20 %}
                #ff4d00 !important;
                --card-mod-icon: mdi:battery-20 !important;
              {% elif states(config.entity) | int <= 30 %}
                #ff9900 !important;
                --card-mod-icon: mdi:battery-30 !important;
              {% elif states(config.entity) | int <= 40 %}
                #ffcc00 !important;
                --card-mod-icon: mdi:battery-40 !important;
              {% elif states(config.entity) | int <= 50 %}
                #e7fc03 !important;
                --card-mod-icon: mdi:battery-50 !important;
              {% elif states(config.entity) | int <= 60 %}
                #aaff00 !important;
                --card-mod-icon: mdi:battery-60 !important;
              {% elif states(config.entity) | int <= 70 %}
                #aaff00 !important;
                --card-mod-icon: mdi:battery-70 !important;
              {% elif states(config.entity) | int <= 80 %}
                #22c402 !important;
                --card-mod-icon: mdi:battery-80 !important;
              {% elif states(config.entity) | int <= 90 %}
                #22c402 !important;
                --card-mod-icon: mdi:battery-90 !important;
              {% elif states(config.entity) | int <= 100 %}
                #22c402 !important;
                --card-mod-icon: mdi:battery !important;
              {% else %}
                blue
                mdi:bug
              {% endif %}
            }
            bar-card-row ha-icon {
              color: color-mix(in srgb, var(--bar-color) 100%, transparent);
              background: color-mix(in srgb, var(--bar-color) 20%, transparent);
            }
            bar-card-row bar-card-indicator {
              color: color-mix(in srgb, var(--bar-color) 100%, transparent);
            }
            ha-card {
              padding: 0px !important;
              margin: -4px 0px !important;
              height: 60px;
            }
sort:
  method: state
  numeric: true
  ip: false
  ignore_case: false
  reverse: false
show_empty: true
card_param: cards

But instead using a template filter like this for example:

Desired Code
              filter:
                template: |-
                  {% for state in states.sensor -%}
                    {%- if state.entity_id | regex_match("sensor.*battery*", ignorecase=False) and "SM" not in state.attributes.friendly_name and "Dimitri" not in state.attributes.friendly_name and 'unknown' not in state.state and 'unavailable' not in state.state and state.state | int <= 10-%}
                      {{
                        {
                          'entity': state.entity_id,
                          'name': state.attributes.friendly_name.split(' battery')[0]
                        }
                      }},
                    {%- endif -%}
                  {%- endfor %}

I cant inject each entity into the same card like you normally would as the colors dont show up correctly if so as i cant use config.entity. So i have to use card_param: cards but i cant for the life of me figure out where to put the card information with the template option.

EDIT: Nevermind, got it with the below! :slight_smile:

Final Code
type: custom:auto-entities
card:
  type: custom:stack-in-card
  card_mod:
    style: |
      ha-card {
        padding-top: 5px;
        padding-bottom: 5px;
      }
card_param: cards
filter:
  template: >-
    {%- for state in states.sensor|selectattr('entity_id','search','battery') |
    selectattr('entity_id','search','office')-%}
      {{
        {
          'type': 'custom:bar-card',
          'entity': state.entity_id,
          'name': state.attributes.friendly_name.split(' Battery')[0].split(' battery')[0],
          'style': 'bar-card-currentbar, bar-card-backgroundbar {
                    border-radius: 13px !important;
                    height: 43px !important;
                    margin-top: -8px !important;
                  }
                  bar-card-row {
                    margin-bottom: 14px !important;
                  }
                  bar-card-name {
                    color: black !important;
                    margin-top: -10px !important;
                  }
                  bar-card-value {
                    color: black !important;
                    margin-top: -10px !important;
                  }
                  ha-icon{
                    padding: 9px;
                    border-radius: 100px;
                    --mdc-icon-size: 28px;
                    margin-top: -12px;
                  }
                  bar-card-row bar-card-currentbar,
                  ha-icon, bar-card-backgroundbar {
                    --bar-color:
                    {% if states(config.entity) | int <= 10 %}
                      #ff0026 !important;
                      --card-mod-icon: mdi:battery-10 !important;
                    {% elif states(config.entity) | int <= 20 %}
                      #ff4d00 !important;
                      --card-mod-icon: mdi:battery-20 !important;
                    {% elif states(config.entity) | int <= 30 %}
                      #ff9900 !important;
                      --card-mod-icon: mdi:battery-30 !important;
                    {% elif states(config.entity) | int <= 40 %}
                      #ffcc00 !important;
                      --card-mod-icon: mdi:battery-40 !important;
                    {% elif states(config.entity) | int <= 50 %}
                      #e7fc03 !important;
                      --card-mod-icon: mdi:battery-50 !important;
                    {% elif states(config.entity) | int <= 60 %}
                      #aaff00 !important;
                      --card-mod-icon: mdi:battery-60 !important;
                    {% elif states(config.entity) | int <= 70 %}
                      #aaff00 !important;
                      --card-mod-icon: mdi:battery-70 !important;
                    {% elif states(config.entity) | int <= 80 %}
                      #22c402 !important;
                      --card-mod-icon: mdi:battery-80 !important;
                    {% elif states(config.entity) | int <= 90 %}
                      #22c402 !important;
                      --card-mod-icon: mdi:battery-90 !important;
                    {% elif states(config.entity) | int <= 100 %}
                      #22c402 !important;
                      --card-mod-icon: mdi:battery !important;
                    {% else %}
                      blue
                      mdi:bug
                    {% endif %}
                  }
                  bar-card-row ha-icon {
                    color: color-mix(in srgb, var(--bar-color) 100%, transparent);
                    background: color-mix(in srgb, var(--bar-color) 20%, transparent);
                  }
                  bar-card-row bar-card-indicator {
                    color: color-mix(in srgb, var(--bar-color) 100%, transparent);
                  }
                  ha-card {
                    padding: 0px !important;
                    margin: -4px 0px !important;
                    height: 60px;
                  }'
        }
      }},
    {%- endfor %}
sort:
  method: state
  numeric: true
  count: 8
5 Likes

Hi everyone,
Is it possible to exclude all areas that do not match a certain name?
E.g. I want to exclude all areas that are not named ‘Living Room’.

I would like to work with exclude instead of include, because the include would require me to add a lot of additional code for the card that is receiving the entities.

There is an exclude filter in the docs, what part is not working for you?

Sorry, fixed it before dinner. The problem was with the undefined areas.
Normally, undefined areas should not be part of a specific area. But strangely it seems that undefined equals all instead of none.

The exclude then was a bit more tricky to get right using Javascript. But I think I just had a mental blockade last night :slight_smile:

This looks very intruiging, Ildar.

Am I correct in thinking that this approach allows the creation of multiple cards of which each receives one entity of the list?

Maybe not, and my code cannot work because I am misreading the use case, but I was trying to use this to auto create multiple charts.

type: custom:auto-entities
card:
  type: custom:mini-graph-card
filter:
  template: >
    {% for ENTITY_ID in states.sensor | selectattr('name', 'match', 'Xiaomi Humidity') | map(attribute='entity_id') | list -%}
        {{
          {
            'entities': ENTITY_ID,
          }
        }}
    {%- endfor %}

Could of course also be my lack of templating talent :see_no_evil:

I just get

mini-graph-card
Entity not available: {'entities':
Entity not available: 'sensor.xiaomi_humidity_master_bathroom'}{'entities':
Entity not available: 'sensor.xiaomi_humidity_master_bedroom'}{'entities':
Entity not available: 'sensor.xiaomi_humidity_living_room'}{'entities':
Entity not available: 'sensor.xiaomi_humidity_walk_in'}{'entities':
Entity not available: 'sensor.xiaomi_humidity_dining_room'}{'entities':
Entity not available: 'sensor.xiaomi_humidity_kitchen'}{'entities':
Entity not available: 'sensor.xiaomi_humidity_office'}{'entities':
Entity not available: 'sensor.xiaomi_humidity_it'}

Should be:

type: custom:auto-entities
card:
  type: custom:mini-graph-card
filter:
  template: >-
    {{ states.sensor |
       selectattr('entity_id','search','[xiaomi,mijia]_.*_co2') |
       map(attribute='entity_id') |
       list }}

Here the destination card has the “entities” option - which is filled by generated list of entities.

BUT - it may be not just a list, it could be a list of DICTS:

  template: >-
    {% for ENTITY_ID in states.sensor | selectattr('entity_id','search','[xiaomi,mijia]_.*_co2') | map(attribute='entity_id') | list -%}
        {{
          {
            'entities': ENTITY_ID,
            'name': 'xyz'
          }
        }},
    {%- endfor %}


So, in your example you misplaced a comma.

My example with m-e-r was exactly about generating a list of dicts.

1 Like

Ah, thank you!

I was just about to test

{% set ns = namespace(list=[]) %}
{% for item in states.sensor | selectattr('name', 'match', 'Xiaomi Humidity') %}
    {% set ns.list = ns.list + [ item.entity_id ] %}
{% endfor %}
{{ ns.list | to_json }}

But it then looks like I cannot create multiple cards with just one entity of the list per card? It is rather an alternative, template based approach to using

include:
  - name: 'Xiaomi Humidity*'

?

Sorry, do not understand.

I was hoping to create a list of entity_ids using auto-entities and then have one mini-graph-card created per entity_id.
So not one line per entity_id but one entire mini-graph-card per entity_id.

I actually found a thread where they successfully do this with mini-graph-card.

The only thing I cannot figure out is how to apply this also to cards like apexcharts-card.

I think this has to do with cards needing entities vs. entity_ids or something. I am not sure.

When I try to apply this approach to apexcharts-cardI get an error message

type: custom:auto-entities
card:
  type: grid
  columns: 2
card_param: cards
filter:
  include:
    - name: Sony*ENERGY Power*
      options:
        type: custom:apexcharts-card
        series: 
          - "this.entity_id"

It looks like the entity_id is passed twice due to the series: but without it apexcharts-card will report the series item missing.

type: custom:auto-entities
card:
  type: vertical-stack
card_param: cards
filter:
  template: >-
    {% for ENTITY_ID in states.sensor |
    selectattr('entity_id','search','[xiaomi,mijia]_.*_co2') |
    map(attribute='entity_id') | list -%}
        {{
          {
            'type': 'custom:mini-graph-card',
            'entities': [
              {
                'entity': ENTITY_ID,
                'color': 'magenta'
              }
            ],
            'show': {
              'points': true,
              'fill': 'fade'
            },
            'hours_to_show': 0.5,
            'points_per_hour': 120
          }
        }},
    {%- endfor %}


Here a collected list of useful posts.

1 Like

I am not sure whether to continue this in this thread or if it would be better to move, but I am not sure which card thread would be the correct one :thinking:

So, I have used your code to create a grid with mutliple, auto-generated charts (happy to share the full code in case anybody is interested!).

Now your code already uses jinja templating plus Python dictionary, if I understand correctly. At least those were the two syntaxes that seemed to work.

If I want to past a template to one of the dictionary values, can this somehow be done directly by using the template inside the dict or do I always have to create a variable outside the dict and then use the var inside?

Let’s say as a simple example I want to use a count as a value.

{{ states.sensor | selectattr('name','search','Sony.*ENERGY Power.*') | map(attribute='entity_id') | list | count }}

in

'header':   {
  'title': {{ states.sensor | selectattr('name','search','Sony.*ENERGY Power.*') | map(attribute='entity_id') | list | count }}
}

This is not working right now, but is it because I made a syntax mistake (e.g. mark as a template inside dictionary) or because it has to be passed into the dictionary from the outside?

I wanted to set it on the outside like

filter:
  template: >-
    {% set DECIMALS = (states.sensor | selectattr('name','search','Sony.*ENERGY Power.*') | map(attribute='entity_id') | list | count) %}
    {% for ENTITY_ID in states.sensor   | selectattr('name','search','Xiaomi
    Humidity.*')   | map(attribute='entity_id')  | list -%}  {{
      {
        'type': 'custom:apexcharts-card',
.
.
.

with

'decimals': DECIMALS,

but well… no luck.

Surely it will not work - you are trying to use a template inside a template:

{{
  ...
  {{ ...}}
  ...
}}

Should be like:

  template: >-
    {% set COUNT = ... -%}
    {%- for ENTITY_ID in ... -%} 
        {{
          {
            'type': 'custom:mini-graph-card',
            'entities': [
              {
                'entity': ENTITY_ID,
                'color': 'magenta',
                'name': 'xxx ' + COUNT | string,
              }
            ],
            ...
          }
        }},
    {%- endfor %}

Here a COUNT is used for every item; adapt this way for your own case.
And note:

 'name': 'xxx ' + COUNT | string,

for a proper conversion, otherwise you will have

ALWAYS CHECK YOUR TEMPLATES IN DEV-TOOLS !!!

1 Like

Ah, that may have been why it did not work when I tried without the brackets.

I also tried it as a number value in the yaxis decimal places without the brackets but it did nothing.
Even tried “forcing it” with Number() and with parseFloat().

I will try again :slight_smile:

Would it then even be possible to do an if expression inside a template?
The non dict version

    graph_span: |-
      ${
        if (states['input_boolean.end_datetime_now'].state == 'on' && states['input_boolean.start_datetime_24h'].state == 'on')
          { 24 +'h' }
        else if (states['input_boolean.end_datetime_now'].state == 'on' && states['input_boolean.start_datetime_24h'].state == 'off')
          { ((states['sensor.timestamp'].state-states['input_datetime.start_datetime'].attributes.timestamp)/60/60).toFixed(2)+'h' }
        else
          { ((states['input_datetime.end_datetime'].attributes.timestamp-states['input_datetime.start_datetime'].attributes.timestamp)/60/60).toFixed(2)+'h' }
      }

or would I need to move this outside the template and do something like

{% if (states['input_boolean.end_datetime_now'].state == 'on' && states['input_boolean.start_datetime_24h'].state == 'on') %}
    set GRAPH_SPAN = 24 +'h' 
{% else if (states['input_boolean.end_datetime_now'].state == 'on' && states['input_boolean.start_datetime_24h'].state == 'off') %}
    set GRAPH_SPAN = ((states['sensor.timestamp'].state-states['input_datetime.start_datetime'].attributes.timestamp)/60/60).toFixed(2)+'h'
{% else %}
    set GRAPH_SPAN = ((states['input_datetime.end_datetime'].attributes.timestamp-states['input_datetime.start_datetime'].attributes.timestamp)/60/60).toFixed(2)+'h'
{% endif %}
.
.
    'graph_span': GRAPH_SPAN,