Turn Off when Open App

I’ve been piecing together an app that will turn off an electrical outlet (which controls a window A/C unit) if a door is opened. It works well up to a point. What I’m having trouble on is when the door is open, I’m capturing the current state of the electrical outlet and storing it as a variable. This is so when the door is closed it returns the switch to the previous state rather than just turning it on. I’m able to grab the state and echo it out in debug so I’m positive the value is being captured. My problem is when I’m running that value through an IF statement to evaluate it. Even variation I try doesn’t match the stored value.
The value that’s displayed through log.debug is [on].
I’ve tried == “on”
== “[on]”
== [on]
== on
I’m sure I’m missing something simple, but I can’t figure out what that is. I hope that someone can point me in the right direction.

preferences {
section("When the door opens..."){
	input "contact", "capability.contactSensor", title: "Where?"
}
section("Turn off an outlet..."){
	input "switches", "capability.switch", multiple: true
}
section("How long to delay"){
	input "delayMinutes", "number", title: "Minutes?", required: false
}
}

def installed()
{
    subscribe(contact, "contact.open", contactOpenHandler)
    subscribe(contact, "contact.closed", contactClosedHandler)
}

def updated()
{
unsubscribe()
subscribe(contact, "contact.open", contactOpenHandler)
subscribe(contact, "contact.closed", contactClosedHandler)
}

def contactOpenHandler(evt) {
log.debug "$evt.value: $evt, $settings"
if (evt.value == "open") {
	log.debug "Door has been opened"
	state.lastStatus = "open"
	state.contactOpenTime = now()
    if(delayMinutes) {
		runIn(delayMinutes*60, turnOffAfterDelay, [overwrite: false])
	} else {
		turnOffAfterDelay()
	}
    state.switchPreviousState = switches.currentState("switch").value
    log.debug "Previous state of switch set to $state.switchPreviousState"
}
}

def contactClosedHandler(evt)  {
log.debug "$evt.value: $evt, $settings"
if (evt.value == "closed") {
	log.debug "Door has been closed"
    state.lastStatus = "closed"
    state.contactOpenTime = null
    turnOnIfOff()
}
}

def turnOffAfterDelay() {
log.debug "Running turnOffAfterDelay"
if (contact.currentState("contact").value == "open" && state.lastStatus != "closed") {
	def elapsed = now() - state.contactOpenTime
	if (elapsed >= (delayMinutes ?: 0) * 60000L) {
    	log.trace "Turning off outlets: $switches"
		switches.off()
	}
}
}

def turnOnIfOff() {
log.debug "Running turnOnIfOff"
log.debug "state.switchPreviousState is $state.switchPreviousState"
log.debug "switches.latestValue is $switches.latestValue"
log.debug "state.switchPreviousState.value is $state.switchPreviousState.value"
if (state.switchPreviousState.value == "on") {
	log.trace "Turning on outlets: $switches"
	switches.on()
}
}

Your “switches” input is a list, not a singe device. So when you try to get the current value, it gives you a list of values. In your case it’s a list that has one value “on”.

Either change your input to be multiple: false, or iterate over the list and test for each value separately.

1 Like

Thank you Alex, you were 100% correct. I had to “fix” some of the code that I was screwing around with while trying to figure out what I was doing wrong, but it was because it was a multiple.

Thanks again.