Program flow executes statement even though WHILE loop has computed to FALSE

  • Platform information:
    • Hardware: PI3
    • OS: rasp OS , openhabian unattended install as addon sw
    • openHAB version: 3.2

This is my code

  events.sendCommand('SaecoMinuto_ONOFFSwitch', 'ON');
  if (typeof this.timers['3'] === 'undefined' || this.timers['3'].hasTerminated()) {
    this.timers['3'] = scriptExecution.createTimer(, function () {
      while (itemRegistry.getItem('SaecoMinuto_Busy').getState() == 'ON') {
        events.sendCommand('SaecoMinuto_EspressoSkipRinse', 'ON');
        events.sendCommand('SaecoMinuto_EspressoSkipRinse', 'OFF');
2022-02-26 10:01:07.404 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'SaecoMinuto_EspressoSkipRinse' received command ON
2022-02-26 10:01:07.410 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'SaecoMinuto_EspressoSkipRinse' predicted to become ON
2022-02-26 10:01:07.418 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'SaecoMinuto_EspressoSkipRinse' changed from OFF to ON
2022-02-26 10:01:07.478 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'SaecoMinuto_RedScreen' changed from ON to OFF
2022-02-26 10:01:07.502 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'SaecoMinuto_Busy' changed from ON to OFF
2022-02-26 10:01:07.605 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'SaecoMinuto_EspressoSkipRinse' received command OFF
2022-02-26 10:01:07.615 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'SaecoMinuto_EspressoSkipRinse' predicted to become OFF
2022-02-26 10:01:07.621 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'SaecoMinuto_EspressoSkipRinse' changed from ON to OFF

I use it to stop rinse cycle in coffee machine . It sends multiple On Off commands to NPN transistor soldered on machine board to “press” the button and interupt rinse . NPN is driven from ESP board running tasmota which is getting orders through mqtt from OH3.

Problem is some delay in WHILE loop…it does “see” that examined item changed state but still executes one statment from its code . I did try to change timing btw sending commands but then it sometimes executes 2 or even 3 On and Off commands before exiting loop.

As can be seen in log about 100ms passes after loging "the end of WHILE loop " and yet looks like it’s somehow slow and another loop gets executed which in turn “presses” another button since machine buttons get assinged new functions depending on screen colour machine state etc (no single button for single function).

Is it a thread.sleep isue or Im missing a key programing knowlege (im a mechanic :slight_smile: ) .

If i increase time btw commands than due to cumulative lag reaching the NPN , machine starts its rinse cycle and can’t be stopped.