Changing group behaviour to ALL home to be ON

Probably a dumb question but I can’t find how to do this.

Usually when one item of a group is on or ‘home’, the entire group is on or ‘home’. Can this be changed so its only on or ‘home’ when ALL items are on or ‘home’?

Thanks

Yes. You can modify the code. :slight_smile:

Short of that, you can use a template to determine if all items in a group are ‘on’ or ‘home’. E.g., in the case of group.all_devices (which is simply all device_tracker entities), you could do:

{{ states.device_tracker|selectattr('state','ne','home')|list|count == 0 }}

This will be true when the state of all device_trackers are ‘home’ (i.e., when none are not ‘home’.)

If you wanted to do the same for a random group, then you could do something like this:

{{ states|selectattr('entity_id','in',state_attr('group.my_group','entity_id'))
     |selectattr('state','ne','on')|list|count == 0 }}

This will return true when the state of all entities in group.my_group are ‘on’ (i.e., when none are not ‘on’.)

Now, having said this, it may depend on how you want to use this information. E.g., if you want it to trigger an automation, or create a template sensor, then these templates probably won’t work because the parser won’t be able to determine which entities to watch for state changes. But there are other ways to handle those situations.

If you’re asking with a particular use case in mind, then maybe explaining what you’re ultimately trying to achieve might help so we can provide a more specific suggestion.

Thanks for your reply.

Actually this is not for device_trackers this is for a group of switches for my CCTV. I want the group to only be ‘ON’ when all of them are switched on. This then tells me my CCTV system armed and motion detection is on for all of them rather than individually.

Group:
    CCTV Arm:
      - switch.cctv_driveway
      - switch.cctv_front
      - switch.cctv_garden

So I think that makes it:

{{ states|selectattr('entity_id','in',state_attr('group.cctv_arm','entity_id'))
     |selectattr('state','ne','on')|list|count == 0 }}

But trying to put that into my sensors with this

sensors:
  cctv_status:
    friendly_name: 'CCTV'
    value_template: >
      {% if is_state({{ states|selectattr('entity_id','in',state_attr('group.cctv_arm','entity_id')) | selectattr('state','ne','on') | list|count == 0 }}, 'True') %}
        Armed
      {% else %}
        Off
      {% endif %}

Is not working. Any ideas?
Thanks

Yes, as I said, “if you want it to trigger an automation, or create a template sensor, then these templates probably won’t work because the parser won’t be able to determine which entities to watch for state changes.”

The only way I can think of to make this work in a template sensor is to list the entities explicitly. You can do this two ways. If you want to use the template, then you’ll need to list the entities in the entity_id parameter. That would look like this:

sensor:
  - platform: template
    sensors:
      cctv_status:
        friendly_name: CCTV
        value_template: >
          {% if states.switch
                  |selectattr('entity_id','in',state_attr('group.cctv_arm','entity_id'))
                  |selectattr('state','ne','on')|list|count == 0 %}
            Armed
          {% else %}
            Off
          {% endif %}
        entity_id:
          - switch.cctv_driveway
          - switch.cctv_front
          - switch.cctv_garden

If you change group.cctv_arm you unfortunately have to update the entity_id list here, too.

The second way (as long as you have to specify each item in the group anyway) is to just use them directly in the template:

sensor:
  - platform: template
    sensors:
      cctv_status:
        friendly_name: CCTV
        value_template: >
          {% if is_state('switch.cctv_driveway','on') and
                is_state('switch.cctv_front','on') and
                is_state('switch.cctv_garden','on') %}
            Armed
          {% else %}
            Off
          {% endif %}

I really can’t think of a better way to do this in YAML (i.e., not having to repeat the list of entities that are in the group.)

The main problem is the sensor needs to know which entities to monitor for state changes so it can update when one changes. It tries to figure this out automatically when parsing the template. But if it can’t figure it out (like in the case when the actual entity_id’s are not used directly), then you have to explicitly list the entities to monitor.

2 Likes

Awesome thanks, gone the easy route and went for

sensor:
  - platform: template
    sensors:
      cctv_status:
        friendly_name: CCTV
        value_template: >
          {% if is_state('switch.cctv_driveway','on') and
                is_state('switch.cctv_front','on') and
                is_state('switch.cctv_garden','on') %}
            Armed
          {% else %}
            Off
          {% endif %}

Works great! Thanks again.

1 Like