Casper
(Casper)
April 8, 2016, 4:16am
1
I’m trying to figure out how to keep track of motion events, for example, I have piece of code as followed:
def initialized() {
subscribe(motion, “motion”, handler)
}
def hander(evt) {
if (evt.value == “active”) {
alertCount += 1
if (alertCount >= 5) executethis()
}
}
Is something like that even possible? I want to set up an alert for motion detector but I want the app to alert only if number of motion events is greater then threshold.
Any input appreciated
1 Like
slagle
(Tim Slagle)
April 8, 2016, 4:26am
2
Something like this should work
def initialized() {
subscribe(motion, "motion", handler)
def alertCount = 0
}
def hander(evt) {
if (evt.value == "active") {
alertCount = alertCount + 1
}
if (alertCount >= 5) {
executethis()
}
}
Casper
(Casper)
April 8, 2016, 4:33am
3
Thanks, but I got error
java.lang.NullPointerException: Cannot execute null+null
at: alertCount = alertCount + 1
slagle
(Tim Slagle)
April 8, 2016, 4:42am
4
Gah, you’re right, didn’t think that one through…
def initialized() {
subscribe(motion, "motion", handler)
def state.alertCount = 0
}
def hander(evt) {
if (evt.value == "active") {
state.alertCount = state.alertCount + 1
}
if (alertCount >= 5) {
executethis()
}
}
Casper
(Casper)
April 8, 2016, 4:48am
5
slagle:
def
I had to take def
out of def state.alertCount = 0
to be able to compile…but that variable is still null when it’s passed to handler()
slagle
(Tim Slagle)
April 8, 2016, 5:14am
6
Haha it’s been a long day sorry, lol…
Still null eh? That seems weird…
So it still says it can’t add one to the state?
Casper
(Casper)
April 8, 2016, 5:21am
7
It works now, I reversed
def state.alertCount = 0
subscribe(motion, “motion”, handler)
and reinstalled the app not sure which change helped
Thanks for ur help
1 Like
greg
(Greg)
April 8, 2016, 11:44am
8
This app sounds really useful, especially if your motion count can be spread over multiple motion detectors, do you share your code?
1 Like
Casper
(Casper)
April 8, 2016, 5:29pm
9
@greg Of course, I actually modified an existing app called “Turn off with motion” by @Kristopher_Kubicki available on ST SmartApps Lights/switches section. What it does is turning off a device if motion is detected, in my case I need to turn off my IP camera (simulated motion detector) but I want a threshold of alerts it in case it is me who triggers motion.
Original source: https://github.com/KristopherKubicki/smartapp-turn-off-with-motion
My source, feel free to use: https://github.com/casper-gh/smartapp-turn-off-with-motion-with-threshold
This app does more than just sending out alert, but you could remove extra code if needed
2 Likes