How can I round a value to 2 digits

val String DnAvgString = String.format("%.2f", DnAvg)
logInfo("dryer ", "Power average is: " + DnAvgString)

different error???

2019-01-14 18:48:33.628 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Dryer Consumption State Machine': f != org.eclipse.smarthome.core.library.items.NumberItem

Old post I know, but for completeness this should work instead…

var DnAvgString = String.format("%.2f", DnAvg) 
logInfo("dryer ", "Power average is: " + DnAvgString)

Had the same issue with

tempSetWarm = Math.round((roomThermostatSetTemp007402363C0A.state as Number)/100)*100

and then added a .floatValue as this:

tempSetWarm = Math.round((roomThermostatSetTemp007402363C0A.state as Number).floatValue/100)*100

and then it worked!

Hi there,

can you give me an advice how to half round a number(int) to x.5 or x.0 depent on input with a rule ​?

Example:

  • Input 21.2 → round to 21.5
  • Input 22.6 → round to 23.0
  • Input 19.8 → round to 20.0
  • Input 20.1 → round to 20.5

BR

For a start the input numbers are no int (as in integer), further you won’t to round UP always to the next half value.
In order to give code hints we would need to know what kind of rule you are trying to use.

PseudoCode:
Seperate the integer part of the number and the decimal part.
If the decimal part is less or equal to .5 return the integer part plus .5,
else return the integer part plus 1.

1 Like

Alternative process;
subject number multiply x 2
round to integer
divide by 2

2 Likes

Thanks! That´s quite simple. I did not think of that.

In practice i controll Homatic radioator thermostats depent on a target temperature and the actual temperature in the room. The HM devices only accept. x.0 or x.5.

BR

Hello. sorry to re-open an old thread on an old subject that doubtless has been covered countless times.

I have a mqtt channel that delivers numbers to 2dp e.g.

rule “UpdatePage1HallTempLabel”
when
Item GenericMQTTThing_HallTemperature changed
then
var tempVal = GenericMQTTThing_HallTemperature.state as Number
logInfo ("updated hall temp ", tempVal.toString)
ToPage1HallTempItem.sendCommand(tempVal)
end

the item then triggers another mqtt channel to forward the number to an openhasp panel.

i want that to be sent as a 1DP number. i.e. instead or 15.67, 15.7 for display neatly in the panel.

I have tried so many things, nothing works or i get errors. where is there a page which explains how the dsl language syntax works? i tried the xtend pages but they are confusing and don’t seem to cover the functions like toString or the formatting codes.

i love openhab and have used it for years but fair to say i dip in and out. when it works, it works, but setting things up like can be very frustrating to a non IT person steeped in java data types etc.

thanks again.

I don’t think you need DSL for this.

In the item definition you can add stateDescription Pattern: %.1f

This does not alter the item value (state), but shows the item as you want it (displayState).

Thanks. So on the inbound (set up in the UI) I have:

image

and sure enough it is displayed at 1DP:

However the underlying number is unaffected e.g.

[INFO ] [openhab.event.ItemCommandEvent ] - Item ‘ToPage1HallTempItem’ received command 16.48

and this is what is transmitted out.

The “outbound” item is set up in text. Is this how to define the state description? Can’t find how in the docs.

String ToPage1HallTempItem “Temperature [%.1f]” {channel = “mqtt:topic:MyMQTTBroker:OpenHASPPanelThing:ToPage1HallTempChannel”}

The first place to look is Textual Rules | openHAB. If that doesn’t answer what you are looking for, the next place is the xtend docs you’ve already found. Finally if that doesn’t answer question the last place to look are the JavaDocs. Which JavaDocs depends on whether what you are looking for is an openHAB class or a core Java class.

toString() isn’t covered separately primarily becuase all classes in Java and therefore all classes in Xtend have this method. It’s going to be covered in basic level Java documentation. Note, toString() is a method on all classes in most languages.

The string formatting options come from the core Java so you would look there. Spericially in the JavaDoc for the String class, the format method. I’m pretty sure this is linked to from somewhere in the OH docs.

On a second note, if you struggle with this I highly recommend looking at Blockly for new rules going forward (I am not suggesting you rewrite your existing rules). Everything is present to you with explanations, it’s almost impossible to make a syntax error, and you almost never need to use the reference docs because everthing is well docimented inline.

But, as @DiegoDf says, you don’t need a Rule for this.

Correct. Premature rounding of values leads to errors which can build upon each other very quickly to the point where any calculation you may do can result in widley inaccurate results. Therefore it is considered best practice to not round numbers until the very last possible moment.

In this case the “last possible moment” is when it’s shown to the user on the display, and when publishing the value to your panel. You have the first case already covered.

To handle the second case, on your MQTT Thing, there is a transformationPatterOut property on the Channel. You can put any transformation there that you want. For example, this JS transformation will round the value to one decimal place

JS: | parseFloat(input).toFixed(1)

You can use Rules DSL too or any of the other languages. But it’s pretty awkward in DSL. Everything is awkward in DSL, which is one reason I no longer recommend it for new development anymore. Any advantages it once had have been met or exceeded in all the rest of the rules options now.

But you don’t even need to write a transformation script. You can use the Round Profile from the Basic Profiles add-on. This is applied at the link between the Item and the Channel. See Items | openHAB.

OK so… JS transform…

I have created a new MQTT channel (number type) with:

and linked an Item:

Number roundedNumber {channel=“mqtt:topic:296339e167:bd0065781b:HallTempRounded”}

I still get the number reported to 2DP:

[INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘roundedNumber’ changed from 15.72 to 15.73

When i try the Basic Profile

Number roundedNumber {channel=“mqtt:topic:296339e167:bd0065781b:HallTempRounded” [profile=“basic-profiles:round”, scale=1]}

I get similar output:

[INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘roundedNumber’ changed from 15.78 to 15.79

and with this rule:

rule “UpdatePage1HallTempLabeltest”

when

Item roundedNumber changed

then

var tempVal = roundedNumber.state as Number

logInfo ("updated hall temp test ", tempVal.toString)

ToPage1HallTempItem.sendCommand(tempVal)

end

it still sends the 2DP number:

[INFO ] [openhab.event.ItemCommandEvent ] - Item ‘ToPage1HallTempItem’ received command 15.83

Well, yes. The number isn’t rounded until the MQTT message is published. The Item’s state themselves will never be rounded with this configuration.

You have to look at the number actually being published to the MQTT topic. events.log is never going top show a rounded value with a state description pattern nor with an outgoing value transformation.

Sadly not:

[INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘roundedNumber’ changed from 17.21 to 17.22

hasp/mainpanel/command/p1b12.text 17.22

By the way, should i have installed anything to use Basic Profile?

Did you try to use the transformation at the input?

GenericMQTTThing_HallTemperature in your first example, roundedNumber in your second example.

Here is an example where I round the Azimut value of the Astro binding:

It is correctly displayed in in the log:

Item 'astroAzimut' changed from 215.34230346192476 ° to 215.34 °

Yes, like I said above, it’s an add-on. Add-ons always have to be installed before you can use them. I would expect to see errors in the logs if it’s not installed.

I can’t see this add-on in the Add-On Store. I only have “JavaScript Scripting” installed. It is definitely defined in the item, does not seem to do anything and, at the same time, is not generating any errors in the log.

Number roundedNumber {channel=“mqtt:topic:296339e167:bd0065781b:HallTempRounded” [profile=“basic-profiles:round”, scale=1]}

What version of OH?