Toggle lights with motion sensor


(Nalexiou) #1

Hi everyone. I am new to SmartThings and the forum.

Here is my scenario:

I have a motion sensor in the bathroom. This is what I would like to do: when there is motion, if lights in that room are off, turn them on, otherwise, if lights in that room are on, turn them off. The smart app called “my light toggle” by Jesse Silverberg is the closest thing I have found. However, it requires a master switch and the only switches in my setup are hue lights. So, I need to have a light on all the time in order for this to work. Any ideas/help?

Thank you.


(Adrian Parker) #2

Hi,

Smart lighting app should do what you need.

It’s in the marketplace in the app

Ade


(Nalexiou) #3

@adeparker thanks Adrian. Are you referring to the Smart Lights app in the marketplace? Btw, is there a way to search for apps in the marketplace?


(Adrian Parker) #4

That’s the one!

I don’t believe there’s a way to search, no. Generally just a case of finding the right category


(Paul) #5

Are you trying to do this:

If the lights are OFF and there is motion, turn the lights ON.
If the lights are ON and there is motion, turn the lights OFF.

I don’t think Smart Lighting can do that. Without some custom code, you’re going to find yourself in a race condition where the lights blink on and off, I believe.

It absolutely could be done with a custom Smart App, however. You’d have to build in some delay.


(Nalexiou) #6

@NorCalLights yes. That is what I want to do. I figured that it would require some custom code. Do you have any suggestion on how to implement the delay? Is there some sample code that I can use to help me figure it out? I have to go through some documentation too to figure out what this entails.


(Paul) #7

Here, try this. Assuming your motion sensor has some reasonable motion delay on it, this should work fine.

preferences {
	section ("When motion is detected...") {
		input "motion1", "capability.motionSensor", title: "Where?"
	}
	section ("Toggle the state of these switches") {
		input "switches", "capability.switch", multiple: true
	}
}


def installed() {
	initialize()
}

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

def initialize() {
	subscribe(motion1, "motion.active", motionActiveHandler)
}

def motionActiveHandler(evt) {
    def currSwitches = switches.currentSwitch

    def onSwitches = currSwitches.findAll { switchVal ->
        switchVal == "on" ? true : false
    }
        
	if(onSwitches.size() == 0) {
    	switches.on()
    }
    else {
    	switches.off()
    }
}

FYI I think @JDRoberts uses motion sensors in boxes as “touchless” switches. He’s posted about it a few times. Maybe give a search and see if there’s overlap between what you’re doing and what he does.


#8

Hi @nalexiou,

EDIT, disregard my original reply. I guess I need to understand your scenario a little better.

I read that like this:

If someone walks into the bathroom ST should turn ON the lights, but if nobody is in the bathroom (no motion) that ST should turn them OFF.


(Nalexiou) #9

Thanks for all the replies. Here some more details about what I am trying to do. I use the smart things motion sensor. And yes, the touchless switches sounds like what I am trying to achieve. So, let’s say the initial state is lights off in the room. When there is motion by entering the room, the lights should turn on (toggle the state). Then I exit the room from the same door, after let’s say a couple of minutes, triggering motion on the same sensor and thus turn off the lights (toggle the state). The smart lights app does let me turn off the light but It does not have a condition to check if the lights are on or off. If I do create two tasks then I would think there would both trigger at the same time for the same sensor/motion, trying to turn on and off the lights.


(Paul) #10

Couldn’t you just use the basic Smart Lighting logic of: if there’s motion, turn the lights on. If there isn’t motion for 10 minutes, turn the lights off.

Based on your example, it seems like this would work.


(Nalexiou) #11

This would work. But if I wanted to stay in the room longer than 10 minutes, then the lights would turn off while I’m there. I just wanted them to turn off dynamically - that is, when I exit the room, whether that is in 2 minutes, 5 minutes, or 15 minutes.


(Paul) #12

As long as your motion sensor is positioned so it can “see” most of the bathroom, you should be fine. The timer will reset every time it detects any motion.

In the meantime, I’ll think about how to modify that code to do exactly what you’re asking.


(Jesse S) #13

Actually, I think “my light toggle” does what you asked for. The app was designed to allow for multiple lights to toggle together according to the status of one particular light. So if some are on and some are off, the toggle only cares about the master’s state and flips only the necessary lights. Try the app with one of your hues as the master switch and all 4 hues in the toggle field. Then, when a motion event gets detected, the smart app looks at the master hue to decide if they all should turn on or off.


