Smart App Number Comparison

I’m starting to wonder why I’m trying to write my own smart apps. Seems like every time I do something different it goes south. Anyway, here is my current issue.

I am looking at a temperature sensor and subscribing to the temperature. In the handler I compare to numbers to either turn on or turn off a heat light. I get an error of java.lang.ClassCastException. Here is my handler code.

log.debug "$evt.value, $onTemperature, $offTemperature"
if (evt.value < onTemperature && garageHeatLight.currentSwitch == "off") {
    log.debug "Heat On"
if (evt.value > offTemperature && garageHeatLight.currentSwitch == "on") {
    log.debug "Heat Off"


The log.debug of values shows correct numbers.

Any ideas as to what I am doing wrong? It seems like a basic comparison.

1 Like

Try force-casting your values:

if (evt.value.toDouble() < onTemperature.toDouble()) …

1 Like

That worked. Can you explain why?

evt.value is passed as a String, and if you didn’t specify onTemperature to be type: number or type: decimal, it will be a String also. If both are strings a “<” operator won’t work. And if you DO use those, I think you get a BigDecimal - and comparing a String to a BigDecimal is a no-no.

You could use evt.doubleValue to ensure you’re using a number, but you need to verify it has a value or you’ll get a different error (some temperature devices can return a null response, or “unavailable” or “err” for temperature values, so doing some checking might be necessary).

More on evt values here:
And on input attribute types here:

1 Like

I’ve probably read that a number of times and it just didn’t click. Read it again and it made perfect sense. Sometimes I think I’m getting too old to learn new programming, going on 70, but I enjoy doing it. I’ve done programming most of my life but every language is different. And for the past 20 years about all I did was PLC programming which is totally different. Still I’m having fun doing it as a hobby now.

Thanks for all the help.