Pulling hair out

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.

sensor:
  - platform: template
    sensors:
      total_lights_on:
        friendly_name: "Total Lights On"
        unit_of_measurement: 'lights'
        value_template: >
          {{ states.light | selectattr('state', 'eq', 'on') | list | count }}

thats the code i bundled together

any pointers?

Put this in the developer tools template editor to work out why:

{{ states.light | selectattr('state', 'eq', 'on') | list | join(', ') }}

It will report a list of lights it considers on.

My guess is that it is picking up light groups too.

Not meaning to sound like an idiot but clearly going to bit where in developer tools do i put it

So new to all this, but thankyounfornspeedy answer

Found it get this error

TemplateSyntaxError: expected token ‘end of statement block’, got '{

Not any wiser :frowning:

You only need to put this bit in the editor:

{{ states.light | selectattr('state', 'eq', 'on')| map(attribute="name")  | list | join(', ') }}

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.

Hi pal, dont you appologose for me being a bumbling fool, everyone on here has bnen so helpful.

I believe i have done as instructed

This is what i get.

Are any of your lights on?

hahahah no i didnt.

so ive had a play around

this is my code

  - 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…

{{ states.light | selectattr('state', 'eq', 'on') | rejectattr('attributes.entity_id', 'defined')| map(attribute="name") | list | count }}

2 Likes

It’'s the only way I know of.

1 Like

hi to both of you many thanks - trying to get my head round this… so my current code is:

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') | list | count }}

and i want to exclude:

Kitchen Long, Kitchen Short, Kids on/off, Sunset Lights On, All Off, Early Lights what does that code look like?

thanks in advance - head hurting :slight_smile:

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

amazing - thank you both for your help.

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

this code now works!!

No problem, happy to help, but you really should be using the new template sensor format.

I tried thet as you mentioned and it was coutomg lights up and down when they went on/off

Thank you if i habent saodnkt btw

Everyone on here been so helpful