Dear Community,
I am trying to implement a generic blockly script which increases or decreases the value of a number item over time. It should be possible to call this script for two or more items at the same time without interfering each other.
My current approach is not bulletproof if it comes to multiple calls for different items - I am aware of that - but it is already failing. I get following error messages in console.log:
2023-01-25 08:14:31.482 [WARN ] [ore.internal.scheduler.SchedulerImpl] - Scheduled job '<unknown>' failed and stopped
jdk.nashorn.internal.runtime.ECMAException: ReferenceError: "itemName" is not defined
at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57) ~[jdk.scripting.nashorn:?]
at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:319) ~[jdk.scripting.nashorn:?]
at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:291) ~[jdk.scripting.nashorn:?]
at jdk.nashorn.internal.objects.Global.__noSuchProperty__(Global.java:1616) ~[jdk.scripting.nashorn:?]
at jdk.nashorn.internal.scripts.Script$Recompilation$5087$1036$\^eval\_.L:26(<eval>:28) ~[?:?]
at jdk.nashorn.javaadapters.org_eclipse_xtext_xbase_lib_Procedures$Procedure0.apply(Unknown Source) ~[?:?]
at org.openhab.core.model.script.actions.ScriptExecution.lambda$0(ScriptExecution.java:97) ~[?:?]
at org.openhab.core.internal.scheduler.SchedulerImpl.lambda$12(SchedulerImpl.java:191) ~[?:?]
at org.openhab.core.internal.scheduler.SchedulerImpl.lambda$1(SchedulerImpl.java:88) ~[?:?]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) [?:?]
at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) [?:?]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
at java.lang.Thread.run(Thread.java:829) [?:?]
It seems like all variables inside my timer body are dead even for the first execution. I fiddled around with the initial delay (using 10s) and the timer name (using “MyTimer”) but still getting the same error. And yes I did make sure that my context attributes were set by using logging messages. For testing purposes I am using startValue=0, endValue=100, intervallInS=60*5, itemName=“Spotify_Volume”
Here is the blockly “code”:
And here the generated code:
var startValue, endValue, intervalInS, itemName, updateIntervalInS, delta, stepCount, stepSize, counter, currentValue;
var scriptExecution = Java.type('org.openhab.core.model.script.actions.ScriptExecution');
var zdt = Java.type('java.time.ZonedDateTime');
if (typeof this.timers === 'undefined') {
this.timers = [];
}
startValue = ctx['startValue'];
endValue = ctx['endValue'];
intervalInS = ctx['intervalInS'];
itemName = ctx['itemName'];
updateIntervalInS = 10;
delta = endValue - startValue;
stepCount = Math.floor(intervalInS / updateIntervalInS);
stepSize = delta / stepCount;
counter = 0;
currentValue = startValue;
if (typeof this.timers[itemName] === 'undefined' || this.timers[itemName].hasTerminated()) {
this.timers[itemName] = scriptExecution.createTimer(zdt.now().plusSeconds(0), function () {
if (counter < stepCount) {
events.sendCommand(itemName, currentValue);
counter = (typeof counter == 'number' ? counter : 0) + 1;
currentValue = (typeof currentValue == 'number' ? currentValue : 0) + stepSize;
if (typeof this.timers[itemName] !== 'undefined') { this.timers[itemName].reschedule(zdt.now().plusSeconds(updateIntervalInS)); }
} else {
events.sendCommand(itemName, endValue);
}
})
}
I would really appreciate your feedback and help. Thank you in advance
Cheers for now
Clemens