SmartThings Community

Check if it's locked app - what am I missing?


(Greg) #1

ok I modified someone’s code where it checks to see if a door is open. I wanted to add the feature of checking to see if a lock is locked as well.

Here is the code and my log messages.

preferences {
section(“Which mode changes trigger the check?”) {
input “newMode”, “mode”, title: “Which?”, multiple: true, required: false
}
section(“When should I check? (once per day)”) {
input “timeToCheck”, “time”, title: “(Optional)”, required: false
}
section(“Which doors should I check?”){
input “door”, “capability.contactSensor”, title: “Which?”, multiple: false, required: true
input “lock”, “capability.lock”, title: “Which?”, multiple: true, required: false
}
}

def installed() {
log.debug “Installed with settings: ${settings}”

initialize()

}

def updated() {
log.debug “Updated with settings: ${settings}”

unsubscribe()
unschedule()
initialize()

}

def initialize() {
if (newMode != null) {
subscribe(location, modeChangeHandler)
}
if (timeToCheck != null) {
schedule(timeToday(timeToCheck, location.timeZone), checkDoor)
}
}

def modeChangeHandler(evt) {
log.debug “Mode change to: ${evt.value}”

// Have to handle when they select one mode or multiple
if (newMode.any{ it == evt.value } || newMode == evt.value) {
    checkDoor()
    checkLock()
}

}

def checkDoor() {
log.debug "Door ${door.displayName} is ${door.currentContact}"
if (door.currentContact == “open”) {
def msg = "${door.displayName} was left open!"
log.info msg
sendPush(msg)
} else {
log.debug “It wasn’t open.”
}
}

def checkLock() {
log.debug "Lock ${lock.displayName} is ${lock.currentLock}"
if (lock.currentLock == “unlocked”) {
def msg = "${lock.displayName} is unlocked!"
log.info msg
sendPush(msg)
} else {
log.debug “It wasn’t unlocked.”
}
}

1:55:32 PM:
debug
It wasn’t unlocked.

1:55:32 PM:
debug
Lock [Front Door] is [unlocked]


1:55:32 PM:
debug
It wasn't open.


1:55:32 PM:
debug
Door Garage Door is closed


1:55:32 PM:
debug
Mode change to: Experimental

I think the error must be in here

def checkLock() {
log.debug "Lock ${lock.displayName} is ${lock.currentLock}"
if (lock.currentLock == “unlocked”) {
def msg = "${lock.displayName} is unlocked!"
log.info msg
sendPush(msg)
} else {
log.debug “It wasn’t unlocked.”
}
}

but it says if it is unlocked send message but it keeps doing the “else”. Suggestions?


(Barry) #2

Try using lock.latestValue(‘lock’) instead.


(Greg) #3

@storageanarchy thanks for the suggestion but that isn’t working either


(Greg) #4

I still can’t get this to work. Says it wasn’t unlocked when it clearly is unlocked.

def checkLock() {
log.debug "Lock ${lock.displayName} is ${lock.currentLock}"
if (lock.latestValue(“lock”) == “unlocked”) {
def msg = "${lock.displayName} is unlocked!"
log.info msg
sendPush(msg)
} else {
log.debug “It wasn’t unlocked.”
}
}

7:32:45 PM:
debug
It wasn’t unlocked.

7:32:45 PM:
debug
Lock [Front Door] is [unlocked]

(Chris) #5

I know I’m coming to this party a little late, and maybe you already solved the issue (it has been over two years after all) but it looks like the problem with the original code you had was that you never subscribed to the lock handler for changes. See documentation here: http://docs.smartthings.com/en/latest/capabilities-reference.html#lock

Anyway, I’m in need of a similar app (check to make sure my door really locked after it was supposed to. Had an issue last night, even though the door sensor was closed… It’s complicated). I can make the necessary changes to the smart app but probably won’t have a chance to until this weekend at the earliest. I’ll post my code when I complete it if nobody gets it working first.


(Chris) #6

I finally got this done and tested. It will work to check multiple locks, and builds a message of all the lock(s) that are unlocked on either mode change or at a specific time. If interested, you can find it here: