Here some code I was adapting for something very similar. I not mine originally.
It loaded as an app. And you have to create virtual switches for each type of event which can then be used in rules. This reads the event log for the lock. I have not fully tested the digital lock/unlock section so its commented out.
name: "Kwikset Lock Events",
namespace: "kuzenkohome",
author: "Mike Kuzenko",
description: "Tracks when a lock is manually or keypad controlled and updates virtual switches",
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 virtualManLockedSwitch = [
name: "virtualManLockedSwitch",
type: "capability.switch",
title: "Virtual Manual Lock Switch",
description: "Virtual switch that should be turned on when lock is manually locked.",
required: true,
multiple: false
]
def virtualManUnlockedSwitch = [
name: "virtualManUnlockedSwitch",
type: "capability.switch",
title: "Virtual Manual Unlock Switch",
description: "Virtual switch that should be turned on when lock is manually unlocked.",
required: true,
multiple: false
]
def virtualKeypadLockedSwitch = [
name: "virtualKeypadLockedSwitch",
type: "capability.switch",
title: "Virtual Keypad Lock Switch",
description: "Virtual switch that should be turned on when lock is locked by Keypad.",
required: true,
multiple: false
]
def virtualKeypadUnLockedSwitch = [
name: "virtualKeypadUnLockedSwitch",
type: "capability.switch",
title: "Virtual Keypad Unlock Switch",
description: "Virtual switch that should be turned on when lock is unlocked by Keypad.",
required: true,
multiple: false
]
def enableLogging = [
name: "enableLogging",
type: "bool",
title: "Enable debug Logging?",
defaultValue: false,
required: true
]
preferences {
page(name: "mainPage", title: "<b>Lock to monitor:</b>", install: true, uninstall: true) {
section("") {
input lock
input virtualManLockedSwitch
input virtualManUnlockedSwitch
input virtualKeypadLockedSwitch
input virtualKeypadUnLockedSwitch
label title: "Assign an app name", required: false
}
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, "lock.locked", manualLockHandler)
subscribe(lock, "lock.locked", keypadLockHandler)
subscribe(lock, "lock.unlocked", manualUnlockHandler)
subscribe(lock, "lock.unlocked", keypadUnlockHandler)
//subscribe(lock, "lock.unlocked", digitalUnlockHandler)
//subscribe(lock, "lock.locked", digitalLockHandler)
}
def lockStatus() {
return lock.currentValue("lock")
}
def keypadLockHandler(evt) {
log("Lock event: ${evt.name} : ${evt.descriptionText}")
if (evt.type == 'physical' && evt.descriptionText.endsWith('locked by keypad')) {
log "${lock.displayName} was locked by keypad"
virtualManLockedSwitch.off()
virtualManUnlockedSwitch.off()
virtualKeypadLockedSwitch.on()
virtualKeypadUnLockedSwitch.off()
} else {
log "${lock.displayName} was locked"
}
}
def keypadUnlockHandler(evt) {
log("Unlock event: ${evt.name} : ${evt.descriptionText}")
if (evt.type == 'physical' && evt.descriptionText.contains('unlocked by code')) {
log "${lock.displayName} was unlocked by keypad code"
virtualManLockedSwitch.off()
virtualManUnlockedSwitch.off()
virtualKeypadLockedSwitch.off()
virtualKeypadUnLockedSwitch.on()
} else {
log "${lock.displayName} was unlocked"
}
}
def manualLockHandler(evt) {
log("Lock event: ${evt.name} : ${evt.descriptionText}")
if (evt.type == 'physical' && evt.descriptionText.endsWith('locked by manual')) {
log "${lock.displayName} was locked manually"
virtualManLockedSwitch.on()
virtualManUnlockedSwitch.off()
virtualKeypadLockedSwitch.off()
virtualKeypadUnLockedSwitch.off()
} else {
log "${lock.displayName} was locked"
}
}
def manualUnlockHandler(evt) {
log("Unlock event: ${evt.name} : ${evt.descriptionText}")
if (evt.type == 'physical' && evt.descriptionText.endsWith('unlocked by manual')) {
log "${lock.displayName} was unlocked manually"
virtualManLockedSwitch.off()
virtualManUnlockedSwitch.on()
virtualKeypadLockedSwitch.off()
virtualKeypadUnLockedSwitch.off()
} else {
log "${lock.displayName} was unlocked"
}
}
/*
def digitalLockHandler(evt) {
log("Lock event: ${evt.name} : ${evt.descriptionText}")
if (evt.type == 'digital' && evt.descriptionText.endsWith('locked by digital command')) {
log "${lock.displayName} was locked digitally"
virtualLockedSwitch.on()
virtualUnlockedSwitch.off()
virtualKeypadLockedSwitch.off()
} else {
log "${lock.displayName} was locked"
}
}
def digitalUnlockHandler(evt) {
log("Unlock event: ${evt.name} : ${evt.descriptionText}")
if (evt.type == 'digital' && evt.descriptionText.endsWith('unlocked by digital command')) {
log "${lock.displayName} was unlocked digitally"
virtualLockedSwitch.off()
virtualUnlockedSwitch.on()
virtualKeypadLockedSwitch.off()
} else {
log "${lock.displayName} was unlocked"
}
}
*/
def log(msg) {
if (enableLogging) {
log.debug msg
}
}
Hope this helps
Mike