Remove .0 from a number before passing it as a variable

I’m trying to create a rule to update my Sensibo API it works fine if I send the targetTemperature as a 2 digit number the problem is my Item has a “.0” (22.0) and this causes the API not to work.

How do I remove the .0? I know it can be done with regex and split but I’m terrible at that and can’t get it to work.

rule "Update Setting to Heatpump"
when Item Heatpump1TargetTemperature received command
then
if (Heatpump1.state == ON && Heatpump1Stable)
    logInfo("Heatpumps", "Heatpump1 Update Settings rule Ran "  +Heatpump1TargetTemperature.state)
    executeCommandLine('curl@@-H@@Content-Type: application/x-www-form-urlencoded/Heat@@-X@@POST@@-d@@{"acState":{"on":true,"mode":"cool","fanLevel":"low","targetTemperature":Heatpump1TargetTemperature.state}}@@https://home.sensibo.com/api/v2/pods/<MYPODID>/acStates?apiKey=<MYAPI>&fields=acState', 5000)
    PauseHeatpump1Updates = createTimer(now.plusSeconds(15))[|
    Heatpump1Stable = true
    ]}
end

The log file to show the .0 being passed:

2016-05-12 22:35:58.423 [INFO ] [openhab.model.script.Heatpumps] - Heatpump1 Update Settings rule Ran 23.0

I was able to copy from another thread where someone wanted something similar this is what I used:

import java.text.DecimalFormat

val DecimalFormat df = new DecimalFormat("#")
val Number Heatpump1TargetTemperatureRounded = (df.format(Heatpump1TargetTemperature.state as DecimalType))

Actually I’m having another issue, I don’t think my variable is getting passed to curl. Is it possible to do what I’m doing?

variable is Heatpump1TargetTemperatureRounded

executeCommandLine('curl@@-H@@Content-Type: application/x-www-form-urlencoded/Heat@@-X@@POST@@-d@@{"acState":"on":true,"mode":"cool","fanLevel":"low","targetTemperature":Heatpump1TargetTemperatureRounded}}@@https://home.sensibo.com/api/v2/pods/<MYPODID>/acStates?apiKey=<MYAPI>&fields=acState', 5000)

Yes but not like you are trying to do it.

executeCommandLine("curl@@-H@@Content-Type: application/x-www-form-urlencoded/Heat@@-X@@POST@@-d@@{\"acState\":\"on\":true,\"mode\":\"cool\",\"fanLevel\":\"low\",\"targetTemperature\":" +
    Heatpump1TargetTemperatureRounded.toString + 
    "}}@@https://home.sensibo.com/api/v2/pods/<MYPODID>/acStates?apiKey=<MYAPI>&fields=acState', 5000)

Changes and Notes:

  • I don’t think you can use single quotes
  • You need escape the double quotes therefore
  • I’m not certain the @@s are required in this context but if they are not hurting don’t worry about them
  • You need to construct the string using a string version of Heatpump1TargetTemperaturerounded. You can’t just plop the var name into your String.
1 Like

That worked, BTW had me scratching my head for awhile though there was missing a curly bracket and you added /Heat. I finally got it all working… I don’t want to admit how much time I’ve spent on this!