Turning off all the lights in a group except the one turned on last

I created a group of lights, and I want to turn them on exclusively.
That means, each time a group’s light is turned on (by sensor) all other lights of the group must be turned off.
I managed to find out which is the last turned on, but it can’t figure out how to turn off all other lights in the group.
This is what I did so far:

- sensor:

  - name: ultima_luce_notturna_accesa

    state: >-

      {%

        set luci_accese =

          states.light |

          selectattr('state', 'eq', 'on') |

          list

      %}

      {% for spy in luci_accese %}

        {% if as_timestamp(spy.last_changed) == as_timestamp(spies | map(attribute='last_changed') | max) %}    

            {{ area_name(spy.entity_id) }}

        {% endif %}

      {% endfor %}

You’ve left out too much detail… Are your lights grouped by area? Are you using Helper groups or Legacy groups? Are you trying to leave only an individual light on or all of an area’s lights?

Here is a basic method, but you will need to clarify how you want it to behave and how you’re grouping them for us to complete the template.

alias: Entrained Lights (only 1 on at a time)
description: ''
trigger:
  - platform: state
    entity_id:
      - light.group_light_1
      - light.group_light_2
      - light.group_light_3
    to: 'on'
condition: []
action:
  - service: light.turn_off
    target:
      entity_id: '{{ inactive_lights }}'
variables:
  inactive_lights: >-
    {{ expand( ? ) 
    | rejectattr('entity_id', 'eq', trigger.to_state.entity_id)
    | map(attribute='entity_id') | list -}}
mode: single

Hi Didgeridrew,

lights aren’t grouped by area. I used a helper group and I’m trying to leave only an individual light.
I’ll try later to understand your code, because I’m not getting it all.
Thank you for the quick tip!

Didgeridrew,

I put my light’s group as the trigger entity, and replaced the question mark in the expand function.
Now I get this error:

Unexpected error calling config validator: ‘NoneType’ object is not iterable

The list is correct, but I don’t know how display what’s in trigger.to_state.entity_id to see if it gets the right value.
Could you help me a lttle further, I’m also confused by all the double/single quotes.

alias: Entrained Lights (only 1 on at a time)
description: ""
trigger:
  - platform: state
    entity_id:
      - light.luci_notturne
    to: "on"
condition: []
action:
  - service: light.turn_off
    target:
      entity_id: "{{ inactive_lights }}"
variables:
  inactive_lights: >-
    {{ expand( light.luci_notturne )  | rejectattr('entity_id', 'eq',
    trigger.to_state.entity_id) | map(attribute='entity_id') | list -}}
mode: single

That is because the light group is not a member of itself. You need to have the individual light entities listed in the trigger, not the light group. The template that defines the lights to turn off takes a list of all the light entities in the group then removes the entity that was turned on.

For helper-defined groups we do not need to use the expand() function because those entities include an attribute that lists the lights included in the group.

alias: Entrained Lights (only 1 on at a time)
description: ""
trigger:
  - platform: state
    entity_id:
      - light.luci_esempio_1
      - light.luci_esempio_2
    to: "on"
condition: []
action:
  - service: light.turn_off
    target:
      entity_id: "{{ inactive_lights }}"
variables:
  inactive_lights: >-
    {{ state_attr('light.luci_notturne', 'entity_id') 
    | reject('eq', trigger.to_state.entity_id) | list -}}
mode: single

It worked like a charme!
Thanks a lot Didgeridrew.