Henke
(Henke)
April 13, 2023, 6:46pm
1
I have a simple template sensor to check all lights on and display it. But how do I also get it to display the ones that have a specific current brightness, for example 1% ?
Current code for the sensor below:
sensor:
- platform: template
lights_on:
friendly_name: "Lights on:"
icon_template: mdi:light
value_template: >
{{ states.light |selectattr('state', 'eq', 'on')
|list |count }}
Olivier1974
(Olivier Toussaint)
April 14, 2023, 8:12am
2
You can add another selectattr
filter to filter by brightness like this:
{{ states.light | selectattr('state', 'eq', 'on') | selectattr('attributes.brightness', 'eq', 1) | list | count }}
I don’t have brightness_pct for my lights, so I can’t test if you can replace brightness with brightness_pct
Henke
(Henke)
April 14, 2023, 8:25am
3
Thanks, that’s one of the variations I actually tried but I get this error (in Template editor):
UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'brightness'
Olivier1974
(Olivier Toussaint)
April 14, 2023, 8:29am
4
Weird, because it works with me, in developer tools, to test, I have:
{{ states.light | selectattr('state', 'eq', 'on') | list | count }}
{{ states.light | selectattr('state', 'eq', 'on') | selectattr('attributes.brightness', '>=', 50) | list | count }}
And the result, when I turn one light at the minimum brightness is
1
0
Henke
(Henke)
April 14, 2023, 8:51am
5
Yeah, weird. Copied your exact code and still same error. I thought that it could perhaps be because I have some switches made into lights, so I tried to reject those with:
|reject('search', 'switch.tellus_plug|switch.wall| ....
But the error remains:
Olivier1974
(Olivier Toussaint)
April 14, 2023, 8:54am
6
Shouldn’t you put the reject regex before trying to read attributes?
Henke
(Henke)
April 14, 2023, 9:03am
7
No, I’d assume not. If I just remove the “new” line below, I get the correct number of lights on.
| selectattr('attributes.brightness', '>=', 50
EDIT: I incorrectly wrote switch instead of light on those entities. But still, same error.
Olivier1974
(Olivier Toussaint)
April 14, 2023, 9:13am
8
If you remove the count and look at the list here, you’ll probably see some entries that doesn’t have a brightness attribute.
These are the one you have to exclude before trying to read the brightness attribute. Or try to put a default
to assume their brightness.
Olivier1974
(Olivier Toussaint)
April 14, 2023, 9:22am
9
Or select the one with a brightness attribute
{{ states.light | selectattr('state', 'eq', 'on') | selectattr('attributes.brightness', 'defined') | selectattr('attributes.brightness', '>=', 50) | list | count }}
Not sure about that one, trying to help
Henke
(Henke)
April 14, 2023, 9:26am
10
Yes! That one (also) works! Marking that as the solution.
I did it like this before seeing your answer, which also works but feels more messy.
{{ states.light | selectattr('state', 'eq', 'on')
|reject('search', 'light.switches_that_were_made_into_lights....')
| selectattr('attributes.brightness', 'eq', 1)
| list | count }}
Thanks for all the help!
1 Like