🔹 Auto-entities - Automatically fill cards with entities

Are you talking about apexchart?
Cannot help, sorry, not using)))

In this example yes. But it is a general question.

Can an entire

{% if (states['input_boolean.end_datetime_now'].state == 'on') %}
    {% set GRAPH_SPAN = '24' + 'h' %}
{% elif states['input_boolean.end_datetime_now'].state == 'on' %}
    {% 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 %}

be placed inside a template or will I always have to do such if expressions on the outside and pass the result into the template as a variable?

Yes, I think

1 Like

Can you think of a way of automatically changing the color for each entity (without knowing how many entities there will be)?

What do you mean?

How I can see it in a simple case:
– entities card has X number of rows
– each row has an unique color (let it be a text color for simplicity)
– each unique color is defined based on a count of items - i.e. dependingly on the count some particular item may get a different color every time

So, I was thinking of adding something like

{% set colors = ['orange', 'green', 'blue', 'red', 'yellow'] %}
{% for COLOR in colors %}
  // your card creation loop here

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

{% endfor %}

But I would need to change the color in the same loop as the creation of the entities.

So COLOR and ENTITY_ID need to use the same counter.

  1. To address a specific color you may use
`color`: colors[loop.index]

I only do not recall how arrays are used in jinja - starting from 1 or from 0 (like in C).

  1. If you have an upper bound for a count of items (100 for instance) - then define an array with 100 elements. Or define a formula like “color_for_item(index_of_item)”. The latter way may give an effect “same item has similar colors” (like “shades of same red”) if you define that formula accordingly (also, if an order is same).
1 Like

Working nicely, Ildar, thank you.

It is index0 to start with in Jinja :slight_smile:

Can you think of a way to modify the code so the user can select to populate one card with all entities instead of creating one card per entity? The user input part is of course easy, but the code needs some kind of if expression or something to tell the card to populate rather than create new :thinking:

Elaborate.
1 picture is better than 100 words)

Switching from

to this

by changing this

type: custom:auto-entities
card:
  type: grid
  columns: 1
card_param: cards
filter:
  template: >-

    // create ns.list

    // if input_text.merge_vs_split.state = 'split'  then create two cards with one entity each (bottom picture)
    // else create one card with two entities (bottom picture)

    {% for item in ns.list -%}  {{
      {                                              // from here on the code should ideally be the shared by both
        'type': 'custom:apexcharts-card',
        'series': [ {
            'entity': item,
            'yaxis_id': 'first',
            'color': colors[loop.index0],
            'show': {
              'in_chart': true,
              'in_brush': true }
            } ],

depending on whether user changes input_text.merge_vs_split state to ‘merge’ or ‘split’

  1. Adding many graphs into one card is defined here: consolidation post → many graphs
  2. Switching between “many entities on 1 card” & “many cards in 1 stack” cannot be done in auto-entities. Use conditional cards like “state-switch” or a conventional Conditional card.

Your link actually gives me the idea that it might work if the card is in a config-template card to allow for templating outside of the template: part of the auto-entites card.

type: custom:config-template-card
card:
  type: custom:auto-entities
  card:
    // if input_text = split
    type: grid    
    columns: 1  
  // else
  type: custom:apexchards-card
  card_param: cards

I would have posted the correct code but I have not been able to make it work yet.

DO NOT.
CTC have to monitor all entities which are supposed to be displayed in auto-entities.
Well, if you have max XX entities which may be listed in auto-entities - then list all of them for CTC (“entities” option), but CTC will redraw the whole card if any of these monitored entities changes (states or attrs).

Yes, I am sadly aware.
But do you know an alternative?

With my old card I monitored a whole bunch of helpers which was not so cool. But this time it would be only one that only changes on user input.

I am getting closer to making it work but I cannot find the final mistake.

type: custom:config-template-card
entities:
  - input_boolean.apexcharts_merge_split
card:
  type: custom:auto-entities
  card:
    type: >-
      ${states['input_boolean.apexcharts_merge_split'].state != 'on' ?
      'entities' : 'grid'}
    columns: '${states[''input_boolean.apexcharts_merge_split''].state != ''on'' ? '''' : ''1''}'
  card_param: |-
    ${states['input_boolean.apexcharts_merge_split'].state != 'on' ?
      '' : 'cards'}
  filter:
    template: >-

      {% set ns = namespace(list=[]) %}    
      {% for item in states.sensor | selectattr('name','search',states['input_text.apexcharts_entity_include'].state,
      ignorecase=True) -%}
          {% set ns.list = ns.list + [ item.entity_id ] %}
      {%- endfor %}     

      {% if states['input_boolean.apexcharts_merge_split'] != 'on' %}
        {{ ns.list }}
      {% else %}  {% for item in ns.list -%}  {{
        {
          'type': 'custom:apexcharts-card',
          'series': [ {
              'entity': item,
              'yaxis_id': 'first',
              'color': 'orange',
              'show': {
                'in_chart': true,
                'in_brush': true }
              } ],
       //... rest of apexcharts config

      {%- endfor %} {%- endif %}

This works for the case of entities, i.e. trigger on but for trigger off, the apexcharts card throws an error that no card type is configured.

Interestingly the grid is created and more than one card is in the grid, so the outer frame work is working.

P.S.: for better readability, is it possible to put everything inside the the apexcharts config into a dictionary(?) and then just call that?
So from this

{% for item in ns.list -%}  {{
        {
          'type': 'custom:apexcharts-card',
          'series': [ {
              'entity': item,
              'yaxis_id': 'first',
              'color': 'orange',
              'show': {
                'in_chart': true,
                'in_brush': true }
              } ],
       //... rest of apexcharts config

      {%- endfor %} {%- endif %}

to something like

{% for item in ns.list -%}  {{
        apexcharts_dict

      {%- endfor %} {%- endif %}

?

!!!

And this is just wrong

type: custom:config-template-card
entities:
  - input_boolean.apexcharts_merge_split
card:
  type: custom:auto-entities

since you needed to list ALL entities here, not just one helper.

But these only show/hide a card, and do not switch between cards, correc?

Meaning I cannot use a common configuration (e.g. the same apexcharts configuration) and I will not be able to use the same e.g. ns.list.
I would need to maintain both cards to keep them in sync rather than the settings of just one?

Because the dashboard template function GitHub - RomRider/apexcharts-card: 📈 A Lovelace card to display advanced graphs and charts based on ApexChartsJS for Home Assistant stores the config in normal yaml syntax whereas for the automatic grid card I need it as jinja dictionary :frowning:

EDIT:
Or can you think of a way to store a card config in a yaml file and then load it into a card either in that yaml format or as dictionary?

EDIT2:
Maybe if I set it as card default for apexcharts-card using decluttering-card.
Maybe that will help. But I need to check if I can then still use templating.

Is there a way to exclude and include entities based on what their measurement unit is? Like include “W” but exclude “kWh”?

Since UoM is an attribute, this could be possible.

Any update on allowing multiple entities for a filter? :slight_smile:

Sorry, why not using multiple filters?