Get time since last event

hi.

i wrote a little app that turns the light on when i get home; i.e. when a my presents sensor is detected.
it worked fine for a long time.
now in the middle of the night the light turns on. i checked the log and sure enough for 1 minutes the presents sensors was gone and then re-detected. so the house thought i was gone and arrived again.

so, i am wondering how i could add to my app a minimum time gone check?

thanks,
stephan.

You’d have to subscribe to both “present” and “not present” events. In your “not present” event handler save current time in the state hash, then in your “present” event handler, calculate elapsed time and decide whether to turn on the light or not.

@geko
thanks.

i looked around and could not find an example that shows how to subscribe to “not present”.

right now i do this:


def installed() {
	log.debug "Installed with settings: ${settings}"
	log.debug "Current mode = ${location.mode}, people = ${people.collect{it.label + ': ' + it.currentPresence}}"
	state.itsDarkOutside = false
    subscribe(people, "presence", presence)
    initialize()
}

def updated() {
	log.debug "Updated with settings: ${settings}"
	log.debug "Current mode = ${location.mode}, people = ${people.collect{it.label + ': ' + it.currentPresence}}"
	state.itsDarkOutside = false
    unsubscribe()
	subscribe(people, "presence", presence)
    initialize()
}

def presence(evt)
{
	log.debug "evt.name: $evt.value"
	def threshold = (falseAlarmThreshold != null && falseAlarmThreshold != "") ? (falseAlarmThreshold * 60 * 1000) as Long : 10 * 60 * 1000L


		def t0 = new Date(now() - threshold)
		if (evt.value == "present") {
….
}
}

full code: