How many lights are on?

As you can tel from the name I am very new to this. I decided at 61 I need to challenge myself to start a new hobby and this looked the perfect fit after having sonoff products for a few years. Three plus weeks in and OMG what cant HA do!! I am in awe of the amount of codes flying about on here to enable all sorts of intergrations, only thing is I havent got a clue what I am doing. Is there a novice page anywhere? I have managed to copy a bit of code from somewhere (forgotten where) and put it in a template card to count how many of my lights are on.

{{ states.outlet|selectattr('state','equalto','on')|list|length }}

Is there any way of showing how many are on in bed 1 or in the longe etc this just counts all of the lights. Any help will be appreciated.

Thanks

Absolutely, you can.

Letā€™s reformat your code a little - it makes no functional difference, but itā€™s easier to break it down:

{{ states.outlet
  |selectattr('state','equalto','on')
  |list
  |length
}}

OK, so line 1 is referring to all states, line 2 selects all things that are on. line 3 lists them and line 4 essentially counts them
What you want to to is only have the Ines that are say, ā€œloungeā€

Weā€™d add an extra filter for thatā€¦

{{ states.outlet
  | selectattr('state','equalto','on')
  | selectattr('entity_id','search','lounge')
  | list
  | length
}}

That extra line is a filter that looks for entity_Id's that have the word lounge in them (bearing in mind it is only choosing from a list that is already set to on).

btw, Iā€™m not sure about states.outlet, Iā€™d use sates.light and also count

{{ states.light
  | selectattr('state','equalto','on')
  | selectattr('entity_id','search','lounge')
  | list
  | count
}}

ā€¦also, the filter that looks for entity_id could also look for friendly_nameā€¦

  | selectattr('attributes.friendly_name','search','lounge')

The search function uses RegEx, so you can play with that as well.

Thank you so much for that, I will have a play tomorrow, she who must be obeyed is backā€¦ Lol. Where I put outlet I had actually changed it to light but was playing with the same Idea with sockets. Iā€™ll post how I get on after having a play. :+1:

Sure thing. Just pop back and ask any questions you have!

Interesting. In my case, it shows one light more than I really have, because I have a group ā€œlight.all_lightsā€ wich is also shown as ā€œonā€, if I turn on a light.
How can I exclude only this device from the counter?

BTW: I have an automation, that turn off all lights after a power failure and I will change it to work with this sensor ā€œif more than 15 lights on ā†’ turn off all lightsā€

A Light Group has an entity_id attribute. The template can be designed to exclude any light entities having an entity_id attribute.

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

My lights are all via Z2M as are my scenes and groups so this doesnā€™t work for me. I need to select entities with the attribute linkquality (there are other attributes that my lights have but groups donā€™t have that I could choose from).

{{ states.light
  | selectattr('state','equalto','on')
  | selectattr('attributes.linkquality', 'defined')
  | list
  | count
}}

edit: typo.

The example I posted was specifically for Light Groups and wonā€™t work for a legacy group of lights or for a Hue group of lights (or Z2M groups and potentially other types of groups of lights). However, the principle it employs, rejecting/selecting entities based on the presence of a distinguishing attribute, can certainly be used for other applications.

1 Like

jchh,

Sorry for the late reply, however, Iā€™m back with good news. It worked fine for me, thank you. I played a little and changed it to show all of my sockets that are on as well. As I said Iā€™m very new to this so itā€™s a huge learning curve, next task if to google Yaml for complete novices and see if I can start playing with that (I have a second Rpi 4 running homeassistent on it just to play with so if I break it thereā€™s no great loss) Iā€™m that much of a novice iā€™m yet to find out where I type the code intoā€¦ wish me luck :+1:

Good luck!

Okay, i have tried and failed.:frowning: I would like to exclude a specific switch from a count and try as I might I cant do it, I have searched and tried a few different things but i am having to put my hand up for help. I know itā€™s possibly simple but to me ā€¦ Maybe not. I have pasted below what I have at the moment I just need to exclude ā€œswitch.xyzā€

Thanks again

{{ states.switch | selectattr(ā€˜stateā€™,ā€˜equaltoā€™,ā€˜onā€™)
| selectattr(ā€˜entity_idā€™,ā€˜searchā€™,ā€˜loungeā€™)
| list | count }} sockets on

With a bit more trying i managed itā€¦

sockets on {{ states.switch | selectattr(ā€˜stateā€™,ā€˜equaltoā€™,ā€˜onā€™)
| selectattr(ā€˜entity_idā€™,ā€˜searchā€™,ā€˜loungeā€™)
| rejectattr(ā€˜nameā€™, ā€˜searchā€™, ā€˜Lounge Lightā€™)
| list | count }}

@jchh
This is the best breakdown/explanation of each line and filter Iā€™ve seen. Iā€™ve been trying to create these counts (windows/doors/motion/lights) and now filtering by ā€œareaā€ or room. This helps so muchā€¦thanks!

1 Like

Feel free to ask if you have any questions.

Is it possible to exclude all the members of a group? In my case I have a WLED light with 5 sections and a master switch. The sections stay on but the master switch is automated and what I keep track of.

Yes, you can either put them in a group and exclude that group or exclude them by name (easier if they have part of their name in common).