Try not to warn multiple times for the same exceedance

master
Marco H 2022-03-09 09:32:43 +01:00
parent a64f6dbdcf
commit 028aee5d11
5 changed files with 197 additions and 148 deletions

View File

@ -1 +1,2 @@
0.01: Initial version
0.02: Do not warn multiple times for the same exceedance

View File

@ -2,10 +2,8 @@
Get a notification when the pressure reaches defined thresholds.
![Screenshot](screenshot.png)
## Settings
* Interval: check interval of sensor data in minutes. 0 to disable automatic check.
* Low alarm: Toggle low alarm
* Low threshold: Warn when pressure drops below this value

View File

@ -2,7 +2,7 @@
"id": "widbaroalarm",
"name": "Barometer alarm widget",
"shortName": "Barometer alarm",
"version": "0.01",
"version": "0.02",
"description": "A widget that can alarm on when the pressure reaches defined thresholds.",
"icon": "widget.png",
"type": "widget",

View File

@ -36,7 +36,7 @@
value: settings.min,
min: 600,
max: 1000,
step: 10,
step: 5,
onchange: x => save("min", x),
},
"High alarm": {
@ -48,9 +48,9 @@
},
"High threshold": {
value: settings.max,
min: 1000,
min: 700,
max: 1100,
step: 10,
step: 5,
onchange: x => save("max", x),
},
"Drop alarm": {

View File

@ -6,14 +6,28 @@
const LOG_FILE = "widbaroalarm.log.json";
const SETTINGS_FILE = "widbaroalarm.json";
const storage = require('Storage');
let settings = Object.assign(
let settings;
function loadSettings() {
settings = Object.assign(
storage.readJSON("widbaroalarm.default.json", true) || {},
storage.readJSON(SETTINGS_FILE, true) || {}
);
}
loadSettings();
function setting(key) {
return settings[key];
}
function saveSetting(key, value) {
settings[key] = value;
storage.write(SETTINGS_FILE, settings);
}
const interval = setting("interval");
let history3 = storage.readJSON(LOG_FILE, true) || []; // history of recent 3 hours
@ -33,11 +47,16 @@
}
}
let alreadyWarned = false;
function didWeAlreadyWarn(key) {
return setting(key) == undefined || setting(key) > 0;
}
function checkForAlarms(pressure) {
if (pressure == undefined || pressure <= 0) return;
let alreadyWarned = false;
const ts = Math.round(Date.now() / 1000); // seconds
const d = {
"ts": ts,
@ -55,18 +74,35 @@
history3.shift();
}
history3.push(d);
// write data to storage
storage.writeJSON(LOG_FILE, history3);
if (setting("lowalarm") && pressure <= setting("min")) {
if (setting("lowalarm")) {
// Is below the alarm threshold?
if (pressure <= setting("min")) {
if (!didWeAlreadyWarn("lastLowWarningTs")) {
showAlarm("Pressure low: " + Math.round(pressure) + " hPa");
saveSetting("lastLowWarningTs", ts);
alreadyWarned = true;
}
if (setting("highalarm") && pressure >= setting("max")) {
} else {
saveSetting("lastLowWarningTs", 0);
}
} else {
saveSetting("lastLowWarningTs", 0);
}
if (setting("highalarm")) {
// Is above the alarm threshold?
if (pressure >= setting("max")) {
if (!didWeAlreadyWarn("lastHighWarningTs")) {
showAlarm("Pressure high: " + Math.round(pressure) + " hPa");
saveSetting("lastHighWarningTs", ts);
alreadyWarned = true;
}
} else {
saveSetting("lastHighWarningTs", 0);
}
} else {
saveSetting("lastHighWarningTs", 0);
}
if (!alreadyWarned) {
// 3h change detection
@ -84,23 +120,37 @@
const diff = oldestPressure - pressure;
// drop alarm
if (drop3halarm > 0 && oldestPressure > pressure) {
if (drop3halarm > 0 && oldestPressure > pressure && !didWeAlreadyWarn("lastHighWarningTs")) {
if (Math.abs(diff) > drop3halarm) {
showAlarm((Math.round(Math.abs(diff) * 10) / 10) + " hPa/3h from " +
Math.round(oldestPressure) + " to " + Math.round(pressure) + " hPa", "Pressure drop");
saveSetting("lastDropWarningTs", ts);
} else {
saveSetting("lastDropWarningTs", ts);
}
} else {
saveSetting("lastDropWarningTs", ts);
}
// raise alarm
if (raise3halarm > 0 && oldestPressure < pressure) {
if (raise3halarm > 0 && oldestPressure < pressure && !didWeAlreadyWarn("lastRaiseWarningTs")) {
if (Math.abs(diff) > raise3halarm) {
showAlarm((Math.round(Math.abs(diff) * 10) / 10) + " hPa/3h from " +
Math.round(oldestPressure) + " to " + Math.round(pressure) + " hPa", "Pressure raise");
saveSetting("lastRaiseWarningTs", ts);
} else {
saveSetting("lastRaiseWarningTs", ts);
}
} else {
saveSetting("lastRaiseWarningTs", ts);
}
}
}
}
}
history3.push(d);
// write data to storage
storage.writeJSON(LOG_FILE, history3);
// calculate 3h average for widget
let sum = 0;
@ -180,6 +230,6 @@ check();
if (interval > 0) {
setInterval(check, interval * 60000);
}
}, 5000);
}, 1000);
})();