Formatting decimals, quantityState

Stromverbrauch_Diff_Tag = items.Elgris_Smart_Meter_WP_Total_Wh_imported_Value_as_Number.persistence.deltaBetween(dateBegin, dateEnd).numericState;
items.Elgris_Stromzaehler_WP_Verbrauch_Tag_persist.postUpdate(Stromverbrauch_Diff_Tag.toString());

logger.info('Elgris Stromverbrauch WP Tag-1: ' + parseFloat(Stromverbrauch_Diff_Tag).toFixed(2) + ' Wh');
logger.info('Elgris Stromverbrauch WP Tag-2: ' + items.Elgris_Stromzaehler_WP_Verbrauch_Tag_persist.quantityState.toUnit('kWh'));
2024-12-02 11:22:30.766 [INFO ] [ab.automation.openhab-js.waermepumpe] - Elgris Stromverbrauch WP Tag-1: 12446.00 Wh
2024-12-02 11:22:30.768 [INFO ] [ab.automation.openhab-js.waermepumpe] - Elgris Stromverbrauch WP Tag-2: 12.446 kWh

Is it possible that the output of

items.Elgris_Stromzaehler_WP_Verbrauch_Tag_persist.quantityState.toUnit('kWh')

is formatted directly? 2 decimal places are to be output:

12.45 kWh

The state description pattern dictates how the value is shown on the UI. You’ve currently have it configured to show three decimal places with %.3f.

In rules, there is no accress to the state description pattern formatted state of an Item.

To show it with only two decimal places in a rule you’ve already shown how to do that on the previous line. You need to get the floatValue and then call toFixed(2), though you’ll have to tack back on the units if you want to show that too.

…_persist.quantityState.toUnit(‘kWh’).floatValue.toFixed(2) + ’ kWh’

Thank you very much. I already suspected that it was just a bit awkward.

items.Elgris_Stromzaehler_WP_Verbrauch_Tag_persist.quantityState.toUnit('kWh').floatValue.toFixed(2) + ' kWh'

returns the following error:

2024-12-02 15:54:00.705 [ERROR] [utomation.script.file.waermepumpe.js] - Failed to execute rule Stromverbrauch-W-rmepumpe-Gesamttag-c487148d-c9f3-4552-97ba-0c0dc9995c34: TypeError: undefined has no such function "toFixed": TypeError: undefined has no such function "toFixed"
        at execute (waermepumpe.js:55)
        at execute (@openhab-globals.js:2)

but the following works:

parseFloat(items.Elgris_Stromzaehler_WP_Verbrauch_Tag_persist.quantityState.toUnit('kWh')).toFixed(2) + ' kWh'