Light on on Knock (Acceleration)

First SmartApp. Writing this so my light comes on when there’s a knock on my door. Based on Smart Nightlight

Stops after set time.

/**
 *  Accelerometer Light
 *
 *  Author: Chuck Norris
 *
 */
definition(
    name: "XLR8 Light",
    namespace: "Norris",
    author: "The Norris",
    description: "Turns on lights when movement is detected. Turns lights off when it becomes light or some time after motion ceases.",
    category: "Convenience",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_motion-outlet-luminance.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_motion-outlet-luminance@2x.png"
)

preferences {
    section("Control these lights..."){
        input "lights", "capability.switch", multiple: true
    }
    section("Turning on when there's movement..."){
        input "accel", "capability.accelerationSensor", title: "Where?"
    }
    section("And then off when it's light or there's been no movement for..."){
        input "delayMinutes", "number", title: "Minutes?"
    }
    
    
}

def installed() {
    initialize()
}

def updated() {
    unsubscribe()
    unschedule()
    initialize()
}

def initialize() {
    subscribe(accel, "acceleration", accelHandler)


}



def accelHandler(evt) {
    log.debug "$evt.name: $evt.value"
    if (evt.value == "active") {
        if (true) {
            log.debug "turning on lights due to motion"
            lights.on()
            state.lastStatus = "on"
        }
        state.motionStopTime = null
    }
    else {
        state.motionStopTime = now()
        if(delayMinutes) {
            runIn(delayMinutes*60, turnOffMotionAfterDelay, [overwrite: false])
        } else {
            turnOffMotionAfterDelay()
        }
    }
}



def turnOffMotionAfterDelay() {
    log.trace "In turnOffMotionAfterDelay, state.motionStopTime = $state.motionStopTime, state.lastStatus = $state.lastStatus"
    if (state.motionStopTime && state.lastStatus != "off") {
        def elapsed = now() - state.motionStopTime
        log.trace "elapsed = $elapsed"
        if (elapsed >= ((delayMinutes ?: 0) * 60000L) - 2000) {
            log.debug "Turning off lights"
            lights.off()
            state.lastStatus = "off"
        }
    }
}
16 Likes

Nicely done Chuck Norris! Worked for me!

3 Likes

I just moved your thread to SmartApps>Community Created SmartApps so others can easily find it.

How did Chuck Norris write a SmartApp? He probably got the IDE to write one for himself. JK. Welcome to the community!

2 Likes

What a great Idea! Home intruders in the US will likely knock to see if someone is home. Light goes on, whoop, someone might be home - they’re out of there!

Good work.

Maybe add a slight delay [option] to turn the light on? That way we can emulate a person’s reaction time to the knock.

EDIT: Bravo on writing in last state into the code to make sure the app doesn’t inadvertently turn off a light if it was already on. I wish some of the built in ST apps did this too.

4 Likes

Hmmph.

Chuck Norris doesn’t use SmartThings to turn on a light, he points his fist at a light and it turns on.

2 Likes

That would be awesome. Light turns on, and an integrated sonos to say “Who is it?” :slight_smile:

1 Like

Thanks! this is great! I installed two instances. One for the front and one for the back door.Good job!

I made a very funny mistake with this app:

I have a garage door, with sensor and companion LFM-20. These are not automated, although I can control it if need be. I installed Light on on Knock, and accidentally selected the LFM-20 instead of the light. So, knock on the garage door, IT OPENS!! Not the security feature I was looking for! I discovered it trying to show off how cool it is to knock on the door and have the light come on. OOPS. :open_mouth:

2 Likes

Yeesh! That’s pretty funny, but very terrible for security! haha. Glad you caught it on early!

Check out this Secret Knock Detecting Door Lock: https://www.youtube.com/watch?v=zE5PGeh2K9k, or the original project: http://grathio.com/2009/11/secret_knock_detecting_door_lock/

1 Like

Definitely something I was thinking about over the weekend, Bruce.


:facepunch:

That would be a hoot! You’d have to look back at the events. I don’t know if those sensors report frequently enough for it to work or not.

1 Like

What device do you use for this SmartApp? It looks like a motion sensor, but that wouldn’t be able to detect the sound of a knock.

That would be a SmartSense Multi Sensor.

Here’s a modified version that has a delayed start, in seconds.
/**
* Accelerometer Light
*
* Author: Chuck Norris
*
*/
definition(
name: “XLR8 Light Plus”,
namespace: “slonob”,
author: “The Norris, Jarrod Stenberg”,
description: “Turns on lights when movement is detected after delay. Turns lights off when it becomes light or some time after motion ceases.”,
category: “Convenience”,
iconUrl: “https://s3.amazonaws.com/smartapp-icons/Meta/light_motion-outlet-luminance.png”,
iconX2Url: “https://s3.amazonaws.com/smartapp-icons/Meta/light_motion-outlet-luminance@2x.png
)

