Code support needed for app "Lights OFF"

I live in Costa Rica and I want to use IFTT to turn off all lights.
I cannot SMS because is not supported with my country, so I tried to make a code to use a virtual switch as a momentary button and turn all lights off but it is not working.
I’m new in the code writing so any help is well received.

preferences {
section(“When this switch is pushed”) {
input “master”, “capability.switch”, title: “Where?”
}

section(“Turn off all of these switches”) {
input “switches”, “capability.switch”, multiple: true, required: false
}
}

def installed() {
log.debug "Installed with settings: ${settings}"
subscribe(master, “capability.switch”, offHandler)
// initialize()
}

def updated() {
log.debug “Updated with settings: ${settings}”
// unsubscribe()
subscribe(master, “capability.switch”, offHandler)
// initialize()
}

def takeAction() {
master.off()
}

def offHandler(evt) {
log.debug "Turning off: " + switches
switches()?.off()
runIn(0.1 * 60 , takeAction)
}

I don’t know that I will be able to help you with your code (i’m certainly no programmer), but as an FYI if you highlight all of your code and press the </> button, or surround your code with a ` symbol it will format your code so that it is easier for people to read (especially helpful for those community members that use TTS)

Are you using a smart lighting automation connected to this virtual switch to turn them all off? I use a similar manner (not always smart lighting) of connecting common groups of switches and bulbs to virtual switches for organization.

I’m not sure if it’s possible to create a virtual switch and connect that switch in the DTH to all other switch functions.

You will need to loop through the array for each switch

switches.each {
    it.off()
}

Not sure if that will work, but should get you close. You could also use CoRe or SmartLighting to do this.

I think this is what you want -

  1. I updated the subscribe methods to “switch.off” instead of “capability.switch”

  2. Removed the () after switches

    preferences {
    section(“When this switch is pushed”) {
    input “master”, “capability.switch”, title: “Where?”
    }

     section("Turn off all of these switches") {
     	input "switches", "capability.switch", multiple: true, required: false
     }
    

    }

    def installed() {
    log.debug "Installed with settings: ${settings}"
    subscribe(master, “switch.off” ,offHandler)
    }

    def updated() {
    log.debug "Updated with settings: ${settings}"
    subscribe(master,“switch.off”, offHandler)
    }

    def takeAction() {
    master.off()
    }

    def offHandler(evt) {
    log.debug "Turning off: " + switches
    switches.off()
    runIn(0.1 * 60 , takeAction)
    }

Thanks, now I can turn the lights OFF with the master virtual switch.
Now I need to work on making it a momentary button, in other words I need the switch to auto reset after 2-3 seconds after activated.

1 Like

Awesome! You have the right idea with using runIn to schedule an event in the future or you can also try this: http://docs.smartthings.com/en/latest/smartapp-developers-guide/devices.html#sending-commands.
Good luck :slight_smile: