More template help!

Hi all,

I have this template working just fine!

{{ states.light
|selectattr('state', 'eq', 'on') 
|selectattr('attributes.is_hue_group', 'true')
|map(attribute='name')
|join('\n')
}}

I want to add this two new rooms to also show if true (they are not philips hue)

light.killarnas_rum_group
light.stellas_rum_group

If all that is working I also want to know if its possible to add next to the rooms how many light are on, like this.

Bedroom (3 Lights are on)
Livingroom (8 Lights are on)

or

Bedroom (3 of 6 Lights is on)
Livingroom (8 of 8 Lights is on)

Best regards
Thekholm

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' }}

Many many thanks,

Both options did work as I wanted :+1:But this code making is to hard to understand, but luckily
there is nice people. :grin:

{% 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' }}

Best regards
Thekholm

Thank you.

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 %}

1 Like

Thanks again, maby it’s time to go back to school? :grin:

So how do I add all my Area rooms in one template or is it better to do one per room?

Template is really the power of Home Assistant, to bad its hard to understand for us normal people or I mean me if anyone gets offended :confused:

But you have helped me with a couple of templates that I am running right know! :pray:

Best regards
Thekholm

Don’t worry. I sweat blood, sweat and tears, and I still don’t understand the subtleties.

You can concatenate areas, copy it into Developer Tools > Templates and play around :slight_smile:


{{ expand(area_entities('Rum 1'), area_entities('Rum 2'), area_entities('Rum 3'), area_entities('Rum 4') ) 



I will play with all this under the weekend. Is it also possible to and bold text, only to the room names? Or even the on lights?

Stellas Rum:
Livingroom:

Have a great friday and weekend :sunglasses:

/ Thekholm

Well, that depends on which card you are using for displaying the light count. In a Markdown Card for example, it it working rather flawlessly.

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' }}

I will try more over the weekend :slight_smile:

Best regards
Thekholm

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 %}

WHAAAAT!?

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 :slight_smile:

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 %}

/ Thekholm

I have played with the template a bit, this is what I have come to. This is my test results!


It’s nice and all but I have 67 light and it will take up two much space in the app.

Is it possible to have like this instead?

Stellas Rum (6/6), Sovrum (6/6), and so on. I will use it as an overview!

The one a have played with will become the way I show light status in each room.

Thanks for all help to get where I’am :slight_smile:

/
Thekholm

Sure:


{% 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: (<b>{{ on }}</b>/{{ all }})

You have some beautiful lamps at home!

If you mean Foscarini and Flos they are really nice :slight_smile:
I think you missunderstood me. The output gives me a list like this:

Stellas Rum (6 / 6)
Sovrum Rum (6 / 6)

I want it more like this:

Stellas Rum (6 / 6), Sovrum (6 / 6)

{% 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: (<b>{{ on }}</b>/{{ all }})

You gave me this code before, and that gives me the right output (but without the number 6/6).

{% 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(', ') }}

I need this ({{ on }}/{{ all }}) some where in the code!?

Best regards
Thekholm

{% 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 %}

Output:

“Sovrum - Fönsterlampa, Ledlist Sovrum, Skrivbordslampa, SĂ€nglampa 1, SĂ€nglampa 2, Taklampa Sovrum”

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.

{{ expand(area_entities('Sovrum'))
|selectattr('state', 'eq', 'on')
|selectattr('domain', 'eq', 'light')
|rejectattr('attributes.is_hue_group', 'true')
|map(attribute='name')
|join(', ')
}}

How can I combine these two? whats the diffrence between this {{ and {%

Sorry for all questions, if you dont have time or are sick and tired of me it absolutly okej, I would be!

Best regards
Thekholm

{{}} are used for Expressions.
{%%} are used for Statements.

If you are repeating the same thing for multiple rooms use a loop:

{%- set areas = ['Sovrum', 'Stellas Rum'] %}
{%- set ns = namespace(z=[])%}{%- for rooms in areas %}
{%- set x = expand(area_entities(rooms)) | selectattr('domain', 'eq', 'light') 
| rejectattr('attributes.is_hue_group', 'true') %}
{%- set on = x | selectattr('state', 'eq', 'on') | map(attribute='name') | list -%}{%- if on|count > 0 -%}
<b>{{ rooms }} - </b>{{- on | join(', ') -}}<br>
{%- else %}{{ '' }}{%- endif %}
{%- set ns.z = ns.z + [ on|count ]%}{%- endfor %}
{{"All Lights are Off" if ns.z|sum == 0 else ''}}

The above will output something like the following, but only the lights that are ‘on’ will be listed:

Sovrum - Fönsterlampa, Ledlist Sovrum, Skrivbordslampa, SÀnglampa 1, SÀnglampa 2, Taklampa Sovrum
Stellas Rum - Stellas Fönsterlampa, Stellas Skrivbordslampa, Stellas SÀnglampa

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.

1 Like

Thats nice,

The only thing missing now is the total light that is on and not on (6 lights on, off 6 total) and space between rooms just like this:

Sovrum (6/6) - Fönsterlampa, Ledlist Sovrum, Skrivbordslampa, SÀnglampa 1, SÀnglampa 2, Taklampa Sovrum

Stellas Rum (6/6) - Stellas Fönsterlampa, Stellas Skrivbordslampa, Stellas SÀnglampa

Best regards
Thekholm

Thanks Drew!

For my part, I will go with your initial suggestion:


type: markdown
content: >-
  {% set areas = ['
', '
', '
'] %}
  {% for rooms in areas %}
  {%- set lights = expand(area_entities(rooms))
  |selectattr('domain', 'eq', 'light') |list -%}
  {%- set on = lights |selectattr('state', 'eq', 'on')
  |map(attribute='name') |list -%}
  {%- if on -%}{{ rooms }}: (<b>{{ on |count }}</b>/{{ lights |count }}) - {{ on | join(', ') }} {% else
  %}{{ '' }}{% endif %}{%- endfor %}

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.

Many thanks :pray:

Best regards
Thekholm

1 Like

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.

@Thekholm this one has the on/total info in it.

{% 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 ''}}