🔹 Auto-entities - Automatically fill cards with entities

although I am not familiar with the mushroom-template-card, I suspect your issue has to do with the quoting, which looks painful to my eyes…

you’d help yourself a lot if you’d use multiline notation, and be done with all of these double single quotes.

As you state the primary works ( it uses 'index' ) , so single single quotes, you’d probably need to use that in the secondary too (now using double single quotes ''index'' )

this would probably make it work:

    - entity_id: input_text.xenia_script_*
      options:
        type: custom:mushroom-template-card
        entity: this.entity_id
        primary: >
          {{ state_attr(entity, 'name') }} [{{ state_attr(entity, index) }}]
        tap_action:
          action: call-service
          service: rest_command.execute_xenia_script
          service-data:
            id: >
              {{ state_attr(entity, index) }}

if index is in fact a variable name. If it is an attribute, the question dibbler raises, you’d need to single quote them. that would be surprising though, given: Input Text - Home Assistant, and would have to been set via customize or some other option. Unless that would be some hidden option in either of the cards/integrations…

Absolutely. They are generated in a sep, Python script. Two attributes are in existence:

  • name
  • index

1 Like

Sorry, you’re right regarding the quoting. This somehow was messed up while copying over to here.

It’s currently in like this:

id: '{{ state_attr(entity, 'index') }}'

index is an attribute and currently already correctly interpreted for “primary” in displaying the attribute value.

image

Repeating the currently used quoting in HA:

type: custom:auto-entities
card:
  square: false
  columns: 1
  type: grid
show_empty: false
card_param: cards
filter:
  include:
    - entity_id: input_text.xenia_script_*
      options:
        type: custom:mushroom-template-card
        entity: this.entity_id
        primary: '{{ state_attr(entity, 'name') }} [{{ state_attr(entity, 'index') }}]'
        tap_action:
          action: call-service
          service: rest_command.execute_xenia_script
          service-data:
            id: '{{ state_attr(entity, 'index') }}'
sort:
  method: entity_id

but now you are having issues with the yaml, as the interpreter can not separate the correct strings. as is clearly displayed in the coloring

if you insist on using single lines, than use double outer quotes and single inner quotes.

its like that with all yaml, everywhere.

Chaging it to multiline notation, but the rest_command has still the same error:

Template variable warning: ‘id’ is undefined when rendering ‘ID={{ id }}’

type: custom:mushroom-template-card
entity: this.entity_id
primary: |
  {{ state_attr(entity, 'name') }}  [{{  state_attr(entity, 'index') }}]
tap_action:
  action: call-service
  service: rest_command.execute_xenia_script
  service-data:
    id: |
      {{ state_attr(entity, 'index') }}

that would be the outcome if you pasted the template in dev tools template, and replace entity with an actual entity_id?

besides all of that, could it be the rest_command expects an entity_id, instead of an attribute only? this would make your challenge probably be rather Off topic for auto-entities.

DevTools - Templates –
This delivers the correct result:
{{ state_attr(‘input_text.xenia_script_17’, ‘index’) }} → 17
This provides “UndefinedError: ‘input_text’ is undefined”:
{{ state_attr(input_text.xenia_script_17, ‘index’) }}
BUT quoting entity in primary: leads to an error.

In the mean time I also experimented with:

    id: |
      {{ states.this.entity_id.attributes.index }}

But the error log still lists the same error.

This is the rest_command:

  execute_xenia_script:
    url: "http://xenia.local/execute_script"
    method: POST
    payload: "ID={{ id }}"
    content_type: "application/x-www-form-urlencoded"

this is all very odd. the second version is wrong ofc, you need to quote the entity_id.

can you screenshot the entity in dev tools state like this:

so this gets weirder per post… how is your entity created? Is this some custom integration, given the presence of an attribute index, and the absence of all core attributes …?

also, given the example in the docs

are you sure payload shouldnt be inside brackets?

I have a python script. that creates those text_inputs (input_text.xenia_script_*) of data retrieved from an external device using hass.services.call and hass.states.set functions. Index is just a value, we could name it totally different.

I see, that would explain the out of the ordinary attribute indeed. Using the hass.states.set myself in python, and though powerful, it is prone to unexpected outcome.

I’d suggest you take this in to a separate topic, and out of auto-entities, because it would be an issue of getting the correct attribute inside that payload template, and not so much the auto-entities part of it here. It’s getting awfully off-topic because of that.

please check the brackets remark I added earlier, and see if thats relevant?

My understanding is, that the bracket is only used when using the Developer Tools.

Thanks so far for your support.

