🔹 Auto-entities - Automatically fill cards with entities

i tried to combine my template code with the new one.

type: custom:auto-entities
card:
  type: entities
filter:
  template: |-
    {% set ns = namespace(areas=[], lights= expand(
      states.light|selectattr('name','ne','All'))| selectattr("state","eq","on") |  list) %}
    {% for elm in ns.lights if not area_id(elm.entity_id) in ns.areas%} {% set
    ns.areas = ns.areas + [area_id(elm.entity_id)] %} {%- endfor %} {% for area
    in ns.areas -%} {% for elm in area_entities(area) if elm in
    ns.lights|map(attribute="entity_id") -%}
      {{{ 'type': 'custom:vertical-stack-in-card',
          'title': area,
          'cards': 
            [ 
            {                     
               'type': 'custom:auto-entities',
               'show_empty' : 'false',
               'card' : 
               { 'type': 'entities'},
               'filter':
               { 'include': 
               [{ 'domain' : 'light', 
                  'state' : 'on',
                   'area' : area,
                   'options':
                   {'type' : 'custom:mushroom-light-card' }}]},
               }]}}},     
    {%- endfor %} {%- endfor %}

the only problem is that it seems to loop to often. and i dont know how to fix this.
all loops are needed to get the correct areas and entities but when i look at my card i have the same amount of vertical stacks and of my entities. so i got 6x vertical stack called ‘office/büro’ and within all 6 there are all the same lights.

If you could fix my code i would be very happy. my brain hurts >.<

i finally got it. works as expected. i dont know what was the turning point with this but im happy to have it with less than 40 lines of code.

EDIT: short explanation:
area_filter = name of areas which should be excluded
filter= entity_id of entities should be excluded (like light.bedroom)
| rejectattr(‘entity_id’, ‘in’, filter) entities will be excluded
| selecttattr(‘entity_id’, ‘in’, filter) only these entities will be included

type: custom:mod-card
card_mod:
  style:
    .: |
      ha-card {
        --ha-card-border-width: 0;
        --ha-card-background: none; } 
card:
  type: custom:auto-entities
  card:
    type: entities
  filter:
    template: |-
      {% set area_filter = ['area1','area2'] %}
      {% set filter = ['light.light1','light.light2'] %}
      {%- set areas = states.light
      | selectattr('state','eq', 'on')
      | rejectattr('entity_id', 'in', filter)
      | map(attribute='entity_id')
      | map('area_name') | unique | reject('none') 
      | reject('in', area_filter)     
      | list -%}      
      {%- for area in areas  -%}  
      {% set lights = states.light
      | selectattr('state', 'eq', 'on') 
      | rejectattr('entity_id', 'in', filter)
      | selectattr('entity_id', 'in', area_entities(area)) 
      |list -%}         
        {{{ 'type': 'custom:mushroom-title-card',
            'title': area}}},
        {%- for light in lights  -%}
            {{{ 'type': 'custom:mushroom-light-card',              
                'entity': light.entity_id,
                'use_light_color': 'true',
                'show_brightness_control': 'true',
                'show_color_control': 'true',
                'collapsible_controls': 'true',
                'show_color_temp_control': 'true', }}},
        {%- endfor %}{%- endfor %} 

which leads to something like this

I will try to do the same for other domains. Of course you can change the cards to others than mushroom. But i think this will list entities with a certain state in there assigned area.

Please test the code if it works for you too and let me know.

3 Likes

Have tried searching a little for template + options here but couldnt find anything…

I want the “tile” card with a template-filter, is that possible? i can’t seem to pass options to the template filter but i can with include filter

type: custom:auto-entities
filter:
  template: |
    {% set lys = expand('light.andre_etasje')|selectattr('state','eq','on')|list %}
    {% for light in lys %}
      {{ light.entity_id }}
      {{ type: tile }}
    {% endfor %}
  include:
    - domain: light
      options:
        type: tile
  exclude: []
card:
  square: false
  type: grid
  columns: 2
show_empty: true
card_param: cards

Hello everyone,

I am struggling with an auto-entities card, maybe someone can help me.

I want to create a card where the ‘Almere vandaag’ and ‘Almere morgen’ entities are displayed at the top.
Then I want to create a filter by date for the other entities (GFT, Plastic, PMD and Papier). Filtering by date works perfect for the other entities, but the Almere vandaag and morgen entities are now always displayed at the bottom of the list, whilst I want to display them at the top.

