If - Then Statement w/ Lock Status


(Jeff L) #1

Hi folks, after 4 hours working at this I can’t for the life of me figure out how to correctly poll the current condition of a device on my network. Specifically I’m trying to get the current state of my schalge z-wave lock. My code is below and clearly I’m doing something wrong. Can someone please point me in the right direction?

-Jeff

definition(
name: “Alarm When Moved & Locked”,
namespace: “everest123”,
description: “Set off a notification and/or trigger an alarm is something moves while a door is locked. Useful as an immediate notifier is someone is trying to break down a door that is sensed with a Smartthings Multi.”,
category: “Safety & Security”,
iconUrl: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png”,
iconX2Url: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png”,
iconX3Url: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png”)

preferences {
section(“When vibrations are detected…”) {
input “accelerationSensor”, “capability.accelerationSensor”, title: “Which Sensor?”
}

section("And this door is locked...") {
	input "lock1", "capability.lock", multiple: true, title: "Which Door?"
}

section("Alarm settings and actions") {
	input "alarms", "capability.alarm", title: "Which Alarm(s)", multiple: true, required: false
	input "silent", "text", title: "Silent alarm only (Yes/No)"
	input "seconds", "number", title: "Delay in seconds before siren sounds"
}

}

def installed() {
log.debug "Installed with settings: ${settings}"
unsubscribe()
initialize()
}

def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
}

def initialize() {
unsubscribe()
subscribe(accelerationSensor, “acceleration.active”, accelerationActiveHandler)
subscribe(lock1, “lock”)
}

