Using OH 4.2.0-release with openHABian.
I’m having issues with calculating item values having UoMs. I want to learn how to use UoMs in Javascripting, so I don’t want to use “rawState”, and add the Unit later.
All my items do have the same Unit (W), not even one with kW
But, I can’t add/substract using +/-
so I thought I can use add(value)
and substract(value)
, but this also throws errors…
So my script is like this:
var interval = time.toZDT().minusSeconds(120);
var grundlast = items.getItem("KOS_HomeConsumption").persistence.averageSince(interval).state;
var waschmaschine = items.getItem("EMS_WaMaAktuellerVerbrauch").persistence.averageSince(interval).state;
var spuelmaschine = items.getItem("EMS_WaMaAktuellerVerbrauch").persistence.averageSince(interval).state;
var whirlpool = items.getItem("EMS_BBAktuellerVerbrauch").persistence.averageSince(interval).state;
var wallbox = items.getItem("evcc_EinfahrtLadeleistung").persistence.averageSince(interval).state;
console.info("DEBUG Grundlast: HomeConsumption: ", grundlast);
console.info("DEBUG Grundlast: Washing Machine: ", waschmaschine);
console.info("DEBUG Grundlast: Spülmaschine: ", spuelmaschine);
console.info("DEBUG Grundlast: Whirlpool: " , whirlpool);
console.info("DEBUG Grundlast: Wallbox: ", wallbox);
// console.info("Ermittle die Grundlast: ", grundlast+waschmaschine+spuelmaschine+whirlpool+wallbox);
// Abzüglich der bekannten Verbraucher
grundlast = grundlast.subtract(waschmaschine);
grundlast = grundlast.subtract(spuelmaschine);
grundlast = grundlast.subtract(whirlpool);
grundlast = grundlast.subtract(wallbox);
console.info("Ermittle die Grundlast: ", grundlast);
if (grundlast != null) {
items.getItem("EMS_Grundlast").postUpdate(grundlast.toString());
}
which brings the following error:
2024-07-22 11:31:22.280 [INFO ] [tomation.script.ui.EMS_CalcGrundlast] - DEBUG Grundlast: HomeConsumption: 3860.371527847215 W
2024-07-22 11:31:22.282 [INFO ] [tomation.script.ui.EMS_CalcGrundlast] - DEBUG Grundlast: Washing Machine: 0.4771407383953940 W
2024-07-22 11:31:22.283 [INFO ] [tomation.script.ui.EMS_CalcGrundlast] - DEBUG Grundlast: Spülmaschine: 0.4771541883987936 W
2024-07-22 11:31:22.284 [INFO ] [tomation.script.ui.EMS_CalcGrundlast] - DEBUG Grundlast: Whirlpool: 2939.975505307183 W
2024-07-22 11:31:22.285 [INFO ] [tomation.script.ui.EMS_CalcGrundlast] - DEBUG Grundlast: Wallbox: 0 W
2024-07-22 11:31:22.288 [ERROR] [.script.javascript.EMS_CalcGrundlast] - Failed to execute script: TypeError: grundlast.substract is not a function
at <js>.:program(<eval>:18)
at org.graalvm.polyglot.Context.eval(Context.java:399)
at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:458)
at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:426)
at java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262)
... 74 more
2024-07-22 11:31:22.289 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'EMS_CalcGrundlast' failed: org.graalvm.polyglot.PolyglotException: TypeError: grundlast.substract is not a function