Any advice - it was working right, messed up the yaml code and re wrote. Now i turn on one light it counts as 3, some count as 2 etc. Im sure its something to do with groups or something.
I’ve updated it a bit to only show the names of the lights that are on. It is tested and working on my system.
This is only a test to see what the template is returning, do not use it in your sensor config. We’ll still use the |count version once we work out what needs rejecting.
Also apologies for not telling you where the template editor was. I assumed you would know as this sort of template is quite advanced. My bad for not seeing you were new to this.
- platform: template
sensors:
current_lights_on:
friendly_name: Lights at this moment
unit_of_measurement: 'on'
value_template: >
{{ states.light | selectattr('state', 'eq', 'on') | list | count }}```
and in the devloper tools it shows
Kids on/off, Kitchen Lights, Sunset Lights On, All Off, Early Lights, Disney Lamp, Kitchen Long, Kitchen Short, Living Room Lamp, Landing light, Dining Room Lamp, Hall Lamp, Finn, Snug Lamp, Bed Lights, TV Lights
my dashboard is showing 17 lights on.
when in reality its 9.
ive tried various reject options and listed the ones i dont want but nothing made much differece.
![dash|630x500](upload://hNoLCSOWNbiGC7nRRtEs4dhRAYS.jpeg)
any ideas?
and keep it simple im stupid haha
it looks like you have some groups (all off, early lights, etc) if so, tom_l is right. if so, does this work for you? (apologies to tom for jumping in… he gets all credit, but thought an answer now sooner is helpful to you if this works). i’m using the existance of the entity_id attribute to determine that it’s a group. not sure if there’s a better way…
The light groups that your template is picking up have an attribute called entity_id that normal lights do not (it lists the lights in the group). So to exclude these group lights you use the reject option Armedad showed above.
sensor:
- platform: template
sensors:
current_lights_on:
friendly_name: Lights at this moment
unit_of_measurement: 'on'
value_template: >
{{ states.light | selectattr('state', 'eq', 'on') | rejectattr('attributes.entity_id', 'defined')| map(attribute="name") | list | count }}
Or in the new template integration format (which you should be using for new sensors):
configuration.yaml (not sensors.yaml)
template:
- sensor:
- name: Lights at this moment
unit_of_measurement: 'on'
state: >
{{ states.light | selectattr('state', 'eq', 'on') | rejectattr('attributes.entity_id', 'defined')| map(attribute="name") | list | count }}