Groovy not too groovy?


#1

Groovy how I hate thee, let me count the ways…

I have an app within initialize() has the statement

subscribe(sensor1, "current", currentInputHandler)

Ok good so far. The handler looks like

def currentInputHandler(evt) {
	def latestCurrent = sensor1.currentValue("current")
   log.trace "Current consumption $latestCurrent"
   if (latestCurrent > 10) {
   	log.trace "its over 10 "
    }

}

As it appears above, it will never be called. No error, no log.trace, nothing. If I remove the if () block, all is well. The log.trace dispays the correct value for current. Makes no sense to me what so ever. The above handler was copied from a smartapp that works just fine. THAT handler looks like

def powerInputHandler(evt) {

	def latestPower = sensor1.currentValue("power")
    log.debug "Power = $latestPower"
	if (latestPower > minimumWattage) {

(Ben W) #2

Where is “latestPower” getting defined?

Looks like you renamed it to “latestCurrent”


#3

It was a typo within this post. I recreated the code for this thread and goofed.


(Alex) #4

There is a bug in ST right now that prevents logs from being printed if there is any error in the in the callback function, even if the error comes after the logging.

In your here

In ST universe, when a variable appears without keyword def, the SmartApp assumes that it’s an input. I presume latestPower is not defined as an input in your app and you are getting null. null is not a number and cannot be compared with such. Therefore you get an error, which is not being printed due to ST bug in logging.

Leave Groovy out of it. Groovy’s great!


(Ben W) #5

What is in the log message? My guess is it is being passed as a string and a string cannot ever be greater than 10.

Try

 if (latestCurrent.toInteger() > 10) {
log.trace "its over 10 "
} else {
 log.debug ("In Else current: ${latestCurrent.toInteger()}")
}

#6

Dang… I should have copied the actual function. So here goes

def currentInputHandler(evt) {
	def latestPower = sensor1.currentValue("current")
   log.trace "Current consumption $latestPower"
   if (latestPower > 10) {
   	log.trace "its over 10 "
    }

}

The above does not work

But…

def currentInputHandler(evt) {
	def latestPower = sensor1.currentValue("current")
   log.trace "Current consumption $latestPower"
}

#7

Thats just it… The log shows nothing unless I remove the if block. The the log.trace statement executes


(Anthony S.) #8

Try

def currentInputHandler(evt) {
   def latestPower = sensor1.currentState("current")?.value.toInteger()
   log.trace "Current consumption $latestPower"
   if (latestPower > 10) {
      log.trace "its over 10 "
   }
}

(Anthony S.) #9

I agree. I absolutely love Groovy


(Ben W) #10

Smartthings is not a full IDE and debugging is lacking for sure. Most likely its dumping out of that method leaving everything on the table before its get passed to logging.


#11

Sorry… No go… Obviously throwing an error that not showing up (or anything else) in the log.


#12

Likewise :frowning: No error, no messages, no go


(Anthony S.) #13

Are you 100% positive you are subscribed to the events?


#14

This “fixed” it

if (latestPower > “10”) {


(Ben W) #15

So what is the value of “latestPower” without the if statement.


(Anthony S.) #16

that’s not right. You can’t do greater than with a string. Can you send us a snip of the sendEvent you use to update the state?


(Ben W) #17

Well it will always validate as True if its not “10”


#18

I am well aware of that. Hence the “” around fix. The problem is if (latestPower > 10) is throwing an error which does not show up in the log. Because of this error even the log.trace statement does not execute


(Anthony S.) #19

Are you sending the ‘current’ value as a string in the sendEvent()?


(Anthony S.) #20

I understand that. I need to see your sendEvent so I can see how you are updating the values