Wonderful idea returning values from scripts. Here’s what I want to do:
I have an automation that performs my early morning wakeup routing. One of the many things required is to ramp up the light intensity. To accomplish that I wrote a script just for the rampup and I call the script (service script. blocking call) from my automation. Got that part working just fine.
If someone “controls” the lamp (turn off or change intensity) partway through the rampup, the script can easily determine that. I want to terminate the script early and let the automation know that there was manual intervention so it can alter the wakeup routine. Perfect application for response variable in the script. Couldn’t ask for anything better.
So I tried returning a response variable (of success) at the end of the script and it was easily seen (and used) in the application. A code snippet from the script would be:
ramp_up_light:
sequence:
- repeat:
count: 10
sequence:
.
<code for the ramping>.
.
- variables:
return_var:
success: true
reason: "Successful completion"
- stop: "Complete"
response_variable: return_var
Great start. Now to put the “manual intervention” stop code in as follows:
ramp_up_light:
sequence:
- repeat:
count: 10
sequence:
.
<code for the ramping>.
- if: <test for manual intervention>
then:
- variables:
return_var:
success: false
reason: "Manual intervention"
- stop: "Incomplete"
response_variable: return_var
.
- variables:
return_var:
success: true
reason: "Successful completion"
- stop: "Complete"
response_variable: return_var
OK, so this continues to work perfectly in the absence of manual intervention. If there is manual intervention, though, it returns {} (empty dict? None?) as shown in the automation trace “changed variables”.
So I am guessing it is a scope issue that the intervention “stop” is nested a block or two down from the other “stop” which is at the top level of the script. Is stop only looking for variables in the top level?
So I tried moving the four manual intervention lines starting with “-variables:” up to the top before “repeat” (i.e. the default return_var is the failure one). No change. I’m really hoping I’ve missed something because right now it looks like you can’t use response_variable from anything other than the top scope.
An alternative is to only use stop at the top level but how would I pass the failure reason from a nested scope up to the top level scope?
Any help would be very much appreciated. I would accept an alternative means of doing this (preferably without using “input” entities as I find that poor practice) but I would really like to understand what “stop” is doing that prevents me from using a response_variable while nested.
TIA
Keith