Calculation with Energy values

I found a lot of help but can’t get my code to work and need some help
Find below snippets from the code and the error

.items
Number:Energy           ShellyWasmachineMeterTotalKWH           "Wasmachine [%.3f Kwh]"         {channel="shelly:shellyplugs:b50ad5:meter#totalKWH"}
Number:Energy           ShellyWasmachineMeterTotalKWHTotal      "Wasmachine totaal [%.3f Kwh]"  <energy>        (BG_Wasruimte) ["PowerOutlet", "Power"]  
Number:Energy           ShellyWasmachineMeterTotalKWHDelta      "Wasmachine delta [%.3f Kwh]"   <energy>        (BG_Wasruimte) ["PowerOutlet", "Power"]  

.rules
rule "System started shelly power control"
when
    System started
then {
    if (ShellyWasmachineMeterTotalKWHTotal.state == NULL) { ShellyWasmachineMeterTotalKWHTotal.postUpdate(0 | kWh) }
    if (ShellyWasmachineMeterTotalKWHDelta.state == NULL) { ShellyWasmachineMeterTotalKWHDelta.postUpdate(0)| kWh)  }
    } 
end 

rule "Shelly power control"
when 
    Item ShellyWasmachineMeterTotalKWH changed 
then {
    var Number totalKWH = (ShellyWasmachineMeterTotalKWH.state as QuantityType<Number>).doubleValue
    var Number totalKWHDelta = (ShellyWasmachineMeterTotalKWHDelta.state as QuantityType<Number>).doubleValue
	//line below is line 87 mentioned in the logging
    var Number totalKWHTotal = (ShellyWasmachineMeterTotalKWHTotal.state as QuantityType<Number>).doubleValue
    if (ShellyWasmachineMeterTotalKWH.state == 0 | kWh) {
        ShellyWasmachineMeterTotalKWHDelta.postUpdate (totalKWHTotal)
    } else {
        totalKWHTotal = totalKWHDelta + totalKWH
        ShellyWasmachineMeterTotalKWHTotal.postUpdate (totalKWHTotal)
    }
    }
end

openhab.log
2021-11-08 15:23:44.423 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘OH3ShellyPowerRestore-2’ failed: Could not cast 0.591 to org.openhab.core.library.types.QuantityType; line 87, column 33, length 64 in OH3ShellyPowerRestore

When I run system started and the value of totalKWHTotal becomes 0 kWh (also shown when hoover) than the assignment of line 87 is working
After the value is assigned and I hoover it shows the value without kWh.
The second time line 87 fails with the error in logfile.
How can I do the assignment ShellyWasmachineMeterTotalKWHDelta.postUpdate (totalKWHTotal) and keep the kWh value because I assume that is the error.

You’re mixing whether to work with or against Quantity types e.g. with units.

That’s a Quantity type Item; it has units (kWh)

Here you’ve decided to extract just the numeric part, with no units.

Later on, you’ve tried to post that just-a-number, with no units, to your Quantity Item that needs units, and it doesn’t like that.

Be consistent; I’d just use Quantities with units throughout.

Thanks.
I tried to be consistent and tried different ways like the assignment below.
It is working but I’m losing the units.

    ShellyWasmachineMeterTotalKWHDelta.postUpdate (ShellyWasmachineMeterTotalKWH.getStateAs(QuantityType).doubleValue)

How to do this correctly?

I think it’s as simple as

ShellyWasmachineMeterTotalKWHDelta.postUpdate (ShellyWasmachineMeterTotalKWH.getStateAs(QuantityType))

or the fallback is, as always, to post a string and let the framework parse out what it wants

ShellyWasmachineMeterTotalKWHDelta.postUpdate (ShellyWasmachineMeterTotalKWH.state.toString)

Thanks.
This is helping very much.
Now it is as I how I wanted it.

rule "System started shelly power control"
when 
    Item ShellyWasmachineMeterTotalKWH changed 
then {
    if (ShellyWasmachineMeterTotalKWH.state == 0 | kWh) {
        ShellyWasmachineMeterTotalKWHDelta.postUpdate (ShellyWasmachineMeterTotalKWHTotal.getStateAs(QuantityType))
    } else {
       ShellyWasmachineMeterTotalKWHTotal.postUpdate (ShellyWasmachineMeterTotalKWHDelta.getStateAs(QuantityType) + ShellyWasmachineMeterTotalKWH.getStateAs(QuantityType))
    }
    }
end
1 Like

Hi!

I trying your code, but not working, can u help me?

rule "Shelly power control"
when 
    Item ShellyWasmachineMeterTotalKWH changed 
then {
    var Number totalKWH = (ShellyWasmachineMeterTotalKWH.state as QuantityType<Number>).doubleValue
    var Number totalKWHDelta = (ShellyWasmachineMeterTotalKWHDelta.state as QuantityType<Number>).doubleValue
    var Number totalKWHTotal = (ShellyWasmachineMeterTotalKWHTotal.state as QuantityType<Number>).doubleValue
    if (ShellyWasmachineMeterTotalKWH.state == 0 | kWh) {
        ShellyWasmachineMeterTotalKWHDelta.postUpdate (totalKWHTotal)
    } else {
        totalKWHTotal = totalKWHDelta + totalKWH
        ShellyWasmachineMeterTotalKWHTotal.postUpdate (totalKWHTotal)
    }
    }
end

Error message: [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Shelly power control’: Could not cast NULL to org.eclipse.smarthome.core.library.types.QuantityType; line 20, column 33, length 64

Try this

//---------------------------------------------------------------------------------------------------------------------------------------------
rule "Wasmachine save elke waarde van shelly en zet die terug na een power storing "
//---------------------------------------------------------------------------------------------------------------------------------------------
when 
    Item WasmachineMeterTotalKWH changed 
then {
    if (WasmachineMeterTotalKWH.state == 0 | kWh) {
        WasmachineMeterTotalKWHDelta.state = WasmachineMeterTotalKWHTotal.getStateAs(QuantityType)
    } else {
       WasmachineMeterTotalKWHTotal.state = WasmachineMeterTotalKWHDelta.getStateAs(QuantityType) + WasmachineMeterTotalKWH.getStateAs(QuantityType) }
    }
end