Group:Number:AVG - formatting/rounding Numbers in eMails

I have a group (Group:Number:AVG) that I show on an eMail coming out of OH 3.4.

{ awaymessage.append("\n - Average Rating is " + gAirIndoorRating.state + “%”) }

This is how it’s being shown → Average Rating is 94.33333333333333333333333333333333%

The item is defined as this and I even tried the meta option to fix it but it didn’t work.

Group:Number:AVG		gAirIndoorRating			"Average Air Quality Indoor Rating [%.0f %%]"	  		{ stateDescription=""[pattern="%.1f",min="0",max="100", readOnly=true] }

My system is file based, so the solution would need to be in within the .item / .rule files.

Best, Jay

Please be aware that the pattern is only for displaying the state in UI. If you want a specific format in a rule, you have to use something like String.format("%.1f",gAirIndoorRating.state.toString)

Used your syntax but it failed with this error.

Script execution of rule with UID ‘default-194’ failed: f != java.lang.String in default

Here’s the eMail syntax for that line.

{ awaymessage.append(“\n - Average Rating is “) }
{ awaymessage.append(String.format(”%.1f”,gAirIndoorRating.state.toString) + “%”) }

Best, Jay

yes, sure, .toString is nonsense…
try

{ awaymessage.append(String.format("%.1f %%", gAirIndoorRating.state)) }

or

{ awaymessage.append(String.format("%.1f %%", (gAirIndoorRating.state as Number))) }

instead…

Both syntax scenarios failed with this error.

failed: f != org.openhab.core.library.types.DecimalType

Best, Jay

Here’s the final solution:

{ awaymessage.append(“\n - Average Rating is “) }
{awaymessage.append(String.format(”%.1f %%”, (gAirIndoorRating.state as DecimalType).floatValue))}

Best, Jay

1 Like

Jepp, always struggling with types :slight_smile:

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.