SmartThings REST API

smartthings-rest-api

Here’s a script that will allow other programs, outside of Smart Apps, you may write to obtain the data from any of your devices. With a few modifications it could also allow your program to control the devices as well. 

This also only allows access to a small subset of the possible devices that work with SmartThings, any devices can be added to this.

I’m using it to train a machine learning algorithm to learn my house’s, and my, trends to see what I can learn. There’s many more possibilities for this though.

After creating the Smart App, there are extra steps you’ll need to complete to handle SmartThings’ OAuth authentication.

definition
(
    name: "Web Service",
    namespace: "MySpace",
    author: "Me",
    description: "Used to access sensor data from smart app",
    category: "My Apps",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
    iconX2Url:"https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
    iconX3Url:"https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png"
)

preferences 
{
    section("Allow external service to control these things...") 
    {
        input "switches", "capability.switch", multiple: true, required: false, title: "Switches"
        input "doors", "capability.contactSensor", multiple: true, required: false, title: "Doors"
        input "windows", "capability.contactSensor",multiple: true, required: false, title: "Windows"
        input "motionSensors", "capability.motionSensor", multiple: true,required: false, title: "MotionSensors"
        input "temperatures", "capability.temperatureMeasurement", multiple: true, required: false, title:"Temperatures"
     }
}

def installed() 
{
    log.debug "Installed with settings: ${settings}"
    initialize()
 }

def updated() 
{
    log.debug "Updated with settings:${settings}"
    unsubscribe()
    initialize()
}

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

mappings 
{
    path("/switches") 
    {
        action: [GET: "listSwitches"]
    }
    path("/doors")
    {
        action: [GET: "listDoors"]
    }
    path("/windows")
    {
        action: [GET: "listWindows"]
    }
    path("/temperatures")
    {
        action: [GET: "listTemperatures"]
    }
    path("/motion")
    {
        action: [GET: "listMotionSensors"]
    }
    path("/switches/:command") 
    {
        action: [PUT:"updateSwitches"]
    }
}

def listTemperatures()
{
    def resp = []
    temperatures.each 
    {
        resp << [name: it.displayName, value:it.currentValue("temperature")]
    }
    return resp
}

def listSwitches() 
{
    def resp = []
    switches.each 
    {
        if (it.currentValue("switch") == "on")
        {
             resp << [name: it.displayName, value: 1]
        }
        else
        {
              resp << [name: it.displayName, value: 0]
        }
    }
    return resp
}

def listDoors() 
{
    def resp = []
    doors.each
    {
         if(it.currentValue("contact") == "closed")
         {
             resp << [name: it.displayName, value: 0]
         }
         else
         {
             resp << [name: it.displayName, value: 1]
         }
     }
     return resp
}

def listWindows() 
{
    def resp = []
    windows.each 
    {
        if (it.currentValue("contact") == "closed")
        {
            resp << [name: it.displayName, value: 0]
        }
        else
        {
            resp << [name: it.displayName, value: 1]
        }
    }
    return resp
}

def listMotionSensors() 
{
    def resp = []
    motionSensors.each 
    {
        if(it.currentValue("motion") == "inactive")
        {
            resp << [name: it.displayName, value: 0]
        }
        else
        { 
            resp << [name: it.displayName, value: 1]
        }
    }
    return resp
}

def updateSwitches() {}

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 […]