I am new here. I tried to write my own SmartApp as shown below. The goal of the code is to lock the door 3-5 minutes after the door is closed. I have a separate contact sensor from the lock.
Here are my troubles:
(1) Currently, runIn() does not seem to work. If I understand correctly, runIn should trigger the lockDoor() 3-5 minutes after the door is closed. However, my door always lock immediately after the door is closed.
(2) Currently, unschedule() does not seem to work. If I understand correctly, if I schedule an action to the lock door 3-5 minutes later, but the door is open 1 minute later, then the contactHandler will be called and the unschedule() should cancel the lock action. However, my lcok still extends the latch (which is incorrect).
Suggestions/comments are strongly appreciated.
preferences
{
section("When a door unlocks...") {
input "lock1", "capability.lock"
}
section("When the door closes..."){
input "contact1", "capability.contactSensor", title: "Where?"
}
section("Lock it how many minutes after the door is closed?") {
input "minutesAfterClosed", "number", title: "When?"
}
}
def installed()
{
log.debug "Auto Lock Door installed."
initialize()
}
def updated()
{
unsubscribe()
unschedule()
log.debug "Auto Lock Door updated."
initialize()
}
def initialize()
{
log.debug "Settings: ${settings}"
subscribe(contact1, "contact", contactHandler)
}
def lockDoor()
{
log.debug "Locking the door"
lock1.lock()
}
def contactHandler(evt) // This procedure runs when the door sensor reports closed.
{
log.debug "The event is trigger by ${evt.displayName} with value ${evt.value}"
unschedule()
if (evt.value == "closed") { // If the door is closed
def secondsAfterClosed = minutesAfterClosed * 60
log.debug "Schedule to lock in ${secondsAfterClosed} seconds"
runIn ( secondsAfterClosed, lockDoor()) // Schedule to lock the door
}
}
Finally, in Unschedule, I typically also supply the name of the handler I want to unschedule - it might not make a difference in your app, but if you write one where you have multiple things scheduled and you only want to unschedule some of them, you can specify like so:
@ykchen913 - Did you get it to work? I tried something similar and found that unschedule() failed to prevent the function within runIn from being called.
Yes, it works for me. I am a little confused about the difference between runIn ( secondsAfterClosed, lockDoor) and runIn ( secondsAfterClosed, lockDoor()). @Steve, can you share your insight why “()” makes the difference? Thanks.