Exclude Entities: Turn on/off everything, except X

I think, the all_group-feature should be mimicable with the recent addition by @jhenkens to my script linked above…

This works for me:

Maybe somebody is looking to switch off lights, switches etc… I was looking for a good way and now I’ve found this too… but in case somebody finds this useful with the formatting…

  - service: light.turn_off
    data_template:
      entity_id: >-
        {% set domain = 'light' %} 
        {% set state = 'on' %}    
        {% set compare = 'eq' %}    
        {% set filter =['nothing to exclude'] %} 
        
        {% set MyVal= states[domain] | selectattr('state',compare, state) 
        |  rejectattr('entity_id','in', filter) 
        |  rejectattr('attributes.is_hue_group', '==' , true) 
        |  map(attribute='entity_id') 
        | join(',') 
        %}

        {% if MyVal.count('.') >=1 %}
        {{ MyVal }}
        {% else %}
        light.dummy
        {% endif %}
  - service: switch.turn_off
    data_template:
      entity_id: >-
        {% set domain = 'switch' %} 
        {% set state = 'unavailable' %}    
        {% set compare = 'ne' %}    
        {% set filter =['switch.57281806d8bfc004de91_1'] %}
        
        {% set MyVal= states[domain] | selectattr('state',compare, state) 
        |  rejectattr('entity_id','in', filter) 
        |  rejectattr('attributes.is_hue_group', '==' , true) 
        |  map(attribute='entity_id') 
        | join(',') 
        %}

        {% if MyVal.count('.') >=1 %}
        {{ MyVal }}
        {% else %}
        switch.dummy
        {% endif %}
  - service: media_player.turn_off
    data_template:
      entity_id: >-
        {% set domain = 'media_player' %} 
        {% set state = 'off' %}    
        {% set compare = 'ne' %}    
        {% set filter =['nothing-to-exclude'] %}
        
        {% set MyVal= states[domain] | selectattr('state',compare, state) 
        |  rejectattr('entity_id','in', filter) 
        |  rejectattr('attributes.is_hue_group', '==' , true) 
        |  map(attribute='entity_id') 
        | join(',') 
        %}

        {% if MyVal.count('.') >=1 %}
        {{ MyVal }}
        {% else %}
        media_player.dummy
        {% endif %}

It should be self explaining. At the beginning you tell what Domain, eq or ne of status, and an exception list. I’m able to switch off all lights except them in the bedroom. I hope somebody finds this useful too.
With the mediaplayers I’m not finished at the moment it works, but they have playing, pause on off etc… so checking for on is not usefull… so I’m checking for not off

3 Likes

Cool! Question: How do we format multiple entities in the exclude list? Also, can you explain what the ‘in’ and ‘hue’ references do here? rejectattr('entity_id','in', filter) | rejectattr('attributes.is_hue_group', '==' , true)

Sorry, I’m a complete noob with templating.

OK, the HUE-GROUP is only for lights important. in HUE you can group and if you turn a group off, all lights will go off. So I exclude them all. [ ‘multi1’,‘multi2’,‘multi3’ ] it’s too easy and that’s the reason I like this solution, hope that helps

I would love this as well.
At night I turn off all switches (about 30) but I have one I want to exclude. I would like to use the all switches group and just exclude the one specific switch instead of turning off all switches at the same time.
An other option would be a group of groups except the specific entity.

1 Like

This is super frustrating. The ability to create custom groups where we can include whatever we’d like. That a light can only be part of one room/group (outside of hue – I use smart dimmers/switches) is sad.

My solution was to remove the device from the home. Now when I say Turn off all lights, it excludes those.

Pain in the ass either way. Not elegant.

Eco’ing onto the chain of this definitely should exist.