Getting an event from a lock for ONLY when the lock is unlocked by a code

Again, I am NOT a developer but I think I figured this out. You need to subscribe to the event lastCodeName and not an event type of physical or digital.

def initialize() {
    log "Lock status: ${lock.displayName} " + lockStatus()
	subscribe(lock, "lastCodeName.Staci", unlockHandler2)
def unlockHandler2(evt) {
	log("Unlock event: ${evt.name} : ${evt.descriptionText}")
	if (evt.name == 'lastCodeName') {
		log "${lock.displayName} was unlocked by a Door Code"
		virtualLockedSwitch.off()
		if (virtualUnlockedSwitch) {
			virtualUnlockedSwitch.on()
		}
	} else {
		log "${lock.displayName} was unlocked"
	}
}

I am...but it's looking like it might be due to the HubConnect Lock driver not supporting that event.

Well this was a fun little learning experience. Sorry it isn't working for you but I will go ahead and post this here for anyone following this thread and wants to use this. I simplified my process and created the app specifically for the purpose of triggering for only when a code is used. I do not see a way with only Rule Machine to do this so it takes this extra app and virtual switch to be used within a rule. I know you could do everything within the app but I like to have the actions within a rule so I can do more with it if I want to.

Schalge Lock Reporter3

/*
Installation Notes:
You must create a Virtual Switch to be used in a rule so when it is on, you will perform an action.
The virtual switch should have the "Enable auto off" set to 5 seconds.

Schlage Lock Events for reference
Lock Name = "Front Door Lock" (Varies per installation)
Virtual Switch Name = "Front Door Code Used"  (Varies per installation)

Locked using keypad Schlage button or code.
Lock event: lock : Front Door Lock was locked by keypad [physical]

Locked using inside dial.
Lock event: lock : Front Door Lock was locked by thumb turn [physical]

Locked using automation.
Lock event: lock : Front Door Lock was locked [digital]

Unlocked using code.
Unlock event: lock : Front Door Lock was unlocked by John

Unlocked using inside dial.
Unlock event: lock : Front Door Lock was unlocked by thumb turn [physical]

Unlocked using automation.
Unlock event: lock : Front Door Lock was unlocked [digital]

Credit to @rvrolyk "Russ Vrolyk" for the original code this is based on.
*/


definition(
    name: "Schlage Lock User Code Reporter",
    namespace: "chipworkz",
    author: "chipworkz",
    description: "Triggers when a user code is used to unlock the door",
    category: "Convenience",
	iconUrl: "",
    iconX2Url: "",
    iconX3Url: "")


def lock = [
    name: "lock",
    type: "capability.lock",
    title: "Lock",
    description: "Select the lock to monitor.",
    required: true,
    multiple: false
]

def virtuaCodeUsedSwitch = [
    name: "virtuaCodeUsedSwitch",
    type: "capability.switch",
    title: "Virtual Code Used Switch",
    description: "Virtual switch that should be turned on when a code is used.",
    required: true,
    multiple: false
]

def enableLogging = [
    name:				"enableLogging",
    type:				"bool",
    title:				"Enable debug Logging?",
    defaultValue:		false,
    required:			true
]

preferences {
	page(name: "mainPage", title: "<b>Schlage Lock User Code Reporter:</b>", install: true, uninstall: true) {
		section("") {
			input lock
			input virtuaCodeUsedSwitch
		}
		section ("<b>Advanced Settings</b>") {
			input enableLogging
		}
	}
}

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

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

def initialize() {
    log "Lock status: ${lock.displayName} " + lockStatus()
	subscribe(lock, "lastCodeName", unlockHandler)
}

def lockStatus() {
	return lock.currentValue("lock")
}

def unlockHandler(evt) {
	log("Unlock event: ${evt.descriptionText}")
	if (evt.name == 'lastCodeName') {
		virtuaCodeUsedSwitch.on()
		}
}

def log(msg) {
    if (enableLogging) {
        log.debug msg
    }
}

As a follow up...this does NOT work in RM either.

That rule NEVER gets triggered when entering any valid code on the lock.

working for me.. i have kwikset locks with zigbee..

Hi @mike.maxwell ,
I can confirm that with latest firmware Jan 2020, the driver for Schlage locks is not emitting the lastCodeName event. When locking/unlocking via the keypad, only lock events are emitted. The only way to disambiguate keypad lock/unlock is to parse the description text, which I agree is undesirable. How can I help provide more detailed debug information. Because I see the full event details in the device Event page on the hub, I thought this could be an issue with the application so I tried multiple: Rule Machine 4.0 and WebCore. It's unclear whether this Hub Portal page consumes events using the same interface that applications use.

Some notes:

  1. The events available to Rule Machine and WebCore are the same: battery, lock, lock code, lock codes. lastCodeName is not an option.

  2. I'll use the WebCore application and the simplest of piston to illustrate that from the lock event, I can determine what device sent the event ($currentEventDevice), whether it was a lock or unlock action ($currentEventValue), and the description text ($currentEventDescription). I simply listen for
    all lock events and log these variables.

    execute
    on events from
    Laundry Room Door's lock
    do
    with
    Location
    do
    Log info "Device: {$currentEventDevice} Value: {$currentEventValue} Description: {$currentEventDescription}";

         end with;
     end on;
    

    end execute;

  3. Interestingly, using the same logging of event details, when I listen for lock code or the plural version (lock codes) events, and log the event details, no lock/unlock actions performed on the device causes log output. These events either a) have no relation to lock/unlock activity, b) no events are emitted or c) there is a bug in WebCore for bubbling these specific events to pistons. I could try the same thing in Rule Machine, but I'm betting the result will be the same. In any case, from what I'm reading on this community thread, it seems clear these "lock code" and "lock codes" events are out of scope for our goal.

