Turn off all lights except one

I have tried to implement this solution: Is there a way to adress 'all' 'except' to turn lights off - #2 by tom_l
This will list and turn off the group of lights the light belongs to. So you will end up in the dark no matter what.
Is there any solution for this?

This is my ‘all but’ motion sensor. You can adapt it easily for your needs. Shout if you need any help.

{% set except = 'study_motion_occupancy' %}
{{ states.binary_sensor
  | selectattr('entity_id','match','.*_motion_occupancy')
  | selectattr('state','eq','on')
  | rejectattr('entity_id','match','binary_sensor.'~except)
  | list
  | count > 0
  }}

You can turn lights off per area also. By selecting all areas and in the area where one light need to not turn off, then you select per lights for that area only.

data: {}
target:
  area_id:
    - garage
    - cave
    - tv
    - etc
action: light.turn_off

Finally figured out how to handle groups too

{% set exclude_light = 'light.oprit_lichten' %}
{% set exclude_groups = states.light 
    | selectattr('attributes.entity_id', 'defined')
    | selectattr('attributes.entity_id', 'contains', exclude_light) 
    | map(attribute='entity_id') 
    | list %}
{% set all_exclude = [exclude_light] + exclude_groups %}
{{ states.light 
    | selectattr('state', 'eq', 'on') 
    | rejectattr('entity_id', 'in', all_exclude) 
    | map(attribute='entity_id') 
    | join(', ') }}

1 Like

glad you got it sorted!

[edit: posted an incorrect version earlier, so this is my 2nd edit]

Here is mine again as a macro (in custom_templates/exceptions.jinja) so it can be easily repeated multiple times.

{% macro binary_sensor(common_element,exception,state) %}
{{ states.binary_sensor
  | selectattr('entity_id','search',common_element)
  | selectattr('state','eq',state)
  | rejectattr('entity_id','search',exception)
  | list
  | count }}
{% endmacro %}

and to call it:

{% from 'exceptions.jinja' import binary_sensor %}
{{ binary_sensor('motion_occupancy','study', 'on') }}