node-red-contrib-energymeterplus 0.0.8 → 0.1.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.
Files changed (2) hide show
  1. package/energyMeterPlus.js +26 -18
  2. package/package.json +1 -1
@@ -5,10 +5,9 @@ module.exports = function(RED) {
5
5
  RED.nodes.createNode(this, config);
6
6
  var node = this;
7
7
 
8
- // Config values
9
8
  let unitCost = Number(config.unitCost) || 0;
10
9
  let filePath = config.filePath || "/addon_configs/a0d7b954_nodered/node_red/solarGen_data.json";
11
- let inputUnit = config.inputUnit || "W";
10
+ let inputUnit = config.inputUnit || "W"; // default to Watts
12
11
 
13
12
  // Numeric baseline corrections
14
13
  let baselineDaily = Number(config.baselineDaily) || 0;
@@ -16,7 +15,7 @@ module.exports = function(RED) {
16
15
  let baselineMonthly = Number(config.baselineMonthly) || 0;
17
16
  let baselineYearly = Number(config.baselineYearly) || 0;
18
17
 
19
- // Baseline counters start at corrections
18
+ // Baseline counters
20
19
  let baseline = { daily: baselineDaily, weekly: baselineWeekly, monthly: baselineMonthly, yearly: baselineYearly };
21
20
  let lastCheck = new Date();
22
21
 
@@ -37,14 +36,21 @@ module.exports = function(RED) {
37
36
  setInterval(checkRollover, 60000);
38
37
 
39
38
  node.on('input', function(msg) {
40
- let factor = (inputUnit === "W") ? 0.001 : 1;
41
- let power = Number(msg.payload) * factor || 0;
39
+ let now = new Date();
40
+ let durationHours = (now - lastCheck) / (1000 * 3600); // elapsed time in hours
41
+ lastCheck = now;
42
+
43
+ let power = Number(msg.payload) || 0;
44
+ let power_kW = (inputUnit === "W") ? power / 1000 : power;
45
+
46
+ // Energy increment = Power × Time
47
+ let energyIncrement = power_kW * durationHours;
42
48
 
43
49
  // Add to counters
44
- baseline.daily += power;
45
- baseline.weekly += power;
46
- baseline.monthly += power;
47
- baseline.yearly += power;
50
+ baseline.daily += energyIncrement;
51
+ baseline.weekly += energyIncrement;
52
+ baseline.monthly += energyIncrement;
53
+ baseline.yearly += energyIncrement;
48
54
 
49
55
  // Calculate costs
50
56
  let daily_cost = baseline.daily * unitCost;
@@ -52,16 +58,18 @@ module.exports = function(RED) {
52
58
  let monthly_cost = baseline.monthly * unitCost;
53
59
  let yearly_cost = baseline.yearly * unitCost;
54
60
 
55
- // Build Output
61
+ // Round all outputs to 2 decimal places
62
+ function round2(val) { return Number(val.toFixed(2)); }
63
+
56
64
  msg.payload = {
57
- daily_kWh: baseline.daily,
58
- weekly_kWh: baseline.weekly,
59
- monthly_kWh: baseline.monthly,
60
- yearly_kWh: baseline.yearly,
61
- daily_cost,
62
- weekly_cost,
63
- monthly_cost,
64
- yearly_cost
65
+ daily_kWh: round2(baseline.daily),
66
+ weekly_kWh: round2(baseline.weekly),
67
+ monthly_kWh: round2(baseline.monthly),
68
+ yearly_kWh: round2(baseline.yearly),
69
+ daily_cost: round2(daily_cost),
70
+ weekly_cost: round2(weekly_cost),
71
+ monthly_cost: round2(monthly_cost),
72
+ yearly_cost: round2(yearly_cost)
65
73
  };
66
74
 
67
75
  // Save snapshot
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-energymeterplus",
3
- "version": "0.0.8",
3
+ "version": "0.1.0",
4
4
  "description": "A node-red energy meter node with editable baseline data, milestone summary, and cost calculation",
5
5
  "author": "Arcfrankye",
6
6
  "license": "MIT",