EMCA 5.1: Parameter for function with createTimer

Hello together,

I would like to start a timer in an EMCA 5.1 (openHAB 3.2) rule for each member of a group (here gRollerShutterUp) and call a corresponding function. Unfortunately I don’t know how to pass a parameter to the function,

scriptExecution.createTimer(zonedDateTime.now().plusSeconds(5), function (paramerter)

e.g. the item name (item.getName()).

this.timers = (this.timers === undefined) ? [] : this.timers;
var count = 0;

itemRegistry.getItem('gRollerShutterUp').members.forEach(function(item) {
  if (this.timers[count] != undefined) {
    logger.info(count + ' Timer cancel');
    this.timers[count] = undefined;
  logger.info('Roller shutter UP: ' + item.getName() + ' count: ' + count);

  if ((this.timers[count] === undefined) || this.timers[count].hasTerminated()) {
    logger.info(count + ' Timer create.');
    this.timers[count] = scriptExecution.createTimer(zonedDateTime.now().plusSeconds(5), function (?para?) {
      logger.info(count + ' Timer gestartet.' + test + ?para?);
      //this.timers[count] = undefined;

There’s a brief description here.

What is the nature of the argument you want to pass? It’s not clear from the code.

There are two ways you can approach this:

  1. Use createTimerWithArgument as demonstrated in Justin’s link.

  2. Use a function generator.

If the variable passed is created inside this forEach a function generator may be a better choice. The problem is JavaScript passes variables by reference. What that means is your forEach will run and pass that variable but when the timers go off, all the Timers will see the last value that variable had instead of the value it had when createTimerWithArgument was called.

A “function generator” is just a fancy name for a function that creates another function. You’ll pass the variables to the function and that is enough to “fix” the value.

var testFunctionGenerator = function (argument) {
  return function() {
    // timer code goes here


scriptExecution.createTimer(zonedDateTime.now().plusSeconds(5), testFunctionGenerator(?para?));

Thanks to both of you for your answer.
The version with the


is what I was looking for.

Thanks Holger