node-red-contrib-energymeterplus 0.2.7 → 0.3.0

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.2.5**
1
+ #### **EnergyMeterPlus Node Version 0.3.0**
2
2
 
3
3
 
4
4
 
@@ -172,7 +172,7 @@ Costs scale automatically with your configured unit cost.
172
172
 
173
173
 
174
174
 
175
- ###### **V2.3:**
175
+ ###### **v2.3:**
176
176
 
177
177
 
178
178
 
@@ -192,7 +192,7 @@ updated counters to continuous absorption, no transfer at rollover
192
192
 
193
193
 
194
194
 
195
- ###### v2.4
195
+ ###### **v2.4**
196
196
 
197
197
 
198
198
 
@@ -208,9 +208,17 @@ Fixed Baselines bug: Baselines captured once, stored in context, and survive aft
208
208
 
209
209
 
210
210
 
211
- v2.5
211
+ ###### **v2.5**
212
212
 
213
213
 
214
214
 
215
215
  Fixed internal counter bug
216
216
 
217
+
218
+
219
+ ###### **v3.0**
220
+
221
+
222
+
223
+ **Major overhaul of UI and internal logic programming to fix bugs in counters and baseline corrections**
224
+
@@ -1,17 +1,18 @@
1
+
1
2
  <script type="text/javascript">
