Is there a global variable that contains all motion sensors so I can subscribe to all events without having to select them


(david) #1

below is some sample code with some commented areas to help ask the question, note: this code is written only to help the conversation. Once I get the answer, I’ll write finished code back so everyone can see it*

preferences {
section(“When there’s movement anywhere…”) {
input “allmotion”, “capability.motionSensor”, title: “What Motion Sensors”, multiple: true
}
}

def installed()
{
subscribe(allmotion, “motion.active”, motionActiveHandler)
}

def updated()
{
unsubscribe()
subscribe(allmotion, “motion.active”, motionActiveHandler)
}

/* start commenting code improvement question…
**instead of forcing user to select all motion sensors can the smartapp
**schedule once a day to resubscribe to allmotion sensors in case an new
**one was added that day. Something like this:

def someEventHandler(evt) {
** execute handler in 24 hours from now
runIn(60*24, handler)
}

def handler() {
//get the global list of allmotion sensors then …
unsubscribe()
subscribe(allmotion, “motion.active”, motionActiveHandler)
}

**end code improvement question/idea */

def motionActiveHandler(evt) {
//do something for all motion events
//log all motion events over time
}


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #2

No - not in the current/legacy “Groovy API”.

The security model required customers to select each device individually. Bulk selection was considered a security risk (i.e., Customers might accidentally offer a SmartApp more than intended); and adding new sensors automatically might also not be the intention of the Customer.