Auto lock door on close


I’m curious if you can give me some feedback. I wrote my first Smartthings app tonight. It appears to work great. However, I just wanted some professional feedback as to if I wrote all the code correctly. I look forward to any criticism! Also, if the app works well how can I go about getting it published?

 *  Lock It When It Closes
 *  Author: Jeremy R. Whittaker
preferences {
	section("When the door closes..."){
		input "contact1", "capability.contactSensor", title: "Where?"
	section("Lock the lock...") {
		input "lock1","capability.lock", multiple: true

def installed() {
	subscribe(contact1, "contact", contactHandler)

def updated() {
	subscribe(contact1, "contact", contactHandler)

def contactHandler(evt) {
	log.debug "$evt.value"
	if (evt.value == "closed") {

Looks good to me. I’m certainly not the most skilled developer here, but I’ve written a handful of programs and this one look good. I only have two suggestions (and minor ones at that) and one question:

Suggestion A.) Put in some documentation. I’m a big fan of this as it’s how I’ve learned to write anything I’ve written in SmartThings. Just looking at other programs, modifying them, and then seeing what happens. If you’re sharing you program with others (which you seem to be willing to do given that you posted it here) it’s always nice to see information in it to help others learn. For example I would do stuff like this:

subscribe(contact1, "contact", contactHandler) // We're subscribing to the door sensor so we can tell if anything happens to it.
def contactHandler(evt) { // This is the procedure that runs when the door sensor reports that something has happened.
if (evt.value == "closed") { // We're checking to see if the door close. If the sensor reports anything other than "closed" we're ignoring it.

Just my personal preference of course.

Suggestion B.) the log.debug line is useful when trying to trouble shoot what’s going on. But what you have here is perhaps a little too light on information. I would put something more like:

log.debug "${contact1.displayName} reported: $evt.value"

Like the documentation, this just helps to understand a little better what’s going on.

Both of these suggestions I totally a personal preference and really make no difference on the program’s operation of course.

Question: Did you intend to allow this program to lock multiple doors when one sensor is closed? In the preferences you’re allowing multiple locks to be selected. Nothing wrong with this if that is the intention, but just be aware that if you select multiple locks, then “lock1” represents ALL of the locks selected and the command lock1.lock() will lock all of them.


Also, if the app works well how can I go about getting it published?
At some point SmartThings plans to have an sort of "app store" where you can publish your apps and allow others to use (or possibly buy) them. This isn't possible yet, unfortunately. You can share them with others, but there are two things be know about this:

1.) Others will have full access to see your code. Obviously you’re not really concerned with this one as you’ve posted it already here. But if you work on something later and don’t want others to have your direct code, then don’t share it.

2.) Others will have to copy you code into their own program in order to use it. We talked about this in the thread about a Hallway night light if you’ll recall. So it isn’t as easy as just sharing it and others run it.

If you want to share you code, after publishing the program for your own use, go back to the the App Settings. This is where we gave the program a name and description. You’ll see a new option: "Program is shared? " Check the box here and then it’ll show up in the Shared SmartApps area of the IDE.

@chrisb thanks for the feedback. You’re right I don’t think it needs to lock multiple doors. I will work on modifying that later. Appreciate the other advice as well.

Excellent project! I just migrated my locks over from a Vera and I wasn’t sure how is was going to address this issue. I always prefer the options to select multiple items as opposed to a single item, adds flexibility.


I always prefer the options to select multiple items as opposed to a single item, adds flexibility.

In theory I agree, but in this case we’re specifically saying: “When this door closes, lock the the lock on this door.” I don’t think it makes sense to say: “If door x closes, lock the lock on door x and y.” Or even “If door x or y close, lock the lock on door x.” For this program there is a very specific one-to-one relationship.

@chrisb I thought about changing the code so that when the door you shut would lock when you shut it. But there may be instances in which you come home and another app unlocks all your doors. Once you enter your house and close your front door you may want to send a lock command to all the rest of your external doors. That is why I left the code as is.

@chrisb Ah, but it does if locks x and y are on the same door. Functionally, it worked for me because it does allow you to specify one door and many locks. I certainly understand it may be uncommon but that’s the flexibility that I find lacking in many ST apps, they tend to make too many limiting assumptions. Hope this makes sense… and that I’m not confusing my thinking with a different app.