Try not to warn multiple times for the same exceedance
parent
a64f6dbdcf
commit
028aee5d11
|
|
@ -1 +1,2 @@
|
||||||
0.01: Initial version
|
0.01: Initial version
|
||||||
|
0.02: Do not warn multiple times for the same exceedance
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,8 @@
|
||||||
|
|
||||||
Get a notification when the pressure reaches defined thresholds.
|
Get a notification when the pressure reaches defined thresholds.
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
## Settings
|
## Settings
|
||||||
|
|
||||||
* Interval: check interval of sensor data in minutes. 0 to disable automatic check.
|
* Interval: check interval of sensor data in minutes. 0 to disable automatic check.
|
||||||
* Low alarm: Toggle low alarm
|
* Low alarm: Toggle low alarm
|
||||||
* Low threshold: Warn when pressure drops below this value
|
* Low threshold: Warn when pressure drops below this value
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
"id": "widbaroalarm",
|
"id": "widbaroalarm",
|
||||||
"name": "Barometer alarm widget",
|
"name": "Barometer alarm widget",
|
||||||
"shortName": "Barometer alarm",
|
"shortName": "Barometer alarm",
|
||||||
"version": "0.01",
|
"version": "0.02",
|
||||||
"description": "A widget that can alarm on when the pressure reaches defined thresholds.",
|
"description": "A widget that can alarm on when the pressure reaches defined thresholds.",
|
||||||
"icon": "widget.png",
|
"icon": "widget.png",
|
||||||
"type": "widget",
|
"type": "widget",
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@
|
||||||
value: settings.min,
|
value: settings.min,
|
||||||
min: 600,
|
min: 600,
|
||||||
max: 1000,
|
max: 1000,
|
||||||
step: 10,
|
step: 5,
|
||||||
onchange: x => save("min", x),
|
onchange: x => save("min", x),
|
||||||
},
|
},
|
||||||
"High alarm": {
|
"High alarm": {
|
||||||
|
|
@ -48,9 +48,9 @@
|
||||||
},
|
},
|
||||||
"High threshold": {
|
"High threshold": {
|
||||||
value: settings.max,
|
value: settings.max,
|
||||||
min: 1000,
|
min: 700,
|
||||||
max: 1100,
|
max: 1100,
|
||||||
step: 10,
|
step: 5,
|
||||||
onchange: x => save("max", x),
|
onchange: x => save("max", x),
|
||||||
},
|
},
|
||||||
"Drop alarm": {
|
"Drop alarm": {
|
||||||
|
|
|
||||||
|
|
@ -6,14 +6,28 @@
|
||||||
const LOG_FILE = "widbaroalarm.log.json";
|
const LOG_FILE = "widbaroalarm.log.json";
|
||||||
const SETTINGS_FILE = "widbaroalarm.json";
|
const SETTINGS_FILE = "widbaroalarm.json";
|
||||||
const storage = require('Storage');
|
const storage = require('Storage');
|
||||||
let settings = Object.assign(
|
|
||||||
|
let settings;
|
||||||
|
|
||||||
|
function loadSettings() {
|
||||||
|
settings = Object.assign(
|
||||||
storage.readJSON("widbaroalarm.default.json", true) || {},
|
storage.readJSON("widbaroalarm.default.json", true) || {},
|
||||||
storage.readJSON(SETTINGS_FILE, true) || {}
|
storage.readJSON(SETTINGS_FILE, true) || {}
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
loadSettings();
|
||||||
|
|
||||||
|
|
||||||
function setting(key) {
|
function setting(key) {
|
||||||
return settings[key];
|
return settings[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function saveSetting(key, value) {
|
||||||
|
settings[key] = value;
|
||||||
|
storage.write(SETTINGS_FILE, settings);
|
||||||
|
}
|
||||||
|
|
||||||
const interval = setting("interval");
|
const interval = setting("interval");
|
||||||
|
|
||||||
let history3 = storage.readJSON(LOG_FILE, true) || []; // history of recent 3 hours
|
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) {
|
function checkForAlarms(pressure) {
|
||||||
if (pressure == undefined || pressure <= 0) return;
|
if (pressure == undefined || pressure <= 0) return;
|
||||||
|
|
||||||
|
let alreadyWarned = false;
|
||||||
|
|
||||||
const ts = Math.round(Date.now() / 1000); // seconds
|
const ts = Math.round(Date.now() / 1000); // seconds
|
||||||
const d = {
|
const d = {
|
||||||
"ts": ts,
|
"ts": ts,
|
||||||
|
|
@ -55,18 +74,35 @@
|
||||||
history3.shift();
|
history3.shift();
|
||||||
}
|
}
|
||||||
|
|
||||||
history3.push(d);
|
if (setting("lowalarm")) {
|
||||||
// write data to storage
|
// Is below the alarm threshold?
|
||||||
storage.writeJSON(LOG_FILE, history3);
|
if (pressure <= setting("min")) {
|
||||||
|
if (!didWeAlreadyWarn("lastLowWarningTs")) {
|
||||||
if (setting("lowalarm") && pressure <= setting("min")) {
|
|
||||||
showAlarm("Pressure low: " + Math.round(pressure) + " hPa");
|
showAlarm("Pressure low: " + Math.round(pressure) + " hPa");
|
||||||
|
saveSetting("lastLowWarningTs", ts);
|
||||||
alreadyWarned = true;
|
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");
|
showAlarm("Pressure high: " + Math.round(pressure) + " hPa");
|
||||||
|
saveSetting("lastHighWarningTs", ts);
|
||||||
alreadyWarned = true;
|
alreadyWarned = true;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
saveSetting("lastHighWarningTs", 0);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
saveSetting("lastHighWarningTs", 0);
|
||||||
|
}
|
||||||
|
|
||||||
if (!alreadyWarned) {
|
if (!alreadyWarned) {
|
||||||
// 3h change detection
|
// 3h change detection
|
||||||
|
|
@ -84,23 +120,37 @@
|
||||||
const diff = oldestPressure - pressure;
|
const diff = oldestPressure - pressure;
|
||||||
|
|
||||||
// drop alarm
|
// drop alarm
|
||||||
if (drop3halarm > 0 && oldestPressure > pressure) {
|
if (drop3halarm > 0 && oldestPressure > pressure && !didWeAlreadyWarn("lastHighWarningTs")) {
|
||||||
if (Math.abs(diff) > drop3halarm) {
|
if (Math.abs(diff) > drop3halarm) {
|
||||||
showAlarm((Math.round(Math.abs(diff) * 10) / 10) + " hPa/3h from " +
|
showAlarm((Math.round(Math.abs(diff) * 10) / 10) + " hPa/3h from " +
|
||||||
Math.round(oldestPressure) + " to " + Math.round(pressure) + " hPa", "Pressure drop");
|
Math.round(oldestPressure) + " to " + Math.round(pressure) + " hPa", "Pressure drop");
|
||||||
|
saveSetting("lastDropWarningTs", ts);
|
||||||
|
} else {
|
||||||
|
saveSetting("lastDropWarningTs", ts);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
saveSetting("lastDropWarningTs", ts);
|
||||||
}
|
}
|
||||||
|
|
||||||
// raise alarm
|
// raise alarm
|
||||||
if (raise3halarm > 0 && oldestPressure < pressure) {
|
if (raise3halarm > 0 && oldestPressure < pressure && !didWeAlreadyWarn("lastRaiseWarningTs")) {
|
||||||
if (Math.abs(diff) > raise3halarm) {
|
if (Math.abs(diff) > raise3halarm) {
|
||||||
showAlarm((Math.round(Math.abs(diff) * 10) / 10) + " hPa/3h from " +
|
showAlarm((Math.round(Math.abs(diff) * 10) / 10) + " hPa/3h from " +
|
||||||
Math.round(oldestPressure) + " to " + Math.round(pressure) + " hPa", "Pressure raise");
|
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
|
// calculate 3h average for widget
|
||||||
let sum = 0;
|
let sum = 0;
|
||||||
|
|
@ -180,6 +230,6 @@ check();
|
||||||
if (interval > 0) {
|
if (interval > 0) {
|
||||||
setInterval(check, interval * 60000);
|
setInterval(check, interval * 60000);
|
||||||
}
|
}
|
||||||
}, 5000);
|
}, 1000);
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue