Dealing with arrays in Groovy - multiple sensors in preferences

(Jeff L) #1

I realize this a silly question and relates to Groovy vs. ST but I’m at wits end. How does one interact with what I’m guessing are arrays of strings created when you have a SmartApp that has a preference that includes multiple sensors, modes, etc? For example, I’m trying to put together an app that will do something if the location is in a certain mode, but not in others.

To start with, I’ll settle with just flipping through the modes that would qualify. Here’s the code:

private CheckCurrentMode() {
def found = false;
log.debug "Entering CheckCurrentMode"
originalMode.each {n -> log.debug “$n.currentValue(originalMode)” }
log.debug "Leaving CheckCurrentMode"
return found

I would expect this to flip debug output of all the originalMode entries in preferences. But instead it kicks an error:

groovy.lang.MissingPropertyException: No such property: currentValue for class: java.lang.String @ line 86

Can anyone shed light? Is there a simple Groovy tutorial that might help shed light on handling strings, variables, etc.?


(Jason Mok) #2

currentValue is a method for device.

Try displaying what is in originalMode.
log.debug "originalMode array content: $originalMode"
originalMode.each { log.debug “originalMode item: $it”

(Jeff L) #3

Thanks! That did it!!!


(Mike Maxwell) #4

Another bit-o-groovy goodness that I use all the time in debugging:

Log.debug anyObject.inspect()

A simple way of seeing what you’re dealing with.

(Jeff L) #5

@Mike_Maxwell - I get a “ava.lang.IllegalArgumentException: Property ‘inspect’ is not supported for devices” when I try that. I’m trying to write an app that needs to know the on/off contition of a siren and once again I’m feeling in the dark. I have no idea how I can figure out what .currentState .sirenState, .whoTheHeckKnows goes after the device name.

I read through the documentation linked to (very helpfully) by @copyninja and that suggests that I would use: alarm.currentValue(“switch”) to reach those values. But this:

log.debug “Current Alarm State is: $alarm.currentValue(“switch”)”

very unsurprisingly throws out a million errors (I doubt it handles nested " very well. So I’m back to feeling int the dark on how to resolve that. I know there is a way. . .but I have no idea how.

Any pointers from anyone? :smile:


(Jeff L) #6

I worked around the syntax problems. . .

def alarmState = alarm.currentValue(“switch”)
log.debug “Current Alarm State is: $alarmState”

@copyninja that link you embedded was tremendously useful! Thanks!!

(Mike Maxwell) #7

So here are the capabilities:
You will be looking for mySelectedAlarmDevice.currentValue(“alarm”) == “off”, make sense?
See how the possible attribute values are listed by device type?
If you have multiple alarm devices, then you will need to check the state of each one in a loop

(Jeff L) #8

Thanks @Mike_Maxwell! I’ll read through that documentation, and thank you very much for that link. To your prediction I am indeed looping through multiple devices, or at least building the app to support that. Here’s the function, which is intended to be used to change modes if a very specific set of motions/events occur, but NOT if a security alarm is actually going off.

private CheckForAlarms() {
log.debug "Entering CheckForAlarms"
def n = 0;
def found = false;
alarm.each {
def alarmState = alarm[n].currentValue(“switch”)
log.debug "Current state for alarm $n is: $alarmState"
if (alarmState == “on”) {
log.debug "I found an alarm going off, refusing to disable it…"
found = true
return found
log.debug "I found no triggered alarms…"
return found

This function feels highly inefficient to me, but I can understand the simple syntax. So it’s working for me!!


(Mike Maxwell) #9

Additional groovy goodness for your consideration:

return alarm.currentValue(“switch”).toString().contains(“open”)