Motion Sensor to turn off light/plug/switch?


(Kenneth C) #1

Hello,

I picked up a Smartsense Motion Sensor in hopes that it would turn off a plug when it stopped sensing motion. I found a Smartapp under “Kids” to turn control lights which seemed suitable, but it only seems to function in the evenings (or specifically after sunset). Without looking at the app itself, it seems to have a rule for “don’t turn on the lights during the day time”.

So a bit more detail about what I’m trying to accomplish. Its basically automating the shutdown of a 3D-printer when a print is done. The printer is plugged into a Leviton appliance plug and the motion sensor is placed to monitor the movement of the printbed. When the print is done, the bed should stop. After 20 minutes, I want the printer to power off. Useful for long prints which almost always seem to end in the early morning hours…

Anyone have a different app I can try? Or is there a setting I can specify in the current smartapp? Perhaps the sunrise off-set?

TIA!


(Keith Croshaw) #2

I think you can create a hello home action “when things quiet down” to turn off a switch.

click on the little chat bubble in the top right. Then the gear at the bottom of the hello home screen. Create a new action to turn off the switch. Set it to automatically happen when things quiet down.

Then create another one to turn it on when “things happen”


(Kenneth C) #3

Thanks for the help! I need to understand modes a bit better and maybe apply to this scenario.

I should expand on what I’m trying to do. Its basically automating the shutdown of a 3D-printer when a print is done. The printer is plugged into a Leviton appliance plug and the motion sensor is placed to monitor the movement of the bed. When the print is done, the bed should stop. After 20 minutes, I want the printer to power off. (Original post modified with this information)

I went down the path of creating a virtual switch which gave me the action of “light off when no motion”. But I can’t figure out how to associate the switch to the plug… probably need to dig around a bit more…


(Keith Croshaw) #4

Cool, I saw the word kids and thought you meant kid motion plus light… I need to read things. Still I think this might apply to your situation. Minus the 20 min offset.


(Brad) #5

Hi all,

A user wrote in asking for something similar. He wanted his aquarium pump to turn off when he was present and turn back on when he left. I threw this together very quickly. Feel free to modify/build off of it:

https://github.com/dbradmit/SmartThings/blob/master/Occupancy%20Off


(Kenneth C) #6

@Brad_ST awesome! Thanks! It’s a bit reverse of what I wanted :). But its a good time to learn groovy.

Do you have a set of commands to add a delay? Something like 20 minutes or user configurable?


(Kenneth C) #7

Just wanted to follow-up on this…

Success! :slight_smile: I was able to fumble my way through grovvy to accomplish what I wanted. WIth @Brad_ST’s initial script, I figured out how to add a 20 minute delay to the NoMotioneventHandler, to run a final switch off routine.

I’d like to figure out how to allow the user to specify the delay (currently its just hardcoded)… And probably not the prettiest, but for now it works. :smile:

/**
 *  Turn It Off When NO Motion
 *
 *  Author: Brad Mitchell (modded by KC)
 */
definition(
    name: "Turn It Off When Motion Stops v1.0",
    namespace: "smartthings",
    author: "SmartThings",
    description: "Turn something off when no motion detected.",
    category: "Convenience",
    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 motion stops..."){
		input "motion1", "capability.motionSensor", title: "Which motion sensor?"
	}
	section("Turn off switches..."){
		input "switches", "capability.switch", multiple: true, title: "Which switches/plugs?"
	}
}


def installed()
{
    subscribe(motion1, "motion.active", MotioneventHandler)
    subscribe(motion1, "motion.inactive", NoMotioneventHandler)
}

def updated(){
    unsubscribe()
    subscribe(motion1, "motion.active", MotioneventHandler)
    subscribe(motion1, "motion.inactive", NoMotioneventHandler)
}

def MotioneventHandler(evt) {
    log.debug "$evt.value: $evt, $settings"
    log.trace "Motion detected, leaving switches on: $switches"
    switches.on()
}

def NoMotioneventHandler(evt) {
    log.debug "$evt.value: $evt, $settings"
    log.trace "No motion detected, waiting to turning off switches: $switches"
    switches.on()
    def offDelay = 60 * 20
    runIn(offDelay, turnOffPlug)
}

def turnOffPlug(evt) {
    log.trace "Wait done, turning off switches: $switches"
    switches.off()
}

(Eric R) #8

Add another input in your preferences where the second parameter is number: input "delayOff", "number", title: "Delay to turn off"


(Kenneth C) #9

Thanks! I’ll give that a try.

