Home » Blog » YAML & Jinja Templating Course Episode 6: Counting your lights

YAML & Jinja Templating Course Episode 6: Counting your lights

Hi, welcome to the sixth episode of this YAML & Jinja course. Today I will show you how to filter data in the Home Assistant. As an example, I’ll show you how to count and display the number of lights that are currently on in your home. This is useful if you are leaving the house and want a quick reminder that some lights are still on. Or to get a message that doors or windows are still open when you leave your home. Let’s do this!


⭐⭐⭐ NOTE: ⭐⭐⭐

This article accompanies a YouTube video. I wrote it for people who would rather read than watch a video. To keep doing this, I would like to ask you to check out the video, leave a comment under the video, give the video a thumbs up, and subscribe to my YouTube channel. This means that the video is offered more often to new visitors so that they also stay informed of the latest Home Assistant tutorials.

Thank you for your support!

Ed

Click here to watch the video

One of the most frequently asked questions is that people want a counter on their dashboard showing how many lights are on in the house. In this YAML & Jinja tutorial, I will show you how to set that up. And, once you know how to do this with lights, you can do it with all kinds of devices in the same way. I assume you’ve already watched the previous episodes of this YAML & Jinja course so you’ll understand everything more easily in this episode. If you haven’t watched the previous episodes, go to this playlist to watch those episodes first.

You can type the code I show in this video from the screen, or download it from the link in the video description for a small fee. This saves you time and frustration, and you ensure I can continue doing this work. I work full time making these videos and without your financial help, I can’t continue to do this because the income from YouTube is too little to live on.

Anyway, let’s get started!

Counting all the lights in your house

You probably have several lights in your house and control them with Home Assistant. Your lights fall within the light domain. If you go to the developer tools and then to the states tab, you can list all your lights by filtering on “Light dot.” You will now see an overview of all your lights’ status and attributes. For example, if you look at light.bedroom, you will see that this light is off and has several attributes. Since the light is off, many attributes are set to “null.”

For instance, if I check the red light behind me, which is called “office back left”, you will see that it is on and that several attributes are filled with values. The brightness is 254, and the RGB color is 255, 137, 18. If I click on the light entity, you can see how the attributes are structured. You will notice that within the attributes, a few lists are also defined. If you want to know how lists work, go to Episode 4 of this course, where I explain how to work with lists.

Let’s start by retrieving the number of lights in the house. You do it as follows:

{{ states.light | count }}

What I’m doing here is using the count filter to count all devices in the light domain. The result is 105, but the total number of lights in my house is actually much lower than 105. This is because light groups are also counted as individual lights. Now, if you use ZHA, you can recognize light groups, but if you use something like Zigbee2MQTT, you can’t tell the difference based on the attributes. The attributes of a light group are exactly the same as those of an individual light. For example, the attributes of the light “light.officeback_left” are the same as the attributes of the group light.office_back.

Now, you could filter based on the friendly name, but that’s a lot of work if you have to filter all the friendly names of groups. It’s smarter to use labels. As you can see, I created a Group label and assigned it to all groups within Home Assistant. Now I can filter based on that label. See this example:

{{ states.light | count }}

{{ label_entities('Group') }}

{{ states.light | rejectattr('entity_id', 'in', label_entities('Group')) | list | count }}

In the first line, I retrieve all entities in the light domain again. So, that’s 105.

In the second line, you see which entities I have labeled as Group.

In the third line, I ensure that the group entities are not included in the total count. First, I fetch all light entities, then I apply a filter called rejectattr. With rejectattr, you exclude all items that meet the properties within rejectattr. In this case, I want to exclude all entities in label_entities(‘Group’). Then, I create a list from this and finally count all the items in this list using the count filter. And now it turns out that I have 81 lights in my house. Well, not quite, because I also have a few other devices registered as lights that are not really lights. For example, an LED indicator in a motion sensor. For this, I created a label “Dummylight” and added an extra line, as you can see here:

{{ states.light | rejectattr('entity_id', 'in', label_entities('Group')) 
                | rejectattr('entity_id', 'in', label_entities('Dummylight')) 
                | list 
                | count }}

As you can see, you can chain filters together. Now, my Dummylights are also not counted. So, I have a total of 64 actual lights in my house. My god, I didn’t even realize that. This smart home hobby has gotten a bit out of hand. Do you also have this many lights, or maybe even more? Let me know in the comments.

How many lights are on in the house?

Now, it’s nice to know how many lights you have in the house, but it’s actually even better to know how many lights are currently on. We can do this by adding another filter that only shows the lights with the state “On.” See this example:

{{ states.light | rejectattr('entity_id', 'in', label_entities('Group')) 
                | rejectattr('entity_id', 'in', label_entities('Dummylight')) 
                | selectattr('state', 'eq', 'on')
                | list 
                | count }}

As you can see, I have now added a selectattr filter. This filter selects all entities that meet the criteria defined within this filter. In this case, I filter all entities whose state equals “On.” As a result, you can see that currently, 15 lights are on. You can achieve the same with, for example, door or window sensors to see how many doors or windows are open. I believe you can now put this together yourself. Let me know in the comments if you succeed.

In the next episode, I will show you how to create a custom sensor from a template in Home Assistant so you can use this value in automations and on your dashboard.

And with that, we are almost at the end of this episode. Thank you for watching. I hope this YAML & Jinja course helps you do more advanced things in Home Assistant. If it does, please join those who sponsor me monthly with a small amount. It ensures that I can keep doing this work. Otherwise, it will eventually stop because the income from YouTube is simply too low to live on. The links to Patreon, Ko-Fi, and how to become a paid member of my channel are in the description of this video.

And don’t forget to give this video a thumbs up and subscribe to my channel.

I’ll see you soon in my next video.

Bye Bye!



Back to all articles