node-red-contrib-energymeterplus 0.2.6 → 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.
Files changed (2) hide show
  1. package/energyMeterPlus.js +27 -13
  2. package/package.json +1 -1
@@ -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 always start from context or zero
14
+ // Counters start from context or zero
15
15
  let baseline = node.context().get("baseline") || {
16
16
  daily: 0,
17
17
  weekly: 0,
@@ -20,21 +20,22 @@ module.exports = function(RED) {
20
20
  };
21
21
 
22
22
  // Capture baselines once on deploy
23
- let storedBaselines = node.context().get("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
- // Apply each baseline only to its own counter
31
- if (storedBaselines.daily) baseline.daily += storedBaselines.daily;
32
- if (storedBaselines.weekly) baseline.weekly += storedBaselines.weekly;
33
- if (storedBaselines.monthly) baseline.monthly += storedBaselines.monthly;
34
- if (storedBaselines.yearly) baseline.yearly += storedBaselines.yearly;
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
+ // Discard baselines from UI after applying
36
37
  node.context().set("baseline", baseline);
37
- node.context().set("storedBaselines", storedBaselines);
38
+ node.context().set("storedBaselines", { daily:0, weekly:0, monthly:0, yearly:0 });
38
39
 
39
40
  // Last timestamp
40
41
  let lastCheck = node.context().get("lastCheck") || new Date();
@@ -70,14 +71,26 @@ module.exports = function(RED) {
70
71
  // Rollover logic
71
72
  function checkRollover() {
72
73
  let now = new Date();
73
- let stored = node.context().get("storedBaselines") || { daily:0, weekly:0, monthly:0, yearly:0 };
74
74
 
75
- if (now.getDate() !== lastCheck.getDate()) baseline.daily = stored.daily;
76
- if (now.getDay() === 1 && lastCheck.getDay() !== 1) baseline.weekly = stored.weekly;
77
- if (now.getMonth() !== lastCheck.getMonth()) baseline.monthly = stored.monthly;
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
78
91
  if (now.getFullYear() !== lastCheck.getFullYear()) {
79
92
  archiveYearly(baseline.yearly, lastCheck.getFullYear());
80
- baseline.yearly = stored.yearly;
93
+ baseline.yearly = 0;
81
94
  }
82
95
 
83
96
  lastCheck = now;
@@ -96,6 +109,7 @@ module.exports = function(RED) {
96
109
  let power_kW = (inputUnit === "W") ? power / 1000 : power;
97
110
  let energyIncrement = power_kW * durationHours;
98
111
 
112
+ // Apply increment separately to each counter
99
113
  baseline.daily += energyIncrement;
100
114
  baseline.weekly += energyIncrement;
101
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.6",
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",