Notify if lock opened with a particular code


(Chris Cooney) #1

I just installed a Yale deadbolt, and when the lock is unlocked, I’m getting this event coming across telling me the user (the particular code) that unlocked the lock. In this example user 1.
Z-Wave Lock was unlocked with code 1
Name Value
data {“usedCode”:1}

I’d like to build an app that notifies me when a particular code is used (User 3). But I’m not sure what to look for to trigger the event. Would this be the proper format? I don’t see any shared apps that reference the actual lock code.
subscribe(lock1, “usedCode.3”, eventHandler)

Thanks in advance!


(Florian Z) #2

You can subscribe to the lock event like this:

subscribe(lockInput, "lock", lockHandler)

Your event handler would look like this:

def lockHandler(evt) {
   if (evt.value == "unlocked") {
      def usedCode = evt.data?.usedCode as Integer
      if (usedCode == 3) {
         sendPush("Door was unlocked with code ${usedCode}")
      }
   }
}

AFAIK, the lock device handler will supply the used code through evt.data.usedCode.


(Chris Cooney) #3

Thanks! I should have time this weekend to implement.


(Morgan) #4

Chris if you implement this, could you share your app? If I have time, I put this app together as well.


(Morgan) #5

@florianz where do you find the documentation for the properties on the different of devices for a zwave lock or switch? I want to get the details form the activity log but I don’t know where the documentation for the properties that are available are.


(Chris Cooney) #6

Yes, I’ll look at it this weekend and publish once complete.


(Morgan) #7

I took a look at it, and I was not getting any data in the evt.data. I looked at a few other examples including the Change User codes, and the virtual and physical locks report different results but I was not able to get which user code was used when unlocking it.

The activity log of physical lock reported it, so the data is there just not sure I was using the correct property.


(Florian Z) #8

@thrash99er - Unfortunately, documentation is sorely lacking. I usually look at the event log of the device in question under the “My Devices” tab of the web IDE. Also, if you’re in the code editor for a custom device handler, you can click on “Device Type Examples” in the top-right corner, to take a look at the source code of the available device handlers. You can tell what kind of events and properties the device provides.

Sorry I don’t have any better ideas. We are told that better documentation is coming very soon.


(Jeff DeWolfe) #9

In case you haven’t seen this: http://build.smartthings.com/zwave.html#configurationV1

Hoping it might help you!


(Morgan) #10

Ah the device type examples has some good code in it. I’ll see if I can hack up my app to get it to work to send a notification when a particular user code is used to unlock the lock.


(Rikard Lundqvist) #11

I have a Schlage lock and the number of the code used to unlock the lock can be extracted from both evt.data and evt.descriptionText.

BTW: Can somebody explain why it appears that the format of evt.data have changed in the last month? Had to go through all SmartApps to change the parsing. Also, is there a better way to parse {“userCode”:3} than to simply compare the whole string or parse out the number? Both methods are quite crude and gives “ugly” code that is prone to stop working as soon as anything is changed. Is there a parser “function” that already takes care of this?


(Tod) #12

@rikard - I agree, it is ugly, and I hope ST creates a better way to obtain the userCode.

For reference to anyone else looking to do the same - first I did just a compare with the evt.descriptionText - but I ended up switching it to a little more dynamic method.

My app changes the mode based on a specific usedCode, triggered by a lock event on an assigned lock. In my case, 2 is the usedCode that is assigned to security code that is used to unlock the door. I set that as a number, which entered in with the preferences on install (along with the lock and the newMode).


def LockHandler(evt) {
if (evt.value == “unlocked”) {
if (evt.data == ‘{“usedCode”:’+code1+’}’ && location.mode != newMode)
{
setLocationMode(newMode)
}
}

Painful, yes - but it gets the job done.


(Florian Z) #13

No need to reinvent the wheel. The data is formatted as a JSON string, and Groovy comes with a json parser:

def data = new JsonSlurper().parseText(evt.data)
log.debug data.usedCode

Make sure to import the JsonSlurper. This line should come before the metadata section:

import groovy.json.JsonSlurper

(Morgan) #14

Has anyone got this to work with the Kwik 910 z-wave lock?


(Tod) #15

@thrash99er - yep, mine is a Kwikset lock, same model as yours. I got it working with the code I wrote and provided above. If you need more detail let me know. I have not revised it to use @FlorianZ’s approach though, I figured it was working, but if I modify it in the future, I’ll use it.


(Alex) #16

@todbrock, @florianz

How do I access “data” property of an event?

When I call evt.data, I get an error “No such property data for class Event”.


(Greg) #17

can anyone post the entire code for this? want to use it with a Schlage Camelot Touchscreen


(Chris Cooney) #18

At one point with this logic I was able to get a notification, but when I tried to also change the mode, I messed something up. Now this doesn’t seem to work, but I imagine it’s something very small because it does compile properly. You can use this as a guide, but it doesn’t appear to do anything in its current form, and I’ve had it…

definition(
name: “Notify Me When Cleaners Open Door”,
namespace: “”,
author: “CCooney”,
description: “Notifies when a specific user code is used on door.”,
category: “Safety & Security”,
iconUrl: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png”,
iconX2Url: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png”)

preferences
{
section(“When a door unlocks…”) {
input “lock1”, “capability.lock”
}
section(“And this user opened the door:”){
input “User1”, “enum”, title: “User Code”, metadata:[values:[“1”,“2”,“3”,“4”,“5”]]
}
section(“Send this message (optional, sends standard status message if not specified)”){
input “messageText”, “text”, title: “Message Text”, required: false
}
section( “Notifications” ) {
input “sendPushMessage”, “enum”, title: “Send a push notification?”, metadata:[values:[“Yes”,“No”]], required:false
input “phone”, “phone”, title: “Send a Text Message?”, required: false
}
section(“And change to this mode”) {
input “newMode”, “mode”, title: “Mode?”, required: false
}

}

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

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

def subscribeToEvents() {

log.debug "Settings: ${settings}"
subscribe(lock1, "lock", eventHandler)

}

def eventHandler(evt) {
if (evt.value == “unlocked”) {
if (evt.data == ‘{“usedCode”:’+User1+’}’)
{ send(evt) }

}}
private send(evt) {
if ( sendPushMessage != “No” ) {
log.debug( “sending push message” )
sendPush( msg )
}

if ( phone ) {
	log.debug( "sending text message" )
	sendSms( phone, msg )
}

log.debug msg

}


(Barry) #19

This might be of interest:


#20

Thanks, chriscooney, that’s just what I needed. I think the reason your version isn’t working is that you store the text of the message in messageText, and then call sendPush or sendSms with msg instead.