Build cards with for-loop

Hello, I am trying to create cards using for-loop:


        card:
          type: horizontal-stack
          cards:
            {% for id in state_attr('[[[ return variables.entity ]]]', 'entity_id') %}
              - type: 'custom:light-entity-card'
                entity: {{{id}}
                effects_list: false
                full_width_sliders: true
                group: false
                header: []
                persist_features: true
                show_slider_percent: true
                smooth_color_wheel: true
             {%- endfor %}

variables.entity represents a light entity. entity_id is an object of light entities [light.x,light.y,light…]. When I try to save this snippet within the raw configuration, I get the error message

Unable to parse YAML: YAMLException: missed comma between flow collection entries at line 54, column 14: {% for id in state_attr('[[[ retu … ^

I can’t get any further. Could someone check my code?

Are you sure that it’s an option to use Jinja in that part of YAML?

Do you think if it works basically or if it’s a good idea to use Jinja here at all?
After some research, I came across this: https://sharethelove.io/tools/jinja-magic-scripts
So I think it should work in principle, but I have a syntax error in my code.

What they’re saying is:

  1. You go to /developer-tools/template, and then you paste the template in.
  2. You copy the output.
  3. You go back to the lovelace editor.
  4. Paste it back in.

It won’t automatically update, you’ll have to go back to the template preview and re-generate it whenever it changes.

I’m close to 100% sure you can’t use templating in this case. Even if you could, your entity line has an extra {, but I don’t think fixing that will make it work…

You might be able to use one of these cards to accomplish this:

Note Ian’s warning though:

If you want to template full configurations, you have to use lovelace_gen. It’s a custom integration and allows you to fully template your entire lovelace setup. I use it to take 150,000 lines of configuration down to about 3000 spread across 30 or so files. It’s the backbone of many of the themes you see like Dwains Theme and @jimz011’s iOS mimic theme (forget the name). You’re welcome to poke around my (wip) setup, it’s linked somewhere in my profile here.

Outside of that, you cannot template any cards. You have to do what @KTibow is saying. You paste your template into the template editor, let it create all your yaml, then paste it into lovelace.

Your current template has invalid syntax because you’re mixing JS and Jinja.

That’s JS

That’s Jinja.


To get this working in the template editor, you’ll have to provide proper jinja only.

You will not be able to get this working with lovelace_gen or any other platform. Primarily because lovelace_gen does not have access to the state machine.

3 Likes

I ended up here because I was curious about this too. For loops would be incredibly useful for cutting down lines of code and making cards more dynamic. For what it’s worth, markdown cards work with them:

This may or may not help the OP, since markdown cards are limited in functionality, but I thought it might be useful to point out.

I’m still looking how to create cards using (area) loops. Is there any example around?

Go to auto-entities thread and search there.

I did but could not find a loop over areas only loop through entities with given filters - so I had to define it for every area