Script to turn off lights based on area and labels does not work as expected

I want to turn off all lights in the front and backyard.
One lamp excluded, a security lamp that turns on movement.

so i made a script to turn off all lamps in the two areas.
I tagged all lights with tag lampen except the security lamp.
And i used the tag in the script.
But still the security light goes off!

i expected an or within areas and label_id, but an and between label_id and area_id

          - action: light.turn_off
            target:
              label_id:
                - lampen
              area_id:
                - achtertuin
                - voortuin
            data: {}
            enabled: true

What are those expectations based on?

I’m not even sure I understand what “an or within areas and label_id” actually means. Should only one Area’s lights be turned off if multiple are listed?

"Turn the kitchen lights OR the bathroom lights off... it doesn't really matter which one"


Targets follow simple AND logic. If you don’t want every light entity in an Area affected, don’t target that Area with a light action.

Since you have labelled everything but the security light, you should only need to target the Label:

- action: light.turn_off
  target:
    label_id:
      - lampen

… unless there are also lights with that Label but in other Areas that shouldn’t turn off, then you would need to use templating to get the intersection of the lists.

- action: light.turn_off
  target:
    entity_id: |
      {{ ['achtertuin', 'voortuin'] | map('area_entities')
      | flatten | select('match', 'light')
      | select('in', label_entities('lampen')) | list }}  

Or, just use a new label for this new use case.

The last one did the trick!

I can now turn off all the lights in two areas accept the security lights that dont have the label lampen and still are part of the areas