Rule to notify when first number of temp changes

I’m trying to round my temperature to the lowest tenth of temperature (27.1 -> 20 and 31.2 -> 30).

Item is:
Number sw_leiliruum_temp3 “Sauna Up [%.2f °C]”

From influx when i get last value from influx i do get the desired result - 20 (last is 27.0)

val Number lastSaunaTemp = (sw_leiliruum_temp3.previousState(true, “influxdb”).state)
val int lastYmardus = Math::round((lastSaunaTemp.longValue / 10) * 10).intValue

But the problem is when i trie to do the same with current temperature.
The result is 30 (temp is same - 27.1).

val calculatedCurrTemp = (Math::round((sw_leiliruum_temp3.state as DecimalType).floatValue()/10) * 10)
logInfo(“saun”, "calculatedCurrTemp: " + calculatedCurrTemp.toString)

I’ve tried different approaches and reading a lot of forum posts - but it’s still the same :frowning:

I’m quite sertain it has something to do with conversions … but i can’t figure it out :frowning:

Can someone help … ?

Math::round by design rounds to nearest whole number. Did you mean to use Math::floor ? And wouldn’t you floor the divide-by-ten number before multiplying by ten again?

Just let integer division sort it out for you. When you divide an integer by an integer, the result will also be an integer (taken as floor). Then you multiply by 10.

val calculatedCurrTemp = (sw_leiliruum_temp3.getStateAs(DecimalType).intValue / 10) * 10

I knew there was another way to do this, but I couldn’t remember it early. It just popped into my head… Integer has a method called divideUnsigned

val calculatedCurrTemp =  10 * Integer.divideUnsigned(sw_leiliruum_temp3.getStateAs(DecimalType).intValue, 10)

Excellent! Thank you! It works!

1 Like