Bare bones code for a (light) switch?

I am a coding beginner with some basic java coding background from college back in the days. I wanted to start experimenting with creating my own device handlers. Per the FAQ i found all the useful documentation listed on the website. I also learned how to copy the existing code template for a simple Z-wave switch. Looking at the code I understand the gist of most of the code and its purpose but not all the little details.

What I was trying to figure out is what is the minimum amount of code that is needed to turn a switch on and off. I get confused with the Z-wave template code because it includes some stuff that from what i understand is more advanced and might not be necessary for me right now in regards to experimenting (communication checks, other functions etc).

I just want to get the bare minimum code for a switch to go on and off so I can understand what sections of a code are required for smarthings to consider it a functioning code and get a simple device such as a switch recognized and controlled. That way i can build upon it slowly understanding the new code in a systematic base.

If anybody has or can point me to a basic code like that or some super basic step by stepit would be much appreciated.

If you look at a Virtual Switch template file, you will see the bare minimum a Device Handler needs in order for the code to run. Please realize that a Virtual Switch is not actually connected to a physical device, which dramatically simplifies the code.

For a simple switch device, you need to declare some metadata about the device, including its Capabilities, in this case, a “Switch”. Device capabilities allow SmartApps to interrogate each device to determine what data is available (e.g. current value of a switch) and what functions can be called (e.g. on() and off() for a switch device.)

Take a look at the documentation for Device Capabilities as it will help to make things clearer. http://docs.smartthings.com/en/latest/capabilities-reference.html

You also need a Tile definition as well as implementation of the functions required for the Capabilities you define at the top.

Here is one of my very old Virtual Switch Device Handlers as very simple example.

Typically, you would fill in actual device communications (i.e. Z-Wave, Zigbee, or LAN) in the “on()” and “off()” functions. Also, the “parse()” routine would handle and data coming from the physical device and would update the device’s tile(s) accordingly. These tiles have intrinsic Attributes (like a current value for the switch, either “on” or “off”) that SmartApps can subscribe to and be notified of when changes occur. Custom Attributes are also possible, but those essentially need custom SmartApps to use them. When possible, stick with using the standard attributes.

/**
 *  Virtual Switch Device Type - VirtualSwitch.device.groovy
 *
 *  Copyright 2014 Daniel Ogorchock
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License. You may obtain a copy of the License at:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
 *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the specific language governing permissions and limitations under the License.
 *
 *  Change History:
 *
 *    Date        Who            What
 *    ----        ---            ----
 *    2015-03-27  Dan Ogorchock  Original Creation
 *
 *
 */
 
metadata {
	definition (name: "Virtual Switch", namespace: "ogiewon", author: "Daniel Ogorchock") {
        capability "Actuator"
        capability "Switch"
	}

	simulator {
		// TODO: define status and reply messages here
	}

	tiles {
    	standardTile("switch", "device.switch", width: 2, height: 2, canChangeIcon: true) {
        	state "on", label: '${name}', action: "switch.off", icon: "st.switches.switch.on", backgroundColor: "#79b821"
        	state "off", label: '${name}', action: "switch.on", icon: "st.switches.switch.off", backgroundColor: "#ffffff"

		}
        
	main(["switch"])
	details(["switch"])
	}
}

// parse events into attributes
def parse(String description) {
	log.debug "Parsing '${description}'"
	// TODO: handle 'contact' attribute

}


// handle commands
def on() {
	log.debug "Virtual Switch - On"
    sendEvent (name: "switch", value: "on", isStateChange: true)
}

def off() {
	log.debug "Virtual Switch - Off"
    sendEvent (name: "switch", value: "off", isStateChange: true)
}

Hope this helps get you started!

Dan

3 Likes