A Good primer for ST Groovy?


(Jed) #1

Hi there. Is there any good (thorough?) documentation for the ST flavor of Groovy? Specifically, I am not familiar with the methods and properties of objects.

For example, I copied the code below from another App, trying to list devices. I am not sure how to use the devices.find, what the types might be, wildcards, etc.

This is a specific question, but I am looking for a reference so I don’t have to bug you guys.

Thanks!

private show(devices, type) {
	def device = devices.find { it.id == params.id }
	if (!device) {
		httpError(404, "Device not found")
	}
	else {
		def attributeName = type == "motionSensor" ? "motion" : type
		def s = device.currentState(attributeName)
		[id: device.id, label: device.displayName, value: s?.value, unitTime: s?.date?.time, type: type]
	}
}
 
 
private device(it, type) {
	it ? [id: it.id, label: it.label, type: type] : null
}

(Jim Anderson) #2

You should check out the Groovy section in the developer documentation.

It won’t cover everything, but gives some general information and includes some other good resources for learning Groovy.


(Joshua Lyon (SharpTools.io Dashboard)) #3

I would also recommend the reference section to get a better understanding of methods and properties of SmartThings-specific classes/objects.


(Jed) #4

Thank you- I did that, but it’s a little sparse.

For example, I have some dimmer switches. They have both a switch and a switchLevel capability.

The Capability table shows a call of setLevel(Number, Number). What’s the second number? Is there a more detailed definition of those capabilities/functions?


(Joshua Lyon (SharpTools.io Dashboard)) #5

There are some commands like that where the parameters aren’t so clear. If I remember correctly, for most switchLevel devices you can actually drop the second argument altogether and just do use the first parameter to dim the lights:

setLevel(50) //dim to 50%.

Only a subset of the switchLevel devices support the second parameter of the setLevel() command which is intended to be used for the duration over which to dim. I don’t recall if it is in seconds or milliseconds as my dimmer switches don’t support the second parameter, but you would do something like:

setLevel(50, 3) //dim to 50% over 3 seconds(?)

(To be clear, I can still send the second parameter to my devices which don’t support it, but the devices just dim on normally and ignore the second parameter)


(Jed) #6

Here’s a specific question, if you don’t mind.

In my Web API smart App, I am trying to list ALL the devices. Why doesn’t this work? (Adapted from a filter that was looking for a specific type of device)

def listAll() {
	devices.collect{devicex(it)}
}

private devicex(it) {
	[id: it.id, label: it.label, type: type]
//  it ? [id: it.id, label: it.label, type: type] : null
}

(Jed) #7

I am now realizing how mistaken I was. First I was assuming there was a “devices” list with everything in it, and that the “device(it, type)” function was filtering. Haha. No. I was also assuming that “type” was a key in the list. Not true either.

I’ve almost got it. Thanks for even looking at the craziness.


(Joshua Lyon (SharpTools.io Dashboard)) #8

Sometimes working through it and figuring out what you did right and what you did wrong is the best way to learn! Keep at it, it sounds like you are making progress!


(Jim Anderson) #9

Our capabilities documentation is lacking. I love that we have a community of rock stars willing to help people out. But obviously, these types of things should be better documented.

It’s on my list, FWIW.


(Jed) #10

Thank you! Good to know!