For groups of most things, the value of the group is a result of ORing the values of it’s entities, groups of devices are ‘home’ if one of the devices are ‘home’ and groups of switches are ‘on’ if one of the group is one.
However I have made a group of template sensors and the value of the group is always ‘unknown’
I use template sensors to make sure my various PIR sensors all report the same when they’re activated, here’s the config for 3 z-wave PIRs:
As for your question at hand. I’m not sure a group of sensors propigate to the group state. With that being said, another method to try would be this template trigger. Mind you, you cannot use the group because you need to get the entity objects to perform selectattr.
- alias: "TRIGGER the alarm when a PIR detects movement."
trigger:
platform: template
value_template: >
{% set group = [ states.sensor.guest_motion, states.sensor.kitchen_motion, states.sensor.lounge_motion, states.sensor.office_motion ] %}
{% set output = group | selectattr('state', 'equalto', True) | list %}
{{ output | length == group | length }}
Unfortunately, you have to hard code the names in the ‘set group’ because they need to be objects for selectattr.
What this does is filters your list to only contain items with a state that is true. If the lenght of the 2 lists are not equal, then one of the items is not true and therefore will not trigger.
I think I could create a sensor using that same template:
- platform: template
sensors:
motion_detected:
friendly_name: "Motion detected by any sensor"
value_template: >
{% set group = [ states.sensor.guest_motion, states.sensor.kitchen_motion, states.sensor.lounge_motion, states.sensor.office_motion ] %}
{% set output = group | selectattr('state', 'equalto', True) | list %}
{{ output | length == group | length }}
and while they are hard coded, it’s only the same as adding them to thr group, and I still only have to change the config in 1 place when I add more PIRs.
I’ll try it and let you know how I get on … Thanks @petro
- platform: template
sensors:
motion_detected:
friendly_name: "Motion detected by any sensor"
value_template: >
{% set group = [ states.sensor.guest_motion, states.sensor.kitchen_motion, states.sensor.lounge_motion, states.sensor.office_motion ] %}
{% set output = group | selectattr('state', 'equalto', True) | list %}
{{ output | length == group | length }}
set group = [etc] needs the separate entities, creating a list, but Id like to use a group here I already have. Group.motion_sensors. If I could use that group in this template sensor, I would not have to change the logic of the sensor, because the motion sensors wouldn’t be hard coded?
using the variable Last motion right now, but this could be a valid alternative.
I don’t think there is a way because of how jinja works. It’s really annoying but the looping scope causes issues when trying to access a groups objects directly and to have the functionality seen above.
The problem is here:
We need a list, we cannot use for loop because that is iterating through the list.
states.group.mygroup.attributes.entity_id returns a tuple of strings. In order to use the selectattr() filter, we need to have an actual object. Well it is not possible to get the tuple of strings into a tuple of objects in jinja without using a for loop.
So your only option is to hard code the objects in the value_template.
i see, thanks. So it is possible with a for loop? That might be another option to try then?
bw, if I fill out my individual sensor entities here, the value remains False, while waving actively
yep, you can use a for loop but that template won’t behave properly with a for loop because of the scope limitations of jinja. It’s pretty much a catch-22.
A you see, this also needs the sensors to be hardcoded, hence my desire to try differently.
and as mentioned, if I use these sensors in the template motion_detected, nothing happens and the value remains False…having changed to selectattr(‘state’, ‘equalto’, ‘on’) since my sensors have that state
I did not know that ‘in’ was an option for selectattr. Nore did I think about using it against the state object… so with that being the case, this is what the jinja would look like instead of hard coding the motion devices.
- platform: template
sensors:
motion_detected:
friendly_name: "Motion detected by any sensor"
value_template: >
{% set group = states | selectattr('entity_id', 'in', state_attr('group.motion', 'entity_id')) | list %}
{% set output = group | selectattr('state', 'equalto', True) | list %}
{{ output | length == group | length }}
The example above is to see if all motion sensors are tripping. Translating that to ‘everyone being home’ requires True to change to ‘Home’ or ‘home’ (not sure which one).
- platform: template
sensors:
everyone_home:
friendly_name: "Sensor that tells me if Everyone is home"
value_template: >
{% set all_people = states | selectattr('entity_id', 'in', state_attr('group.family', 'entity_id')) | list %}
{% set home_people = all_people | selectattr('state', 'equalto', 'home') | list %}
{{ all_people | length == home_people | length }}