How to format quantity type in rules?

Hi,
How do I format quantity type in rules? I have the following item:

Number:Power                Output_power                 "Output Power [%.2f kW]"                   (gEaseeBox)           {channel="xxxxxxxxxxxxxxxxxxxxxxx", unit="kW"}

In the rules I’m logging the value, it prints too many digits after decimal points (looks like item formatting doesn’t apply for rules):

Charging has started at speed 10.767000198364258 kW

Now I try this:

logInfo("Tesla", "Charging has started at speed " + String.format("%.2f", Output_power.state.))

But I got the follow error:

Script execution of rule with UID 'Tesla-1' failed: f != org.openhab.core.library.types.QuantityType in Tesla

Any hints? Thanks in advance!

logInfo("Tesla", "Charging has started at speed " + Output_power.state.format("%.2f %unit%"))

Thanks, I tried the following:

    var totalPower = (Output_power.state as QuantityType<?>).toUnit('kW')
    var powerPct = new PercentType((totalPower/maxChargingSpeed).toBigDecimal*100)
    logInfo("EaseeBox", "The charging power has changed to {}, corresponding to charging power pct {}", totalPower.format("%.2f %kW%"),  powerPct)

However I see the error:

Script execution of rule with UID 'EaseeBox-2' failed: Conversion = 'k' in EaseeBox
    logInfo("EaseeBox", "The charging power has changed to {}, corresponding to charging power pct {}", totalPower.format("%.2f %unit%"),  powerPct)

The %unit% is actually literally that. Don’t replace it with kW. It will be replaced by format with the actual unit that the variable currently holds.

1 Like

Works thanks a lot!

1 Like