[SOLVED] Timer isRunning not working as expected

Hi,

I’m currently playing with timers. So I created a small ruleset for testing.

var Timer tTestlicht = null

rule "Timerdemo"
when
  Time cron "0 0/1 * * * ? *"
then

  if(tTestlicht !== null) {

    if(tTestlicht.isRunning) logInfo("TEST", "Is running!")
      else logInfo("TEST", "Is NOT running!")

    if(tTestlicht.hasTerminated) logInfo("TEST", "Has terminated!")
      else logInfo("TEST", "Has NOT terminated!")

  } else {

    logInfo("TEST", "Creating tTestlicht!")
    tTestlicht = createTimer(now.plusSeconds(180)) [|
      logInfo("TEST", "tTestlicht is done!")
    ]
  }
end

And that’s the output of the 3 minutes running ruleset:

13:17:00.365 ... - Creating tTestlicht!
13:18:00.022 ... - Is NOT running!          <-- WHY?
13:18:00.032 ... - Has NOT terminated!
13:19:00.029 ... - Is NOT running!          <-- WHY?
13:19:00.038 ... - Has NOT terminated!
13:20:00.015 ... - Is NOT running!          <-- WHY?
13:20:00.024 ... - Has NOT terminated!
13:20:00.370 ... - tTestlicht is done!

So my questions are:

  • Why is isRunning not usable without a if(tTestlicht !== null) statement? When trying this, I get this error: Error during the execution of rule 'Timerdemo': cannot invoke method public abstract boolean org.eclipse.smarthome.model.script.actions.Timer.isRunning() on null
  • Why is isRunning always false? That’s not what I expected when using it :wink:

I’m doing this test currently on an openHAB 2 2.2.0-1 instance (openHABian).

Cheers,
Marianne

Have your tried:
(Testlicht.isRunning())
and
(Testlicht.hasTerminated())

Yes I did, but it makes no difference; tTestlicht.hasTerminated does work BTW, it is true when the 180 seconds are done… only isRunning is always false.

I think there is a misconception of the property .isRunning The meaning is to be found here:

    /**
     * Determines whether the scheduled code is currently executed.
     * 
     * @return true, if the code is being executed, false otherwise
     */
    public boolean isRunning();

So this is only true if the timer expires and the scheduled code is executed at this very moment.

I guess if putting a Thread::sleep(10000) into the scheduled code, there would be a chance to get a .isRunning == true :wink:
And I’m sure this will work either:

tMyTimer = createTimer(now.plusSeconds(1), [ |
    logInfo ("test","tMyTimer.isRunning = {}",if(tMyTimer.isRunning) "true" else "false")
    ]
)
2 Likes

Thank you very much, Udo :green_heart: I misunderstood the intention of isRunning completely it seems :smiley: