toInteger causing error

Have a tablet that I'm running Fully Kiosk. Using the Fully driver it reports battery level. That part is fine.

In an app I subscribe to the battery level then do this function
def Battery = evt.value.toInteger()

This has worked great for quite awhile. Now all of a sudden I'm getting this error. What would have changed? I didn't change anything.

app:26492021-11-17 04:24:05.578 am errorjava.lang.NumberFormatException: For input string: "52.999996185302734" on line 62 (method startHandler)

That is not an integer value..

Looks like your value is a String too.. so try this:

Integer battery = Math.round(Float.parseFloat(evt.value))

I thought the toInteger was to change a string to an integer value.

Still curious as to how it worked just fine over the past month or so.

I will try your suggestion.

Did it always have a long decimal number?

That didn't work either. Got this error.

app:26492021-11-17 08:26:58.945 am errorgroovy.lang.MissingMethodException: No signature of method: static java.lang.Float.parseFloat() is applicable for argument types: ([C) values: [83.22233] Possible solutions: parseFloat(java.lang.String) on line 62 (method updated)

Ahh .. Ok.. Guess it's not a String..

Integer battery = Math.round(evt.value.toFloat())

Or if it's already a numeric value you might be able to just do:

Integer battery = Math.round(evt.value)

Don't know. Since it worked I really didn't check. I'm looking at the driver, but can't seem to figure out whether it is a string or number.

No luck there either. I think I'm loosing my mind...:slight_smile:

Thinking it might be a number rather than a string, I did a direct comparision, ie an IF statement, and it errored. So that kinda tells me it is a string value.

Event value should always be a string (https://docs.hubitat.com/index.php?title=Event_Object#value)

That was what I thought, it was a string. But it was my understanding, being a limited programmer, that the toInteger would convert a string number to a integer.

Would have thought it would have worked, but could also try:

int battery = Integer.parseInt(evt.value)

No luck there either.

For testing purposes I put this code in but it doesn't work. So for some reason the toInteger() is not converting a string to integer.
def something = "93.45"
def Battery = something.toInteger()

If I leave the quotes off which makes something a number it works fine.

Figured it out. If I do a toFloat instead of toInteger it works. And I can still do my comparison to a integer number.

If I do this I don't get any errors:
def something = "93"
def Battery = something.toInteger()

I'm thinking maybe I was just lucky and most of the time the value was coming in without any decimals.

Well shoot. When I went back to evt.value it bombed again.

There is something weird about evt.value. I changed it to brTablet.currentbattery.toInteger() and all is ok.

Let's go back to @bcopeland's suggestion and modify it slightly by adding a cast to String first

Integer battery = Math.round(Float.parseFloat((String)evt.value))
1 Like

I sat back and thought this through and realized maybe I misspoke and made an error on my own.

What I am doing is nothing more than turning an outlet on/off based on battery level. When I said it had been working, I was correct as the battery was getting charged. However, I hadn't really looked at the logs. What I believe was going on was the battery level, and thus evt.value, was coming in at integer values often enough to make it work. When it came in with a decimal it caused the error. But since it worked often enough I never caught the error.

Next thing I did, was when I was trying all those suggestions you guys gave me I was forcing an evt. Just dawned on me that that was not necessarily evt.value. So my bad. Sorry for all the trouble.

Net result, apparently the toInteger will work only if the string is an integer without decimals. Not sure I understand that, but it is what it is. I'm gonna stick with the toFloat as that seems to work no matter what.

Thanks again for all the help, and again sorry for the trouble.

1 Like