Regarding auto-entities and what I need to achieve:

  1. My device provides me with a list with an unknown quantity of data pairs: Text + ID
  2. The “Text” part of each of these data pairs should be a button on my front.end.
  3. Pressing the button, a rest_command (an URL) should be called that ends with the provided ID for the button (http://<fixed.URL>/execute_script?ID=ID of data pair)

I started my implementation with generating the input_text entities to achieve this.

What would be your suggestion to approach this?

well, its a bit of a guess on my behalf, but it could be a few things.
if the command expects a number, you should provide it. Currently you’re providing a string. try adding |float at that template?

could also be you are providing the full data pair, while you only need to provide the number?

I cant say, because can not test locally.

and about the brackets: this is a misconception, I use them in all of by payloads. the ones with only 1 data pair, and also the ones with more than that.

I have been using your code for some time now and everything is working fine. Today I miserably tried to apply an enhancement. I want to display an icon based on the entity id. If it is a light, display a lightbulb, if it is a fan, display a fan. What am I missing? This is my code:

type: custom:auto-entities
card:
  type: grid
  columns: 4
  square: false
  title: Lights On
card_param: cards
filter:
  template: |-
    {% for x in expand('group.Lights_House') | selectattr('state','eq','on') %}
    {{- 
    {
      'type': 'custom:mushroom-entity-card', 
      'entity': x.entity_id,
      'primary_info': 'state',
      'secondary_info': 'name',
      'icon': >
        {% if 'fan' in x.entity_id %}
          mdi:fan
        {% else %}
          mdi:lightbulb
        {% endif %}
      'icon_color': 'amber',
      'vertical': true,
      'tap_action': {
        'action': 'call-service',
        'service': 'homeassistant.turn_off',
        'target': { 'entity_id': x.entity_id }
       }
    }
    -}},{%- endfor -%}

Sorry, most probably an easy case. But I spend a lot of time searching and can not figure out why this code won’t style? Neither icons nor border…

type: custom:auto-entities
card:
  title: Batteries
  type: glance
filter:
  exclude:
    - entity_id: '*_battery_state*'
    - entity_id: '*_battery_temperature*'
  include:
    - entity_id: '*battery*'
sort:
  method: state
  numeric: true
unique: true
card_mod:
  style: |
    :host {
      --paper-item-icon-color:
        {% if states(config.entity) | int < 10 %}
          red
        {% elif states(config.entity) | int > 30 %}
          hsl(120, 93%, 39%) !important
        {% else %}
          hsl(60, 100%, 50%)
        {% endif %};
    }

    ha-card {
      border-radius: 150px 15px 15px 15px;

      --ha-card-background: hsla(60,15%,60%,0.4);
    }

I solved this. Per definition templates are not possible for actions. [ Actions - Home Assistant ]

So I now pass the current entity (this.entity_id) as parameter to the rest_command and do the needed templating there.

Thanks @Mariusthvdb for your support.

hmm. I believe you are mixing several concepts and options for those concepts, as auto-entities is a completely different beast than the core card options for actions. Especially since you were using mushroom…

and as I already suspected you needed the entity, not just the attribute, so using this.entity_id would seem a logical solution indeed.

what would help, is if you’d post some results. You’ve been scarce in the ask, but now show even less.

great you’ve got it working now, please post your final solution, so others might benefit from it.

Team,

Is it possible to have and “options:” section in a template filter, in auto-entities ?

I am looking for something like below code - see “options:…”
(The template does generate the correct entities.
The intent is to have the tap_action set to none for these filtered entities).

        - type: conditional
          conditions:
            - entity: input_boolean.on_off_toggle_conditional_unavailable_devices_on
              state: 'on'
            - entity: sensor.unavailable_devices
              state_not: '0'
          card:
            type: custom:auto-entities
            show-empty: false
            filter:
              template: >
                {{ state_attr('sensor.unavailable_devices', 'unavail_list_entity_id') }}
              options:
                icon_color: green
                tap_action:
                  action: none
            card:
              type: glance
              show_state: false
              columns: 6

Thank you for your guidance.

The same problem was also in existence with a default button-card.

So overall this is my solution with providing the complete entity to the rest_command and do the templating there:

Input-Value – generated through a sep. Python script.

Auto-Entities Card to display all entities of this specific input_value entities calling a rest_command and passing over this.entity_id as parameter when tapped (tap_action).

type: custom:auto-entities
card:
  square: false
  columns: 1
  type: grid
show_empty: false
card_param: cards
filter:
  include:
    - entity_id: input_text.xenia_script_*
      options:
        type: custom:mushroom-template-card
        entity: this.entity_id
        primary: |
          {{ state_attr(entity, 'name')}} [{{state_attr(entity, 'index')}}]
        tap_action:
          action: call-service
          service: rest_command.execute_xenia_script_entity
          data:
            entity_id: this.entity_id

rest_command that interprets the handed over entity just for one attribute value within a template using state_attr

rest_command:
  execute_xenia_script_entity:
    url: "http://xenia.local/execute_script?ID={{ state_attr(entity_id, 'index') }}"

1 Like