node-red-contrib-energymeterplus 0.0.7 → 0.0.8
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.html +1 -1
- package/energyMeterPlus.js +15 -30
- package/package.json +1 -1
package/energyMeterPlus.html
CHANGED
package/energyMeterPlus.js
CHANGED
|
@@ -5,9 +5,10 @@ module.exports = function(RED) {
|
|
|
5
5
|
RED.nodes.createNode(this, config);
|
|
6
6
|
var node = this;
|
|
7
7
|
|
|
8
|
+
// Config values
|
|
8
9
|
let unitCost = Number(config.unitCost) || 0;
|
|
9
10
|
let filePath = config.filePath || "/addon_configs/a0d7b954_nodered/node_red/solarGen_data.json";
|
|
10
|
-
let inputUnit = config.inputUnit || "
|
|
11
|
+
let inputUnit = config.inputUnit || "W";
|
|
11
12
|
|
|
12
13
|
// Numeric baseline corrections
|
|
13
14
|
let baselineDaily = Number(config.baselineDaily) || 0;
|
|
@@ -15,48 +16,35 @@ module.exports = function(RED) {
|
|
|
15
16
|
let baselineMonthly = Number(config.baselineMonthly) || 0;
|
|
16
17
|
let baselineYearly = Number(config.baselineYearly) || 0;
|
|
17
18
|
|
|
18
|
-
// Baseline counters
|
|
19
|
+
// Baseline counters start at corrections
|
|
19
20
|
let baseline = { daily: baselineDaily, weekly: baselineWeekly, monthly: baselineMonthly, yearly: baselineYearly };
|
|
20
21
|
let lastCheck = new Date();
|
|
21
22
|
|
|
22
|
-
//
|
|
23
|
+
// Rollover logic
|
|
23
24
|
function checkRollover() {
|
|
24
25
|
let now = new Date();
|
|
25
|
-
|
|
26
26
|
if (now.getDate() !== lastCheck.getDate()) baseline.daily = baselineDaily;
|
|
27
27
|
if (now.getDay() === 0 && lastCheck.getDay() !== 0) baseline.weekly = baselineWeekly;
|
|
28
28
|
if (now.getMonth() !== lastCheck.getMonth()) baseline.monthly = baselineMonthly;
|
|
29
29
|
if (now.getFullYear() !== lastCheck.getFullYear()) {
|
|
30
30
|
let archiveName = filePath.replace(".json", `_${lastCheck.getFullYear()}.json`);
|
|
31
|
-
try {
|
|
32
|
-
|
|
33
|
-
} catch (err) {
|
|
34
|
-
node.error("Failed to archive yearly file: " + err);
|
|
35
|
-
}
|
|
31
|
+
try { fs.writeFileSync(archiveName, JSON.stringify(baseline, null, 2)); }
|
|
32
|
+
catch (err) { node.error("Failed to archive yearly file: " + err); }
|
|
36
33
|
baseline.yearly = baselineYearly;
|
|
37
34
|
}
|
|
38
|
-
|
|
39
35
|
lastCheck = now;
|
|
40
36
|
}
|
|
41
|
-
|
|
42
37
|
setInterval(checkRollover, 60000);
|
|
43
38
|
|
|
44
39
|
node.on('input', function(msg) {
|
|
45
40
|
let factor = (inputUnit === "W") ? 0.001 : 1;
|
|
41
|
+
let power = Number(msg.payload) * factor || 0;
|
|
46
42
|
|
|
47
|
-
//
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
baseline.monthly += power;
|
|
53
|
-
baseline.yearly += power;
|
|
54
|
-
} else if (typeof msg.payload === "object") {
|
|
55
|
-
baseline.daily = (msg.payload.daily_kWh || baseline.daily) * factor;
|
|
56
|
-
baseline.weekly = (msg.payload.weekly_kWh || baseline.weekly) * factor;
|
|
57
|
-
baseline.monthly = (msg.payload.monthly_kWh || baseline.monthly) * factor;
|
|
58
|
-
baseline.yearly = (msg.payload.yearly_kWh || baseline.yearly) * factor;
|
|
59
|
-
}
|
|
43
|
+
// Add to counters
|
|
44
|
+
baseline.daily += power;
|
|
45
|
+
baseline.weekly += power;
|
|
46
|
+
baseline.monthly += power;
|
|
47
|
+
baseline.yearly += power;
|
|
60
48
|
|
|
61
49
|
// Calculate costs
|
|
62
50
|
let daily_cost = baseline.daily * unitCost;
|
|
@@ -77,17 +65,14 @@ module.exports = function(RED) {
|
|
|
77
65
|
};
|
|
78
66
|
|
|
79
67
|
// Save snapshot
|
|
80
|
-
try {
|
|
81
|
-
|
|
82
|
-
} catch (err) {
|
|
83
|
-
node.error("Failed to write to file: " + err);
|
|
84
|
-
}
|
|
68
|
+
try { fs.writeFileSync(filePath, JSON.stringify(msg.payload, null, 2)); }
|
|
69
|
+
catch (err) { node.error("Failed to write to file: " + err); }
|
|
85
70
|
|
|
86
71
|
node.send(msg);
|
|
87
72
|
});
|
|
88
73
|
}
|
|
89
74
|
|
|
90
75
|
RED.nodes.registerType("energyMeterPlus", EnergyMeterPlus, {
|
|
91
|
-
color: "#
|
|
76
|
+
color: "#d9910cfe" // Dutch Orange
|
|
92
77
|
});
|
|
93
78
|
}
|
package/package.json
CHANGED