Timer “contextual info” "event" is not defined

OH 4.3.0 release

Trying to build a simple timer in Blockly using contextual info to make the script more generic.
I prefer to stay with the standard blocks whenever possible.

Unsuccessful:
I can’t get it working with the standard timer block → ReferenceError: “event” is not defined

2025-01-05 15:02:59.801 [INFO ] [mation.script.ui.test_blockly_timer3] - Rule fired ...
2025-01-05 15:02:59.802 [INFO ] [mation.script.ui.test_blockly_timer3] - ZWaveNode016FGS213SingleSwitch2_Switch1
2025-01-05 15:03:09.803 [WARN ] [ore.internal.scheduler.SchedulerImpl] - Scheduled job 'MyTimer' failed and stopped
org.graalvm.polyglot.PolyglotException: ReferenceError: "event" is not defined

Successful
I can get it working with a timer block from this library.

Comparing the 2 blocks:

It seems as if the context cannot be set to “event” in the standard block - actually nothing changes to the code whatever config I try. The code remains unchanged as follows:

function (timer_context)

With the custom block it works.

function (event)

Here is the full code:

var scriptExecution = Java.type('org.openhab.core.model.script.actions.ScriptExecution');

var zdt = Java.type('java.time.ZonedDateTime');


console.info('Rule fired ...');
console.info(event.itemName);
if (cache.private.exists('MyTimer') === false || cache.private.get('MyTimer').hasTerminated()) {
  cache.private.put('MyTimer', actions.ScriptExecution.createTimer('MyTimer', time.ZonedDateTime.now().plusSeconds(10), function (timer_context) {
    items.getItem(event.itemName).sendCommand('OFF');
    }, (timer_context)));
} else {
  cache.private.remove('MyTimer').cancel();
};
if (typeof this.timers === 'undefined') {
  this.timers = [];
}
if (typeof this.timers['MyTimer'] === 'undefined' || this.timers['MyTimer'].hasTerminated()) {
  this.timers['MyTimer'] = scriptExecution.createTimerWithArgument(zdt.now().plusSeconds(10),event, function (event) {

  items.getItem(event.itemName).sendCommand('OFF');

  })
} else {
  this.timers['MyTimer'].cancel();
this.timers['MyTimer'] = undefined;

}

Is there a way to pass the event variable to the standard timer block?

Once you are inside the timer body you are no longer dealing with “contextual info”. There is no event inside the timer. There is just timer_context which you can reference inside the timer body using the block in the Timer category.

image

That timer_context block is a reference to what ever it was that you passed to the timer block. In your case if you want the triggering item name inside the timer your block should look something like:

You made my day, Rich :upside_down_face: