Help with Code to flash an Outdoor Security Light via wall switch

Hi, Newbie here with no knowledge of code. I’m new to ST and and am having difficulty integrating an existing outdoor motion light into my system. I’m controlling the light via a GE wall switch and it comes on with a built in motion sensor automatically as long as the switch is on. When activated via built in motion sensor, it shuts the light off after a few minutes of no motion. The light has an override feature where if you shut the switch off then back on, it will override the motion sensor and leave the light on. I’d like to be able to have a routine where I have this light and other lights all turn on with one button. My other lights are no problem to integrate but I can’t get this one to integrate because of the code. I adapted some code I found to make the switch flash the light successfully and reliably but the app uses apptouch to be activated. I’d like to be able to use a virtual momentary switch to initiate the app. I’d truly appreciate any help on removing the apptouch trigger from this code and replacing it with a virtual momentary switch that the app asks you to select preferably. The code which I adapted from “BusyWait Test” is below.

preferences {
section(“Preferences”){
input “mySwitch”, “capability.switch”, title: “Switch to flash:”, required: true, multiple: true
input “pauseMillis”, “number”, title: “Pause duration in Milliseconds:”, required: true
}
}

def installed() {
subscribe(app, appTouch)
log.debug “Installed with settings: ${settings}”
}

def updated() {
unsubscribe()
subscribe(app, appTouch)
log.debug “Updated with settings: ${settings}”
}

def appTouch(evt) {
log.debug “appTouch with Event: $evt”

switchesOff()
runIn ( pauseMillis )
switchesOn()
runIn ( pauseMillis )
switchesOff()
runIn ( pauseMillis )
switchesOn()

log.debug "Exec complete."

}

def runIn (millis) {
def passed = 0
def now = new Date().time
log.debug “pausing… at Now: $now”
/* This loop is an impolite busywait. We need to be given a true sleep() method, please. */
while ( passed < millis ) {
passed = new Date().time - now
}
log.debug “… DONE pausing.”
}

def switchesOn() {
mySwitch.on()
def t = new Date()
log.debug “ON at time $t”
}

def switchesOff() {
mySwitch.off()
def t = new Date()
log.debug “OFF at time $t”
}

/* =========== /
/
End of File /
/
=========== */

2 Likes

runIn() is a built-in / reserved word method and is asynchronous, so this won’t do what you want.

Name your method something else!

Thanks a lot for the reply. I believe the original code I found (possibly posted by you) had
pause( pauseMillis )
which I found v2 doesn’t support and I found runIn() as a replacement. If this isn’t correct, can you suggest something better?

Interestingly enough, the code as is operates my wall switch and light perfectly and has been for quite some time now. Originally, when I first got the code to work (just had to switch the pause for runIn), I tried flashing at 600 milliseconds which ST had no problems doing, but was too fast for my light. I’m currently flashing at 1000 milliseconds which has worked flawlessly.

Really, my struggle is just modifying the code to be triggered by a virtual momentary switch vs appTouch so I can incorporate this light into one of my routines. Any help on that would be very much appreciated.

Thanks again!

Sorry to hear you couldn’t get it to work. Which light did you choose?

Teaching programming “via forum” isn’t really the purpose of the Community, nor very practical.
If you want tutoring for a reasonable fee, please PM me.

There’s nothing in V1 that V2 doesn’t support. “pause( millis)” was not a built-in method, but it was referred to in the code. It should still work just fine (for short durations).

This is “really easy” once you have a grasp of basic SmartApp programming: i.e., Requesting a device via an input statement, subscribing to the push() event of that device, and then calling a method from that subscription.

In fact, the Getting Started / SmartApps portion of the SmartThings Developer documentation is an excellent and quite relevant example. Definitely not a waste of time to read it.

http://docs.smartthings.com/en/latest/getting-started/first-smartapp.html

Thank you. I actually wound up modifying “the flasher” and it’s working well so far. All I had to do was to use two virtual momentary switches; one to trigger a light on/override motion flash sequence (1200ms flash) and the other to trigger a longer flash to set it back on motion. I also had to reverse the flash on and then off to flash off and then on in the code. So far so good. I’m sure there are other ways to do this more efficiently but so far so good. Thanks again for the above links. They certainly helped. I’m now able to use this in the routine I wanted.

1 Like