Turn on Switch if multisensor closed, stay on when multisensor opens?


#1

I am automating my chicken coop door to open (turn on switch) only if multisensor shows it is closed. Works fine EXCEPT as soon as the sensor open, the power to the outlet ceases and the door opens only an inch or so. I want the power to stay on for 2 minutes. Here is the applicable code. What am I doing wrong??

def contactHandler(evt){
state.contact1Now = evt.value
}

def appTouch(evt) {
if (state.contact1Now == ‘closed’){
switch1.on()
def fiveMinuteDelay = 60 * 2
runIn(fiveMinuteDelay, turnOffSwitch)
}
}
def turnOffSwitch() {
switch1.off()
}


#2

What device (brand and model) is providing the power? And what DTH are you using for it?


#3

It’s a Sylvania smart plug which works just fine for all my other lights and such that I want to turn on or off. I am using a few of them around the house. The multisensor is the newest one from Smartthings. The DTH is the standard Smartthings one for that brand of outlet, it finds and pairs the outlet switch without issue or installation of any device handler, and as I said, the switch works fine as long as it is not tied to the multisensor being open or closed. I can duplicate the problem by having a light go on when the door is opened, and the light then goes off when the door is closed and the multisensor closes. I want it to stay on. Thanks for any help you may have.


(Tony Fleisher) #4

The code you posted doesn’t look to have any obvious problems (other than calling your variable “fiveMinuteDelay”).
What do the event subscription lines look like?


#5

Yes, it was originally 5 minutes, but I only need 2 to give it enough time to open the door all the way. Here is all of it except for the definition. Seems like it should be so easy.

preferences {
section(“Turn on switch for 2 minutes…”){
input “switch1”, “capability.switch”}
section(“Based on this sensor”) {
input “contact1”, “capability.contactSensor”}
}

def installed()
{
subscribe(app, appTouch)
subscribe(contact1, “contact”, contactHandler)
}
def updated()
{
unsubscribe()
subscribe(app, appTouch)
subscribe(contact1, “contact”, contactHandler)
}
def contactHandler(evt){
state.contact1Now = evt.value
}

def appTouch(evt) {
if (state.contact1Now == ‘closed’){
switch1.on()
def fiveMinuteDelay = 60 * 2
runIn(fiveMinuteDelay, turnOffSwitch)
}
}


#6

If I manually keep the contact switch closed, it works perfectly, turns on the power for two minutes, then shuts it off. It is as soon as the contact opens that the power immediately shuts off.


(Tony Fleisher) #7

Are you sure the off command is coming from this app and not something else?


#8

Forgot the last lines of the code.

def turnOffSwitch() {
switch1.off()
}


#9

I do have other apps that use this switch, but just for testing. This is the only live app and it requires touching it to activate. If you see nothing wrong with the code, should I delete the device and reinstall it with a different name in case one of the other apps might be calling it? Is there some easy way to delete all my obsolete smartapps from the list?


#10

Looking at all my test apps (I’m a noob at this) I created one that closes the door when the multisensor is open, and this one that opens the door when the multisensor is closed. Both are activated by appTouch. Could they be conflicting even though I don’t press both?


(Tony Fleisher) #11

You can Check in the events list (recently tab) for the switch and see if it has information on what app sent the off command.


#12

I put a log.info “turning off switch” right before the switch.off() command and it is never called, yet it goes off anyway. Let me look at the events list and see what I can find.


#13

You were right! I had other iterations of this and another test app using this multisensor that were still installed and sending commands when it opened or closed. Once I uninstalled them, this app works as expected. So simple to resolve, but so easy for me to miss. Thank you for your help.