Unschedule doesn’t always work

I have an app I wrote to monitor a door. When the door opens I do a
runIn(60,handler). If I see a close event before the timeout I do a
Unschedule(handler). This works most of the time.

However, on occasion I get a double open, close, open, close. (Not sure why). When this happens the unschedule doesn’t seem to work. It’s like it can’t unschedule twice fast enough or something.

It’s not a major issue, but wife wonders why echo says door left open when she is sure she closed it.

If your contact sensor generates duplicate events, your theory seems plausible to me: you could get two instances of your app running at the same time, and if the second event fires before the first instantiation of the app finishes un-doing the subscription, then it will still be in place when the second "open" event happens, waking the app again. The first place I'd try to address this is at the driver level: what kind of contact sensor is this, and what driver is it using? This was an issue with some (primarily? all?) Z-Wave sensors a while back, and some drivers added an option to suppress these. That is better since it would apply to anything on the platform that uses these events, not just a specific app.

If that isn't possible, I suppose you could implement some app-specific workaround. The first thing I'd try would probably be saving something to atomicState (I'd prefer this over state given that the latter doesn't get written until the app finishes execution), maybe a "last executed" time that you could compare to the current time and some cooldown period of your choosing (or perhaps an option in the app).

1 Like

I use to have a contact sensor on a closet that did that. I found by adjusting the width between the sensor and magnet fixed it. Not sure that is the type contact sensor you are using but I thought I would mention it. Sometimes it's a hardware problem.

1 Like

Download the Hubitat app