Device list and attributes/current states as JSON?

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

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

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

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.

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

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:, 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

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

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!

I now see the attributes under the devices list in the web portal

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

Have you looked here?

Here’s a link to ST draft documentation which is where I found the above:

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