SmartThings SmartApps

SmartThings SmartApps

One of the best features of the SmartThings home automation system is the ability to create scripts, or small programs, which can automate your system for any need you might have. Want your driveway and entry lights to be on when it’s dark, or lights in a room to turn on when you enter it? You can write a simple SmartApp to do it.

I have a few examples of SmartApps that you can look at and use if you want. Please contact us if you have any suggestions for other SmartApps I can add to this list! If you are interested in how to write your own SmartApp, then keep reading!

Automated Garage Lights

Outdoor lights on when dark

Random lights when security is armed

If you want to write your own SmartApp, it’s not too hard. It will require some programming, but if you keep it simple I know you can do it. For example. let’s turn on a light switch any time a motion sensor detects motion.

First off, you’ll need to log into the SmartThings IDE (https://graph.api.smartthings.com) to access all of the developer tools. You should use the same credentials that you use for your SmartThings system.

Then find the “My SmartApps” link and click on it. If you already have any SmartApps, here is where they’d be listed. Since we’re starting on our first SmartApp, click the “New SmartApp” link on the right of the window. Give it a good useful name, put your name in for the Namespace, and give it a nice, descriptive description. Then hit Create at the bottom.

Once that is done, you’re ready to start coding. You’ll see in the window that they already have some code for you on the screen. We’ll just need to modify it for what we want.

Find the section that starts with “preferences”. Update that section to look like this:

preferences{

section(“Motion Sensors”){

input “MotionSensors”, “capability.motionSensor”, required: true, multiple: false, title:”Motion Sensors”

}

section(“Lights”){

input “Lights”, “capability.switch”, required: true, multiple: false, title:”Lights”

}

}

This code defines two types of inputs for the program. One is the motions sensor which will control the lights, and the other is the light that will be controlled. You can see that they are pretty similar. In each section, we give the input a name, say that the user is required to select at least one device for each input, that only one device can be selected.

Next we need to tell the program to listen for changes in the motion sensors. Find the section that starts with “def initialize()” and update it to match this:

def initialize() {

subscribe(MotionSensors, “motion.active”, motionDetectedHandler)
subscribe(MotionSensors, “motion.inactive”, motionNotDetectedHandler)

}

This code tells the program that any time the motion sensors are active, find and run the section of code named motionDetectedHandler. Likewise, any time the sensors detect that motion has stopped, run the motionNotDetectedHandler section of code.

Ok. We’re almost there. Now we need to make those handler sections that actually turns the lights on and off.

At the bottom of the code, add this:

def motionDetectedHandler(evt)
{

log.debug “Motion Detected $evt”
Lights.on()

}

This section will turn the light on any time it runs.

Next add this section at the bottom:

def motionUndetectedHandler(evt)
{

log.debug “Motion Undetected $evt”

Lights.off();

}

This section will turn the light off any time it runs.

And that’s it! We’ve written a SmartApp.

Now we just need to install it.

Hit the Publish button on the right, and select “For Me” in the dropdown menu.

Now you can go to the SmartThings app on your phone, navigate to the SmartApps section and find your new SmartApp.

Recent Content

link to Baby Dimmer Control

Baby Dimmer Control

Manage your Baby’s dimmer   This little app will help control the dimmer switch in your baby’s nursery, or any other dimmer switch. Perhaps late at night you don’t want the light at 100% brightness. This app will take care of that by allowing you to specify a time and what level to set the […]
link to Garage Door Monitor

Garage Door Monitor

Here’s a little app which will monitor your garage doors and alert you if they remain open for longer than you would like. It’s always a terrible feeling when you realize that you left the garage door open all evening, or throughout the heat of the day. You can also specify some motion sensors which […]