Adding dynamic property, using static or global vars?

I’m working on a door-knocking app. I have a SmartSense Multi on my front door and it can detect if the door is shut and if there’s a knock. Awesome. However, I’m getting a false positive for every time I shut the door (the contact is closed and its acceleration is active).

What I’d like to do is add a custom property to the multi (without having to tinker with writing a whole new custom device type) to register the last time the door was open. If the door was opened within a set amount of time, it’s probably just you shutting or locking the door. If adding a property to an existing object is not possible, is there a way to use a static variable within a function - or even a global var to store this last timestamp?

Sorry for the noob questions, java / groovy are new to me.

I’d look to handling this in the code of the app rather than in the devise type.

What I would look at doing is subscribing to when the sensor is opened and closed. When it’s opened use change a state variable to false, and then when close run a procedure in a minute to change the state.

Something like this:

def doorSensorOpen(evt) {
     state.doorClosed = false
     }

When the door opens, we change the state variable to false.

def doorSensorsClose(evt) {
     runIn(60, closeState)
     }

def closeState() {
     state.doorClosed = true
     }

What this does is when the door closes, it schedule the ‘closeState’ procedure to run in 60 seconds. This gives a 1 minute for the accelerator time to stop moving.

Then in your procedure where you run the actions when acceleration happens, put in a conditional:

def accelerationCheck {
     if (state.doorClosed = true) {
          Insert you code to do stuff here
     }
}

That way you need acceleration AND the variable needs to say closed. And that won’t say closed until the door has been closed for at least a minute.

Ahh - the “state” variable was the secret sauce I was looking for. I can load state.lastOpen = now() when a door is open and check for elapsed time when a knock is felt to see if the door’s been opened recently.

Thanks so much for all your help!

Just a personal preference probably, but I’d base it on when the door is closed. If someone were to leave the door open for a while and the time expired then you’d get a false positive when the door closed.

Fair enough - I’ll give that a go. I did have that set originally but moved it to “open” for a reason I can no longer recall. I was juggling things around a lot trying to sort out a way of storing the timestamp, so I’m sure it’s irrelevant now. Updated on SmartThings - will update github later.