Device list and attributes/current states as JSON?


(Szac) #1

I’ve been unsuccessfully hacking on the REST example for a SmartApp to try to recreate what is essentially available http://graph.api.smartthings.com/device/list detail view for each device. Is there a way to get that content as JSON?


(Florian Z) #2

Yes, in Groovy you can do:

def myGroovyMap = [ someKey: "someValue" ]
def json = new groovy.json.JsonBuilder(myGroovyMap)

// Returns a string with the contents of myGroovyMap in JSON
json.toString()

//Returns a string with the contents of myGroovyMap in JSON, applies formatting such as line breaks and indention
json.toPrettyString()

In a SmartApp, you may be able to (or have to) omit the groovy.json when instantiating the new JsonBuilder.

EDIT: Now, that I am reading your question again, I am unsure whether you are having trouble converting to JSON, or getting the map of devices and device values, in the first place.


(Szac) #3

Thanks for the reply. I am having trouble getting the map of devices and device values in the first place.


(Florian Z) #4

I am unsure whether you can just get a list of all the devices known to the hub. If you look at how the SmartApps interface is designed, it’s pretty obvious that there is an attempt to provide a sandboxed execution environment, where users have to grant these apps explicit permission for accessing devices.

What you would have to do is provide separate inputs to your SmartApp for switches, motion sensors, open/close sensors, sirens, etc., and allow these to accept multiple items. You can declare these inputs in the preferences meta-data section. Like so (from the Ubi app):

section("Allow a web application to control these things...") {
    input "switches", "capability.switch", title: "Which Switches?", multiple: true, required: false
    input "motions", "capability.motionSensor", title: "Which Motion Sensors?", multiple: true, required: false
    input "locks", "capability.lock", title: "Which Locks?", multiple: true, required: false
    input "contactSensors", "capability.contactSensor", title: "Which Contact Sensors?", multiple: true, required: false
    input "presenceSensors", "capability.presenceSensor", title: "Which Presence Sensors?", multiple: true, required: false
}

Then, in your app you can simply iterate over all these containers (each input being one container) and gather the latestValue (and whatever else you want) for the corresponding device. For example, something like:

// Some dictionary
def myMap = [:]

// Collect switch values
myMap.switches = switches.collect { [ id: it.id, name: it.displayName, value: it.currentState("switch").value] }

// Collect motion sensor values
myMap.motions = motions.collect { ... }

// Generate a json map
def json = new JsonBuilder(myMap)
return json.toString()

When you install this app, you have to manually select all the devices (which could be ALL the devices in your network) to expose to the app. Does that make sense?

Maybe there is another way to get to a list of all your devices


(Szac) #5

Great! Thanks for the head start. Do you know where to find the options for the individual capabilities? Such as,

id: it.id, name: it.displayName, value: it.currentState("switch").value

but for motion, temperatures, etc? Is there a list of this? I’ve checked in the various places but come up short. Thanks again!


(Szac) #6

I now see the attributes under the devices list in the web portal http://graph.api.smartthings.com/device/list

If these are all outlined in a doc somewhere that would be awesome too.


(Taryn Brice Rowland) #7

Have you looked here? http://graph.api.smartthings.com/ide/doc/capabilities

Here’s a link to ST draft documentation which is where I found the above: https://support.smartthings.com/categories/20063695-Build-with-SmartThings

I know this stuff is kind of all over the place, so I thought I’d share what I’d found.