Make Your Home Smart

Overview

SmartThings can transform any home into a smart home. Keep an eye on things or even personalize with every mood wherever you are. Stay connected, be protected, save energy, and monitor things with ease.

Setting up the Environment

Prepare the following to create a SmartApp:

  • SmartThings Account

Testing

  • SmartThings App

  • SmartThings Hub

  • Things (e.g. contact sensors, motion sensor, outlet, etc.)

Step 1. Preparing the Area

a. Go to https://graph.api.smartthings.com/ and log in to your SmartThings account.

Figure 1 Signing in to SmartThings Account

Figure 1 Signing in to SmartThings Account

b. In the My SmartApps tab, click + New SmartApp.

Figure 2 Adding New SmartApp

Figure 2 Adding New SmartApp

c. Fill in the necessary details of your app.

Figure 3 SmartApp Details

Figure 3 SmartApp Details

Step 2. Coding the SmartApp

a. Define what information the SmartApp needs from the user in the preferences method. Each section can group similar items together along with a text description. As for the input method, it specifies the devices that the user will choose from. The first argument is the device’s identifier, next is its capability, and lastly the required argument that lets the user select a device to install the SmartApp.

preferences {
	section("Turn on when motion detected:") {
		input "themotion", "capability.motionSensor", required: true, title: "Where?"
	}
	section("Turn off when there's been no movement for") {
		input "minutes, "number", required: true, title: "Minutes?"
	}
	section ("Which door should be monitored?") {
		input "thedoor", "capability.contactSensor", required: true
	}
	section("Turn on this light") {
		input "theswitch", "capability.switch", required: true
	}
}
Note

For more information on the Capabilities of SmartThings, go to:
http://docs.smartthings.com/en/latest/capabilities-reference.html

b. In the initialize() method, subscribe to any event that will serve as a trigger in the app. The arguments of the subscribe() method includes the identifier, the attribute, and the method to be called on such event.

def initialize() {
	subscribe(themotion, "motion.active", motionDetectedHandler)
	subscribe(themotion, "motion.inactive", motionStoppedHandler)
	subscribe(thedoor, "contact.open ", doorOpenedHandler)
}

c. Define the first event handler that is to be called when there is motion detected by the sensor. Call the on() method of theswitch to turn on the light.

def motionDetectedHandler(evt) {
	log.debug "motionDetectedHandler called: $evt"
	theswitch.on()
}

d. The second event handler is called when motion is inactive.

e. In the motionStoppedHandler(), the light will be turned off after the specified time by the user. Use the runIn() method to set an interval, the first argument, before invoking a method, the second argument.

def motionStoppedHandler(evt) {
	log.debug "motionStoppedHandler called: $evt"
	runIn(60 * minutes, checkMotion)
}

f. As for the doorOpenedHandler() method, check if the switch is currently turned off. If so, turn it on.

def doorOpenedHandler(evt){
	if (theswitch.currentSwitch != "on")
	theswitch.on()
}

g. Define the checkMotion() to handle turning off the switch. First, check if the current state of the motion is still inactive. Next, check if the elapsed time since the change of state is greater or equal to the threshold (user specified value). After securing all those, turn the switch off with the off() method.

def checkMotion() {
	log.debug "In checkMotion scheduled method"

	def motionState = themotion.currentState("motion")

	if (motionState.value == "inactive") {
		// get the time elapsed between now and when the motion reported inactive
		def elapsed = now() - motionState.date.time

		// elapsed time is in milliseconds, so the threshold must be converted to milliseconds too
		def threshold = 1000 * 60 * minutes

		if (elapsed >= threshold) {
			log.debug "Motion has stayed inactive long enough since last check ($elapsed ms):  turning switch off"
			theswitch.off()
		} else {
			log.debug "Motion has not stayed inactive long enough since last check ($elapsed ms):  doing nothing"
		}
	} else {
		// Motion active; just log it and do nothing
		log.debug "Motion is active, do nothing and wait for inactive"
	}
}

Step 3. Testing on the Devices

a. Save all changes, then hit Publish to make it available to the app.

Figure 4 Publishing SmartApp

Figure 4 Publishing SmartApp

b. Make sure that the hub is up to date and the Things are connected to it and the app. Over at the SmartThings App, head on over to the Marketplace, then go to My Apps. It then shows a list of apps that you published for yourself.

Figure 5 Accessing Your SmartApp

Figure 5 Accessing Your SmartApp

c. After selecting the app, specify the required fields in all the sections

Figure 6 Configuring Your SmartApp

Figure 6 Configuring Your SmartApp

d. Test the motion and contact sensors if it affects the light.