From 02bb0ab481cdcf26699c901ecdd140d088b7922a Mon Sep 17 00:00:00 2001 From: Marco H Date: Sun, 12 Jun 2022 17:46:25 +0200 Subject: [PATCH] Calculate 3h average only if data is available --- apps/widbaroalarm/widget.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/apps/widbaroalarm/widget.js b/apps/widbaroalarm/widget.js index 46ac9d480..d8eb43995 100644 --- a/apps/widbaroalarm/widget.js +++ b/apps/widbaroalarm/widget.js @@ -171,11 +171,15 @@ storage.writeJSON(LOG_FILE, history3); // calculate 3h average for widget - let sum = 0; - for (let i = 0; i < history3.length; i++) { - sum += history3[i]["p"]; + if (history3.length > 0) { + let sum = 0; + for (let i = 0; i < history3.length; i++) { + sum += history3[i]["p"]; + } + threeHourAvrPressure = sum / history3.length; + } else { + threeHourAvrPressure = undefined; } - threeHourAvrPressure = sum / history3.length; }