Code support needed for app "Lights OFF"


#1

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)
}


(Jason) #2

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.


(Ben W) #3

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.


(vlad) #4

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)
    }


#5

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.


(vlad) #6

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: