Kwikset 914 autolock application

I was having a hard time finding a Smart App for my exact needs, so I decided to hobble some together and build my own. I thought others might find it useful. The most critical things I needed was:

  1. I don’t have an open/close sensor on the door - so I just want it to lock when it’s been left open
  2. Unlike the built in functionality of the lock, I only want it to basically auto lock at night or hours that I set
  3. When it does have to auto lock, I want a text

This is pretty bare bones… I hope you all like it…

Russ

/**
 *  Russ Door Lock
 *
 *  Copyright 2015 Russ Pearlman
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License. You may obtain a copy of the License at:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
 *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the specific language governing permissions and limitations under the License.
 *
 */
definition(
    name: "Russ Lock Door",
    namespace: "russbp",
    author: "Russ Pearlman",
    description: "Automatically lock doors",
    category: "My Apps",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
    iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")
    
preferences
{
    section("Which door lock?") {
        input "lock1", "capability.lock"
    }
    section("Lock after how many minutes?") {
        input "minutesLater", "number", title: "When?"
    }
    section("During these hours?") {
    	input "startTime", "time", title: "Start?", required: false
    	input "endTime", "time", title: "End?", required: false
    }
    section( "Notifications" ) {
		input "sendPushMessage", "enum", title: "Send a push notification?", metadata:[values:["Yes", "No"]], required: false
		input "phoneNumber", "phone", title: "Enter phone number to send text notification.", required: false
	}
}

def installed()
{
    log.debug "Russ Lock Door installed. (URL: http://www.github.com/smartthings-users/smartapp.auto-lock-door)"
    initialize()
}

def updated()
{
    unsubscribe()
    unschedule()
    log.debug "Russ Lock Door updated."
    initialize()
}

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

def lockDoor()
{
	log.debug "lockDoor()"
	log.debug "Locking ${lock1}."
    lock1.lock()
    log.debug ( "Sending Push Notification..." ) 
    if ( sendPushMessage != "No" ) sendPush( "${lock1} locked because it was left unlocked for ${minutesLater} minutes." )
    log.debug("Sending text message...")
    if ( phoneNumber != null ) sendSms( phoneNumber, "${lock1} locked because it was left unlocked for ${minutesLater} minutes.")
}

def doorHandler(evt)
{
	log.debug "doorHandler(evt)"

    log.debug "Lock ${evt.name} is ${evt.value}."
    
    //Always cancel scheduled event if we get a lock notice
    if (evt.value == "locked") 
    {                  
        log.debug "Cancelling previous lock task..."
        unschedule( lockDoor )                 
    }
 
    //If we get an unlock, potentially schedule the relock
    if (evt.value == "unlocked")
    {
    	if(startTime != null && endTime !=null)
    	{
			def startTimeOfDay = timeToday(startTime)
    		def endTimeOfDay = timeToday(endTime)
        	def nowTime = now()
    		log.debug "Start ${startTimeOfDay.time}."
    		log.debug "End ${endTimeOfDay.time}."
    		log.debug "Now ${nowTime}."
			if(nowTime >= startTimeOfDay.time && nowTime <= endTimeOfDay.time)
    		{
   				log.debug "Within time!"
            	def delay = minutesLater * 60       
        		log.debug "Re-arming lock in ${minutesLater} minutes (${delay}s)."
        		runIn( delay, lockDoor )   
			} 
   	 		else
   	 		{
   				log.debug "Not within time!"
			}  
    	}
    	//If they haven't specified a time interval, go ahead and schedule on unlock
    	else
    	{     
    		log.debug "No valid schedule."
        	def delay = minutesLater * 60       
        	log.debug "Re-arming lock in ${minutesLater} minutes (${delay}s)."
        	runIn( delay, lockDoor )               
    	}
	}
}

Apparently I suck at formatting - hopefully someone who knows what they are doing with that might be able to help.

Use to </> format option in the editor formatting bar here.

Or surround the entire code with three back single quotes ` before and after.

Or give GitHub a try… It’s definitely not as hard as it looks to create a single project and paste in code.

Great!

You can also use the pencil :pencil2: icon below any of your posts to edit them instead of reposting (unless the pencil doesn’t exist yet for new users perhaps). :wink:

It did, and thanks for the help. Sadly, I’m a fairly well educated and skilled developers (at least I was 10-15 years ago). So you’d think I’d know how to use a basic forum!

1 Like

I made a huge mistake in the original code. If you had the time set to overnight, it wouldn’t work (e.g., from 10pm to 6am). I’ve made some updates to fix this so it works as expected. Here is the new code:

/**
 *  Russ Door Lock
 *
 *  Copyright 2015 Russ Pearlman
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License. You may obtain a copy of the License at:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
 *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the specific language governing permissions and limitations under the License.
 *
 */
definition(
    name: "Russ Lock Door",
    namespace: "russbp",
    author: "Russ Pearlman",
    description: "Automatically lock doors",
    category: "My Apps",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
    iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")
    
preferences
{
    section("Which door lock?") {
        input "lock1", "capability.lock"
    }
    section("Lock after how many minutes?") {
        input "minutesLater", "number", title: "When?"
    }
    section("During these hours?") {
    	input "startTime", "time", title: "Start?", required: false
    	input "endTime", "time", title: "End?", required: false
    }
    section( "Notifications" ) {
		input "sendPushMessage", "enum", title: "Send a push notification?", metadata:[values:["Yes", "No"]], required: false
		input "phoneNumber", "phone", title: "Enter phone number to send text notification.", required: false
	}
}

def installed()
{
    log.debug "Russ Lock Door installed. (URL: http://www.github.com/smartthings-users/smartapp.auto-lock-door)"
    initialize()
}

def updated()
{
    unsubscribe()
    unschedule()
    log.debug "Russ Lock Door updated."
    initialize()
}

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

def lockDoor()
{
	log.debug "lockDoor()"
	log.debug "Locking ${lock1}."
    lock1.lock()
    
    if ( sendPushMessage != "No" ) 
    {
    	log.debug ( "Sending Push Notification..." ) 
    	sendPush( "${lock1} locked because it was left unlocked for ${minutesLater} minutes." )
   	}
    
    if ( phoneNumber != null )
    {
    	log.debug("Sending text message...")
    	sendSms( phoneNumber, "${lock1} locked because it was left unlocked for ${minutesLater} minutes.")
   	}
}

def doorHandler(evt)
{
	log.debug "doorHandler(evt)"

    log.debug "Lock ${evt.name} is ${evt.value}."
    
    //Always cancel scheduled event if we get a lock notice
    if (evt.value == "locked") 
    {                  
        log.debug "Cancelling previous lock task..."
        unschedule( lockDoor )                 
    }
 
    //If we get an unlock, potentially schedule the relock
    if (evt.value == "unlocked")
    {
    	if(startTime != null && endTime !=null)
    	{
			def startTimeOfDay = timeToday(startTime)
    		def endTimeOfDay = timeToday(endTime)
        	def nowTime = now()
    		log.debug "Start ${startTimeOfDay.time}."
    		log.debug "End ${endTimeOfDay.time}."
    		log.debug "Now ${nowTime}."
            
            //Overnight
            if(endTimeOfDay.time < startTimeOfDay.time)
            {
            	log.debug "Within time! (Overnight)"
 				if(nowTime >= startTimeOfDay.time || nowTime <= endTimeOfDay.time)
    			{
   					log.debug "Within time!"
            		def delay = minutesLater * 60       
        			log.debug "Re-arming lock in ${minutesLater} minutes (${delay}s)."
        			runIn( delay, lockDoor )   
				} 
   	 			else
   	 			{
   					log.debug "Not within time!"
				}
            }
            else
            {
              	log.debug "Within time! (Not Overnight)"
 				if(nowTime >= startTimeOfDay.time && nowTime <= endTimeOfDay.time)
    			{
   					log.debug "Within time!"
            		def delay = minutesLater * 60       
        			log.debug "Re-arming lock in ${minutesLater} minutes (${delay}s)."
        			runIn( delay, lockDoor )   
				} 
   	 			else
   	 			{
   					log.debug "Not within time!"
				}
           }
            
 
    	}
    	//If they haven't specified a time interval, go ahead and schedule on unlock
    	else
    	{     
    		log.debug "No valid schedule."
        	def delay = minutesLater * 60       
        	log.debug "Re-arming lock in ${minutesLater} minutes (${delay}s)."
        	runIn( delay, lockDoor )               
    	}
	}
}
1 Like