Performing calculations on device values

(Keith Croshaw) #1

I’m having some issues performing calculations on device values. Could somebody please take a look and tell me what I’m doing wrong, it seems like I’m getting data type mismatches. Can’t find much documentation on data types of device values.

def sensor1Val = sensor1.currentTemperature
def sensor2Val = sensor2.currentTemperature
def heatSP = thermostat.heatingSetpoint

if ((sensor1Val + sensor2Val) / 2 > heatSP) {


(Jim Anderson) #2

Can you post the definitions of sensor1 and sensor2, as well as the exception you are getting? Thanks!

(Mike Maxwell) #3

Try converting them to integers first.
Def sensorvalue1 = sensor1.currentTemperature.toInteger()

(Keith Croshaw) #4

sensor1&2 are inputs:

input "sensor1", "capability.temperatureMeasurement", title: "Temp Sensor", required: true, multiple: false
input "sensor1", "capability.temperatureMeasurement", title: "Temp Sensor", required: true, multiple: false

(Keith Croshaw) #5

Thanks I’ll try this tonight! Is this in the documentation somewhere?

Probably above the ST layer I guess… That stuff is rather new to me unfortunately.

(Mike Maxwell) #6

This is just groovy stuff, unless otherwise specified, everything ST is a string value.
Sometimes the strings evaluate to ints on their own, sometimes they don’t, I know it’s not Magic, and I also know I don’t know Groovy well enough to tell you where/when/why you have to do it one way or another.
All I can tell you is that when I get errors evaluating numbers, casting them to integers (or whatever you need) eliminates the type errors

(Keith Croshaw) #7

Good to know I’m not alone. I’m used to strictly declaring and knowing what datatype everything is so groovy and ST is clear as mud…

Thanks again, haven’t tested it yet.

(Keith Croshaw) #8

So my first error was:

java.lang.ClassCastException: cannot be cast to java.math.BigDecimal

Should I be trying to convert everything to .toBigDecimal() ?

Any suggestions / tips would be appreciated.

@Jim feel free to chime in. :smiley: PS I tried your mySensor.currentState(“temperature”) to no avail.

Pardon any programming vulgarities, I’m just struggling through this one. (Like not understanding how to create new variables of the correct datatype on line 114-117…)

(Jim Anderson) #9

for the setpoints, it looks like you’re just using the attribute itself, like thermostat.heatingSetpoint. I think that just returns the attribute itself, like you’re seeing in the exception.

Try getting the current value via the current<attributeName> method: thermostat.currentHeatingSetpoint, just like you are for the temperature attributes.

Hope that helps.

(Keith Croshaw) #10

@Jim thanks a lot, that seemed to be it. Just out of curiosity I don’t see that here:

Is that the wrong place for that kind of info?

(Jim Anderson) #11

It’s actually documented on the Device documentation:

(Keith Croshaw) #12

Hmm I kinda see how that satisfies that documentation.

Any word on the ide showing context options as you type to show what’s actually available as you type?

(Patrick Stuart [@pstuart]) #13

Be careful of converting decimals to integers. ST is plagued with this shortcut all over the place. Keep in mind, this truncates decimal values.

So a temp of 75.9 and 75.0 will be equal.

The proper method is to round not truncate in most comparison situations prior to conversion to integer.

(Keith Croshaw) #14

Thanks for the heads up!