Accessing Motion Sensor Recent Activity


(N) #1

Hi all

Is it possible to access the history of motion sensors in smart apps? I’d like to be able to only trigger events in the room with the most recent activity. Ie, when an external door opens blink the lights in an occupied room only.

Any advise or thoughts greatly appreciated

-N


(Jim Anderson) #2

Yes, totally possible.

You can get the events for a device using device.events() (sorted in reverse chronological order).

http://docs.smartthings.com/en/latest/ref-docs/device-ref.html#events

So you could find all the “active” events, then sort to find the most recent one.

Here’s a quick n dirty pass at it (not tested), and I bet there are some ninjas out there who could whip something more terse and efficient, because something seems off about this. I look forward to seeing other solutions…

def getMostRecentMotion() {
    	def activeEvents = []
	motions.each { motion ->
    	    activeEvents += motion.events()?.findAll({it.value == "active"})
        }
        activeEvents?.max{it.date}?.device
}