(Nalexiou) #14

@GuyInATie hi Jesse! Thanks for joining the conversation. actually last night I gave your app another shot. I created a virtual simulated switch that I have on and added the lights I wanted switched on and off and it works! However I know noticed that the motion sensor, once it becomes activated, it stays active for 15 seconds or so until it sends no motion. I am not sure if this is related to having to re-pair the sensor with the hub. When I connected it the fist time, the motion status was pretty instant. Now, after having reset the sensor (did so b/c it’s stopped responding), the hub shows motion for at least 15 seconds with no motion. Is there something I can do to address that? A setting/calibration or should I try removing and adding the sensor again?[quote=“GuyInATie, post:13, topic:47375, full:true”]
Actually, I think “my light toggle” does what you asked for. The app was designed to allow for multiple lights to toggle together according to the status of one particular light. So if some are on and some are off, the toggle only cares about the master’s state and flips only the necessary lights. Try the app with one of your hues as the master switch and all 4 hues in the toggle field. Then, when a motion event gets detected, the smart app looks at the master hue to decide if they all should turn on or off.
[/quote]


(Jesse S) #15

@nalexiou Happy to – it’s my pleasure to offer any help you need with it!

The short answer about the sensor’s cooldown delay is that it depends on the make/model. I have 3 different types of motion sensors and I’ve found each one behaves slightly differently. As far as I know, the ST motion sensors are not adjustable in their cooldown time. My Ecolink pet-friendly motion sensors, on the other hand, are very adjustable. Inside the case there’s a jumper that allows for options ranging from 1 second to 4 minutes cooldown before updating their status with the hub.

I wasn’t clear from your last message – did the 15 second delay start occurring since you introduced the virtual switch & reset the connection, or did you just notice the 15 second delay after these steps? From experience, re-pairing devices hasn’t introduced any delays like the one you describe.


(Nalexiou) #16

@GuyInATie it is actually kind of hard to tell what happened. I am not sure if the 15 second delay is related to the virtual switch and/or your app. Actually, I believe I tried adding a virtual switch as a GE switch. Then, I tried using that and the virtual switch went OFFLINE and the motion sensor stopped responding - which makes no sense to me. So, I decided to start off from scratch, reset everything and reconnect the sensor. I’ll try playing around with the sensor when I get home and see if I can get a better idea of the problem.


(Jesse S) #17

The first thing I would do is to watch the IDE and see what the delay is with the motion sensor cool down. If the delay in there is about 15 seconds (which actually sounds about right for most of the sensors I’ve looked at), then that identifies what’s happening. On the other hand, if the IDE is seeing much shorter cooldowns, then you have a bit more sleuthing to do!

[I know you said you’re new to ST, but this is exactly how the addiction starts! As you read more on the forums, you’ll see other people talking about how easy it is to get sucked into HA as a hobby!]


(Nalexiou) #18

Thanks Jesse. Sorry for not responding sooner - did not have much time to play around with the setup. I now got the Ecolink sensor to try out since you mentioned it has a faster cool down (I moved the jumper to TEST). However, it registers motion for at least 6-10 seconds after motion has stopped. Do you have any ideas? How do I determine the exact time from the IDE?


(Jesse S) #19

No worries @nalexiou – it’s been a busy weekend!

In any case, I did a quick Google to find the Ecolink sensor manual and it says the test mode should have a 5 second refresh rate for motion:

https://support.smartthings.com/hc/en-us/article_attachments/200698694/Ecolink-PIRZWAVE2-ECO_manual_rev1.pdf

To get a precise measurement for your particular setup, log into the IDE and go to the live logging section. You should see events happen in real time as the ST platform picks them up. Since everything is time stamped with hh:mm:ss, you should get a good read on how responsive the sensor is. If this is longer than 5ish seconds, then the next thing to try is to bring the motion sensor near your hub and try the live logging experiment again – this will verify that there’s no meaningful interference slowing you down. The next thing to try is a simple SmartLighting automation (STs staple lighting SmartApp) with the motion sensor and a lightbulb. While this won’t let you toggle the light, it’ll give you an independent measurement of the response time and help diagnose your setup.

Let me know what you find!