No matter what I have tried, the ‘Almere vandaag and morgen’ entities always show up at the bottom of the list, even if I place them somewhere outside of the filter list.

Anyone knows what I am doing wrong?

Mine without template

Screenshot 2023-07-12 091109

type: custom:auto-entities
card:
  square: false
  type: grid
  title: Phòng khåch
  columns: 2
card_param: cards
filter:
  include:
    - group: switch.living_room_group
      options:
        type: tile
show_empty: false
sort:
  method: friendly_name
  reverse: false

and that base on @Bourner code

type: custom:auto-entities
show_empty: false
card:
  type: vertical-stack
card_param: cards
filter:
  template: |-
    {% set area_filter = ['wohnung','Steckdose Wohnzimmer','Allgemein'] %}
    {% set areas = states.switch
      | map(attribute='entity_id')
      | map('area_name') | unique | reject('none') | rejectattr('entity_id','in', area_filter) | list %}
    {%- for area in areas -%}  
        {%- if area not in area_filter -%}
           {{{ 'type': 'vertical-stack',
          'title': area,
          'cards': 
            [{                     
               'type': 'custom:auto-entities',
               'show_empty' : 'false',
               'card' : 
               { 'type': 'grid',
                  'columns': 2,
                  'square': false,
               },
               'card_param': 'cards',
               'filter':
               { 'include': 
               [{ 'domain' : 'switch', 
                   'area' : area,
                   'options':
                   {'type' : 'tile'}}],
                 'exclude': 
               [{  'name' : 'zhimi*'}]},           
         
               }]}}}, 
      {%- endif -%} 
      {%- endfor %}

hope this help!

You’re not using the power of auto-entities by listing all of the entities…
if you have fixed entities, put them inside the entities list, for all of the others, include them under the filter using a filter of your choice.
That’s how it works, as explained in the docs

After my code works very good for me i wanted to do one more step.

This is the code right now:

type: custom:auto-entities
filter:
  include:
    - domain: light
      state: 'on'
      area: '*'
show_empty: false
card:
  type: custom:mod-card
  card_mod:
    style:
      mushroom-template-card$: |
        ha-card {padding: 10px 0px 10px 0px !important;}          
  card:
    type: custom:mushroom-template-card
    primary: Lichter
    secondary: >-
      {% set filter =['light.schreibtisch_1','light.schreibtisch_2','light.schreibtisch_3','light.wled','light.wled_2','light.steckdose_schlafzimmer','light.zhimi_za4_8475_indicator_light','light.schlafzimmer','light.f440c321_db5fdad4_screen','light.lichtgruppe_buero_2','light.buro']%}
       {{ states.light
         | selectattr('state', 'eq', 'on') 
         | rejectattr('entity_id', 'in', filter)
         | list | length }} an
    icon: mdi:lightbulb
    layout: vertical
    tap_action:
      action: fire-dom-event
      browser_mod:
        service: browser_mod.popup
        data:
          title: Lichter
          content:
            type: custom:mod-card
            card_mod:
              style:
                .: |
                  ha-card {
                    --ha-card-border-width: 0;
                    --ha-card-background: none; } 
            card:
              type: custom:auto-entities
              card:
                type: entities
              filter:
                template: >-
                  {% set area_filter = ['wohnung','SteckdoseWohnzimmer','Allgemein'] %}             
                  {% set filter = ['light.schreibtisch_1','light.schreibtisch_2','light.schreibtisch_3','light.wled','light.steckdose_schlafzimmer','light.zhimi_za4_8475_indicator_light','light.schlafzimmer']%}            
                  {%- set areas = states.light
                  |selectattr('state','eq', 'on')
                  |rejectattr('entity_id', 'in', filter)
                  |map(attribute='entity_id')
                  | map('area_name') |unique | reject('none')
                  | reject('in',area_filter) | list -%} 
                  {%-for area in areas  -%} 
                    {% set lights =states.light
                    | selectattr('state', 'eq','on')
                    | rejectattr('entity_id', 'in',filter)
                    | selectattr('entity_id', 'in',area_entities(area)) |list -%}         
                    {{{ 'type': 'custom:mushroom-title-card',
                        'title': area}}},
                    {%- for light in lights -%}
                        {{{ 'type': 'custom:mushroom-light-card',              
                            'entity': light.entity_id,
                            'use_light_color': 'true',
                            'show_brightness_control': 'true',
                            'show_color_control': 'true',
                            'collapsible_controls': 'true',
                            'show_color_temp_control': 'true', }}},
                    {%- endfor %}{%- endfor %}

