Using Target Selector in first script and passing to second helper script

Hi

I’ve read the documentation a few times and tried various things but to no avail.

I’d like to start making use of fields in my scripts in particular the selector target so it shows in the UI. Id like to pass this to a second script which is a helper script to transition the lights correctly due to a light limitation.

here is what i have so far.

script 1

scene_test:
  alias: "Activate Scene Focus Test"
  description: "Activates Focus Scene on Tradfri Bulb"
  fields:
    entities:
      description: "Device or Devices to active scene on"
      example: "light.lounge_main_light"
      selector:
        target:
          entity:
            domain: light
  sequence:
    - service: script.turn_on_ikea_light_test
      target: "{{ entities }}"
      data:
        brightness_pct: 100
        color_temp: 250
        transition: 2

script 2 - helper script

turn_on_ikea_light_test:
  sequence:
    # Set the brightness of the lights.
    - service: homeassistant.turn_on
      target: "{{ entities }}"
      data:
        brightness_pct: "{{ brightness_pct | int }}"
        transition: "{{ transition | int / 2 }}"
    - delay: "00:00:{{ transition | int / 2}}"
    # Set the color temperature.
    - service: homeassistant.turn_on
      target: "{{ entities }}"
      data:
        color_temp: "{{ color_temp | int }}"
        transition: "{{ transition | int / 2 }}"

I’ve tried !input but that seems to be limited to blueprints. I have no idea where to go from here so throwing it out in the hopes someone else has done this. Searching the forums has been fruitless, maybe I’m searching the wrong thing, I dont know.

Help me home assistant community, you are my only hope.

Thanks in advance

Information on passing variables to scripts can be found here, you must’ve read the wrong documentation a few times :wink:. In short, anything in the data section of the service call can be used as variables in the script, as long as they are set as fields in the script that you are calling.

Also, I don’t think you should have a target on direct script service calls like that, only if using script.turn_on, as the target for those service calls is the script you want to turn on.

Thanks very much. I have read that many times and I’ve managed to fix it. You were on the money with the target on the first script, thats what I failed to understand. Minor edit on the second script as well to make it work. homeassistant.turn_on does not support target by the looks of it. light.turn_on does though.

complete two scripts for anyone else with this issue.

script 1:

scene_focus:
  alias: "Activate Scene Focus"
  description: "Activates Focus Scene on Tradfri Bulb"
  fields:
    entities:
      description: "Device or Devices to active scene on"
      example: "light.lounge_main_light"
      selector:
        target:
          entity:
            domain: light
  sequence:
    - service: script.turn_on_ikea_light
      data:
        entities: "{{ entities }}"
        brightness_pct: 100
        color_temp: 250
        transition: 2

script 2 - helper script

turn_on_ikea_light:
  sequence:
    # Set the brightness of the lights.
    - service: light.turn_on
      target: "{{ entities }}"
      data:
        brightness_pct: "{{ brightness_pct | int }}"
        transition: "{{ transition | int / 2 }}"
    - delay: "00:00:{{ transition | int / 2}}"
    # Set the color temperature.
    - service: light.turn_on
      target: "{{ entities }}"
      data:
        color_temp: "{{ color_temp | int }}"
        transition: "{{ transition | int / 2 }}"

Thanks again
Chris

2 Likes

Awesome, glad you got it working. Don’t forget to mark a solution now that your issue is resolved :slight_smile: