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.
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
//---------------------------------------------------------------------------------------------------------------------------------------------
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