Is it possible to keep the threshold of motion events?

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

Something like this should work :slight_smile:

def initialized() {
  subscribe(motion, "motion", handler)
  def alertCount = 0
}

def hander(evt) {
  if (evt.value == "active") {
    alertCount = alertCount + 1
  }   
  if (alertCount >= 5) {
    executethis()
  }   
}

Thanks, but I got error

java.lang.NullPointerException: Cannot execute null+null

at: alertCount = alertCount + 1

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()
  }   
}

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() :confused:

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?

It works now, I reversed

def state.alertCount = 0
subscribe(motion, “motion”, handler)

and reinstalled the app :smiley: not sure which change helped

Thanks for ur help

1 Like

This app sounds really useful, especially if your motion count can be spread over multiple motion detectors, do you share your code?

1 Like

@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