which shows me
image
which is also hidden when no lights are turned on. also has a nice pop up which gives me this.

image

works as expected.

But is there any way to do this only one time?

{% set filter =['light.schreibtisch_1','light.schreibtisch_2','light.schreibtisch_3','light.wled','light.wled_2','light.steckdose_schlafzimmer','light.zhimi_za4_8475_indicator_light','light.schlafzimmer','light.f440c321_db5fdad4_screen','light.lichtgruppe_buero_2','light.buro']%}

1 Like

you could store that in secrets, of maybe even better in custom_templates, where you could also start building the rest of that generator

or use a light group

Nearlly, but I see the options-tag is within the regular “include” tag in the template :thinking:

This gave me an idea or two, so thanks! :slight_smile:

Thank you for your help!
I have already tried that, but that still gives me the same result. (see picture).
Maybe there is something wrong with my code?

no, you are still listing all entities under the include filter.

just use:

filter:
  include:
    entity_id: sensor.almere_*

it will re-list the 2 already included entities from the top entities list, so either take those out also, or, exclude them under

exclude:
  entity_id:

either verbosely, or yet again using wild cards:

      filter:
        exclude:
          - entity_id: sensor.afvalwijzer*next*
          - entity_id: sensor.afvalwijzer*to*
        include:
          - entity_id: sensor.afvalwijzer_*

also, If you want to be in complete control, and always need those 2 entities at the top, just take them out of the auto-entities and list them in the containing entities card

    - type: entities
      title: Entities
      entities:
        - sun.sun
        - sensor.moon
        - type: custom:auto-entities
          card:
            type: entities
            card_mod:
              style: |
                ha-card {
                  box-shadow: none;
                  margin: 0px -16px
                }
          filter:
            exclude:
              - entity_id: sensor.afvalwijzer*next*
              - entity_id: sensor.afvalwijzer*to*
            include:
              - entity_id: sensor.afvalwijzer_*
1 Like

Thank you so much for your help! This worked!

Gotten anywhere with this?:sweat_smile:

Working on the same thing currently!

I did find a way. I described my project here Food in the Freezer Tracker - Share your Projects! - Home Assistant Community (home-assistant.io)

This works if all of the info you need can be derived from the entity being sent by auto-entities. There is an example in there of me using one entity to get another because they are named similar.

As many of you I use the auto-entities card for battery levels and use the yaml below:

type: custom:auto-entities
card:
  show_header_toggle: false
  title: Battery low
  type: entities
  state_color: true
filter:
  include:
    - attributes:
        device_class: battery
      state: <=10
  exclude:
    - name: /[Ll]ow/
    - name: /[Ss]tate/
    - name: Lux*
sort:
  methode: state
  numeric: true
show_empty: false

Is there a way to send a notification if a battery entity’s state is ‘triggered’ and becomes visible in the card?

I have a input number slider in a vertical stack card with the auto-entities card. I want the input number’s value as input of the state attribute. For some reason this is not functioning. What am I missing? See code:

type: vertical-stack
cards:
  - type: entities
    entities:
      - entity: input_number.slider_battery_level
        name: Battery level min.
  - type: custom:auto-entities
    card:
      show_header_toggle: false
      title: Battery low
      type: entities
      state_color: true
    filter:
      include:
        - attributes:
            device_class: battery
          state: >-
            {{"<=" + states('input_number.slider_battery_level') | int | string
            }}
      exclude:
        - name: /[Ll]ow/
        - name: /[Ss]tate/
        - name: Lux*
    sort:
      methode: state
      numeric: true
    show_empty: false

Templates are not supported for this option.
Your possible way is using a “template” option which does support jinja templates.
You will have to define in this “template” option a code to include & exclude required entities.

1 Like

How to define Markdown cards in auto-entities

If a markdown card is used to show a static content:

  - type: custom:auto-entities
    card_param: cards
    card:
      type: vertical-stack
    filter:
      include:
        - entity_id: input_boolean.test_boolean_*         ### define your own mask
          options:
            type: conditional
            conditions:
              - entity: this.entity_id
                state_not: 'off'
            card:
              type: markdown
              content: some content

Note: to make the task a bit more complex, a Conditional card is used here ))).

