Script with Blocky: reschedule block triggers only once [SOLVED]

I created a rule to turn on a light when motion is detected and turn off when no motion detected for 3 minutes.
The rule is triggered by the motion sensor changing to ON:

the issue is that when the “else if” condition is met for the first time the timer is rescheduled, but the second time it’s not.
Is this a “feature” of the reschedule block or there is an issue?
thanks

Errors in the log?

Add some logging blocks to see what the states of those two items are every time the timer block runs. Are you sure the else if it’s running?

Hi Rich,

the behavior I experience is the following:
If I stay in the room for less than 5 minutes (I tested 4.5 minutes) and the I exit, the light turns off.
If I stay in the room for more than 5 minutes and then exit, the light never turns off.
Consider also that the motion sensor has a 60 seconds “cooldown” after being triggered, and if motion is detected when it’s already ON, there is no change in the item state.

I will add some logging blocks to try to pinpoint the issue and post them here.

I modified the script as follows:

the relevant logs:

2021-11-25 18:53:12.398 [INFO ] [org.openhab.rule.b7323e80df         ] - timer start
2021-11-25 18:56:12.413 [INFO ] [org.openhab.rule.b7323e80df         ] - Timer finished
2021-11-25 18:56:12.527 [INFO ] [org.openhab.rule.b7323e80df         ] - Else if condition met > reschedule
2021-11-25 18:59:12.502 [INFO ] [org.openhab.rule.b7323e80df         ] - Timer finished
2021-11-25 18:59:12.551 [INFO ] [org.openhab.rule.b7323e80df         ] - Else if condition met > reschedule

After these nothing else is logged and the light is still on.

To me it seems that the reschedule block works only once, but I’m open to better ideas :slight_smile:

IT does look like it should be rescheduling. My recommendation is to file an issue on the openhab-webui repo where more information will be requested and further trouble shooting steps can be taken.

Hi Rich,

I opened an issue here:

hope it’s the right place

@enrico.mcc The issue has been fixed with 3.2.0. Do you mind adding [SOLVED] to the title here?

Sure, thanks for the fix! :slight_smile:

I think the problem is back again in openHAB 4. I tried to build the same rule as it is described in the documentation: Rules Blockly

here is my code:

var WarningRetry;


WarningRetry = 4;
console.warn('StarteTESTretry');
if (cache.private.exists('TESTTIMER') === false || cache.private.get('TESTTIMER').hasTerminated()) {
  cache.private.put('TESTTIMER', actions.ScriptExecution.createTimer('TESTTIMER', time.ZonedDateTime.now().plusSeconds(5), function () {
    console.warn((['Nach 5s ','Starting Retry: ',WarningRetry,''].join('')));
    if (WarningRetry > 1) {
      console.warn('in retry');
      WarningRetry = (typeof WarningRetry === 'number' ? WarningRetry : 0) + -1;
      if (cache.private.exists('TESTTIMER')) { cache.private.get('TESTTIMER').reschedule(time.ZonedDateTime.now().plusSeconds(5)); };
    }
    cache.private.remove('TESTTIMER');
  }));
};

can someone verify, that it is not belonging to my Blockly rule? I am using openHAB 4.0.2

cheers
Andreas

It is almost always better to open a new thread than to reopen a years old thread. In this case Blockly has received a nearly complete overhaul for OH 4 so anything in the thread above isn’t going to apply.

But in a short answer, an issue has already been filed for this. It’s a bug. The work around is to create a new Timer instead of rescheduling. Or use an inline script block with the code above with the line cache.private.remove('TESTTIMER') moved into an else after the last if:

if (cache.private.exists('TESTTIMER') === false || cache.private.get('TESTTIMER').hasTerminated()) {
  cache.private.put('TESTTIMER', actions.ScriptExecution.createTimer('TESTTIMER', time.ZonedDateTime.now().plusSeconds(5), function () {
    console.warn((['Nach 5s ','Starting Retry: ',WarningRetry,''].join('')));
    if (WarningRetry > 1) {
      console.warn('in retry');
      WarningRetry = (typeof WarningRetry === 'number' ? WarningRetry : 0) + -1;
      if (cache.private.exists('TESTTIMER')) { cache.private.get('TESTTIMER').reschedule(time.ZonedDateTime.now().plusSeconds(5)); }
      else { cache.private.remove('TESTTIMER');};
    }
    
  }));
};
1 Like

Rich, I yesterday browsed all issues with Blockly and I couldn’t find this issue. Do you know which one that is and ping @florian-h05 to assign this to me?
I unfortunately hadn’t had too much time to work on issues but I hope I can catch up shortly.

it’s poorly named but this is the one.

Any time the looping timer block is created the line to remove it from the cache gets added so that there isn’t a timer to reschedule the first time it runs.

1 Like

@florian-h05 can you assign it to me, pls?

Update: fixed, you may have a look at it and merge it.

thanks! will this fix be included in the next release/milestone?

Depends on when this being reviewed and merged but, yes, that’s the intention

1 Like