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…