iantrich / config-template-card

📝 Templatable Lovelace Configurations
MIT License
425 stars 55 forks source link

Template in variables section #141

Closed Jonny-T-cyber closed 1 month ago

Jonny-T-cyber commented 1 month ago

Hi! I have this template:

    {% set count = namespace(counter=0) %} 
    {% for state in states.update %}
      {% if (state.state == 'on') %}
        {% set count.counter = count.counter + 1 %}  
      {% endif %}
    {% endfor %}
    {{ count.counter }}

I would like to assign the value of count.counter to a variable and I'm trying the following way but, obviously, it's not the correct way:

image

So... How do I assign the value of count.counter to the variable COMPONENT_UPDATES? And, also, where/how do I write the template?

Thanks

ildar170975 commented 1 month ago

The config-template-card (CTC) supports JS in templates. What you posted is a jinja template.

Your alternatives:

  1. Rewrite the template using JS. Discussing a JS code syntax is not supposed to be done in this repo. Suggest to answer all card-related questions in the dedicated CTC thread in HA community (https://community.home-assistant.io/t/100-templatable-lovelace-configurations/105241).

  2. Create a template sensor like this:

    template:
    - sensor:
      - name: my_super_sensor
        state: ... your jinja code ...

    and then use this sensor inside CTC:

    type: custom:config-template-card
    variables:
    XXX: states['sensor.my_super_sensor'].state
    entities:
    ...
    card:
    ...