mariva
March 3, 2025, 8:20pm
1
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 %}
mariva
March 4, 2025, 6:34am
3
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
I slightly improved the code you provided to adapt it for my use. Sharing in case it helps someone, just as the code @123 helped me. I used Mushroom Template Card and two basics Home Assistant script.
Assistant script :
alias: light_ground_floor_off
sequence:
- service: light.turn_off
data: {}
target:
floor_id: ground_floor
description: ""
alias: light_ground_floor_on
sequence:
- service: light.turn_on
data: {}
target:
floor_id: ground_floor
description: ""…
1 Like
mariva
March 4, 2025, 6:41am
6
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
mariva
March 4, 2025, 6:45am
8
yes youre right , i will do it. im new in the hass world. still figure things out
1 Like
No worries, I have been at it for quite some time and am still learning every day!
mariva
March 4, 2025, 2:59pm
10
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