OH1 rule has me puzzled; boolean and timer issue

I reckon I looked at my construct for far too long, and do not see what must be obvious.

The idea is:
receive geo location
check if within 400m of fixed point
send an email
set a lock; so that further locations within the 400m do not trigger another email
start a timer which will reset the lock

Below is the code, and further below the log; the code looks OK to me, but it doesn’t do what I want it to do > so the code must be wrong.

outside rule:
var Boolean blnEmailLockLocus_1 = false

inside rule:
          // if within 400 metres of Locus_1
          if (DistanceFromLocus_1.state < 0.400) {
            // if we haven't sent an email yet
            if (blnEmailLockLocus_1 == false) {
              logInfo("Geo.3.4", "Send email (at Locus_1)")
              sendMail(emailToPete, "Max is 20 minutes away", "Max is roughly 20 minutes away from pick-up location")
              logInfo("Geo.3.5", "set blnEmailLockLocus_1 to true (={})", blnEmailLockLocus_1)
              blnEmailLockLocus_1 = true
              
              // start timer to reset blnEmailLockLocus_1 to false in 120 seconds
              createTimer(now.plusSeconds(120), [|
                logInfo("Geo.3.6", "set blnEmailLockLocus_1 to false (={})", blnEmailLockLocus_1)
                blnEmailLockLocus_1 = false
              ])
            }
          }
2018-11-16 06:06:35.715 [INFO ] [g.openhab.model.script.Geo.3.4] - Send email (at Locus_1)
2018-11-16 06:06:42.242 [INFO ] [g.openhab.model.script.Geo.3.4] - Send email (at Locus_1)
2018-11-16 06:06:51.976 [INFO ] [g.openhab.model.script.Geo.3.4] - Send email (at Locus_1)
2018-11-16 06:06:53.271 [INFO ] [g.openhab.model.script.Geo.3.5] - set blnEmailLockLocus_1 to true (=false)
2018-11-16 06:06:55.375 [INFO ] [g.openhab.model.script.Geo.3.5] - set blnEmailLockLocus_1 to true (=true)
2018-11-16 06:07:05.348 [INFO ] [g.openhab.model.script.Geo.3.5] - set blnEmailLockLocus_1 to true (=true)
2018-11-16 06:08:55.826 [INFO ] [g.openhab.model.script.Geo.3.6] - set blnEmailLockLocus_1 to false (=true)
2018-11-16 06:08:56.294 [INFO ] [g.openhab.model.script.Geo.3.6] - set blnEmailLockLocus_1 to false (=true)
2018-11-16 06:09:05.369 [INFO ] [g.openhab.model.script.Geo.3.6] - set blnEmailLockLocus_1 to false (=false)

Any hints appreciated.

not sure if this will help, but whenever I use Timers, I declare them and I use the declaration later

var	Timer	blnEmail_Timer = null
val	Integer	blnEmail_TimeOut = 2
...
			// start timer to reset blnEmailLockLocus_1 to false in 2 minutes
			blnEmail_Timer = createTimer(now.plusMinutes(blnEmail_TimeOut))
			[|
				logInfo("Geo.3.6", "set blnEmailLockLocus_1 to false (={})", blnEmailLockLocus_1)
				blnEmailLockLocus_1 = false
			]
...
1 Like

There is something weird about booleans in Rules DSL (which is not pure java)

The fix is just as weird
var blnEmailLockLocus_1 = new Boolean(“false”)

1 Like

I should have checked my other timers…
Yes, thanks… I am sure this will make a difference

outside rule
var Timer timerLockEmailLocus_1 = null

inside rule
              timerLockEmailLocus_1 = createTimer(now.plusSeconds(120), [|
                logInfo("Geo.3.6", "set blnEmailLockLocus_1 to false (={})", blnEmailLockLocus_1)
                blnEmailLockLocus_1 = false
                timerLockEmailLocus_1 = null
              ])

What is really interesting, I have another timer that is the same construct that actually works; maybe one timer in a rule file works without declaration; another one without a name does not work; but I agree, they should be declared, which I have done for both timers in that rule file.

I will test this on Monday (as I hit this geolocation Mon-Fri)… and then update this post.

1 Like

Thanks, I hope you’re wrong :smiley: as this confuses the heck out of me :smiley:
If my timer (or more so Dim’s declaration) works that would be great.
But then there is: val Number GeoLoc_Home_Lat = new DecimalType()

I just discovered another mistake I made: location of the log entry (it came before the flag / boolean was changed).

This is what it should be:

            if (blnEmailLockLocus_1 == false) {
              logInfo("Geo.3.4", "Send email (at Locus_1)")
              sendMail(emailToPete, "Max is 20 minutes away", "Max is roughly 20 minutes away from pick-up location")
              blnEmailLockLocus_1 = true
              logInfo("Geo.3.5", "set blnEmailLockLocus_1 to true (={})", blnEmailLockLocus_1)
              
              // start timer to reset blnEmailLockLocus_1 to false in 120 seconds
              timerLockEmailLocus_1 = createTimer(now.plusSeconds(120), [|
                blnEmailLockLocus_1 = false
                logInfo("Geo.3.6", "set blnEmailLockLocus_1 to false (={})", blnEmailLockLocus_1)
                timerLockEmailLocus_1 = null
              ])
            }

You can now use the timer as an email lock
eg: if (timerLockEmailLocus_1 === null) { ...

1 Like