that template will work but you need to list all lights inside the entity_id field so the system knows when to update the sensor. See entity_id field in the docs. And here is the explanation why.
Thanks @petro for you fast response.
Sorry but I don’t understand.
I tried also with this but nothing, so I suppose that something wrong:
{% set domain = 'light' %}
{% set state = 'on' %}
{% set count = states[domain] | selectattr('state','eq', state) | map(attribute='entity_id') | list | count == 0%}
I have ti put every single ID? In this case, where I have to put?
Please, can you give me an example with two lights (ie: light_1, light_2).
Thanks for your patience
Ok @petro, sorry.
I clicked only second link.
Now it works.
So I understand that for every single light that I add, I have to add manualy to the sensor.
It’s right?
There isn’t a way to do it automatically? (ie: like the olda group.all_lights)
Thansk for your support
Unfortunately no, not when you use it in a template sensor. This is a restriction of the template sensor. It requires a list of entities to detect changes on. If you provide it a group, it will only use the group. Not the entities inside the group.
I’ve been following this post carefully and still have a problem. I would like to create a group with several entities and not the entire domain. I have five entities from the light domain from which I want to form a group when they are switched off. That should be mapped in a script. I use the following script for the domain light which also works. But how do I get the entities built in without the complete domain?
light_1
light_2
light_3
light_4
light_5
alias: Group.set
sequence:
- service: group.set
data_template:
object_id: test_lights
entities: >-
{% set domain = 'light' %} {% set state = 'off' %} {{ states[domain] |
selectattr('state','eq', state) | map(attribute='entity_id') | list |
join(', ') }}
mode: single
I want to create a group with some (five) entities with the state off. By using the above example I receive the group test_lights with the complete domain light.
My goal is to switch ON the light which has the status OFF. After three minutes these lights should be switched OFF with the help of a script and a delay or timer.
Light that was switched ON beforehand remains unaffected by the delay / timer. This works very well with this script. In this script I create a temporary group.test_lights which is then operated via the delay / timer. Now I asked myself whether this could be simplified and the temporary group group.test_lights could be dispensed with? Maybe it could be easier?
Very well! It works with using variables.
Now there is a little problem. If all lights of the group are ON, there is an error message:
- Group.set: Error executing script. Invalid data for call_service at pos 1: Entity ID is an invalid entity id for dictionary value @ data['entities']
- not a valid value for dictionary value @ data['entity_id']
If at least one lamp of the group is switched OFF, it works properly.
alias: example 1
variables:
lights_off: >
{% set x = expand('group.living') | selectattr('state','eq','off')
| map(attribute='entity_id') | list %}
{{ x | join(', ') if x | length > 0 else 'none' }}
sequence:
- service: light.turn_on
data:
entity_id: "{{ lights_off }}"
- delay: '00:03:00'
- service: light.turn_off
data:
entity_id: "{{ lights_off }}"
mode: single
When all lights were already on, the template produced an empty string which is unacceptable for entity_id. The revised version produces none which is understood to mean no entities. The services will simply ‘do nothing’.