Hello,
to reduce redundancy in my rules code, I have decided to put things in functions to make things also easier to maintain. Now I have a function that is used for each of my three roof rollershutters to close them and checks, if the corresponding window is closed and if not creates a wait timer for the shutter, closes the window and then the timer closes the shutter. IN THEORY
To me it seems that timers and functions are a bit tricky to use. I have created a small code example to demonstrate my issue:
var Timer myTimer = null
val Functions.Function2<String, String, void> myFunction = [ String a, String b |
logInfo("TEST", "Timer: " + myTimer)
if (myTimer === null) {
myTimer = createTimer(now.plusSeconds(5), [|
logInfo("TEST", "Printing String a: " + a)
])
logInfo("TEST", "Printing String b: " + b)
}
return null
]
rule "TimerTest"
when
Time cron "*/10 * * * * ?"
then
logInfo("TEST", "Before function call. Timer: " + myTimer)
myFunction.apply("Hello Timer!", "Hello World!")
logInfo("TEST", "After function call. Timer: " + myTimer)
end
which I have in a file test.rules. The behavior is though rather unexpected as neither the after function call nor the b-string are logged, i.e., they seem not being executed:
# tail -n 50 -f /var/log/openhab2/*log | grep TEST
2018-08-24 10:06:40.071 [INFO ] [.eclipse.smarthome.model.script.TEST] - Before function call. Timer: null
2018-08-24 10:06:40.071 [INFO ] [.eclipse.smarthome.model.script.TEST] - Timer: null
2018-08-24 10:06:45.072 [INFO ] [.eclipse.smarthome.model.script.TEST] - Printing String a: Hello Timer!
2018-08-24 10:06:50.003 [INFO ] [.eclipse.smarthome.model.script.TEST] - Before function call. Timer: null
2018-08-24 10:06:50.005 [INFO ] [.eclipse.smarthome.model.script.TEST] - Timer: null
2018-08-24 10:06:55.006 [INFO ] [.eclipse.smarthome.model.script.TEST] - Printing String a: Hello Timer!
2018-08-24 10:07:00.020 [INFO ] [.eclipse.smarthome.model.script.TEST] - Before function call. Timer: null
2018-08-24 10:07:00.021 [INFO ] [.eclipse.smarthome.model.script.TEST] - Timer: null
2018-08-24 10:07:05.021 [INFO ] [.eclipse.smarthome.model.script.TEST] - Printing String a: Hello Timer!
2018-08-24 10:07:10.001 [INFO ] [.eclipse.smarthome.model.script.TEST] - Before function call. Timer: null
2018-08-24 10:07:10.002 [INFO ] [.eclipse.smarthome.model.script.TEST] - Timer: null
After the âTimer: nullâ output, I get the following error message that I do not understand:
2018-08-24 10:25:40.008 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'TimerTest': Cannot assign a value in null context.
What is especially weird to me is that the timer is obviously created and executed but the line after that never executed.
I hope somebody can help me a) understand whatâs going wrong there and b) help fix the issue