Number conversion in rule

  • Platform information:
    • Hardware: Synology DS918+ (Intel)
    • OS: OH Docker
    • openHAB version: 1.8

Hello,

I send some JSON data from my heat pump to OH. The whole JSON-string is set to a variable using rest API. Input data is looking like this:

[{'name': 'mode', 'resp': '1.00', 'timestamp': 1543221486.85231}]

A rule extracts values using JSONPATH transform every time the input item is changed and set data for each item (here only listed one).

rule "parseRotexInput"
  when
    Item RotexInput changed
  then
    var String json = (RotexInput.state as StringType).toString.replace(".0","")
     postUpdate(Rotex_mode,          Integer::parseInt( transform("JSONPATH", "$.[?(@.name == 'mode')].resp", json).replace("\"","").replace("[","").replace("]","")))
   end

My problem: the input value (1.00) is converted to integer 10 instead of 1. If I change parseInt to parseFloat in rule, then I only can display value 1.0 instead of 1. But I only want to see the value 1. What I am doing wrong?

Thank you

That’s simpler:

rule "parseRotexInput"
when
    Item RotexInput changed
then
    var String json = RotexInput.state.toString
    Roetx_Mode.postUpdate(Integer::parseInt(transform("JSONPATH", "$.[0].resp", json))
end

In the sitemap:

Text item=Rotex_mode label="Mode [%d]"

Thank you Vincent. This is not working, because of this I had added the replace functions to remove this.

 Error during the execution of rule 'parseRotexInput': For input string: "["1.00"]" 

I found a working solution. Not simple, but working.

var String json = (RotexInput.state as StringType).toString.replace(".00","").replace(".0","")

Your approach is simpler, so I want to try to use this. jsonpath.com is telling me my JSON string is not valid. Maybe because of the [] instead of {}? Could this cause the error?

I think it’s the ', try replacing it:
It works in jsonpath.com when I replace it.

rule "parseRotexInput"
when
    Item RotexInput changed
then
    var String json = RotexInput.state.toString.replace("'","\"")
    Roetx_Mode.postUpdate(Integer::parseInt(transform("JSONPATH", "$.[0].resp", json))
end