Skip to content

๐Ÿ’ก Area Lights with Exclusions โ€‹

Controls all light entities inside a target Home Assistant area (turn_on or turn_off), with optional exclusions by entity or label, optional brightness and transition, and a response payload listing the affected lights.

โš™๏ธ Parameters โ€‹

FieldSelectorRequiredDefaultDescription
areaareaYesTarget area to pull light entities from.
operationselectNoturn_offAction to perform (turn_off or turn_on).
brightness_pctnumberNoBrightness percentage (1 to 100) when turning lights on.
transitionnumberNoFade duration in seconds (0 to 60).
exclude_labelstextNoComma-separated label names whose entities should be skipped.
exclude_entitiesentityNoExplicit list of light entities to skip (takes priority over labels).

๐Ÿ” How It Works โ€‹

  1. Expands all entities in area and keeps only the light domain.
  2. Resolves exclusions: if exclude_entities is non-empty, it is used directly; otherwise, exclude_labels is split by comma and resolved via label_entities.
  3. Only calls light.turn_on or light.turn_off if at least one target light remains after filtering, passing brightness_pct and transition only when provided.
  4. Ends with response_variable: result, returning area, operation, lights_in_area, excluded_entities, affected_entities, and lights_turned_off.

๐Ÿงช Example Call โ€‹

yaml
action: script.area_lights_control_with_exclusions
data:
  area: living_room
  operation: turn_off
  transition: 2
  exclude_labels: always_on, night_light

๐Ÿ“œ Full Script YAML โ€‹

Import in Home Assistant

Go to Settings -> Automations & Scenes -> Scripts -> Add Script -> Edit in YAML and paste the configuration below. Use the language selector to switch between English and Italian.

Area Lights with ExclusionsYAML
alias: ๐Ÿ’ก Area lights control with exclusions
description: >-
  Controls (turns on or off) all lights inside a specified area,

  optionally excluding:


  โ€ข all entities matching specific labels  

  โ€ข or an explicit list of entities (takes priority)


  ๐Ÿ“Œ Parameters:

  - Area: target area to select lights from (required)

  - Operation: turn_off (default) or turn_on (optional)

  - Brightness (%): optional brightness percentage when turning on (keeps the
  previous state if omitted)

  - Transition: fade duration in seconds (optional)

  - Labels to exclude: comma-separated labels (optional)

  - Entities to exclude: manual list of excluded entities (optional)


  ๐Ÿ” Returns:

  โ€ข final list of affected lights
icon: mdi:lightbulb-multiple-outline
mode: single
fields:
  area:
    name: Area
    description: ๐Ÿ  Target area to select lights from
    required: true
    selector:
      area: {}
  operation:
    name: Operation
    description: 'โšก Choose whether to turn lights on or off (optional, default: Turn off)'
    required: false
    default: turn_off
    selector:
      select:
        options:
          - label: ๐Ÿ”Œ Turn off
            value: turn_off
          - label: ๐Ÿ’ก Turn on
            value: turn_on
  brightness_pct:
    name: Brightness (%)
    description: >-
      ๐Ÿ”† Brightness percentage to apply when turning on (optional: if omitted,
      lights turn on at their previous brightness)
    required: false
    selector:
      number:
        min: 1
        max: 100
        unit_of_measurement: '%'
  transition:
    name: Transition (seconds)
    description: โฑ๏ธ Fade transition duration in seconds (optional)
    required: false
    selector:
      number:
        min: 0
        max: 60
        unit_of_measurement: s
  exclude_labels:
    name: Labels to exclude
    description: ๐Ÿท๏ธ Comma-separated list of labels to exclude (optional)
    required: false
    selector:
      text:
        multiline: false
  exclude_entities:
    name: Entities to exclude
    description: >-
      ๐Ÿšซ Explicit list of entities to exclude (optional, takes priority over
      labels)
    required: false
    selector:
      entity:
        multiple: true
        filter:
          domain: light
