Contact Sensor closed within last x minutes

I’m new to writing apps for SmartThings. I’m trying to detect if a contact sensor was in an open state within a specified timeframe. The code below isn’t working reliably. If the log trace shows recent events, doorWasOpen is still false. The code is mostly copied from and modified from “It’s Too Cold”. Can someone explain the use of the {it.value == “”}
Thanks

def deltaMinutes = 10
def timeAgo = new Date(now() - (1000 * 60 * deltaMinutes).toLong())
def recentEvents = contact.eventsSince(timeAgo)?.findAll { it.name == "open" }
log.trace "Found ${recentEvents?.size() ?: 0} events in the last $deltaMinutes minutes"
def doorWasOpen = recentEvents.count { it.value == "open" } > 1

have a look at this app, it makes heavy use of past events.

1 Like

Dead link… too bad…

You realize this post is over 3 years old right? Mike is one of the founders of Hubitat and for obvious reasons pulled all his code. Search Google for “github zoneMotionChild” and maybe you will find it :wink:.

1 Like