historicState() - HistoricItem.State to QuantityType<Energy>

I would like to do some calculations based on historicState.

Unfortunately I don’t understand how to get the QuantityType from the HistoricItem’s State. Is the information about quantity and unit lost when retrieving it from HistoricItem?

Number:Energy         ShellyplugSC1823F19216817861MeterTotalKWH_C1823F        "Shelly2 Meter total kwh"    {channel="shelly:shellyplugs:C1823F:meter#totalKWH"}
val String filename = "strom.rules"

rule "Strom kWh"

when  

    Time cron "0/4 * * * * ? *"

then

    logInfo(filename, filename)
    
    // works as QuantityType
    val load = ShellyplugSC1823F19216817861MeterTotalKWH_C1823F.state as QuantityType<Energy>
    logInfo(filename, "currentTotalLoad:" + load)

    val since = now.minusMinutes(10)
    logInfo(filename, "since:" + since)
    // does not work as QuantityType: Could not cast 10.03296 to org.openhab.core.library.types.QuantityType
    val loadSince = ShellyplugSC1823F19216817861MeterTotalKWH_C1823F.historicState(since).state as QuantityType<Energy>
    logInfo(filename, "loadSince:" + loadSince)

    //consumptionTag.postUpdate(loadDay)

end
2021-12-17 15:28:56.132 [INFO ] [penhab.core.model.script.strom.rules] - strom.rules
2021-12-17 15:28:56.137 [INFO ] [penhab.core.model.script.strom.rules] - currentTotalLoad:10.059 kWh
2021-12-17 15:28:56.143 [INFO ] [penhab.core.model.script.strom.rules] - since:2021-12-17T15:18:56.140869+01:00[Europe/Berlin]
2021-12-17 15:28:56.155 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'strom-1' failed: Could not cast 10.03296 to org.openhab.core.library.types.QuantityType; line 14, column 21, length 99 in strom

No, it’s lost when the data is recorded
BUT
It is reconstructed when retrieved using the Item’s default unit, as expressed in its [state presentation] or metadata ‘pattern’.

Your Item doesn’t seem to have a default unit.

Beware, that means if you change the default unit, historic data can come out wrong. Choose your default to match the unit it previously got from binding.

Oh thanks,

Number:Energy         ShellyplugSC1823F19216817861MeterTotalKWH_C1823F        "Shelly2 Meter total kwh [%.2f kWh]"    {channel="shelly:shellyplugs:C1823F:meter#totalKWH"}

After changing it it works fine.
Thanks, I did not know that the default unit is used from there before. Is this the preferred way to define it?
It would be great if you could specify it like Number:Energy:kWh (btw)

“It depends”
If you define Items from file, OH2 style, then it is a sensible choice.

Yes, I’m fully OH2 style in this regard.
For completeness:
To add the pattern via metadata one can use (not well tested by me and I don’t know the downsides):

Number:Energy         ShellyplugSC1823F19216817861MeterTotalKWH_C1823F        "Shelly2 Meter total kwh"    {channel="shelly:shellyplugs:C1823F:meter#totalKWH", stateDescription=""[pattern="%.2f kWh"]}

I’m wondering why the binding/thing/channel does not define this pattern though. Or maybe it does but it does not work well in this case.

It’s not obliged to, and indeed the ‘binding auto suggested pattern’ is leading to much gnashing of teeth due to unwanted side effects.

An alternative approach here is a enhancement to the persistence service - if it cannot identify a defined default unit when retrieving for a Quantity type Item, why not use the existing state value unit, if it currently has one.