node-red-contrib-energymeterplus 0.2.5 → 0.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/energyMeterPlus.js +28 -15
- package/package.json +1 -1
package/energyMeterPlus.js
CHANGED
|
@@ -11,7 +11,7 @@ module.exports = function(RED) {
|
|
|
11
11
|
let inputUnit = config.inputUnit || "W";
|
|
12
12
|
let currencyCode = config.currency || "USD";
|
|
13
13
|
|
|
14
|
-
// Counters
|
|
14
|
+
// Counters start from context or zero
|
|
15
15
|
let baseline = node.context().get("baseline") || {
|
|
16
16
|
daily: 0,
|
|
17
17
|
weekly: 0,
|
|
@@ -20,22 +20,22 @@ module.exports = function(RED) {
|
|
|
20
20
|
};
|
|
21
21
|
|
|
22
22
|
// Capture baselines once on deploy
|
|
23
|
-
let storedBaselines =
|
|
23
|
+
let storedBaselines = {
|
|
24
24
|
daily: Number(config.baselineDaily) || 0,
|
|
25
25
|
weekly: Number(config.baselineWeekly) || 0,
|
|
26
26
|
monthly: Number(config.baselineMonthly) || 0,
|
|
27
27
|
yearly: Number(config.baselineYearly) || 0
|
|
28
28
|
};
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
// Apply baselines once
|
|
31
|
+
baseline.daily += storedBaselines.daily;
|
|
32
|
+
baseline.weekly += storedBaselines.weekly;
|
|
33
|
+
baseline.monthly += storedBaselines.monthly;
|
|
34
|
+
baseline.yearly += storedBaselines.yearly;
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
36
|
+
// Discard baselines from UI after applying
|
|
37
|
+
node.context().set("baseline", baseline);
|
|
38
|
+
node.context().set("storedBaselines", { daily:0, weekly:0, monthly:0, yearly:0 });
|
|
39
39
|
|
|
40
40
|
// Last timestamp
|
|
41
41
|
let lastCheck = node.context().get("lastCheck") || new Date();
|
|
@@ -71,14 +71,26 @@ module.exports = function(RED) {
|
|
|
71
71
|
// Rollover logic
|
|
72
72
|
function checkRollover() {
|
|
73
73
|
let now = new Date();
|
|
74
|
-
let stored = node.context().get("storedBaselines") || { daily:0, weekly:0, monthly:0, yearly:0 };
|
|
75
74
|
|
|
76
|
-
|
|
77
|
-
if (now.
|
|
78
|
-
|
|
75
|
+
// Daily rollover
|
|
76
|
+
if (now.getDate() !== lastCheck.getDate()) {
|
|
77
|
+
baseline.daily = 0;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Weekly rollover (Sunday midnight → Monday start)
|
|
81
|
+
if (now.getDay() === 1 && lastCheck.getDay() !== 1) {
|
|
82
|
+
baseline.weekly = 0;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Monthly rollover
|
|
86
|
+
if (now.getMonth() !== lastCheck.getMonth()) {
|
|
87
|
+
baseline.monthly = 0;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Yearly rollover
|
|
79
91
|
if (now.getFullYear() !== lastCheck.getFullYear()) {
|
|
80
92
|
archiveYearly(baseline.yearly, lastCheck.getFullYear());
|
|
81
|
-
baseline.yearly =
|
|
93
|
+
baseline.yearly = 0;
|
|
82
94
|
}
|
|
83
95
|
|
|
84
96
|
lastCheck = now;
|
|
@@ -97,6 +109,7 @@ module.exports = function(RED) {
|
|
|
97
109
|
let power_kW = (inputUnit === "W") ? power / 1000 : power;
|
|
98
110
|
let energyIncrement = power_kW * durationHours;
|
|
99
111
|
|
|
112
|
+
// Apply increment separately to each counter
|
|
100
113
|
baseline.daily += energyIncrement;
|
|
101
114
|
baseline.weekly += energyIncrement;
|
|
102
115
|
baseline.monthly += energyIncrement;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-red-contrib-energymeterplus",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.7",
|
|
4
4
|
"description": "A custom Node-RED node that integrates power readings into energy totals with persistence and cost calculation.",
|
|
5
5
|
"author": "Arcfrankye",
|
|
6
6
|
"license": "MIT",
|