I'd be delighted to provide any further debug info. Thanks a ton for all you do to support the community! As a brand new Hubitat user coming off SmartThings, I'm looking forward to being a more active community participant.

I should have been more clear: there is no new regression in the latest firmware. I observed this same behavior on (n-1) firmware also.

Another possibility is I misunderstand your earlier question to this discussion related to lastCodeName event. I assumed you meant there is a unique event named lastCodeName, and this is a distinct event from battery, lock, etc. emitted by the device driver. If this isn't the case, and lastCodeName is a property of a different event that I can't find in my sleuthing.

You can create a virtual lock and test out if there are differences. My understanding is if a known code (named code) is used, you should get the event as you describe.

If a code that is not known is used, you will not receive the lastCodeName event update/change.

LastCodeName is a real event.
Are you seeing events for it in your device logs?

Yes, I do see the event in the device logs for both physical devices and virtual lock device. That's why I'm a bit perplexed. lastCodeName is not an option to choose as a Trigger Event in Rule Machine, nor is the event an option to choose in WebCore.

For kicks, I created a virtual lock and created a test code. Same outcome. When I unlock or lock this virtual lock device using the test code, the Device log shows the lastCodeName event; however, this event is not available as a Trigger Event in Rule Machine, nor is the event an option to choose in WebCore. For whatever reason I'm not allowed to upload a JPG screenshot or put links to screenshots in posts, so if it would help, let me know where to drop them.

Again, happy to provide any other debug info, send logs, or whatever you need. Thanks!

1 Like

This can be reproduced using the virtual device lock, but the specific device driver for my physical locks is: Schlage BE468/469 Lock.

I'm having no problems with other events and the locks are working very well otherwise.

I know how to solve my problem using Description Test, I just want to provide the clarity that Mike and others need to resolve the case of the missing event =)

I can probably add this to the Auto Lock app pretty easily. Maybe as a choose your own adventure input string box for whatever you want to trigger on and use it to trigger a rule in rule machine.

1 Like

It's a curious case that no doubt Mike will educate on. If your application is able to consume the event, it would probably mean there is a bug in both the WebCore and Rule Machine app. That's why I asked the question about how the Hub portal is consuming these events because it clearly can. The Device log shows the event, but not clear if it's capturing it using the same interface that applications would use to consume the events.

It can read the descriptive text. Mike already said they weren't going to use that because there is no standard format for the descriptive text field. Using a user entered field let's you put whatever text you are looking for. Is that a bug? I already use descriptive text to notify for specific physical or virtual events. It's not a big leap to add a search string to compare the descriptive text event against.

@thebearmay it's also interesting to point out that for these Schlage locks, If I test a specific lock code the same as you show in your piston snip, it always returns true regardless what code is entered. Your second condition would cover up this bug. So if I unlocked the door using lock code #3, that first conditional returns true. Fun times! This was a regression I found when switching from SmartThings last week. I'm uncovering a few like this. Not sure if that would also be the case for the virtual device lock. If so, the bug probably is not specific to lock manufacturer.

The first one is the trigger, the expression is the condition.

Or give me a day or two and I could write a custom app that only did what I mentioned.

I can get at descriptive text also. That's working as I would expect and I could totally do some text parsing of that text to cludge something together. I agree with Mike on the point that anyone who relies on that text assuming it is a well-defined contract is risking their code to break in the future. He noted that this specific event is the recommended approach, and I'm noting here that I don't see this event being emitted. That's the bug. It could be a bug in the two rule applications I tried, or it could be a bug in the driver. If your Auto Lock app can consume this specific event, then it would tell me that there is a hungry hungry hippo somewhere inside Rule Machine and WebCore eating up events =)