Most efficient way to delay an event?


(DawgOnKing) #1

Yeah, my groovy is not so groovy.

What’s the best way to delay a call to event?

In my case: if a door is open, let’s wait 1 minute to see if it get closed - if not, send message or blah, blah, blah

I’m trying to decipher “Left It Open” but there’s too much there I don’t need.

I want to capture time of “contact.open” evt by declaring a variable def nowTime = now()

Then declare another variable to add 60 seconds to nowTime?
def delayTime = now() + 3000*20

And finally call second event openTooLong after one minute…

The following code works but it doesn’t wait for the delay to call openTooLong…

preferences {
	section("Select a sensor...") {
		input "allyDoor", "capability.contactSensor", title: "Available Sensors...", multiple: false
	}

	section("Select a switch") {
		input "allySwitch", "capability.switch", title: "Available Switches...", multiple: false
	}
}

def installed() {
	subscribe (allyDoor, "contact.open", doorHandler)
}

def updated() {
	unsubscribe()
	subscribe (allyDoor, "contact.open", doorHandler)
}

def doorHandler(evt) {

	log.trace "doorHandler($evt.name: $evt.value)"
    def timeNow = now()
	log.trace "current time is ${timeNow}"
    def delay = 5000
    runIn(timeNow + delay, openTooLong)
}

def openTooLong() {
	log.trace "running part 2"
}

(Im Brian) #2

This sounds fairly similar to what you’re talking about: https://github.com/imbrianj/door_knocker/blob/master/door_knocker.groovy


(DawgOnKing) #3

@imbrian

Thanks. Yeah, they are nearly identical.

His declaration:
def delay = knockDelay ?: 5

What does this mean in groovy guys? It’s similar to the declaration in “Open Too Long” :
def threshold = ((openThreshold != null && openThreshold != “”) ? openThreshold * 60000 : 60000) - 1000

… which I interpret to mean if openThreshold isn’t null or blank then it’s the input value * 60000 — but I don’t get the colon.


(Brian Steere) #4

It’s a ternary operator. Shortcut for if/else.

So the above translates to

if(openThreshold != null && openThreshold != "") {
    def threshold = openThreshold * 60000
} else {
    def threshold = 60000
}

threshold = threshold - 1000

(DawgOnKing) #5

@dianoga

Thanks!