New to writing SmartApps- Could use a little help, as Im a newbie to coding and to SmartThings:
Trying to use my Garage Lock as a door sensor: as it is typically always locked: ie; when the lock is unlocked the door is considered open. This would save me from having to add a door sensor on the door as well.
The goal - Turn on garage lights when lock is unlocked, turn off after 15 minutes
I’ve tried to modify the code from the “Turn it on when it opens” code to be applicable to the Lock as opposed to a door sensor.
Heres what I have so far, havent added the timer part yet. Any help would be appreciated!
/**
* Turn It On When It Opens
*
* Author: SmartThings
*/
definition(
name: "Turn It On When Garage Lock Opens",
namespace: "smartthings",
author: "SmartThings",
description: "Turn something on when an open/close sensor opens.",
category: "Safety & Security",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_contact-outlet.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_contact-outlet@2x.png"
)
preferences {
section("When the door opens..."){
input "Lock1", "capability.lock", title: "Where?"
}
section("Turn on a light..."){
input "switches", "capability.switch", multiple: true
}
}
def installed()
{
subscribe(lock1, "lock.open", contactOpenHandler)
}
def updated()
{
unsubscribe()
subscribe(lock1, "lock.open", contactOpenHandler)
}
def contactOpenHandler(evt) {
log.debug "$evt.value: $evt, $settings"
log.trace "Turning on switches: $switches"
switches.on()
}
LOL… thought you had everything working but the timer. Sorry, no experience with locks, but I seem to remember reading that the integration does not allow the lock to send an event when manually actuated. Don’t know if this is a shortcoming with the lock or the SmartThings integration.
Hopefully someone with lock experience can chime in here.
[quote=“scottinpollock, post:4, topic:5610”]
LOL… thought you had everything working but the timer. Sorry, no experience with locks, but I seem to remember reading that the integration does not allow the lock to send an event when manually actuated. Don’t know if this is a shortcoming with the lock or the SmartThings integration.[/quote]
Yeah, that’s my first thought as well. I’m pretty sure that manually turning the lock will not register with ST. If you use a code or use the smartapp to unlock it should update, but if you’re manually flipping the lock (or using a key?) it doesn’t register right away.
I think that’s the case at least. If I remember I’ll do some testing with my kwikset lock tonight and report back.
Briefly, that lines says: Watch for events from the device “lock1”. Specifically look for “.open” event. When that happens, run the contactOpenHandler procedure.
Because a lock doesn’t ever report as open (only lock or unlock) it’s watching for that .open event and never see it, so never turns on your light. If we change that to unlock, it’ll hopefully work.
Figured it out, Thank you ChrisB, that helped, and if someone could explain to me what I actually did I’d be much obliged, as I’m not sure why this implementation is working!! lol.
So my understanding that that it subscribes to the Lock1, waiting for the lock.open status; but was then immediately unsubscribing to it. I could be wrong, as I mentioned, I am just getting started in writing these.
The def installed() process runs whenever the application is first installed. In the case of this smartapp it just does one thing: Subscribes to the item as discussed above.
When you uninstall a smart all that will automatically unsubscribe from anything that you subscribed to in the app. But what about if you just update an app? If you don’t unsubscribe from items that you subscribed to you could run into problems, especially if the app was changed and re-published between install and update.
So, just like the installed process is run when you install, the update process is run when you update a SmartApp. In this case we’re doing two things:
Cleaning up by unsubscribing to anything this SmartApp subscribed too.
Subscribe to the necessary events (the lock unlocking.)
Generally speaking install() and update() should look identical except for the unsubscribe() event at the beginning of the procedure.
Now, to contradict myself, there is another possibility of things to add to update(): unschedule()
Some apps schedule events using a couple of different methods. If you write or work on a SmartApp that does this you should put unschedule() in the updated() procedure on the line right after unsubscribe(). This will “cleanup” any scheduled events this app is running.