How to deal with null value using averageSince in ECMA-2021?

Dear community,

I started rewriting my Rules DSL to ECMA 2021. Now I have an issue where averageSince returns a null value and not ‘0’ like in Rules DSL when no values have written in the item-persistence the last 10minutes (in my example).

Here is my rule in ECMA 2021 and in the comment the working Rules DSL.

var GeschirrspuelerRemote = items.getItem("Dishwasher_RemoteStartAllowanceState");
var GeschirrspuelerOperation = items.getItem("Dishwasher_OperationState");
var GeschirrspuelerAction = items.getItem("Dishwasher_ProgramActionsState");
var WaschkuecheWaschmaschineStrom = items.getItem("KG_Waschkueche_SchukoStrom_Waschmaschine_Calculated");
var EchoWzTts = items.getItem("Echo_Wohnzimmer_TTS");

var jetzt = time.ZonedDateTime.now();
var vergangenheit = jetzt.minusMinutes(10);
var average = WaschkuecheWaschmaschineStrom.history.averageSince(vergangenheit);


if (GeschirrspuelerRemote.state == "ON" && GeschirrspuelerOperation.state == "Ready" && ((parseFloat(WaschkuecheWaschmaschineStrom.history.averageSince(vergangenheit)) === null) || (parseFloat(WaschkuecheWaschmaschineStrom.history.averageSince(vergangenheit)) < 1))) {
  console.log("Zeit", jetzt, vergangenheit, average); 
  console.log("Geschirrspüler startet");
  GeschirrspuelerAction.sendCommand("start");
  EchoWzTts.sendCommand('<speak>Geschirrspüler gestartet</speak>');
}

console.log("Zeit", jetzt, vergangenheit, average); 
console.log("Geschirrspüler startet nicht");

/*
if (Dishwasher_RemoteStartAllowanceState.state == ON && Dishwasher_OperationState.state == "Ready" && (KG_Waschkueche_SchukoStrom_Waschmaschine_Calculated.averageSince(now.minusMinutes(10)) as Number) < 1) {
  // && KG_Waschkueche_SchukoStrom_Waschmaschine_Calculated.averageSince(now.minusMinutes(10)) as Number < 1
  Dishwasher_ProgramActionsState.sendCommand("start")
  Echo_Wohnzimmer_TTS_Volume.sendCommand(50)
  Echo_Wohnzimmer_TTS.sendCommand('<speak>Geschirrspüler gestartet</speak>')
}
*/

Please give me a hint how to deal with the null value.
The log says the following

2022-01-21 11:05:01.763 [INFO ] [org.openhab.automation.script ] - Zeit “2022-01-21T11:05:01.705+01:00[SYSTEM]” “2022-01-21T10:55:01.705+01:00[SYSTEM]” null
2022-01-21 11:05:01.764 [INFO ] [org.openhab.automation.script ] - Geschirrspüler startet nicht

Thanks in advance, Br Thomas

Hey,

averageSince should return a Number or null.
parseFloat(null) returns NaN which is both false in your if statement. Since averageSince already returns a Number parseFloat should be obsolete.

Maybe you could try the following:

if (GeschirrspuelerRemote.state == "ON" && GeschirrspuelerOperation.state == "Ready" && (average === null || average < 1))

Thanks I will try that!

Thanks David - that did the trick!