2
3
  RED.nodes.registerType('energyMeterPlus', {
3
4
  category: 'energy',
4
- color: '#f3a108fe', <!-- Dutch Orange -->
5
+ color: '#f3a108fe', //-- Dutch Orange --
5
6
  defaults: {
6
7
  name: {value:""},
7
- baselineDaily: {value:0},
8
- baselineWeekly: {value:0},
9
- baselineMonthly: {value:0},
10
- baselineYearly: {value:0},
11
- unitCost: {value:0.15},
12
- currency: {value:"USD"}, // NEW field
13
- filePath: {value:"/config/node_red/solargen_data.json"},
14
- inputUnit: {value:"kW"}
8
+ baselineDaily: {value:0, required:false},
9
+ baselineWeekly: {value:0, required:false},
10
+ baselineMonthly: {value:0, required:false},
11
+ baselineYearly: {value:0, required:false},
12
+ unitCost: {value:0.15, required:true},
13
+ currency: {value:"USD", required:true},
14
+ filePath: {value:"/config/node_red/solargen_data.json", required:false},
15
+ inputUnit: {value:"W", required:true}
15
16
  },
16
17
  inputs:1,
17
18
  outputs:1,
@@ -19,8 +20,33 @@
19
20
  label: function() {
20
21
  return this.name || "energyMeterPlus";
21
22
  },
23
+ oneditprepare: function() {
24
+ // Nothing special here yet
25
+ },
22
26
  oneditsave: function() {
23
- // Cosmetic blanking: clear baseline fields in the editor UI after deploy
27
+ // Collect baseline values
28
+ var daily = Number($("#node-input-baselineDaily").val()) || 0;
29
+ var weekly = Number($("#node-input-baselineWeekly").val()) || 0;
30
+ var monthly = Number($("#node-input-baselineMonthly").val()) || 0;
31
+ var yearly = Number($("#node-input-baselineYearly").val()) || 0;
32
+
33
+ var baselineMsg = {
34
+ nodeId: this.id,
35
+ payload: { daily, weekly, monthly, yearly }
36
+ };
37
+
38
+ // Feedback logic
39
+ if (daily === 0 && weekly === 0 && monthly === 0 && yearly === 0) {
40
+ RED.notify("⚠️ Baseline values are all zero — nothing applied", {type:"warning", timeout:3000});
41
+ } else if (isNaN(daily) || isNaN(weekly) || isNaN(monthly) || isNaN(yearly)) {
42
+ RED.notify("❌ Baseline values invalid — please enter numbers", {type:"error", timeout:4000});
43
+ } else {
44
+ RED.comms.send("energyMeterPlus/applyBaseline", baselineMsg);
45
+ RED.notify("✅ Baseline offset applied successfully", {type:"success", timeout:2000});
46
+ console.log("Baseline offset applied:", baselineMsg.payload);
47
+ }
48
+
49
+ // Clear fields after save
24
50
  $("#node-input-baselineDaily").val("");
25
51
  $("#node-input-baselineWeekly").val("");
26
52
  $("#node-input-baselineMonthly").val("");
@@ -34,24 +60,28 @@
34
60
  <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
35
61
  <input type="text" id="node-input-name">
36
62
  </div>
63
+
64
+ <!-- Baseline offsets -->
37
65
  <div class="form-row">
38
- <label for="node-input-baselineDaily">Baseline Daily (kWh)</label>
39
- <input type="number" id="node-input-baselineDaily">
66
+ <label for="node-input-baselineDaily"><i class="fa fa-calendar-day"></i> Baseline Daily Offset</label>
67
+ <input type="number" id="node-input-baselineDaily" placeholder="Daily offset">
40
68
  </div>
41
69
  <div class="form-row">
42
- <label for="node-input-baselineWeekly">Baseline Weekly (kWh)</label>
43
- <input type="number" id="node-input-baselineWeekly">
70
+ <label for="node-input-baselineWeekly"><i class="fa fa-calendar-week"></i> Baseline Weekly Offset</label>
71
+ <input type="number" id="node-input-baselineWeekly" placeholder="Weekly offset">
44
72
  </div>
45
73
  <div class="form-row">
46
- <label for="node-input-baselineMonthly">Baseline Monthly (kWh)</label>
47
- <input type="number" id="node-input-baselineMonthly">
74
+ <label for="node-input-baselineMonthly"><i class="fa fa-calendar-alt"></i> Baseline Monthly Offset</label>
75
+ <input type="number" id="node-input-baselineMonthly" placeholder="Monthly offset">
48
76
  </div>
49
77
  <div class="form-row">
50
- <label for="node-input-baselineYearly">Baseline Yearly (kWh)</label>
51
- <input type="number" id="node-input-baselineYearly">
78
+ <label for="node-input-baselineYearly"><i class="fa fa-calendar"></i> Baseline Yearly Offset</label>
79
+ <input type="number" id="node-input-baselineYearly" placeholder="Yearly offset">
52
80
  </div>
81
+
82
+ <!-- Other settings -->
53
83
  <div class="form-row">
54
- <label for="node-input-unitCost">Cost per Unit</label>
84
+ <label for="node-input-unitCost"><i class="fa fa-money"></i> Cost per Unit</label>
55
85
  <input type="number" id="node-input-unitCost" step="0.01">
56
86
  </div>
57
87
  <div class="form-row">
@@ -63,14 +93,21 @@
63
93
  </select>
64
94
  </div>
65
95
  <div class="form-row">
66
- <label for="node-input-filePath">Filepath</label>
96
+ <label for="node-input-filePath"><i class="fa fa-file"></i> Filepath</label>
67
97
  <input type="text" id="node-input-filePath">
68
98
  </div>
69
99
  <div class="form-row">
70
- <label for="node-input-inputUnit">Input Unit</label>
100
+ <label for="node-input-inputUnit"><i class="fa fa-bolt"></i> Input Unit</label>
71
101
  <select id="node-input-inputUnit">
72
102
  <option value="kW">kW</option>
73
103
  <option value="W">W</option>
74
104
  </select>
75
105
  </div>
76
106
  </script>
107
+
108
+ <script type="text/x-red" data-help-name="energyMeterPlus">
109
+ <p><b>Energy Meter Plus</b> tracks energy consumption and cost.</p>
110
+ <p>Outputs <code>energyDaily</code>, <code>energyWeekly</code>, <code>energyMonthly</code>, and <code>energyYearly</code> values, plus cost calculations.</p>
111
+ <p>You can set baseline offsets (daily, weekly, monthly, yearly) to adjust counters. These offsets are additive and applied on top of live increments.</p>
112
+ <p>Rollover logic resets counters at midnight (daily), Sunday midnight (weekly), first day of month (monthly), and Dec 31 midnight (yearly, with archive).</p>
113
+ </script>
@@ -2,6 +2,24 @@ const fs = require("fs");
2
2
  const path = require("path");
3
3
 
4
4
  module.exports = function(RED) {
5
+
6
+ // Listener for baseline messages from the editor
7
+ RED.comms.subscribe("energyMeterPlus/applyBaseline", function(baselineMsg) {
8
+ const nodeInstance = RED.nodes.getNode(baselineMsg.nodeId);
9
+ if (nodeInstance) {
10
+ let baseline = nodeInstance.context().get("baseline") || {
11
+ daily:0, weekly:0, monthly:0, yearly:0
12
+ };
13
+ // Additive offsets
14
+ baseline.daily += baselineMsg.payload.daily;
15
+ baseline.weekly += baselineMsg.payload.weekly;
16
+ baseline.monthly += baselineMsg.payload.monthly;
17
+ baseline.yearly += baselineMsg.payload.yearly;
18
+
19
+ nodeInstance.context().set("baseline", baseline);
20
+ }
21
+ });
22
+
5
23
  function EnergyMeterPlus(config) {
6
24
  RED.nodes.createNode(this, config);
7
25
  var node = this;
@@ -11,36 +29,23 @@ module.exports = function(RED) {
11
29
  let inputUnit = config.inputUnit || "W";
12
30
  let currencyCode = config.currency || "USD";
13
31
 
14
- // Counters start from context or zero
15
- let baseline = node.context().get("baseline") || {
16
- daily: 0,
17
- weekly: 0,
18
- monthly: 0,
19
- yearly: 0
20
- };
21
-
22
- // Capture baselines once on deploy
23
- let storedBaselines = {
24
- daily: Number(config.baselineDaily) || 0,
25
- weekly: Number(config.baselineWeekly) || 0,
26
- monthly: Number(config.baselineMonthly) || 0,
27
- yearly: Number(config.baselineYearly) || 0
28
- };
29
-
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
-
36
- // Discard baselines from UI after applying
32
+ // Baseline offsets
33
+ let baseline = node.context().get("baseline") || { daily:0, weekly:0, monthly:0, yearly:0 };
34
+
35
+ // Live increments
36
+ let accumulated = node.context().get("accumulated") || { daily:0, weekly:0, monthly:0, yearly:0 };
37
+
38
+ // Apply initial baselines from config
39
+ baseline.daily += Number(config.baselineDaily) || 0;
40
+ baseline.weekly += Number(config.baselineWeekly) || 0;
41
+ baseline.monthly += Number(config.baselineMonthly) || 0;
42
+ baseline.yearly += Number(config.baselineYearly) || 0;
43
+
37
44
  node.context().set("baseline", baseline);
38
- node.context().set("storedBaselines", { daily:0, weekly:0, monthly:0, yearly:0 });
45
+ node.context().set("accumulated", accumulated);
39
46
 
40
- // Last timestamp
41
47
  let lastCheck = node.context().get("lastCheck") || new Date();
42
48
 
43
- // Helpers
44
49
  function currencySymbol(code) {
45
50
  switch (code) {
46
51
  case "USD": return "$";
@@ -61,39 +66,44 @@ module.exports = function(RED) {
61
66
  if (fs.existsSync(archivePath)) {
62
67
  archive = JSON.parse(fs.readFileSync(archivePath));
63
68
  }
64
- archive.push({ year, total_kWh: total, timestamp: new Date().toISOString() });
69
+ archive.push({ year, total: total, timestamp: new Date().toISOString() });
65
70
  fs.writeFileSync(archivePath, JSON.stringify(archive, null, 2));
66
71
  } catch (err) {
67
72
  node.error("Failed to archive yearly total: " + err);
68
73
  }
69
74
  }
70
75
 
71
- // Rollover logic
76
+ // Rollover logic resets counters
72
77
  function checkRollover() {
73
78
  let now = new Date();
74
79
 
75
- // Daily rollover
80
+ // Daily reset at midnight
76
81
  if (now.getDate() !== lastCheck.getDate()) {
82
+ accumulated.daily = 0;
77
83
  baseline.daily = 0;
78
84
  }
79
85
 
80
- // Weekly rollover (Sunday midnight → Monday start)
86
+ // Weekly reset (Sunday midnight → Monday start)
81
87
  if (now.getDay() === 1 && lastCheck.getDay() !== 1) {
88
+ accumulated.weekly = 0;
82
89
  baseline.weekly = 0;
83
90
  }
84
91
 
85
- // Monthly rollover
92
+ // Monthly reset
86
93
  if (now.getMonth() !== lastCheck.getMonth()) {
94
+ accumulated.monthly = 0;
87
95
  baseline.monthly = 0;
88
96
  }
89
97
 
90
- // Yearly rollover
98
+ // Yearly reset
91
99
  if (now.getFullYear() !== lastCheck.getFullYear()) {
92
- archiveYearly(baseline.yearly, lastCheck.getFullYear());
100
+ archiveYearly(baseline.yearly + accumulated.yearly, lastCheck.getFullYear());
101
+ accumulated.yearly = 0;
93
102
  baseline.yearly = 0;
94
103
  }
95
104
 
96
105
  lastCheck = now;
106
+ node.context().set("accumulated", accumulated);
97
107
  node.context().set("baseline", baseline);
98
108
  node.context().set("lastCheck", lastCheck);
99
109
  }
@@ -109,26 +119,26 @@ module.exports = function(RED) {
109
119
  let power_kW = (inputUnit === "W") ? power / 1000 : power;
110
120
  let energyIncrement = power_kW * durationHours;
111
121
 
112
- // Apply increment separately to each counter
113
- baseline.daily += energyIncrement;
114
- baseline.weekly += energyIncrement;
115
- baseline.monthly += energyIncrement;
116
- baseline.yearly += energyIncrement;
122
+ accumulated.daily += energyIncrement;
123
+ accumulated.weekly += energyIncrement;
124
+ accumulated.monthly += energyIncrement;
125
+ accumulated.yearly += energyIncrement;
117
126
  }
118
127
 
119
128
  lastCheck = now;
120
129
  node.context().set("lastCheck", lastCheck);
121
- node.context().set("baseline", baseline);
130
+ node.context().set("accumulated", accumulated);
122
131
 
132
+ // Output = baseline + accumulated
123
133
  msg.payload = {
124
- daily_kWh: round2(baseline.daily),
125
- weekly_kWh: round2(baseline.weekly),
126
- monthly_kWh: round2(baseline.monthly),
127
- yearly_kWh: round2(baseline.yearly),
128
- daily_cost: round2(baseline.daily * unitCost),
129
- weekly_cost: round2(baseline.weekly * unitCost),
130
- monthly_cost: round2(baseline.monthly * unitCost),
131
- yearly_cost: round2(baseline.yearly * unitCost),
134
+ energyDaily: round2(baseline.daily + accumulated.daily),
135
+ energyWeekly: round2(baseline.weekly + accumulated.weekly),
136
+ energyMonthly: round2(baseline.monthly + accumulated.monthly),
137
+ energyYearly: round2(baseline.yearly + accumulated.yearly),
138
+ daily_cost: round2((baseline.daily + accumulated.daily) * unitCost),
139
+ weekly_cost: round2((baseline.weekly + accumulated.weekly) * unitCost),
140
+ monthly_cost: round2((baseline.monthly + accumulated.monthly) * unitCost),
141
+ yearly_cost: round2((baseline.yearly + accumulated.yearly) * unitCost),
132
142
  currency: currencySymbol(currencyCode)
133
143
  };
134
144
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-energymeterplus",
3
- "version": "0.2.7",
3
+ "version": "0.3.0",
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",