OH4 Blockly count script doesn't work anymore

after updating to OH4 the following script does not work with ECMAScript 262 edition 11:

image

var count;

console.info('SCRIPT count');
count = 0;
if (cache.private.exists('MyTimer') === false || cache.private.get('MyTimer').hasTerminated()) {
  cache.private.put('MyTimer', actions.ScriptExecution.createTimer('MyTimer', time.ZonedDateTime.now().plusSeconds(1), function () {
    if (count <= 10) {
      count = (typeof count === 'number' ? count : 0) + 1;
      console.info(count);
      if (cache.private.exists('MyTimer')) { cache.private.get('MyTimer').reschedule(time.ZonedDateTime.now().plusSeconds(1)); };
    }
    cache.private.remove('MyTimer');
  }));
};

the output ends after the 2nd pass but should count to 10:

==> /var/log/openhab/openhab.log <==
2023-07-31 13:08:01.617 [INFO ] [nhab.automation.script.ui.a08104671c] - SCRIPT count
2023-07-31 13:08:02.619 [INFO ] [nhab.automation.script.ui.a08104671c] - 1
2023-07-31 13:08:03.620 [INFO ] [nhab.automation.script.ui.a08104671c] - 2

have I missed something?

This may be a regression. The problem is that last line cache.private.remove('MyTimer');. Definitely file an issue. It might be tricky to fix though. It might require creating a separate looping timer block.

In the mean time, you can make this work through a slight adjustment.

  1. Instead of a reschedule, create a new timer at the end of the if statement
  2. Append the count to the name of the timer

This will convert it from trying to reschdule to creating a new timer each time.

Thank you, I created an issue:

but i have not yet understood how to call the new timer with the count in the name. Can you please show me an example in blockly?

It’s just a string concatenation, nothing complicated.

I

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.