node-red-contrib-energymeterplus 0.2.0 → 0.2.2

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/README.md CHANGED
@@ -1,4 +1,4 @@
1
- #### **EnergyMeterPlus Node Version 0.1.2**
1
+ #### **EnergyMeterPlus Node Version 0.2.1**
2
2
 
3
3
 
4
4
 
@@ -70,7 +70,7 @@ Input Unit: "W" for Watts (default) or "kW" if your source already provides kilo
70
70
 
71
71
 
72
72
 
73
- Baselines: Initial values for daily, weekly, monthly, and yearly counters. Can also be used to correct baseline values or left blank if not needed.
73
+ Baselines: Initial values for daily, weekly, monthly, and yearly counters. Can also be used to correct baseline values or left blank if not needed. Baseline values are applied once at startup or upon a config change and are cleared from the UI. New values can be entered as needed, and the cycle repeats.
74
74
 
75
75
 
76
76
 
@@ -55,46 +55,55 @@ module.exports = function(RED) {
55
55
  }
56
56
  }
57
57
 
58
- // Rollover logic
58
+ // Archive yearly totals
59
+ function archiveYearly(total, year) {
60
+ try {
61
+ const archivePath = path.join(path.dirname(filePath), "yearly_archive.json");
62
+ let archive = [];
63
+ if (fs.existsSync(archivePath)) {
64
+ archive = JSON.parse(fs.readFileSync(archivePath));
65
+ }
66
+ archive.push({
67
+ year: year,
68
+ total_kWh: total,
69
+ timestamp: new Date().toISOString()
70
+ });
71
+ fs.writeFileSync(archivePath, JSON.stringify(archive, null, 2));
72
+ } catch (err) {
73
+ node.error("Failed to archive yearly total: " + err);
74
+ }
75
+ }
76
+
77
+ // Safe rounding helper
78
+ function round2(val) {
79
+ if (val === null || val === undefined || isNaN(val)) return 0;
80
+ return Number(val.toFixed(2));
81
+ }
82
+
83
+ // Rollover logic with baseline reset
59
84
  function checkRollover() {
60
85
  let now = new Date();
61
86
 
62
87
  // Daily rollover (midnight)
63
88
  if (now.getDate() !== lastCheck.getDate()) {
64
- baseline.weekly += baseline.daily; // absorb yesterday
65
- baseline.daily = 0;
89
+ // Reset daily back to baseline offset instead of 0
90
+ baseline.daily = baselineDaily;
66
91
  }
67
92
 
68
93
  // Weekly rollover (Sunday → Monday)
69
94
  if (now.getDay() === 1 && lastCheck.getDay() !== 1) {
70
- baseline.monthly += baseline.weekly; // absorb last week
71
- baseline.weekly = 0;
95
+ baseline.weekly = baselineWeekly; // reset to baseline offset
72
96
  }
73
97
 
74
98
  // Monthly rollover (1st of month)
75
99
  if (now.getMonth() !== lastCheck.getMonth()) {
76
- baseline.yearly += baseline.monthly; // absorb last month
77
- baseline.monthly = 0;
100
+ baseline.monthly = baselineMonthly; // reset to baseline offset
78
101
  }
79
102
 
80
103
  // Yearly rollover (Jan 1)
81
104
  if (now.getFullYear() !== lastCheck.getFullYear()) {
82
- try {
83
- const archivePath = path.join(path.dirname(filePath), "yearly_archive.json");
84
- let archive = [];
85
- if (fs.existsSync(archivePath)) {
86
- archive = JSON.parse(fs.readFileSync(archivePath));
87
- }
88
- archive.push({
89
- year: lastCheck.getFullYear(),
90
- total_kWh: baseline.yearly,
91
- timestamp: new Date().toISOString()
92
- });
93
- fs.writeFileSync(archivePath, JSON.stringify(archive, null, 2));
94
- } catch (err) {
95
- node.error("Failed to archive yearly total: " + err);
96
- }
97
- baseline.yearly = 0;
105
+ archiveYearly(baseline.yearly, lastCheck.getFullYear());
106
+ baseline.yearly = baselineYearly; // reset to baseline offset
98
107
  }
99
108
 
100
109
  lastCheck = now;
@@ -114,8 +123,14 @@ module.exports = function(RED) {
114
123
 
115
124
  let energyIncrement = power_kW * durationHours;
116
125
 
126
+ // Add increment to daily
117
127
  baseline.daily += energyIncrement;
118
128
 
129
+ // Continuous absorption
130
+ baseline.weekly += energyIncrement;
131
+ baseline.monthly += energyIncrement;
132
+ baseline.yearly += energyIncrement;
133
+
119
134
  function round2(val) { return Number(val.toFixed(2)); }
120
135
 
121
136
  msg.payload = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-energymeterplus",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
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",
@@ -13,4 +13,3 @@
13
13
  }
14
14
  }
15
15
  }
16
-