preferences {
    section("Control these lights..."){
        input "lights", "capability.switch", multiple: true
    }
    section("Turning on when there's movement..."){
        input "accel", "capability.accelerationSensor", title: "Where?"
    }
    section("Delay turning on to simulate human response..."){
        input "delayStartSeconds", "number", title: "Seconds:"
    }
    section("And then off when it's light or there's been no movement for..."){
        input "delayStopMinutes", "number", title: "Minutes:"
    }
    
    
}

def installed() {
    initialize()
}

def updated() {
    unsubscribe()
    unschedule()
    initialize()
}

def initialize() {
    subscribe(accel, "acceleration", accelHandler)


}



def accelHandler(evt) {
    log.debug "$evt.name: $evt.value"
    if (evt.value == "active") {
        if (true) {
            log.debug "turning on lights due to motion with $delayStartSeconds seconds delay"
            runIn(delayStartSeconds, turnOnLights, [overwrite: false])            
        }
        state.motionStopTime = null
    }
    else {
        state.motionStopTime = now()
        if(delayStopMinutes) {
            runIn(delayStopMinutes*60, turnOffMotionAfterDelay, [overwrite: false])
        } else {
            turnOffMotionAfterDelay()
        }
    }
}

def turnOnLights() {
	lights.on()
	state.lastStatus = "on"
}

def turnOffMotionAfterDelay() {
    log.trace "In turnOffMotionAfterDelay, state.motionStopTime = $state.motionStopTime, state.lastStatus = $state.lastStatus"
    if (state.motionStopTime && state.lastStatus != "off") {
        def elapsed = now() - state.motionStopTime
        log.trace "elapsed = $elapsed"
        if (elapsed >= ((delayStopMinutes ?: 0) * 60000L) - 2000) {
            log.debug "Turning off lights"
            lights.off()
            state.lastStatus = "off"
        }
    }
}

Definitely going to keep going with making stuff like this (slight mod in this case, thanks Mr. Norris). I think this is an excellent burglar deterrent.

I added more lights to it to emulate movement in the house:
/**
* Accelerometer Light
*
* Author: Chuck Norris, modified by Slonob
*
*/
definition(
name: “XLR8 Light Plus”,
namespace: “slonob”,
author: “The Norris, Jarrod Stenberg”,
description: “Turns on lights when a knock is detected. Can delay turning on multiple lights to simulate someone being home. Turns lights off when it becomes light or some time after motion ceases.”,
category: “Convenience”,
iconUrl: “https://s3.amazonaws.com/smartapp-icons/Meta/light_motion-outlet-luminance.png”,
iconX2Url: “https://s3.amazonaws.com/smartapp-icons/Meta/light_motion-outlet-luminance@2x.png
)

preferences {
    section("First light(s):"){
        input "lights1", "capability.switch", multiple: true
    }
    section("First light delay to simulate occupancy (recommend 5-10):"){
        input "delayStartSec1", "number", title: "Seconds:"
    }
    section("Second light(s):"){
        input "lights2", "capability.switch", multiple: true
    }
    section("Second light delay to simulate occupancy (recommend 8-15):"){
        input "delayStartSec2", "number", title: "Seconds:"
    }
    section("Triggering sensor:"){
        input "accel", "capability.accelerationSensor", title: "Where?"
    }
    section("Turn lights off when there's been no movement after:"){
        input "delayStopMinutes", "number", title: "Minutes:"
    }
    
    
}

def installed() {
    initialize()
}

def updated() {
    unsubscribe()
    unschedule()
    initialize()
}

def initialize() {
    subscribe(accel, "acceleration", accelHandler)


}

def accelHandler(evt) {
    log.debug "$evt.name: $evt.value"
    if (evt.value == "active") {
		log.debug "turning on lights due to motion with $delayStartSec1, $delayStartSec2 seconds delay"
        runIn(delayStartSec1, turnOnLights1, [overwrite: false])            
        runIn(delayStartSec2, turnOnLights2, [overwrite: false])            
        state.motionStopTime = null
    }
    else {
        state.motionStopTime = now()
        if(delayStopMinutes) {
            runIn(delayStopMinutes*60, turnOffMotionAfterDelay, [overwrite: false])
        } else {
            turnOffMotionAfterDelay()
        }
    }
}

def turnOnLights1() {
	lights1.on()
}

def turnOnLights2() {
	lights2.on()
	state.lastStatus = "on"
}

def turnOffMotionAfterDelay() {
    log.trace "In turnOffMotionAfterDelay, state.motionStopTime = $state.motionStopTime, state.lastStatus = $state.lastStatus"
    if (state.motionStopTime && state.lastStatus != "off") {
        def elapsed = now() - state.motionStopTime
        log.trace "elapsed = $elapsed"
        if (elapsed >= ((delayStopMinutes ?: 0) * 60000L) - 2000) {
            log.debug "Turning off lights"
            lights1.off()
            lights2.off()
            state.lastStatus = "off"
        }
    }
}