Change Icon Color with variable

Hi,

i try to change the color acording to the count of lights. What am i doing wrong here ? Is it not possible to get a var ?

Thanks for any hint.


      - type: template
        content: |-
          {% set ns = namespace(lights_on=0) %}
          {% set region = ['Büro'] %}
          {% for room in region %}
            {% set ns.lights_on = ns.lights_on + area_entities(room) | select('match', 'light.*') | select('is_state', 'on') | list | count %}
          {% endfor %}

          {% if ns.lights_on == 1 %}
            Es ist {{ ns.lights_on }} Licht im Büro an
          {% elif ns.lights_on > 1 %}
            Es sind {{ ns.lights_on }} Lichter im Büro an
          {% else %}
            Alle Lichter sind aus
          {% endif %}
        icon: mdi:lightbulb-group
        icon_color: |-
          {% if ns.lights_on > 0 %}
            orange
          {% else %}
            grey
          {% endif %}
        tap_action:
          action: perform-action
          perform_action: light.turn_off
          target:
            area_id: buro
        entity: light.sonoff_element_lights
        hold_action:
          action: none
        double_tap_action:
          action: none

A few questions…

Is this a custom:mushroom-chips-card??

Have you tested this code?

{% set ns = namespace(lights_on=0) %}
          {% set region = ['Büro'] %}
          {% for room in region %}
          {% set ns.lights_on = ns.lights_on + area_entities(room) | select('match', 'light.*') | select('is_state', 'on') | list | count %}
          {% endfor %}

It’s generating a string and then you are comparing a string to a number here

{% if ns.lights_on == 1 %}

Hi, its not an string. because i use count. it should be an integer.

but anyway, even if i use:


        icon_color: |-
          {% if ns.lights_on | int > 0 %}
            orange
          {% else %}
            grey
          {% endif %}

it doesnt work.

yes the the chips-card work, except the icon color. if i put only 1 color in it works. but not with my condition. the same for the “else” part, it doesnt take any color inside the condition.

Here is an example to work with…

icon_color: |
      {% set state = states('sensor.living_room_temperature') | int(0) %}
      {% if state >= 50 %} red
      {% elif state >= 30 %} orange
      {% else %}
      blue
      {% endif %}

Are you pulling code from this thread

1 Like

i got it. didnt know if i set a variable its only available inside each block. i thought i can use it in every block inside a “card”

icon_color: |-
          {% set lights_on = area_entities('Büro') | select('match', 'light.*') | select('is_state', 'on') | list | count %}
          {% if lights_on > 0 %}
            orange
          {% endif %}

this works. i have to count it again for the color.

1 Like

I’d template the count so you don’t have to recalculate it over and over.

1 Like

yes youre right , i will do it. im new in the hass world. still figure things out :slight_smile:

1 Like

No worries, I have been at it for quite some time and am still learning every day!

The final code if someone intrested in.

Mushroom chips card:

      - type: template
        content: |-
          {% set lights_on = states('sensor.your--name') | int(0) %}
          {% if lights_on == 1 %}
            Es ist {{ lights_on }} Licht im Büro an
          {% elif lights_on > 1 %}
            Es sind {{ lights_on }} Lichter im Büro an
          {% else %}
            Alle Lichter sind aus
          {% endif %}
        icon: mdi:lightbulb-group
        icon_color: >-
          {% set lights_on = states('sensor.your--name') | int(0) %}
          {% if lights_on > 0 %}
            orange
          {% endif %}
        tap_action:
          action: perform-action
          perform_action: light.turn_off
          target:
            area_id: buro
        hold_action:
          action: none
        double_tap_action:
          action: none

template (name: sensor.your–name):

{% set ns = namespace(lights_on=0) %}
{% set region = ['Büro'] %}
{% for room in region %}
  {% set ns.lights_on = ns.lights_on + area_entities(room) | select('match', 'light.*') | select('is_state', 'on') | list | count %}
{% endfor %}
{{ ns.lights_on }}
1 Like