Groovy not too groovy?

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) {

Where is “latestPower” getting defined?

Looks like you renamed it to “latestCurrent”

3 Likes

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

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!

3 Likes

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()}")
}

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"
}

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

Try

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

I agree. I absolutely love Groovy

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.

1 Like

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

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

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

This “fixed” it

if (latestPower > “10”) {

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

1 Like

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?

1 Like

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

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

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

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