Question on evaluating switch state

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’))) {
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"
        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”
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!


What are you expecting the behavior to be if there are 2 switches selected as switch1 and one is off and the other is on?

Hi - thank you for the reply! I would like for them to return to the original state, so if Switch1 is On, and Switch2 is off when the arrival event is triggered, I would like to have Switch1 stay the same, and Switch2 turn on for the delay period, and turn off again after.

Just re-read your q back to me. So maybe that is the crux of my question - I am not sure how to deal with them as an array instead of a single entity - make sense?