Script variable within an area expand template

Hi All,

I’m building a script to standardise the behavior across all my hue switches. So i’m building a script that can be called on an action from any switch. The service will pass on the area and the action (On Press, Dim Press, Off Press, etc…) via fields / variables. Basic On/Off works fine.

Where i am stuck, is i have a template that works currently which expands all lights in an area that are on in a list, so that only the lights that are on will be dimmed. I can’t seem to work out how to put the area/variable into the expand template without getting an error.

Fields are set as follows:

fields:
  area:
    selector:
      target: {}
    name: Area
    description: Target Area
    required: true
  step_value:
    selector:
      number:
        min: -10
        max: 10
        step: 1
    name: step value
    required: false
  type:
    selector:
      text: null
    name: Type
    required: true

Here is the part where i’m trying to embed the area into the expand template:

      - conditions:
          - condition: template
            value_template: "{{ type == 'dim down' }}"
        sequence:
          - service: light.turn_on
            data:
              transition: 1
              brightness_step_pct: "{{ step_value }}"
            target:
              entity_id: |-
                {{ expand(area_entities(area)) | selectattr('domain', 'eq',
                        'light') | selectattr('state', 'eq', 'on') |
                        map(attribute='entity_id') | list }}

Full script here:

alias: Test Dim Script
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ type == 'off' }}"
        sequence:
          - service: light.turn_off
            data: "{{ area }}"
      - conditions:
          - condition: template
            value_template: "{{ type == 'on' }}"
        sequence:
          - service: light.turn_on
            data: "{{ area }}"
      - conditions:
          - condition: template
            value_template: "{{ type == 'dim down' }}"
        sequence:
          - service: light.turn_on
            data:
              transition: 1
              brightness_step_pct: "{{ step_value }}"
            target:
              entity_id: |-
                {{ expand(area_entities(area)) | selectattr('domain', 'eq',
                        'light') | selectattr('state', 'eq', 'on') |
                        map(attribute='entity_id') | list }}
mode: single
fields:
  area:
    selector:
      target: {}
    name: Area
    description: Target Area
    required: true
  step_value:
    selector:
      number:
        min: -10
        max: 10
        step: 1
    name: step value
    required: false
  type:
    selector:
      text: null
    name: Type
    required: true

You are using a target selector in the area variable… use an area selector.

alias: Test Dim Script
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ type == 'off' }}"
        sequence:
          - service: light.turn_off
            data: {}
            target:
              area_id: "{{ area }}"
      - conditions:
          - condition: template
            value_template: "{{ type == 'on' }}"
        sequence:
          - service: light.turn_on
            data: {}
            target:
              area_id: "{{ area }}"
      - conditions:
          - condition: template
            value_template: "{{ type == 'dim down' }}"
        sequence:
          - service: light.turn_on
            data:
              transition: 1
              brightness_step_pct: "{{ step_value }}"
            target:
              entity_id: |-
                {{ area_entities(area) | select('is_state', 'on') | select('search', 'light\.') | list }}
mode: single
fields:
  area:
    selector:
      area:
    name: Area
    description: Target Area
    required: true
  step_value:
    selector:
      number:
        min: -10
        max: 10
        step: 1
    name: step value
    required: false
  type:
    selector:
      text: null
    name: Type
    required: true

Thanks, that makes sense.

Although i’m now getting “template value is None @ data[‘area_id’][0]”

I’ll keep plugging away.

Got it! Thanks for the help. I found your other post here: How to toggle all lights in an Area (or Group)? - #6 by Didgeridrew

You can’t embed templates within templates. I had to replace:

area_id: "{{ area_id(area) }}"

With:

area_id: "{{ area }}"
1 Like

Yep, for some reason I was thinking the selector returned the area name. I’ve corrected the post above.