Same when the content is dynamic (i.e. some jinja templates used) & does NOT depend of the currently processing entity_id:

            ...
            card:
              type: markdown
              content: >-
                {{ .... }}

or when the processed entity_id is NOT a part of a template:

            ...
            card:
              type: markdown
              content: >-
                {{ ... }}
                xxx: this.entity_id
                {{ ... }}

The problem is that that this.entity_id variable is not resolved inside these templates.

If your templates DO depend on the currently processed entity_id (as you have in your case) - then you need to use the template option:

  - type: custom:auto-entities
    card_param: cards
    card:
      type: vertical-stack
    filter:
      template: >-
        {% for entity in states.input_boolean|selectattr('entity_id','search','input_boolean.test_boolean_') -%}
          {%- if (entity.entity_id.split('test_boolean_')[1])|int(0) > 4 -%}
            {%- set CONTENT = 'xx: ' + entity.entity_id -%}
          {%- else -%}
            {%- set CONTENT = 'yy: ' + entity.entity_id -%}
          {%- endif -%}
          {{
            {
              'type': 'conditional',
              'conditions': [
                {
                  'entity': entity.entity_id,
                  'state_not': 'off'
                }
              ],
              'card': {
                'type': 'markdown',
                'content': CONTENT
              }
            }
          }},
        {%- endfor %}

That part simulates a dynamically defined content:

          {%- if .... -%}
            {%- set CONTENT = ... -%}
          {%- else -%}
            {%- set CONTENT = ... -%}
          {%- endif -%}

Hello, it would be possible to make an autoentities or mushroom card that indicates the sensors of a specific integration in my mqtt case (the majority are from z2mqtt) that do not update for more than 1 hour, in order to know if there is any sensor in house that has lost connectivity or has run out of battery… Thank you!!!

I just managed to get this badge card sorted that way I want…

image

type: custom:auto-entities
card: 
  type: custom:badge-card
card_param: badges
entities:
  - entity: sensor.time
  - entity: sensor.period_of_day
filter: 
  include:
    - entity_id: sensor.next_s*
      sort:
        method: attribute
        attribute: timestamp
        reverse: false

I had to modify my templated entities and added a timestamp of the sun.sun attribute since my state is just HH:MM with nothing else useful to sort by. last_updated, last_changed and last_triggered were useless!

  - name: "Next Solar Midnight"
    unique_id: next_solar_midnight
    state: '{{ as_timestamp(state_attr("sun.sun","next_midnight")) | timestamp_custom("%H:%M", true)}}'
    icon: mdi:weather-sunset-up
    attributes:
      timestamp: '{{ as_timestamp(state_attr("sun.sun","next_midnight")) }}'
    
  - name: "Next Dawn"
    unique_id: next_dawn
    state: '{{ as_timestamp(state_attr("sun.sun","next_dawn"),0) | timestamp_custom("%H:%M", true)}}'
    icon: mdi:weather-sunset-up
    attributes:
      timestamp: '{{ as_timestamp(state_attr("sun.sun","next_midnight")) }}'
  
  - name: "Next Sunrise"
    unique_id: next_sunrise
    state: '{{ as_timestamp(state_attr("sun.sun","next_rising"),0) | timestamp_custom("%H:%M", true) }}'
    icon: mdi:weather-sunset-up
    attributes:
      timestamp: '{{ as_timestamp(state_attr("sun.sun","next_rising")) }}'

  - name: "Next Solar Noon"
    unique_id: next_solar_noon
    state: '{{ as_timestamp(state_attr("sun.sun","next_noon"),0) | timestamp_custom("%H:%M", true)}}'
    icon: mdi:weather-sunset-up
    attributes:
      timestamp: '{{ as_timestamp(state_attr("sun.sun","next_noon")) }}'
    
  - name: "Next Sunset"
    unique_id: next_sunset
    state: '{{ as_timestamp(state_attr("sun.sun","next_setting"),0) | timestamp_custom("%H:%M", true) }}'
    icon: mdi:weather-sunset-down  
    attributes:
      timestamp: '{{ as_timestamp(state_attr("sun.sun","next_setting")) }}'

  - name: "Next Dusk"
    unique_id: next_dusk
    state: '{{ as_timestamp(state_attr("sun.sun","next_dusk"),0) | timestamp_custom("%H:%M", true)}}'
    icon: mdi:weather-sunset-up
    attributes:
      timestamp: '{{ as_timestamp(state_attr("sun.sun","next_dusk")) }}'