There are certainly more efficient solutions, but as a first aid measure try the following to display which rooms are on:
{% set l = ['light.killarnas_rum_group', 'light.stellas_rum_group'] %}
{% set room = states.light
|selectattr('entity_id', 'in', l)
|selectattr('state', 'eq', 'on')
|list
%}
{% set hue = states.light
|selectattr('attributes.is_hue_group', 'true')
|selectattr('state', 'eq', 'on')
|list
%}
{{ (hue + room) |map(attribute='name') |join('\n') }}
But in my opinion itâs much easier to assign the lamps to the matching rooms by using the area feature. You can check whether you are using areas under your.homeassistant.ip:8123/config/areas/dashboard
# Count lights per room
{% set room = expand(area_entities('Stellas Rum'))
|selectattr('domain', 'eq', 'light')
|list
%}
{% set on = room |selectattr('state', 'eq', 'on') |list |count %}
{% set all = room |count %}
{{ 'Stellas Rum: ' ~ on ~ ' of ' ~ all ~ ' lights' if on != 0 else 'Stellas Rum: All lights are off' }}
Both options did work as I wanted But this code making is to hard to understand, but luckily
there is nice people.
{% set room = expand(area_entities('Stellas Rum')) |selectattr('domain', 'eq', 'light') |list %} {% set on = room |selectattr('state', 'eq', 'on') |list |count %} {% set all = room |count %} {{ 'Stellas Rum: ' ~ on ~ ' of ' ~ all ~ ' lights' if on != 0 else 'Stellas Rum: All lights are off' }}
The first part forms the basis for the counters. area_entities('Stella's Rum') contains all of the assigned entities. To access these in detail, we use expand.
But we only want the lights, so: |selectattr('domain', 'eq', 'light'). We then list the lights with |list.
The second part counts only the lights with status = on.
The third part counts all the lights in the room, whether on or off.
The output in the curly brackets is simply a shorthand notation of
{% if ... %}
{% else %}
{% endif %}
The tildes (~) connect the variables room, on, all to text, with the text enclosed in quotes.
Here is another example that takes singular and plural into account (stolen from user 123):
{% set on = expand(area_entities('Diele'))
|selectattr('domain', 'eq', 'light')
|selectattr('state', 'eq', 'on')
|list
|count
-%}
{% set all = expand(area_entities('Diele'))
|selectattr('domain', 'eq', 'light')
|list
|count
-%}
{% if on != 0 -%}
Diele ({{ on }} light{{'' if on == 1 else 's'}} of {{ all }} {{'is' if on == 1 else 'are'}} on)
{% else -%}
(All lights are off)
{% endif %}
I found out how to do it in Markdown Card with the code from (user 123) but not with the code from you! I could fix bold text on Stellas Rum but not on the light number.
{% set room = expand(area_entities('Stellas Rum')) |selectattr('domain', 'eq', 'light') |list %} {% set on = room |selectattr('state', 'eq', 'on') |list |count %} {% set all = room |count %} {{ 'Stellas Rum: ' ~ on ~ ' of ' ~ all ~ ' lights' if on != 0 else 'Stellas Rum: All lights are off' }}
You can drop the shorthand notition and use the normal one instead:
{% set room = expand(area_entities('Stellas Rum')) |selectattr('domain', 'eq', 'light') |list %}
{% set on = room |selectattr('state', 'eq', 'on') |list |count %}
{% set all = room |count %}
{% if on |int(0) > 0 %} <b>Stellas Rum: {{ on }}</b> of {{ all }} lights
{% else %} <b>Stellas Rum:</b> All lights are off
{% endif %}
You also can colour characters:
{% set room = expand(area_entities('Stellas Rum')) |selectattr('domain', 'eq', 'light') |list %}
{% set on = room |selectattr('state', 'eq', 'on') |list |count %}
{% set all = room |count %}
{% if on |int(0) > 0 %}<b>Stellas Rum: <font color='red'>{{ on }}</b></font> of {{ all }} lights
{% else %} <b>Stellas Rum:</b> All lights are off
{% endif %}
Colors within a template when using Markdown Card! HTLM, I should have know that!
That changed alot in my planing of using the Markdown Card, Nice
You just blown my head offâŠ
{% set room = expand(area_entities('Stellas Rum')) |selectattr('domain', 'eq', 'light') |list %}
{% set on = room |selectattr('state', 'eq', 'on') |list |count %}
{% set all = room |count %}
{% if on |int(0) > 0 %}<b>Stellas Rum: <font color='red'>{{ on }}</b></font> of {{ all }} lights
{% else %} <b>Stellas Rum:</b> All lights are off
{% endif %}
{% set all = expand(area_entities('Sovrum'))
|selectattr('domain', 'eq', 'light')
|rejectattr('attributes.is_hue_group', 'true')
|map(attribute='name')
|list |join(', ')-%}
{% if on != 0 -%}
Sovrum - {{ on }}{{'' if on == 1 else ''}}{{ all }}{{'is' if on == 1 else ''}}
{% else -%}
{% endif %}
How do I get âTaklampa Sovrumâ to disaper when I turn it off? The others should stay as turned on.
if I use this code it works, but then I dont have the possibility to change names and text.
You can add as many areas as you want to monitor. If an area doesnât have any lights on it will not appear in the list. If no lights are on it will display the message in the last line.
Youâre a đ§ââïž arenât you!? Thanks alot for the time and all help you have given me. I got to end with my Lovelace view.
This is how it all ended up (still under development). Now I have all the code I need for this project, and with a little luck a can learn something along the way.
I didnât really like how that one listens to every entity that has a defined area⊠The following checks for lights being âonâ first, that way the only non-light entities that get listened for are those in areas where there are lights on.
{% set areas = ['Bedroom', 'Living Room']%}
{%- set on_lights = states.light | selectattr('state','eq','on') | map(attribute='entity_id') | list %}
{%- set all_lights = states.light | map(attribute='entity_id') | list %}
{%- set ns = namespace(on=[], z=[]) %}
{%- for light in on_lights %}
{%- if area_name(light) %}
{%- set ns.on = ns.on + [area_name(light)]%}
{%- endif %}{%- endfor %}
{%- for x in ns.on | unique | list %}
{%- set total = expand(area_entities(x)) | selectattr('domain','eq','light') | list | count %}
{% set on = expand(area_entities(x)) | selectattr('domain','eq','light') | selectattr('state','eq','on') | list %}
<b>{{ x }} {{on|count}}/{{total}}- </b>
{{- on | map(attribute='entity_id') | join(', ') -}}<br>
{% set ns.z = ns.z + [ on | count ]%}
{%- endfor %}
{{"All Lights are Off" if ns.z | sum == 0 else ''}}