[SOLVED] Rule for gate status monitoring

Don’t bother figuring out what’s wrong with the current rule: you should not make use of Thread::sleep for more than a second or so, as explained here.

There are two alternatives: Timers (also briefly explained by Rich in the mentioned thread) and Expiring items.

Something like this will probably work. Two rules and a repeating timer:

var Timer tmGateOpen = null
var DateTime dtGateOpened = null

rule "Timer-reminder for opened gate"
when
    // Tamper sensor (OFF=open)
    Item GateState changed to OFF
then
    var String tag = "rules.IB"
    logInfo(tag, "Gate just opened, remember start time")

    tmGateOpen?.cancel  // Cancel any running timer, just to be sure
    dtGateOpened = now    // Gate just opened, remember

    tmGateOpen = createTimer(now.plusMinutes(10), [ |
        // Report gate still open
        logWarn(tag, "Gate opened for " + now.millis - dtGateOpened.millis + " minutes...")
        //:TODO: Send notification or whatever...
        tmGateOpen.reschedule(now.plusMinutes(10))  // Check back in 10 minutes
    ])
end

rule "Gate closed reminder"
when
    Item GateState changed to ON
then
    logInfo("rules.IB","Gate closed")
    tmGateOpen?.cancel  // Cancel the timer
    GateOpened = null   // Forget opened time
end