Unable to use data_template to pass variables to a script?

I’m trying to make an automation which turns on (or adjusts) my office lights with a brightness based on the detected luminance. My automation is written as follows:

- alias: "Turn on lights when dark while working"
  trigger:
    - platform: state
      entity_id: sensor.office_multi_luminance_3
    - platform: numeric_state
      entity_id: sensor.laptop_work_david
      above: 500
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: switch.turn_on_lights_when_dark_while_working
        state: 'on'
      - condition: numeric_state
        entity_id: sensor.office_multi_luminance_3
        below: 30
      - condition: numeric_state
        entity_id: sensor.laptop_work_david
        above: 500
  action:
    - service: script.turn_on
      entity_id: script.office_worklight
      data_template:
        variables: >-
          {%- set lux = states.sensor.office_multi_luminance_3.state | int -%}
          {%- set perc = 60 - lux -%}
          {%- set bri = ((255 * perc) / 100) | int -%}
          brightness: {{ bri }}

And the script being called looks like this:

office_worklight:
  sequence:
    - service: light.turn_on
      data:
        entity_id:
          - light.lamp_desk_left
          - light.lamp_desk_right
        rgb_color: [211, 191, 157]
        transition: 5
      data_template:
        brightness: '{{ brightness }}'

The script works perfectly. I’ve tested it manually through the HASS frontend, on the Services page, by calling it with the following data: { "brightness": "86" }

However, the automation keeps returning the following error:

16-09-06 13:28:53 homeassistant.core: Invalid service data for script.turn_on: expected dict for dictionary value @ data['variables']. Got 'brightness: 86'

I tried to rewrite the data_template section a few different ways, but my variable seems to get parsed as a string no matter what I try. I fear I may have stumbled onto another (showstopping, for me) bug but I thought I’d check here first. There is very little information to be found anywhere when it comes to passing variables to scripts, especially when trying to template the variables.

1 Like

I found a workaround for the issue/bug above!

In the past I had looked at this page but I could never make sense of the following bit:

Well I finally figured out how that works. For convenience’s sake I’m only going to show how I rewrote my action in my automation, as the rest is irrelevant:

action:
  - service: script.office_worklight
    data_template:
      brightness: >-
        {%- set lux = states.sensor.office_multi_luminance_3.state | int -%}
        {%- set perc = 60 - lux -%}
        {%- set bri = (255 * (perc / 100)) | int -%}
        {{ bri }}

So before, the service script.turn_on was called with script.office_worklight as the entity_id.
Now, script.office_worklight gets called directly as a service, with everything under data_template being available as variables to the script.

Finally, I have this automation up and running! :sweat_smile:

6 Likes

Interresting. Thank you.

I’m having similar issue but can’t find out where. Can you have a please?

https://github.com/home-assistant/home-assistant/issues/6424#issuecomment-302748008

Thank you !