How can I round a value to 2 digits

I also thought about the expire binding, but when I write the values every 5 minutes to my db, it receives an update, even if the value is the same.
Or is there a difference between writing a value which can be the same as the previous one and an update?

They are completely separate. Updates can be saved to the database but saving to the database does not cause updated to the item. And I’m pretty sure that NULL doesn’t get saved to the database so that that into consideration for what you set the item to with expire.

I added expire="10s" for testing. The state of this item changes to UNDEF after 10 seconds, but the group where this item is member of, always reports UNDEF

Group:Number:OR(UNDEF,0) Grp_Xiaomi_sensor_check

I don’t think you can use OR with a Number type. What would you expect it to be?\

But, if we assume it works, you probably need to use NULL instead of UNDEF.

As written, again assuming it works, Grp_Xiaomi_sensor_check will be 0 if there are no UNDEF Items and UNDEF if there is one or more UNDEF Items. But since UNDEF really isn’t a valid Item state I’m not sure what it would do.

This approach will probably not work then…

I first tried to give my sensor a unrealistic value (‘999’), I used MAX for the group, so when the group is 999, I know there’s something wrong. But I don’t want that it also writes this value in my db (my graphs are f*cked up now :slight_smile:)

It might work with NULL instead of UNDEF.

You could use a proxy Item and a Rule instead of the Group to keep track of whether a sensor is offline or reporting a bad value or something. But over all I suspect you can’t do this with just a Group.

var Double pi=3.14159265359
String.format("%.2f", pi)

… can be used to shorten doubles to 2 digits, e.g. during output within rules.

Looking for some help to round to 2 dec. places.

Here’s the question
My log entry is this

2019-01-14 13:27:14.980 [INFO ] [pse.smarthome.model.script.wmachine ] - Power avarage is 1.675235979963836

from this in my rule.

val Number DnAvg = tplinksmarthome_hs110_B1AFF9_power.averageSince(now.minusMinutes(3)) as Number
    
    logInfo("dryer ", "Power avarage is: {} ", DnAvg)

And I would like it to be this.

Power avarage is 1.67

And this is my items file def for that value.

Number DnAvg            "Dryer Power Avg"                                    (BF_LaundryRoom , GetPower)

Could this be formatted at the items file level for logging and persistence, to fix this issue?

Thanks for any help.

No, an Number is a Number, Make no difference to the system.
You, however as a Human, would like 2 decimals:

The item:

Number DnAvg            "Dryer Power Avg [%.2f kW]"                                    (BF_LaundryRoom , GetPower)

In the rule:
Combine the answer from 13d ago and your logINfo:
(By the way, average not avarage!!)

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

Tried that and I got an .

2019-01-14 14:24:07.739 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Dryer Consumption State Machine': f != org.eclipse.smarthome.core.library.types.DecimalType


Try that:

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

Same error???

2019-01-14 15:21:11.682 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Dryer Consumption State Machine': f != org.eclipse.smarthome.core.library.types.DecimalType


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