OH 4.0.4 createTimerWithArgument has been deprecated

Hi,

I am currently migrating my rules from OH3.4 to OH4 and with one of my most favorite rules & uses cases I got the warning mentioned above.

Usercase:
I want to get a reminder after 10 minutes, if windows are still open.
In OH I have put all my window items into a group, triggered a rule “if a member of the group changed from CLOSED to OPEN” and started a timers. The trick was to have A) the item name as the name of the timer, therefore I could have multiple timer scheduled with the same rule and B) passing the event item to the timer, so that I could access the event information from within the timer

Sample code from OH3:

if (event.itemState == 'OPEN') {
    if (typeof this.timers === 'undefined') {
      this.timers = [];
    }
    if (typeof this.timers[String(event.itemName) + '_Timer'] === 'undefined' || this.timers[String(event.itemName) + '_Timer'].hasTerminated()) {
      this.timers[String(event.itemName) + '_Timer'] = scriptExecution.createTimerWithArgument(zdt.now().plusMinutes(intialDuration),event, function (event) {
        notificationText = 'Please close the Window: ' + String(itemRegistry.getItem(event.itemName).getLabel());
        if (typeof this.timers[(String(event.itemName) + '_Timer')] !== 'undefined') { this.timers[(String(event.itemName) + '_Timer')].reschedule(zdt.now().plusMinutes(rescheduleDuration)); }
      })
  }
} else if (event.itemState == 'CLOSED') {
  if (typeof this.timers[(String(event.itemName) + '_Timer')] !== 'undefined') {
    this.timers[(String(event.itemName) + '_Timer')].cancel();
    this.timers[(String(event.itemName) + '_Timer')] = undefined;
  }
}

For OH4 I can run the following code successfully, however get a warning that the createTimerWithArgument function is deprecated:

"createTimerWithArgument" has been deprecated and will be removed in a future release. Use "createTimer" or "setTimeout" instead.
        if (cache.private.exists(String(event.itemName)) === false || cache.private.get(String(event.itemName)).hasTerminated()) {
          cache.private.put(String(timer_name), actions.ScriptExecution.createTimerWithArgument(String(timer_name), time.ZonedDateTime.now().plusSeconds(5),event, function (event) {
          console.info(event.itemName);
         }));
        };

As the regular createTimer function will not work for my usecase, from current perspective I could only use setTimeout(functionRef, delay, param1) to have the original event object available in the timer.

Questions:

  1. Is there any other recommendation beside using setTimeout to pass additional parameters to the timer? Do I miss something?
  2. Is there any chance that createTimerWithArgument is not getting deprecated and this decission will be reviewed?
  3. If answer to #2 is no, is there a timeline, when this function will be removed?

Maybe see this: EMCA 5.1: Parameter for function with createTimer - #3 by rlkoshak
For the history, see this thread: [Js Scripting] Why are we deprecated createTimer?

Documented here: GitHub - openhab/openhab-js: openHAB JavaScript Library for JavaScript Scripting Automation see the " Pass variables using a function generator" section.

1 Like

Thanks, that’s super helpful.
I have searched the forum before but missed this