[SOLVED] Limit Decimals in rule & logs

I’ve got a knx temperature sensor to gives a very detailled temperature…
But I now want to fill the logs with a more ‘readable’ value. The sitemap is working great with the item label.


Number   SCH_BURO1_TEMP   "Temperatuur [%.2f C]"  <temperature>   { knx="3/2/0"  }

In the events.log:

2018-11-22 16:28:30.393 [ome.event.ItemCommandEvent] - Item 'SCH_BURO1_TEMP' received command 18.730000000000000426325641456060111522674560546875
2018-11-22 16:29:30.393 [ome.event.ItemCommandEvent] - Item 'SCH_BURO1_TEMP' received command 18.69000000000000127897692436818033456802681640625 
2018-11-22 16:37:30.393 [ome.event.ItemCommandEvent] - Item 'SCH_BURO1_TEMP' received command 18.69                                                       

Now following part of my rule:

logInfo("HVAC_Bureel", "HVAC113: Temperature is " + SCH_BURO1_TEMP.state )

gives me:

2018-11-22 16:43:30.370 [INFO ] [e.smarthome.model.script.HVAC_Bureel] - HVAC123: Temperature is 18.690000000000001278976924368180334568023681640625
2018-11-22 16:44:30.367 [INFO ] [e.smarthome.model.script.HVAC_Bureel] - HVAC123: Temperature is 18.71000000000000085265128291212022304534912109375                                                                                                        
2018-11-22 16:45:30.364 [INFO ] [e.smarthome.model.script.HVAC_Bureel] - HVAC123: Temperatuue is 18.660000000000000142108547152020037174224853515625

I would like to see in my logs only the value with 2 decimals after the comma.
I tried several things form the forum, but the closed thing gave me 8 decimals.
Any suggestion/tip what I should do best?

There are several ways to do this. Here are a couple.


    // Truncated to 2 decimal places
    logInfo("test", "Number={}", (((TestNumberItem.state as Number)*100).intValue)/100.0)
    logInfo("test", "Number={}", String::format("%.2f", (TestNumberItem.state as Number).doubleValue))
1 Like

Hey Mark,
What do you mean with this part?
Not clear to me where/how I must implement this. :blush:

Ah, OK. Seems that his is enough?

logInfo("HVAC_Bureel", "HVAC123: Temperature is {}", (((SCH_BURO1_TEMP.state as Number)*100).intValue)/100.0)

Sorry, that was just the statement I used to load the value into an item for use later in the log statements.

But, it seems you already figured that out. :wink:

1 Like