Condition syntax help - somehow 195 != 195?

ok, giving up and hoping some kind soul can tell me why 195 != 195 apparently :sweat_smile:

obviously I am not a pro, but I thought I was capable enough to write a simple conditional statement in any language.

I had a full head of hair before I started writing this app.

def cHandler(evt) {
    log.info "New priority set: $evt.value"
    state.currentSchedulePriority = evt.value
    if (logEnable) log.debug ("\tNew State Value ${state.currentSchedulePriority}")
    
    if (state.currentSchedulePriority.isInteger()){
        if (logEnable) log.debug ("$state.currentSchedulePriority is an integer")   
    } else {
        if (logEnable) log.debug ("$state.currentSchedulePriority is NOT an integer")                                   
    }
    
    if (state.currentSchedulePriority == 195){
        if (logEnable) log.debug ("setting active Temp Group to Avg Temp - NST1 - Excluding MBR")
        state.activeTempGroup = "Avg Temp - NST1 - Excluding MBR"
    }
    else if (state.currentSchedulePriority == 197){
        if (logEnable) log.debug ("setting active Temp Group to Master Bedroom")
        state.activeTempGroup = "Master Bedroom"
    }
    else{
        log.warn ("activeTempGroup NOT SET")   
    }
}

edit: added code

If "evt" is coming from different sources, this could cause the flip/flops. Check the type of evt.value itself and you may find that to be the source of the concern.

If you want the state to be an integer, I would just give up and use the following in line 3:

state.currentSchedulePriority = evt.value.toInteger()

Dave

2 Likes

Thanks for responding Dave. I have 2 subscription handlers - is that what you mean by different sources?

tried converting the evt.value.toInteger() directly in the handler as you suggested but it did not like that:

groovy.lang.MissingMethodException: No signature of method: java.lang.Integer.isInteger() is applicable for argument types: () values: []
Possible solutions: toInteger(), getInteger(java.lang.String), getInteger(java.lang.String, int), getInteger(java.lang.String, java.lang.Integer), toBigInteger() on line 122 (cHandler)

got rid of that error by removing my isInteger() check on the state value and... IT WORKED!!

Still not sure how, but thanks for unsticking my brain!

1 Like

At 71, I have oldtimer's disease. They say the mind is the first thing to go. I can not remember the remaining items on the list.

2 Likes

I've been burnt by this a few times. One of those type of bugs that you could spend a hour just to find.

The ".isInteger()" method does not tell you if the variable type is an integer, it will tell if you the string is an integer that can be converted to an integer. That is probably why the logic was failing.

In the end you would want to do something like below that will force the currentSchedulePriority to be an integer if evt.value is a number in a string format. Then later on your if statements will work.

state.currentSchedulePriority = evt.value.toInteger()

The other thing you could have done is wrapped the 195 and 197 in quotes so they are strings as well then they if statements should work.

3 Likes

This.

If you were allowed to use reflection you would still see that state.currentSchedulePriority.getClass().getName() is java.lang.String at any point in that execution. You have verified that it is in integer format. Now you need to toInteger() it in the comparisons (or another one of the ways you could convert it).

2 Likes

By the way, if you really wanted to check if it was an java.lang.Integer you would use "instanceof". I think you have marked the solution incorrectly. I don't think that two instances of the driver would have a reference to the same event anyway.

1 Like

you're correct, my understanding of isInteger() was wrong. Updating Solution.