[SOLVED] Trying to understend rules with timer


Still trying to understand rules. For this I’m writing small parts of code without a real purpose exept understanding what is happening. There is something I don’t get about the following code

var Timer sireneTimer = null
var Number timesthrough = 0
var Number timesthroughElse = 0
var Integer SireneOnTime = 3000

rule "Deur_open"
   Member of gDoors changed
   logInfo("deur", "Member of gDoors:" + triggeringItem.label + " changed from " + previousState.toString + "to:" + triggeringItem.state )
    sireneTimer = createTimer(now, [
    if(triggeringItem.state == OPEN) {
	  timesthrough = timesthrough + 1
	  SireneOnTime = SireneOnTime + 1000
	  timesthroughElse = timesthroughElse - 1
          logInfo("deur", "This is " + timesthrough + " time through the timer")
    else {
      logInfo("deur", "into else statement with timesthroughElse =" + timesthroughElse)
      sireneTimer = null  

When i open a doorsensor I get the following log:

2019-04-27 16:16:17.086 [INFO ] [.eclipse.smarthome.model.script.deur] - Member of gDoors:Door Sensor changed from CLOSEDto:OPEN

2019-04-27 16:16:17.131 [INFO ] [.eclipse.smarthome.model.script.deur] - This is 1 time through the timer

2019-04-27 16:16:21.165 [INFO ] [.eclipse.smarthome.model.script.deur] - This is 2 time through the timer

2019-04-27 16:16:22.175 [INFO ] [.eclipse.smarthome.model.script.deur] - Member of gDoors:Door Sensor changed from OPENto:CLOSED

2019-04-27 16:16:22.202 [INFO ] [.eclipse.smarthome.model.script.deur] - into else statement with timesthroughElse =-2

2019-04-27 16:16:26.188 [INFO ] [.eclipse.smarthome.model.script.deur] - into else statement with timesthroughElse =-2

It is the two last logs, when the script goes in the “else”-block, that I have questions about.
I think I undertsand why there are two logs but I would feel confident if the community confirms my thoughts.

I think that one of the lines is comming form the first thread of the rule triggered by opening the door while the other one is from the second thread triggered by closing the door. Is that correct?

What I don’t get is why the “timesthroughElse” is, at least for one of the loglines, not zero. I would of thought that a new thread of that rule, triggered by closing the door would use a new instance of the variable timesthroughElse with 0 as result.


Note, that although you have a global variable sireneTimer to store a handle to your timer - you don’t check to see if it is already in use before creating a new timer and assigning that to your variable.
Doing that second creation does not stop or destroy the first timer, you just lose the handle to it. It will carry on and do its thing later.

I haven’t though that hard, but guess you might create timer “A” , execute that, timer “A” reschedules itself. Then you create a new timer “B”, execute that, don’t reschedule. Then reschedule “A” executes.

Thx @rossko57 !!!