Starting help

Hi all,

I have started to make a smart app to interface many devices with an API server. I must admit im finding the whole ST IDE a bit tricky.

Im starting small and hoping to build up but im getting stuck at the most basic level.

My code is take of the examples for a switch and looks like

preferences {
    section {
        input "theSwitch", "capability.switch"
    }
}

def install() {
    subscribe(theSwitch, "switch.on", switchOnHandler)
}

def switchOnHandler(evt) {
    log.debug "switch turned on!"
}

my issue is that the log does not show “switch turned on!”. I then thought it may be the simulator as when I turn the virtual switch on the actual image does not change. So i changed my code to:

preferences {
     section {
        input "theSwitch", "capability.switch"
    }
    section {
        input "otherSwitch", "capability.switch"
    }
}

def installed() {
	log.debug "Installed with settings: ${settings}"
	subscribe(theSwitch, "switch.on", switchOnHandler)
	initialize()
}

def updated() {
	log.debug "Updated with settings: ${settings}"
	unsubscribe()
    subscribe(theSwitch, "switch.on", switchOnHandler)
	initialize()
}

def initialize() {
	// TODO: subscribe to attributes, devices, locations, etc.
}

// TODO: implement event handlers
def switchOnHandler(evt) {
    log.debug "switch turned on!"
    otherSwitch.on()
}

The goal of this test is to have a switch (theSwitch) turn on another switch (otherSwitch). This time I installed this on my app but again nothing is happening. What am I missing?

Take a look at the code for the big switch Here on Github. It’s a good example of doing something when a switch is turned on or off

I did notice in your first example you have install() which should be installed()

You should also add an updated() section that looks like this. The updated will run any time you change the preferences. A good learning step would also be to put log.debug in all your functions so you can see when they’re called

def updated(){
     unsubscribe()
     subscribe(theSwitch, "switch.on", switchOnHandler)
}