There are probably better solutions but you can create your own array of devices you want to combine. It’s however something you need to update manually all the time.
I for example use it to only the spots in my bathroom to display how many spots are on. Its a bit the reverse of what you want however as I use it to limit and not count all lights.
If that is an option and useful for you, you could change the device_class and change them all to lights. I did that with some contact-sensors classed as ‘door’ while they are on a window.
I would like to modify it so it also count switches that have an entity id that follows the pattern switch.*fan Could someone show me how to modify the template to achieve this? Thanks!
The template you posted selects alllight entities that are on and then excludes Light Group entities and reports the quantity.
If you want to count the Light Group entities (that are on) but not their members, you have to identify all the group members and then exclude them from the list of all lights.
Copy-paste the following template into the Template Editor and see if it works the way you want:
value_template: >-
{% set members = states.light
| selectattr('attributes.entity_id', 'defined')
| expand | map(attribute='entity_id') | list %}
{{ states.light | selectattr('state', 'eq', 'on')
| map(attribute='entity_id')
| reject('in', members) | list }}
That’s odd. If the expand filter works in the Template Editor it shouldn’t cause this error when it appears in a Template Sensor: TemplateAssertionError: No filter named 'expand'.
I’ve confirmed that if expand is used as a function then it passes Check Configuration.
value_template: >-
{% set members = expand(states.light | selectattr('attributes.entity_id', 'defined'))
| map(attribute='entity_id') | list %}
{{ states.light | selectattr('state', 'eq', 'on')
| map(attribute='entity_id')
| reject('in', members) | list }}
EDIT
When the Template Sensor is configured using legacy format, Check Configuration doesn’t accept expand used as a filter (only as a function). However, it the Template Sensor is configured using modern format, Check Configuration accepts expand used either as a function or filter.
Nope. Upon further investigation, expand when used a filter is rejected in either a legacy or modern format Template Sensor. The only place it works as a filter is in the Template Editor.
I’ve reported this unexpected behavior as an Issue in the GitHub Core repo.
The code works great in the template section in developer tools, but when I add it to the template sensors in my yaml, the sensor doesn’t show up even after reloading the template entities.
I see that it’s supposed to get fixed in the next release, but that’s already been more than a month ago and I’m wondering if the 2022.10.5 update is supposed to fix that
So is it going to get fixed eventually or should I look for alternative template code? Cause right now the code works great in the template section of dev tools, but if there’s no plans to make it work in the HA config.yaml then I need to search for alternative.
Hello to all. Thank you for this posts, I’m looking for configure my lights but maybe I’ve not understand how to do.
I’ve some lights in my open_space and other in my kitchen. I want to count thats lights on in two different sensors. I’ve try this code but have two error: