Negative value to positive

Hi,
I am new to openhab and i am trying to do HVAC controller of raspberry pi b+. I have done some rules for a week now and today i hitted a wall.

My question is for advanced users is that how can i change negative value to positive value.

Rule example:

/// HVAC
rule "Lämmitys käyrä"
when
Item Netatmo_Outdoor_Temperature changed or
Item PatteriAsetusTmp changed
then
val asetuskayra = (Netatmo_Outdoor_Temperature.state as DecimalType) - 40
Lammitys_viesti.postUpdate(asetuskayra)

At this point if outside temperature is 10*C and i minus 40 it will be a negative value. I did this because i want to control water temperature linear with the outside temperature.

i appreciate for any help, thanks.

val asetuskayra = ((Netatmo_Outdoor_Temperature.state as DecimalType) - 40 )* -1

This is what i was looking.

It would be more flexible to use Math::abs(((Netatmo_Outdoor_Temperature.state as DecimalType) - 40).floatValue)

That way you will have a positive value whether temp-40 is positive or negative.

Hi Rich,

Thank you for answer that is useful information for me.

I have now added slope feature to the code and it looks like this:

/// HVAC
rule "Lämmitys käyrä"
when
Item Netatmo_Outdoor_Temperature changed or
Item PatteriAsetusTmp changed
then
//Changes outdoor celcius temp to kelvin
val UlkoTmpKelvin = (Netatmo_Outdoor_Temperature.state as DecimalType) + 273.15
//Setpoint value for water temp when outdoor temp is 20
var LattiaAsetusPlus20 = LattialammitysAsetusTmp.state as DecimalType
//Setpoint value for slope
var LattiaAsetusKerroin = LattiaAsetusKerroinSet.state as DecimalType
//Calculating Slope * 293.25 - Outdoor temperature
val LattiAsetusKerroinMuunnettu = (LattiaAsetusKerroin)*(293.15 - (UlkoTmpKelvin))
//Final calculating for water temperature which depending on outside temperature
val LattiaAsetuslampotila =(LattiaAsetusPlus20) + (LattiAsetusKerroinMuunnettu)

//Sends value to item
Lammitys_viesti.postUpdate(LattiaAsetuslampotila)

Is there any cons if i do calculating like this? Or is it always better youse math functions (didin’t find any documentation about math function).

Not really. Use what ever you are comfortable with. I mainly used Math:abs() above because it is less code than:

val temp = (Netatmo_Outdoor_Temperature.state as DecimalType) - 40
val asetuskayra = if(temp < 0) temp * -1 else temp

I would recommend using Math methods if you get into trig or logarithmic functions.