Round a number to use in a push-message (in a rule)

“simple” problem, but don’t found aworking solution :frowning:

I get the average temperatur for last 24 hours from my influxdb:

TempAvgToday = Garden_Temperature.averageSince(now.minusHours(24), "influxdb")

Result is i.e. 12.469565645672

Calculating with this is OK, but I also need to send the variable with a push-message inside the rule like:

   sendPushoverMessage(pushoverBuilder("Temperature-info:" +
                                    "\nDurchschnittstemperatur 24h     : " + TempAvgToday + " Grad" +

And here I will send only with 2 decimal numbers like:

But how? Value must not be roundet mathematically correct, “cutting” is also enough.

I test some hints, i.e. this:

   val dummy = (TempAvgToday).floatValue() * 100
   dummy = Math::round(dummy)
   TempAvgToday = dummy / 100

But here I get: 12.460000000000

I know in sitemap I can use the openHAB formatting with: .%2f
But in a rule and push-message ?

I think you can use the same formatter in a rule
String::format("%.2f", TempAvgToday)

Thanks Rossko, I try:

sendPushoverMessage(pushoverBuilder("Rasen-Bewaesserung-Infos:" + String::format("%.2f", TempAvgToday))

But get:
[ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘automatic irrigation, calculate irrigation time …’: f != org.eclipse.smarthome.core.library.types.DecimalType

(or must I also activate these lines again, with this lines it seems to work. Unclear why …:
// dummy = (TempAvgToday).floatValue() * 10
// dummy = Math::round(dummy)
// TempAvgToday = dummy / 10

I think you’d need to massage TempAvgToday into a Number type or something.
(TempAvgToday as DecimalType)

Thanks, but not working …

Had tried (from my side more “try and error”, maybe this is totally wrong) :
TempAvgToday = Garden_Temperature.averageSince(now.minusHours(12), “influxdb”) as DecimalType

Just need to change from 12.469565645672 to 12.46 :wink:
Really so hard in oH ?

DecimalType is the wrong type. (TempAvgToday as Number). The averageSince method returns a number.

How did you declare TempAvgToday? I don’t see the var definition.

Yes, sorry,.
TempAvgToday is declared as Number

then DecimalType is definitely the wrong type. Use Number.

I’ve not done much with String::format directly, it may require primitives. If as Number doesn’t work try TempAvgToday.floatValue.