sequence:
  - alias: ๐Ÿงฎ Compute area lights and exclusions
    note: >-
      Filters all light entities belonging to the area, resolves exclusions,
      and builds the final target list
    variables:
      operation_mode: '{{ operation | default(''turn_off'') }}'
      lights_in_area: |
        {{ expand(area_entities(area))
           | selectattr('domain', 'equalto', 'light')
           | map(attribute='entity_id')
           | list }}
      entities_to_exclude: |
        {% if exclude_entities is defined and exclude_entities | count > 0 %}
          {{ exclude_entities }}
        {% elif exclude_labels is defined and exclude_labels | length > 0 %}
          {{ exclude_labels.split(',')
             | map('trim')
             | map('lower')
             | map('label_entities')
             | sum(start=[]) }}
        {% else %}
          []
        {% endif %}
      final_entities: |
        {{ lights_in_area
           | reject('in', entities_to_exclude)
           | list }}
      result:
        area: '{{ area }}'
        operation: '{{ operation_mode }}'
        lights_in_area: '{{ lights_in_area }}'
        excluded_entities: '{{ entities_to_exclude }}'
        affected_entities: '{{ final_entities }}'
        lights_turned_off: '{{ final_entities }}'
  - alias: ๐Ÿšฆ Check target entities and send light command
    note: >-
      Sends the command only if at least one light entity remains after applying
      exclusion filters
    if:
      - alias: ๐Ÿ” Check non-empty entity list
        note: >-
          Verifies that the final list of target lights contains at least one
          entity
        condition: template
        value_template: '{{ final_entities | count > 0 }}'
    then:
      - alias: ๐Ÿ”€ Dispatch command by operation
        note: >-
          Selects and executes either turn_on or turn_off based on
          operation_mode
        choose:
          - alias: ๐Ÿ’ก Turn on area lights (turn_on)
            note: >-
              Turns on target lights, applying brightness and transition only if
              explicitly provided
            conditions:
              - alias: โšก Requested operation equals turn_on
                note: Checks if the requested operation matches turn_on
                condition: template
                value_template: '{{ operation_mode == ''turn_on'' }}'
            sequence:
              - alias: ๐Ÿ’ก Send turn_on with optional parameters
                note: >-
                  Calls light.turn_on with the filtered entity list, including
                  brightness_pct and transition only when set
                action: light.turn_on
                target:
                  entity_id: '{{ final_entities }}'
                data: >-
                  {% set b = brightness_pct | int if (brightness_pct is defined
                  and brightness_pct is not none and (brightness_pct | string |
                  trim) != '') else none %} {% set t = transition | int if
                  (transition is defined and transition is not none and
                  (transition | string | trim) != '') else none %} {% if b is
                  not none and t is not none %}
                    {{ {'brightness_pct': b, 'transition': t} }}
                  {% elif b is not none %}
                    {{ {'brightness_pct': b} }}
                  {% elif t is not none %}
                    {{ {'transition': t} }}
                  {% else %}
                    {{ {} }}
                  {% endif %}
          - alias: ๐Ÿ”Œ Turn off area lights (turn_off)
            note: >-
              Turns off target lights, applying the optional transition if
              provided
            conditions:
              - alias: โšก Requested operation equals turn_off
                note: >-
                  Checks if the requested operation matches turn_off or the
                  default value
                condition: template
                value_template: '{{ operation_mode == ''turn_off'' }}'
            sequence:
              - alias: ๐Ÿ”Œ Send turn_off with optional transition
                note: >-
                  Calls light.turn_off with the filtered entity list and the
                  optional transition parameter
                action: light.turn_off
                target:
                  entity_id: '{{ final_entities }}'
                data: >-
                  {% set t = transition | int if (transition is defined and
                  transition is not none and (transition | string | trim) != '')
                  else none %} {% if t is not none %}
                    {{ {'transition': t} }}
                  {% else %}
                    {{ {} }}
                  {% endif %}
  - alias: ๐Ÿ Finish script and return response
    note: >-
      Completes script execution and returns the result object containing the
      list of modified entities
    stop: Script completed
    response_variable: result