I want to pass an array to a script and don't kow how

After 3 hours trying to find out the correct Jinja/YAML/whatever syntax for that problem, I’ve given up and hope, someone else can point me into the right direction.

I just want to pass an array of two integers from an automation to this script:

myscript:
  sequence:
    - service: notify.html5_push
      data_template:
        message: 's:{{ stbrightness[0] }} / t:{{ stbrightness[1] }}'

When calling this script from the following automation…

- id: myautomation
  trigger:
    platform: time_pattern
    minutes: '/1'
  action:
    - service: script.myscript
      data_template:
        stbrightness: [23,42]

… the notification becomes “s:23 / t:42”, just as expected.

But - and here’s the tricky part - In my use case I want to pass different values to the script, depending on the current time (actually the time, the automation got triggered).

So, the following syntax isn’t working, obviously, because everything after the > is becoming a string and the script doesn’t output the two integers anymore, but the first two characters of the “array” string, f.e. “s:[ / t:1” or “s:[ / t:5”, depending on the current time:

- id: myautomation
  trigger:
    platform: time_pattern
    minutes: '/1'
  action:
    - service: script.myscript
      data_template:
        stbrightness: >
          {% set time=trigger.now.strftime('%H%M')|int %}
          {% if   time < 1000 %} [1  , 40 ]
          {% elif time < 2000 %} [128, 128]
          {% elif time < 2300 %} [5  , 5  ]
          {% else             %} [0  , 0  ]
          {% endif %}

But how do I rewrite this template for stbrightness to preserve the values inside the conditions as arrays? In the last hours I tried countless variants and googled the hell out of my browser, but really didn’t find out, how to write a multiline template that outputs anything else than a string…

You can’t, unless you write two templates:

data_template:
  stbrightness:
    - {% set ... endif %}
    - {% set ... endit %}

Not very practical in this case, especially since both templates will be 99% the same.

If you want to pass two numbers, just pass them as a string representation of two numbers, then extract them in the script:

- id: myautomation
  trigger:
    platform: time_pattern
    minutes: '/1'
  action:
    - service: script.myscript
      data_template:
        stbrightness: >
          {% set time=trigger.now.strftime('%H%M')|int %}
          {% if   time < 1000 %}   1,  40
          {% elif time < 2000 %} 128, 128
          {% elif time < 2300 %}   5,   5
          {% else             %}   0,   0
          {% endif %}

Then in the script:

myscript:
  sequence:
    - service: notify.html5_push
      data_template:
        message: >
          {% set b = stbrightness.split(',') %}
          s:{{ b[0].strip() }} / t:{{ b[1].strip() }}

Ok, good to know. At least I didn’t seem to overlook something obvious. :grinning:

Yes, that’s what I went with in the end. Thanks for the clarification.