petro
(Petro)
September 14, 2021, 11:28am
41
This will return a list
{%- set search_state = 'on' %}
{%- set search_area = 'Living Room' %}
{%- set ns = namespace(lights=[]) %}
{%- for light in states.light | selectattr('state','eq', search_state) if area_name(light.entity_id) == search_area %}
{%- set ns.lights = ns.lights + [ light.entity_id ] %}
{%- endfor %}
{{ ns.lights }}
3 Likes
Works like a charm, thanks a lot. When trying to convert to count, i get it working but when i want to use it as a sensor, it fails (of course i hardcoded the search state search area and such instead of configurables, but canât get it working). What am I doing wrong?
this works for sensor:
{{ states | selectattr('entity_id','in', ['light.laundry_room','light.hobby_room_spot_2','light.hobby_room_spot_3','light.hobby_room_spot_1','light.living_room_color_1_table' ) |selectattr('state','eq','on') | list | count }}
but as soon as I add area_id or area_name to it, it fails to give me results.
Please assist
petro
(Petro)
September 14, 2021, 2:04pm
43
just use my template and change the last line to
{{ ns.lights | count }}
And how can I add that into a sensor? Thatâs whats not working, since ns.lights is something you predefined earlier, right?
petro
(Petro)
September 14, 2021, 2:06pm
45
you use the whole template in the template sensor
1 Like
petro:
{{ ns.lights | count }}
works perfectly, thanks so much!
pedolsky
(Pedolsky)
September 14, 2021, 2:25pm
47
Is it possible to avoid light groups to be counted (I canât see an attribute like is_group
or so) without hardcoding?
petro
(Petro)
September 14, 2021, 2:33pm
48
add a rejectattr('attributes.entity_id', '!=', undefined)
to the for loop line before the if statement
2 Likes
123
(Taras)
September 14, 2021, 2:46pm
49
Try this in the Template Editor (replace 'Kitchen'
with the name of your area). I tested a similar version on my system and it correctly reported the number of switches (that are on
) in the Kitchen area.
{{ expand('light.laundry_room', 'light.hobby_room_spot_2',
'light.hobby_room_spot_3', 'light.hobby_room_spot_1',
'light.living_room_color_1_table')
| selectattr('state', 'eq', 'on')
| map(attribute='entity_id')
| map('area_name')
| select('eq', 'Kitchen')
| list | count }}
1 Like
petro
(Petro)
September 14, 2021, 2:49pm
50
That will work if you donât want entity_idâs
123
(Taras)
September 14, 2021, 3:00pm
51
Yes, thatâs right but should be adequate for martheijnen who, unless I am mistaken, only requested a count.
petro
(Petro)
September 14, 2021, 3:01pm
52
youâre not mistaken, I was providing a list of entities
Luis_Angel
(Luis Angel)
October 5, 2021, 6:30pm
53
I have this code
sensor:
- platform: template
sensors:
lucesencendidas:
entity_id:
- light.habitacion
- light.mesita
- light.sala
- light.comedor
- light.lectura
# - light.gateway_light_7811dcfab6cc
# #- switch.cocina
# - switch.sonoff_1000649ca0
# - switch.sonoff_10005b8eb0
value_template: "{{ (states | selectattr('entity_id','in',state_attr('group.luces','entity_id')) | selectattr('state','eq','on') | list | count) }}"
It works but i donât know why from 0 to 1 it counts inmediately, to update to other counts it takes a minute or so. Also happens when turning off (going downâŚ). It takes time.
Anything about this issue?
I think that one two years ago (v.0.92) (i didnât updated HA) it was working fine.
1 Like
123
(Taras)
October 5, 2021, 6:38pm
54
Does group.luces
contain any of these entities?
- light.habitacion
- light.mesita
- light.sala
- light.comedor
- light.lectura
petro
(Petro)
October 5, 2021, 6:41pm
56
Whenever you use states
in a template, the template is throttled to 1 update per minute.
Fix it by using this template instead:
value_template: "{{ expand('group.luces') | selectattr('state','eq','on') | list | count) }}"
Luis_Angel
(Luis Angel)
October 5, 2021, 6:43pm
57
Yes all of these.
group:
luces:
name: IluminaciĂłn
entities:
- sensor.lucesencendidas
- light.habitacion
- light.mesita
- light.sala
- light.comedor
- light.lectura
- light.gateway_light_7811dcfab6cc
- sensor.illumination_7811dcfab6cc
- sensor.illumination_158d0003029a7a
Luis_Angel
(Luis Angel)
October 5, 2021, 6:46pm
58
Just trying⌠tell you in a minute after restarting.
Luis_Angel
(Luis Angel)
October 5, 2021, 6:50pm
59
Perfect !!! Thank you so much.
Two hours trying to figure out the problemâŚuntil i decided to post it here.
NowâŚno delay.
123
(Taras)
October 5, 2021, 6:51pm
60
If theyâre all in the group then petroâs suggestion is the way to go.