node-red-contrib-energymeterplus 0.2.8 → 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 +12 -4
- package/energyMeterPlus.html +52 -26
- package/energyMeterPlus.js +44 -53
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#### **EnergyMeterPlus Node Version 0.
|
|
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
|
-
###### **
|
|
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
|
+
|
package/energyMeterPlus.html
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
|
|
1
2
|
<script type="text/javascript">
|
|
2
3
|
RED.nodes.registerType('energyMeterPlus', {
|
|
3
4
|
category: 'energy',
|
|
4
|
-
color: '#f3a108fe',
|
|
5
|
+
color: '#f3a108fe', //-- Dutch Orange --
|
|
5
6
|
defaults: {
|
|
6
7
|
name: {value:""},
|
|
7
8
|
baselineDaily: {value:0, required:false},
|
|
@@ -9,9 +10,9 @@
|
|
|
9
10
|
baselineMonthly: {value:0, required:false},
|
|
10
11
|
baselineYearly: {value:0, required:false},
|
|
11
12
|
unitCost: {value:0.15, required:true},
|
|
12
|
-
currency: {value:"USD", required:true},
|
|
13
|
+
currency: {value:"USD", required:true},
|
|
13
14
|
filePath: {value:"/config/node_red/solargen_data.json", required:false},
|
|
14
|
-
inputUnit: {value:"
|
|
15
|
+
inputUnit: {value:"W", required:true}
|
|
15
16
|
},
|
|
16
17
|
inputs:1,
|
|
17
18
|
outputs:1,
|
|
@@ -20,22 +21,36 @@
|
|
|
20
21
|
return this.name || "energyMeterPlus";
|
|
21
22
|
},
|
|
22
23
|
oneditprepare: function() {
|
|
23
|
-
|
|
24
|
+
// Nothing special here yet
|
|
24
25
|
},
|
|
25
26
|
oneditsave: function() {
|
|
26
|
-
//
|
|
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
|
+
|
|
27
33
|
var baselineMsg = {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
payload: {
|
|
31
|
-
daily: Number($("#node-input-baselineDaily").val()) || 0,
|
|
32
|
-
weekly: Number($("#node-input-baselineWeekly").val()) || 0,
|
|
33
|
-
monthly: Number($("#node-input-baselineMonthly").val()) || 0,
|
|
34
|
-
yearly: Number($("#node-input-baselineYearly").val()) || 0
|
|
35
|
-
}
|
|
34
|
+
nodeId: this.id,
|
|
35
|
+
payload: { daily, weekly, monthly, yearly }
|
|
36
36
|
};
|
|
37
|
-
|
|
38
|
-
|
|
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
|
|
50
|
+
$("#node-input-baselineDaily").val("");
|
|
51
|
+
$("#node-input-baselineWeekly").val("");
|
|
52
|
+
$("#node-input-baselineMonthly").val("");
|
|
53
|
+
$("#node-input-baselineYearly").val("");
|
|
39
54
|
}
|
|
40
55
|
});
|
|
41
56
|
</script>
|
|
@@ -45,24 +60,28 @@
|
|
|
45
60
|
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
46
61
|
<input type="text" id="node-input-name">
|
|
47
62
|
</div>
|
|
63
|
+
|
|
64
|
+
<!-- Baseline offsets -->
|
|
48
65
|
<div class="form-row">
|
|
49
|
-
<label for="node-input-baselineDaily">Baseline Daily
|
|
50
|
-
<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">
|
|
51
68
|
</div>
|
|
52
69
|
<div class="form-row">
|
|
53
|
-
<label for="node-input-baselineWeekly">Baseline Weekly
|
|
54
|
-
<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">
|
|
55
72
|
</div>
|
|
56
73
|
<div class="form-row">
|
|
57
|
-
<label for="node-input-baselineMonthly">Baseline Monthly
|
|
58
|
-
<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">
|
|
59
76
|
</div>
|
|
60
77
|
<div class="form-row">
|
|
61
|
-
<label for="node-input-baselineYearly">Baseline Yearly
|
|
62
|
-
<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">
|
|
63
80
|
</div>
|
|
81
|
+
|
|
82
|
+
<!-- Other settings -->
|
|
64
83
|
<div class="form-row">
|
|
65
|
-
<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>
|
|
66
85
|
<input type="number" id="node-input-unitCost" step="0.01">
|
|
67
86
|
</div>
|
|
68
87
|
<div class="form-row">
|
|
@@ -74,14 +93,21 @@
|
|
|
74
93
|
</select>
|
|
75
94
|
</div>
|
|
76
95
|
<div class="form-row">
|
|
77
|
-
<label for="node-input-filePath">Filepath</label>
|
|
96
|
+
<label for="node-input-filePath"><i class="fa fa-file"></i> Filepath</label>
|
|
78
97
|
<input type="text" id="node-input-filePath">
|
|
79
98
|
</div>
|
|
80
99
|
<div class="form-row">
|
|
81
|
-
<label for="node-input-inputUnit">Input Unit</label>
|
|
100
|
+
<label for="node-input-inputUnit"><i class="fa fa-bolt"></i> Input Unit</label>
|
|
82
101
|
<select id="node-input-inputUnit">
|
|
83
102
|
<option value="kW">kW</option>
|
|
84
103
|
<option value="W">W</option>
|
|
85
104
|
</select>
|
|
86
105
|
</div>
|
|
87
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>
|
package/energyMeterPlus.js
CHANGED
|
@@ -5,17 +5,16 @@ module.exports = function(RED) {
|
|
|
5
5
|
|
|
6
6
|
// Listener for baseline messages from the editor
|
|
7
7
|
RED.comms.subscribe("energyMeterPlus/applyBaseline", function(baselineMsg) {
|
|
8
|
-
// baselineMsg should include nodeId so we know which instance to update
|
|
9
8
|
const nodeInstance = RED.nodes.getNode(baselineMsg.nodeId);
|
|
10
9
|
if (nodeInstance) {
|
|
11
10
|
let baseline = nodeInstance.context().get("baseline") || {
|
|
12
11
|
daily:0, weekly:0, monthly:0, yearly:0
|
|
13
12
|
};
|
|
14
|
-
|
|
15
|
-
baseline.daily
|
|
16
|
-
baseline.weekly
|
|
17
|
-
baseline.monthly
|
|
18
|
-
baseline.yearly
|
|
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;
|
|
19
18
|
|
|
20
19
|
nodeInstance.context().set("baseline", baseline);
|
|
21
20
|
}
|
|
@@ -30,36 +29,23 @@ module.exports = function(RED) {
|
|
|
30
29
|
let inputUnit = config.inputUnit || "W";
|
|
31
30
|
let currencyCode = config.currency || "USD";
|
|
32
31
|
|
|
33
|
-
//
|
|
34
|
-
let baseline = node.context().get("baseline") || {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
monthly: Number(config.baselineMonthly) || 0,
|
|
46
|
-
yearly: Number(config.baselineYearly) || 0
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
// Apply baselines once
|
|
50
|
-
baseline.daily += storedBaselines.daily;
|
|
51
|
-
baseline.weekly += storedBaselines.weekly;
|
|
52
|
-
baseline.monthly += storedBaselines.monthly;
|
|
53
|
-
baseline.yearly += storedBaselines.yearly;
|
|
54
|
-
|
|
55
|
-
// 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
|
+
|
|
56
44
|
node.context().set("baseline", baseline);
|
|
57
|
-
node.context().set("
|
|
45
|
+
node.context().set("accumulated", accumulated);
|
|
58
46
|
|
|
59
|
-
// Last timestamp
|
|
60
47
|
let lastCheck = node.context().get("lastCheck") || new Date();
|
|
61
48
|
|
|
62
|
-
// Helpers
|
|
63
49
|
function currencySymbol(code) {
|
|
64
50
|
switch (code) {
|
|
65
51
|
case "USD": return "$";
|
|
@@ -80,39 +66,44 @@ module.exports = function(RED) {
|
|
|
80
66
|
if (fs.existsSync(archivePath)) {
|
|
81
67
|
archive = JSON.parse(fs.readFileSync(archivePath));
|
|
82
68
|
}
|
|
83
|
-
archive.push({ year,
|
|
69
|
+
archive.push({ year, total: total, timestamp: new Date().toISOString() });
|
|
84
70
|
fs.writeFileSync(archivePath, JSON.stringify(archive, null, 2));
|
|
85
71
|
} catch (err) {
|
|
86
72
|
node.error("Failed to archive yearly total: " + err);
|
|
87
73
|
}
|
|
88
74
|
}
|
|
89
75
|
|
|
90
|
-
// Rollover logic
|
|
76
|
+
// Rollover logic resets counters
|
|
91
77
|
function checkRollover() {
|
|
92
78
|
let now = new Date();
|
|
93
79
|
|
|
94
|
-
// Daily
|
|
80
|
+
// Daily reset at midnight
|
|
95
81
|
if (now.getDate() !== lastCheck.getDate()) {
|
|
82
|
+
accumulated.daily = 0;
|
|
96
83
|
baseline.daily = 0;
|
|
97
84
|
}
|
|
98
85
|
|
|
99
|
-
// Weekly
|
|
86
|
+
// Weekly reset (Sunday midnight → Monday start)
|
|
100
87
|
if (now.getDay() === 1 && lastCheck.getDay() !== 1) {
|
|
88
|
+
accumulated.weekly = 0;
|
|
101
89
|
baseline.weekly = 0;
|
|
102
90
|
}
|
|
103
91
|
|
|
104
|
-
// Monthly
|
|
92
|
+
// Monthly reset
|
|
105
93
|
if (now.getMonth() !== lastCheck.getMonth()) {
|
|
94
|
+
accumulated.monthly = 0;
|
|
106
95
|
baseline.monthly = 0;
|
|
107
96
|
}
|
|
108
97
|
|
|
109
|
-
// Yearly
|
|
98
|
+
// Yearly reset
|
|
110
99
|
if (now.getFullYear() !== lastCheck.getFullYear()) {
|
|
111
|
-
archiveYearly(baseline.yearly, lastCheck.getFullYear());
|
|
100
|
+
archiveYearly(baseline.yearly + accumulated.yearly, lastCheck.getFullYear());
|
|
101
|
+
accumulated.yearly = 0;
|
|
112
102
|
baseline.yearly = 0;
|
|
113
103
|
}
|
|
114
104
|
|
|
115
105
|
lastCheck = now;
|
|
106
|
+
node.context().set("accumulated", accumulated);
|
|
116
107
|
node.context().set("baseline", baseline);
|
|
117
108
|
node.context().set("lastCheck", lastCheck);
|
|
118
109
|
}
|
|
@@ -128,26 +119,26 @@ module.exports = function(RED) {
|
|
|
128
119
|
let power_kW = (inputUnit === "W") ? power / 1000 : power;
|
|
129
120
|
let energyIncrement = power_kW * durationHours;
|
|
130
121
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
baseline.yearly += energyIncrement;
|
|
122
|
+
accumulated.daily += energyIncrement;
|
|
123
|
+
accumulated.weekly += energyIncrement;
|
|
124
|
+
accumulated.monthly += energyIncrement;
|
|
125
|
+
accumulated.yearly += energyIncrement;
|
|
136
126
|
}
|
|
137
127
|
|
|
138
128
|
lastCheck = now;
|
|
139
129
|
node.context().set("lastCheck", lastCheck);
|
|
140
|
-
node.context().set("
|
|
130
|
+
node.context().set("accumulated", accumulated);
|
|
141
131
|
|
|
132
|
+
// Output = baseline + accumulated
|
|
142
133
|
msg.payload = {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
daily_cost:
|
|
148
|
-
weekly_cost:
|
|
149
|
-
monthly_cost: round2(baseline.monthly * unitCost),
|
|
150
|
-
yearly_cost:
|
|
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),
|
|
151
142
|
currency: currencySymbol(currencyCode)
|
|
152
143
|
};
|
|
153
144
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-red-contrib-energymeterplus",
|
|
3
|
-
"version": "0.
|
|
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",
|