Using Script Fields to provide entities for light.turn_on/off

Im trying to create script that I can reuse in different automations.
I want to use Fields to provide variables to script and have managed to do functioning conditions for IFs.
I have field where calling automation would provide one or more lights that would be turned off or to some specific brightness based on script logic.
Light.turn_on service how ever gives me following error:

“Error: Template rendered invalid entity IDs: {‘entity_id’: ‘light.wiz_rgbww_tunable_e8c724’}”

What I have now is:

service: light.turn_off
data: {}
target:
  entity_id: '{{ lights_to_control }}'

I have tried to google around and tested other things but so far havent got this one working.

Any help is welcome.

Post the automation’s code that calls the script and passes entity_ids via the variable lights_to_control.

This is automation that im trying this with:

alias: Remote - Kulma - Short Press
description: ""
trigger:
  - device_id: c02cabe09dfb7362d5d0712c04916de5
    domain: zha
    platform: device
    type: remote_button_short_press
    subtype: turn_on
condition: []
action:
  - service: script.lights_control_with_2_off_states
    data:
      condition_light:
        entity_id: light.wiz_rgbww_tunable_e8c724
      lights_to_control:
        entity_id: light.wiz_rgbww_tunable_e8c724
      off_state_condition:
        entity_id: light.keittio_katto_ikkuna_light
mode: single

And yes I have those other fields used and those seem to work.
Here is my full script if its any use:

alias: Lights Control With 2 Off States
sequence:
  - if:
      - condition: or
        conditions:
          - condition: template
            value_template: "{{ is_state('condition_light', 'off') }}"
          - condition: template
            value_template: >
              {{ states.light.keittio_katto_ikkuna_light.attributes.brightness <
              80 }}
    then:
      - service: light.turn_on
        data:
          kelvin: 3800
          brightness_pct: 75
        target:
          entity_id: "{{ lights_to_control }}"
    else:
      - if:
          - condition: template
            value_template: "{{ is_state('off_state_condition', 'on') }}"
        then:
          - service: light.turn_on
            data:
              kelvin: 3800
              brightness_pct: 25
            target:
              entity_id: "{{ lights_to_control }}"
        else:
          - service: light.turn_off
            data: {}
            target:
              entity_id: "{{ lights_to_control }}"
fields:
  condition_light:
    selector:
      target:
        entity:
          domain: light
    name: Condition Light
    description: Light thats state/brightness is used as condition
    required: true
  lights_to_control:
    selector:
      target:
        entity:
          domain: light
    name: Lights to Control
    required: true
    description: Lights to control
  off_state_condition:
    selector:
      target: null
    name: Off state condition
    description: Determines what is off state of lights
mode: queued
max: 10

In your automation, you defined the value of condition_light like this:

    data:
      condition_light:
        entity_id: light.wiz_rgbww_tunable_e8c724

You defined a dictionary with a single item consisting of a key named entity_id with an associated value of light.wiz_rgbww_tunable_e8c724. A dictionary is not what the script’s light.turn_on service call expects. It expects to receive a list so that’s why you got an error message.

Error: Template rendered invalid entity IDs:
 {'entity_id': 'light.wiz_rgbww_tunable_e8c724'}

Automation

alias: Remote - Kulma - Short Press
description: ""
trigger:
  - device_id: c02cabe09dfb7362d5d0712c04916de5
    domain: zha
    platform: device
    type: remote_button_short_press
    subtype: turn_on
condition: []
action:
  - service: script.lights_control_with_2_off_states
    data:
      condition_light: light.wiz_rgbww_tunable_e8c724
      lights_to_control:
        - light.wiz_rgbww_tunable_e8c724
      off_state_condition: light.keittio_katto_ikkuna_light
mode: single

In your script you did things like this:

         - condition: template
            value_template: "{{ is_state('condition_light', 'off') }}"

By wrapping condition_light in quotes, you converted it from the being the name of a variable to a string. So whatever value was passed by condition_light was never received by the Template Condition.

Script

alias: Lights Control With 2 Off States
sequence:
  - if:
      - condition: or
        conditions:
          - condition: template
            value_template: "{{ is_state(condition_light, 'off') }}"
          - condition: template
            value_template: >
              {{ state_attr('light.keittio_katto_ikkuna_light', 'brightness') < 80 }}
    then:
      - service: light.turn_on
        data:
          kelvin: 3800
          brightness_pct: 75
        target:
          entity_id: "{{ lights_to_control }}"
    else:
      - if:
          - condition: template
            value_template: "{{ is_state(off_state_condition, 'on') }}"
        then:
          - service: light.turn_on
            data:
              kelvin: 3800
              brightness_pct: 25
            target:
              entity_id: "{{ lights_to_control }}"
        else:
          - service: light.turn_off
            data: {}
            target:
              entity_id: "{{ lights_to_control }}"
fields:
  condition_light:
    selector:
      target:
        entity:
          domain: light
    name: Condition Light
    description: Light thats state/brightness is used as condition
    required: true
  lights_to_control:
    selector:
      target:
        entity:
          domain: light
    name: Lights to Control
    required: true
    description: Lights to control
  off_state_condition:
    selector:
      target: null
    name: Off state condition
    description: Determines what is off state of lights
mode: queued
max: 10

Thanks this works!

Had to change Fields in a script a bit. Removed target: so it works with GUI.
Also found a logic error in script :laughing:

Heres now working script in case anyone in future finds it anyways useful.

alias: Lights Control With 2 Off States
sequence:
  - if:
      - condition: or
        conditions:
          - condition: template
            value_template: "{{ is_state(condition_light, 'off') }}"
          - condition: template
            value_template: |
              {{ state_attr(condition_light, 'brightness') < 80 }}
    then:
      - service: light.turn_on
        data:
          kelvin: 3800
          brightness_pct: 75
        target:
          entity_id: "{{ lights_to_control }}"
    else:
      - if:
          - condition: template
            value_template: "{{ is_state(off_state_condition, 'on') }}"
        then:
          - service: light.turn_on
            data:
              kelvin: 3800
              brightness_pct: 25
            target:
              entity_id: "{{ lights_to_control }}"
        else:
          - service: light.turn_off
            data: {}
            target:
              entity_id: "{{ lights_to_control }}"
fields:
  condition_light:
    selector:
      entity:
        domain: light
    name: Condition Light
    description: Light thats state/brightness is used as condition
    required: true
  lights_to_control:
    selector:
      entity:
        multiple: true
        domain: light
    name: Lights to Control
    required: true
    description: Lights to control
    default: light.eteinen_katto1_light
  off_state_condition:
    selector:
      entity: null
    name: Off state condition
    description: Determines should lights go off or dimm down.
mode: queued
max: 10