While adding a motion sensor, I found the “Light On with Motion” Smartapp which is what I was essentially looking for. I guess it threw me of with “Light”… thought it only worked with a bulb or Hue.

It was a fun foray into groovy programming. :smile:


(Brad) #10

Sorry about my first code. I didn’t read well enough apparently. :smile: Nice work though!

Here’s a version of your SmartApp with a variable time delay:

    /**
 *  Turn It Off When NO Motion
 *
 *  Author: Brad Mitchell & KC
 */
definition(
    name: "Turn It Off When Motion Stops v1.0",
    namespace: "smartthings",
    author: "SmartThings",
    description: "Turn something off when no motion detected.",
    category: "Convenience",
    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 motion stops..."){
		input "motion1", "capability.motionSensor", title: "Which motion sensor?"
	}
	section("Turn off switches..."){
		input "switches", "capability.switch", multiple: true, title: "Which switches/plugs?"
	}
        section("Timeout Delay") {
        input "threshold", "number", title: "Minutes"
    }
}


def installed()
{
		subscribe(motion1, "motion.active", MotioneventHandler)
        subscribe(motion1, "motion.inactive", NoMotioneventHandler)
}

def updated()
{
	unsubscribe()
    subscribe(motion1, "motion.active", MotioneventHandler)
    subscribe(motion1, "motion.inactive", NoMotioneventHandler)
}

def MotioneventHandler(evt) {
	log.debug "$evt.value: $evt, $settings"
	log.trace "Motion detected, leaving switches on: $switches"
	switches.on()
}

def NoMotioneventHandler(evt) {
	log.debug "$evt.value: $evt, $settings"
	log.trace "No motion detected, waiting to turning off switches: $switches"
	switches.on()
    	def offDelay = threshold.toInteger() * 60
    	state.status = "waiting"
        log.debug "runIn($offDelay)"
        runIn(offDelay, turnOffPlug)
}

def turnOffPlug(evt) {
    log.trace "Wait done, turning off switches: $switches"
	switches.off()
}

(Kenneth C) #11

Thanks for the follow up.

I was able to stumble through adding the delay, but the thing I missed was resetting the delay on detection of motion. So on some prints the bed actually stops at the beginning of the project for ten seconds, which triggered the delay to start. Even though the motion is detected, the delay would not be reset. Therefore the printer would power off in 20 minutes.

So I copy and pasted a piece of code which schedules a check of the state of the motion sensor every minute. Now it seems to be working well.

When I get back to a laptop, I’ll update the thread with the final working code.


(Jason M) #12

I am trying to use Brad’s code for a specific purpose. I have 2 kids with rooms downstairs, they constantly leave their bedroom lights on. I installed Ecolink Z-Wave motion detectors in their rooms, I created the app using Brad’s code, I deleted the following portion of the code to avoid the lights coming on automatically when motion is detected.

The app turns off the lights 5 minutes after the motion detector determines there is no motion in the room. I don’t want the lights to go on automatically when they enter the room, I only want the lights to turn off when they leave (after being manually turned on). It is partially working that way now. The problem with the app is, when the kids manually turn out the lights at bedtime, the motion sensor is triggering the lights to go ON when no motion is detected (after they’ve fallen asleep). I was hoping someone could alter the code so that if the lights are already off, nothing is triggered when motion stops. Also, it would be great if I could set this to only happen certain times of the day.

I hope this explanation is clear and these are functions that can be added.

Thanks in advance. Sorry for dredging up such an old thread.


(Joe) #13

Did you ever figure this out? I want to do the exact some thing. I’d also like to make it so I can select multiple motion sensors and then if all of them are quiet, turn off the lights.

I can’t use a hello home action because it only allows you to select “When things quiet down” if you select a mode change. In this case I do not want to change the mode.


#14

One of the newest smartapps, #15 under convenience, is “lights off with no motion.” This may be what you want.


(Joe) #15

I saw that one, but everyone doesn’t have a presence sensor. I was looking to modify that in the IDE, but I didn’t see it as a template. Any way to get to the app so I can mod it for my needs?


#16

You can use a virtual presence sensor and just leave it on all the time.


(John S) #17

So… that one was so close to what I needed, I used it as a base for Lights of with No Motion Anywhere.

Waiting for the test run to verify it works - only change was to enable it working for a set of motion detectors, all of which have to go quiet for the timer countdown to begin. Will post the changes up once I am sure it works. Goal for that is to have a master “once everyone turns in, turn everything off” action.