def accelerationActiveHandler(evt) {
log.debug "Detected Vibrations on the Door! Okay, working now 12."
if (lock1.lock == “locked”) {
log.debug “Made it inside!!!”
} else {
log.debug “Skipped the conditional statement…again :frowning: :frowning: :(”
}

}


(Jeff L) #2

I’m sorry, I have no idea how to post code properly so it’s easy to read. So sorry.


(Geko) #3

Use this:

if (lock1.currentValue("lock") == "locked") {

(Jeff L) #4

No, unfortunately that doesn’t work. I think I tried that somewhere around hour 2.5! LOL! For anyone reviewing, the crux of the problem is below (with the suggested code above). The door is locked. But the if statement is universally ignored no matter what I do.

-Jeff

def accelerationActiveHandler(evt) {
log.debug "Detected Vibrations on the Door! Okay, working now 13."
if (lock1.currentValue(“lock”) == “locked”) {
log.debug “Made it inside!!!”
} else {
log.debug “Skipped the conditional statement…again :frowning: :frowning: :(”
}

}


(Ron S) #5

Do you have lock set to multiple ; true? In that case you have an array of locks and your statement will fail?

Saw your settings in the code above, you do have multiple set to true on locks.


(Jeff L) #6

Actually, the problem may be with how I’ve subscribed to the lock. I’m seeing this in my logs: lock from Safe Room Deadbolt was provided with null…creating subscription

That doesn’t seem healthy - but I’m feeling along in the dark here.

-Jeff


(Jim Anderson) #7

That warning is telling you that you created a subscription to the lock, but didn’t provide an event handler.

That may not explain why you’re not entering that if block, however. I think @smart is on to something about the multiple: true. Since it’s set to multiple, you’ll have an array of locks. Here’s a code snippet from the “Lock it When I Leave” example smart app (you can find this in the IDE). It uses multiple locks, and then to find out if any are unlocked:

def anyUnlocked = lock1.count{it.currentLock == "locked"} != lock1.size()

But if you don’t have multiple locks, probably easiest just to not set multiple: true


(Ron S) #8

If you have multiple set to true, you will always get an array back and you have to iterate thru the collection to get the status. That line of code will never work unless you get rid of multiple if you are using a single lock or iterate thru it to get the status of individual locks in the array in case of multiple. Hope this helps.


(Geko) #9

Yes, @smart is right, I missed that part, but lock1.lock won’t work anyway.


(Jeff L) #10

Thanks guys, I’ve got it now!!! I’m working on notifications now and will post the whole project once it’s done. It’s not a bad app. If you use a multi to monitor door status and put the multi on the frame, it’ll alarm/notify if someone is trying to pry/batter the door. It’ll alarm is someone knocks if it’s on the door iteself, so that’s not so good. I can’t figure out how to adjust the sensitivity on the multi.

-Jeff


(Keith Croshaw) #11

Hello, any progress with this SmartApp? I’m trying to work with lock status as well.


(Jeff L) #12

Yes, I figured this out. Here’s the code I’m using that uses a multi-sensor (on my door frame) in conjunction with a Schlage Camelot lock, and a Utilitech siren. It works fine. The motion sensing part of the multi is on the door frame, which is steel. So it takes a good pounding to trigger vibration. If there is vibration and the door is locked. . .the alarm goes off and I get notified. One minor item, I treat the utilitech like a switch so it stays triggered when it goes off. The critical code is below and the entire code is below as well.

if (lock1.currentValue(“lock”) == “locked”) {
log.debug "$accelerationSensor has vibrated, triggering alarm & notifications"
alarm1.on()
SendNotification()
} else {
log.debug “Acceleration detected, but the lock is open.”
}

-Jeff

/**

  • Alarm When Moved & Locked
  • Copyright 2014 Jeff Lunglhofer
  • Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except
  • in compliance with the License. You may obtain a copy of the License at:
  •  http://www.apache.org/licenses/LICENSE-2.0
    
  • Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
  • on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
  • for the specific language governing permissions and limitations under the License.

*/
definition(
name: “Alarm When Moved & Locked”,
namespace: “everest123”,
author: “Jeff Lunglhofer”,
description: “Set off a notification and/or trigger an alarm is something moves while a door is locked. Useful as an immediate notifier is someone is trying to break down a door that is sensed with a Smartthings Multi.”,
category: “Safety & Security”,
iconUrl: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png”,
iconX2Url: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png”,
iconX3Url: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png”)

preferences {
section(“When vibrations are detected…”) {
input “accelerationSensor”, “capability.accelerationSensor”, title: “Which Acceleration/Vibration Sensor?”, required: true
}

section("And this door is locked...") {
	input "lock1", "capability.lock", title: "Which Door?", required: true
}

section("Alarm settings and actions") {
	input "alarm1", "capability.switch", title: "Which Alarm(s)", required: true
}
    
section("Send SMS Notification?"){
	input "smsnotify", "enum", title: "Send a SMS notification?", required: false, options: ["Yes","No"]
	input "phone1", "phone", title: "Phone Number?", required: false
}

}

def installed() {
log.debug "Installed with settings: ${settings}"
unsubscribe()
initialize()
}

def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
}

def initialize() {
unsubscribe()
subscribe(accelerationSensor, “acceleration.active”, acceleration)
subscribe(lock1, “lock”, null)
subscribe(alarm1, “alarm”, null)
state.alarmActive = null
}

def acceleration(evt) {
/* log.debug “Detected Vibrations on the Door! Okay, working now 16.” */
if (lock1.currentValue(“lock”) == “locked”) {
log.debug "$accelerationSensor has vibrated, triggering alarm & notifications"
alarm1.on()
SendNotification()
} else {
log.debug “Acceleration detected, but the lock is open.”
}

}

def SendNotification() {

sendPush("${accelerationSensor.label} - TAMPER ALERT")
if (smsnotify) {
	log.debug "$accelerationSensor has moved, texting $phone1"
	sendSms(phone1, "${accelerationSensor.label} - TAMPER ALERT")
}

}


(Keith Croshaw) #13

Thanks, and darn, I was hoping you figured out how to account for multiple doors. Although I guess if you have multiple doors with multiple sensors it’s hard to figure out which one is tied to which.


(Jeff L) #14

Well. . .you could simply run multiple instances of that app for each door/sensor combo. It’s possible to allow for multiple “pairings” in the app but that would require additional coding. If you can tell me what you want to do I’ll think about how it might be done.

-Jeff


(Keith Croshaw) #15

Well luckily what I’m trying to do is much more simple than all of this. I was thinking much more hypothetical.

I’m simply trying to turn a light on if any door is unlock. As simple as it sounds I’m having some difficulties.

The stock app has one that if a door is open turn a light on and i’ve found some bits and pieces throughout the community.


(Keith Croshaw) #16

I guess I’m mostly having a hard time understanding how to check the state of a lock.


(Jeff L) #17

For that use case I don’t think you need to check the status of any of your locks. You just need to respond to any unlock event by turning on a light. I’ll work up a sample to illustrate.


(Keith Croshaw) #18

So far I have this thanks to @geko, but I need to check the status upon initialization too.


(Eric R) #19

You don’t need these in your code. You only need to subscribe to something if you are waiting from an event from that device. Even if you don’t subscribe to lock1, lock1.currentValue("lock") == "locked" should still work


(Jeff L) #20

I just propsed a fix on Github for you. But I just tested it and the code below works fine. I only tweaked a few things.

-Jeff

-edited to remove the erroneously posted code, see below for working code