Math function

im trying to compare two values but am getting the following error, can someone advise on what im doing wrong

log.debug  " ${device.currentValue("thermostatSetpoint")} < ${state.temperature}"
log.debug  " ${device.currentValue("thermostatSetpoint")< state.temperature}"

debug 17.5 < 18.06
followed by
errorjava.lang.ClassCastException: java.lang.String cannot be cast to java.math.BigDecimal on line 174 (parse)

once ive got it working i was going to put it in an IF
if (state.temperature < device.currentValue("thermostatSetpoint")){

thermostat setpoint is a string as used there. You need to get it into a number variable or convert it.

Actually they are probably both strings, now that I think about it...

i thought that but have tried asIntger, double, but im not a programmer so no idea how to convert and cant find an example of it in practices

Any of the normal groovy transforms should work. .toInteger() is common, but you need a float not an integer.

sorry how do i convert a string to float?

log.debug " ${Float.parseFloat(device.currentValue("thermostatSetpoint"))< Float.parseFloat(state.temperature)}"

gives

errorgroovy.lang.MissingMethodException: No signature of method: static java.lang.Float.parseFloat() is applicable for argument types: (java.math.BigDecimal) values: [16.5] Possible solutions: parseFloat(java.lang.String) on line 174 (parse)

That's easily found in google... "groovy string to float"....

But, to be more helpful, I would try one of these:

variable.toFloat()
or
variable.toBigDecimal()

Depending on how I was using it I do one or the other. I don't have a way to test it offhand, but I think that would do it.

Good luck!!!

log.debug " ${variable.toFloat(device.currentValue("thermostatSetpoint"))< variable.toFloat(state.temperature)}"

gives

errorjava.lang.NullPointerException: Cannot invoke method toFloat() on null object on line 174 (parse)

device.currentValue("thermostatSetpoint").toFloat()

2 Likes

Try...

log.debug " ${device.currentValue("thermostatSetpoint").toFloat()< state.temperature.toFloat()}"

If that doesn't work, try something like

float setPoint = Float.parseFloat(device.currentValue("thermostatSetpoint"))
float temperature = Float.parseFloat(state.temperature)
log.debug " ${setPoint< temperature}"

2 Likes

if (state.temperature.toFloat() < device.currentValue("thermostatSetpoint").toFloat()){

Thanks guys

3 Likes

Glad you got it going! :+1: