Change CSS property (e.g. badge color) based on state w/ lovelace_gen and card-mod

i am trying to use raw to copy a sensor attribute value to a lovelace-gen variable. once its in the lovelace-gen variable the for syntax is something i use extensively in other places and it works fine.

if you would please help figure out the syntax for the set line i think i can get this to work. what would be the right syntax to use state and raw to copy the attribute value of a sensor name stored in a lovelace-gen variable to another lovelace-gen variable?

thanks.

edit:

{% set lovelace-gen-foo = {% raw %}states[{% endraw %}lovelace-gen-bar{% raw %}].attributes.devices{% endraw %} %}

I’m telling you that it will not work at all. Unless you provide more context than what you have above, that will never work with any combination of raw.

The only combination it will work in is this:

{% raw %}{% set theseLights = states['{% endraw %}{{ lightsOn }}{% raw %}'].attributes.devices %}{% endraw %}
{% raw %}
{% for l in range(theseLights | len) %}
    - type: light
      entity: {{ theseLights[l] }}
{% endfor %}
{% endraw %}

and I’m fairly sure tha’ts not what you want because it’s result is an invalid template that produces yaml.

ok let me go back to the original problem.

i have a sensor that stores lights on count in state and list of lights that are on in an attribute called devices. the name of the sensor is store in a lovelace-gen variable.

so lovelace-gen-variable is set to someSensor.

using this information how do i create a light card for each light device stored in states['{{ lovelace-gen-variable }}'].attributes.devices?

someSensor
  state: 2
  devices:
    - light.hue_color_1
    - light.hue_color_2

You can’t because you’re trying to access information from the state machine in lovelace gen. It’s just not possible.

sigh … for the life of me i don’t understand why entity does not simply accept a template!

I still don’t think you understand what I’m saying. All the functions like states and state_attr do not exist in lovelace_gen.

o that part i got. but using my example if entity accepted a template i could have done this based on my example, for a sensor that has 5 devices in attributes.

Without having to build a dictionary array of lights for each area that i am interested in displaying individual light cards on demand. Much more efficient and lot less typing:

- type: light
  entity: '[[[ return states["{{ lightsOn }}"].attributes.devices[0] ]]]'
- type: light
  entity: '[[[ return states["{{ lightsOn }}"].attributes.devices[1] ]]]'
- type: light
  entity: '[[[ return states["{{ lightsOn }}"].attributes.devices[2] ]]]'
- type: light
  entity: '[[[ return states["{{ lightsOn }}"].attributes.devices[3] ]]]'
- type: light
  entity: '[[[ return states["{{ lightsOn }}"].attributes.devices[4] ]]]'

if lightsOn is the exact same entity in all cases, then there’s no need for raw because thats a JS template…

{% for i in range(5) %}
- type: light
  entity: '[[[ return states["{{ lightsOn }}"].attributes.devices[{{ i }}] ]]]'
{% endfor %}

If lightsOn is pulled from the states object, or the number of iterations is pulled from the number of devices, what you want is not possible because that information is not available in Lovelace_gen.

lightsOn is a lovelace-gen variable but i would have preferred to pull the number of iterations from the number of devices but lets leave that aside for a moment.

i tried the code you shared with fixed number of iterations as in your code and below is the output. that’s what i meant when i said entity does not seem to accept a JS template, which is puzzling to me. what am i missing?

thank you.

The js or jinja templates are only available if the card accepts it.

thanks. yeah kind of figuring that out now. but why the default button-card not accept JS or jinja is a mystery to me :slight_smile:

Because it doesn’t have that functionality. Templates are not standard in the front end. They are typically only available in custom cards.

There is only 1 built in card that accepts templates. The markdown card. Nothing else provided by core allows templates in the frontend.

got it. but that was kind of my point too - its such a basic functionality that core not supporting it for built-in cards is surprising - to me :slight_smile:

It shouldn’t be, it’s not basic. It’s very advanced. HA has had this stance since it started the ui in 2013

it may have been advanced 10 years ago. but 10 years since then HA has come a long way and its time to reconsider updating their stance to have this be the new basic now :slight_smile: