Goal: Keep track of the initial value of switches so that if they were on initially, I don’t turn them off as part of app
Issue: I am not sure how to track the initial state, since it is multi-valued
Here is my relevant code:
preferences {
section(“Turn on/off a light…”){
input “switch1”, “capability.switch”, multiple: true
}
}
def switch1state = switch1.currentValue(‘switch’)
log.debug "Switch1 State: " + switch1state.inspect()
if(presenceValue && (now > sunTime.sunset) && (switch1.currentValue(‘switch’).contains(‘off’))) {
switch1.on()
log.debug “Welcome home at night!”
//TODO: Need to find out how to figure out if each switch was on/off and track state for that so I can only turn it off if it was off before starting this...
// note that switch1 is multi-valued, so need a way to deal with that...
log.debug "Starting off timer"
unschedule("scheduledTurnOff")
def delay = minutes * 60
runIn(delay, "scheduledTurnOff")
}
So where the //TODO part is, I would like to be able to evaluate whether the switch was on prior to running this, and leave it on if so. But if it was off initially, turn it back off (after the delay expires).
Anyone have thoughts/ideas on how to do that? I tried:
if (switch1State == “off”) {
log.debug “Starting off timer”
unschedule(“scheduledTurnOff”)
def delay = minutes * 60
runIn(delay, “scheduledTurnOff”)
}
But since it’s multi-valued (I think that’s the right term), it is not evaluating the “off” correctly, even when I just have one switch.
Your help is very much appreciated!
-randy