@enyo-energy/sunspec-sdk 0.0.96 → 0.0.97

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.
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SunspecMeter = exports.SunspecBattery = exports.SunspecInverter = exports.BaseSunspecDevice = void 0;
4
4
  exports.detectFeaturesFromRegisters = detectFeaturesFromRegisters;
5
+ exports.sumMpptBatteryPowerW = sumMpptBatteryPowerW;
5
6
  exports.selectLiveBatteryPowerW = selectLiveBatteryPowerW;
6
7
  exports.filterFeaturesByCalibrationResult = filterFeaturesByCalibrationResult;
7
8
  const sunspec_interfaces_js_1 = require("./sunspec-interfaces.cjs");
@@ -496,7 +497,20 @@ class SunspecInverter extends BaseSunspecDevice {
496
497
  // to Connected. No-op unless this device is still marked offline.
497
498
  await this.reassertConnectedIfRecovered();
498
499
  if (inverterData) {
499
- const { pvPowerW, source: pvPowerSource } = this.selectPvPowerW(dcStrings, inverterData);
500
+ // When PV is sourced from the inverter total (no usable MPPT strings),
501
+ // the AC fallback includes any battery charge/discharge on a hybrid.
502
+ // Fetch the live battery power (positive = charging, negative =
503
+ // discharging) so selectPvPowerW can back it out — otherwise a battery
504
+ // discharging at night is reported as phantom PV production.
505
+ let batteryPowerW;
506
+ if (this.wouldUseInverterTotal(dcStrings.length)) {
507
+ batteryPowerW = sumMpptBatteryPowerW(mpptDataList);
508
+ if (batteryPowerW === undefined) {
509
+ const batteryData = await this.sunspecClient.readBatteryData(this.unitId);
510
+ batteryPowerW = selectLiveBatteryPowerW(undefined, batteryData);
511
+ }
512
+ }
513
+ const { pvPowerW, source: pvPowerSource } = this.selectPvPowerW(dcStrings, inverterData, batteryPowerW);
500
514
  console.debug(`Got PV Power from ${pvPowerSource}: ${pvPowerW}W`);
501
515
  const inverterMessage = {
502
516
  id: (0, node_crypto_1.randomUUID)(),
@@ -834,29 +848,54 @@ class SunspecInverter extends BaseSunspecDevice {
834
848
  * Both inverter-total steps require a strictly positive value, so NOT_IMPLEMENTED
835
849
  * DCW (decodes to 0) falls through to AC, and a negative AC reading (e.g. a hybrid
836
850
  * importing from grid) falls through to 0 rather than reporting negative production.
837
- * Edge case: in Auto/Inverter a hybrid discharging its battery while its MPPT is
838
- * unreadable reports AC (incl. battery) rather than 0 — acceptable, and strictly
839
- * better than reporting 0.
851
+ *
852
+ * The AC fallback backs out battery power via `batteryPowerW` (signed: positive =
853
+ * charging, negative = discharging). A hybrid's AC total is PV − charge + discharge,
854
+ * so real PV = AC + batteryPowerW. Without this, a battery discharging at night while
855
+ * the MPPT is unreadable would be reported as phantom PV production. `batteryPowerW`
856
+ * is undefined when no live battery source is available, in which case AC is used
857
+ * uncorrected (still better than reporting 0).
858
+ *
859
+ * The returned `pvPowerW` is always clamped to ≥ 0 — PV production is non-negative by
860
+ * definition and must never surface as a negative value on the data bus.
840
861
  */
841
- selectPvPowerW(dcStrings, inverterData) {
842
- const stringSumW = dcStrings.reduce((sum, s) => sum + (s.powerW || 0), 0);
843
- const useStrings = this.pvPowerSource === sunspec_interfaces_js_1.SunspecPvPowerSource.Mppt ||
844
- (this.pvPowerSource === sunspec_interfaces_js_1.SunspecPvPowerSource.Auto && dcStrings.length > 0);
845
- if (useStrings) {
846
- return { pvPowerW: stringSumW, source: 'DC strings' };
862
+ selectPvPowerW(dcStrings, inverterData, batteryPowerW) {
863
+ if (!this.wouldUseInverterTotal(dcStrings.length)) {
864
+ const stringSumW = dcStrings.reduce((sum, s) => sum + (s.powerW || 0), 0);
865
+ return { pvPowerW: Math.max(0, stringSumW), source: 'DC strings' };
847
866
  }
848
- return this.inverterTotalPvPowerW(inverterData);
867
+ return this.inverterTotalPvPowerW(inverterData, batteryPowerW);
868
+ }
869
+ /**
870
+ * Whether {@link selectPvPowerW} will source PV from the inverter total (rather than
871
+ * the summed MPPT strings) for the given string count. Mppt never does; Inverter
872
+ * always does; Auto does only when there are no strings. Exposed so `readData` can
873
+ * decide up front whether it needs to fetch battery power for the AC correction.
874
+ */
875
+ wouldUseInverterTotal(dcStringCount) {
876
+ if (this.pvPowerSource === sunspec_interfaces_js_1.SunspecPvPowerSource.Mppt)
877
+ return false;
878
+ if (this.pvPowerSource === sunspec_interfaces_js_1.SunspecPvPowerSource.Inverter)
879
+ return true;
880
+ return dcStringCount === 0; // Auto
849
881
  }
850
882
  /**
851
- * Inverter total PV power from Model 101: DCW when strictly positive, else AC when
852
- * strictly positive, else 0. See {@link selectPvPowerW} for why positivity matters.
883
+ * Inverter total PV power from Model 101: DCW when strictly positive, else AC (with
884
+ * battery power backed out) when the result is strictly positive, else 0. See
885
+ * {@link selectPvPowerW} for the battery correction and why positivity matters.
853
886
  */
854
- inverterTotalPvPowerW(inverterData) {
887
+ inverterTotalPvPowerW(inverterData, batteryPowerW) {
855
888
  if (typeof inverterData.dcPower === 'number' && inverterData.dcPower > 0) {
856
889
  return { pvPowerW: inverterData.dcPower, source: 'DC power (Model 101)' };
857
890
  }
858
891
  if (typeof inverterData.acPower === 'number' && inverterData.acPower > 0) {
859
- return { pvPowerW: inverterData.acPower, source: 'AC power (Model 101)' };
892
+ // AC = PV − charge + discharge on a hybrid; batteryPowerW is signed
893
+ // (positive = charging, negative = discharging), so PV = AC + batteryPowerW.
894
+ const correctedW = inverterData.acPower + (batteryPowerW ?? 0);
895
+ if (correctedW > 0) {
896
+ return { pvPowerW: correctedW, source: 'AC power (Model 101)' };
897
+ }
898
+ return { pvPowerW: 0, source: 'none' };
860
899
  }
861
900
  return { pvPowerW: 0, source: 'none' };
862
901
  }
@@ -1089,6 +1128,31 @@ function detectFeaturesFromRegisters(batteryData) {
1089
1128
  * Exported as a free function so the preference logic can be unit-tested
1090
1129
  * without the full `SunspecBattery` scaffold.
1091
1130
  */
1131
+ /**
1132
+ * Sum battery charge/discharge power from Model 160 MPPT strings.
1133
+ *
1134
+ * - `StCha 3` → charging power (added, positive)
1135
+ * - `StDisCha 4` → discharging power (subtracted)
1136
+ *
1137
+ * The result follows the battery-power sign convention used across this file:
1138
+ * positive = charging, negative = discharging. Returns `undefined` when neither
1139
+ * battery string is present, so callers can fall through to another source.
1140
+ *
1141
+ * Exported as a free function so it can be unit-tested and reused by both the
1142
+ * SunspecBattery live-power path and the SunspecInverter AC-fallback correction.
1143
+ */
1144
+ function sumMpptBatteryPowerW(mpptDataList) {
1145
+ let batteryPowerW;
1146
+ for (const mppt of mpptDataList) {
1147
+ if (mppt.stringId === 'StCha 3' && mppt.dcPower !== undefined) {
1148
+ batteryPowerW = (batteryPowerW ?? 0) + mppt.dcPower;
1149
+ }
1150
+ else if (mppt.stringId === 'StDisCha 4' && mppt.dcPower !== undefined) {
1151
+ batteryPowerW = (batteryPowerW ?? 0) - mppt.dcPower;
1152
+ }
1153
+ }
1154
+ return batteryPowerW;
1155
+ }
1092
1156
  function selectLiveBatteryPowerW(mpptBatteryPowerW, batteryData) {
1093
1157
  if (mpptBatteryPowerW !== undefined) {
1094
1158
  return mpptBatteryPowerW;
@@ -1428,18 +1492,7 @@ class SunspecBattery extends BaseSunspecDevice {
1428
1492
  * StDisCha 4: Battery discharging power (negative)
1429
1493
  */
1430
1494
  extractBatteryPowerFromMPPT(mpptDataList) {
1431
- let batteryPowerW;
1432
- mpptDataList.forEach((mppt) => {
1433
- if (mppt.stringId === 'StCha 3' && mppt.dcPower !== undefined) {
1434
- // Charging power is positive
1435
- batteryPowerW = (batteryPowerW || 0) + mppt.dcPower;
1436
- }
1437
- else if (mppt.stringId === 'StDisCha 4' && mppt.dcPower !== undefined) {
1438
- // Discharging power is negative
1439
- batteryPowerW = (batteryPowerW || 0) - mppt.dcPower;
1440
- }
1441
- });
1442
- return batteryPowerW;
1495
+ return sumMpptBatteryPowerW(mpptDataList);
1443
1496
  }
1444
1497
  /**
1445
1498
  * Determine battery state from MPPT strings
@@ -255,17 +255,32 @@ export declare class SunspecInverter extends BaseSunspecDevice {
255
255
  * Both inverter-total steps require a strictly positive value, so NOT_IMPLEMENTED
256
256
  * DCW (decodes to 0) falls through to AC, and a negative AC reading (e.g. a hybrid
257
257
  * importing from grid) falls through to 0 rather than reporting negative production.
258
- * Edge case: in Auto/Inverter a hybrid discharging its battery while its MPPT is
259
- * unreadable reports AC (incl. battery) rather than 0 — acceptable, and strictly
260
- * better than reporting 0.
258
+ *
259
+ * The AC fallback backs out battery power via `batteryPowerW` (signed: positive =
260
+ * charging, negative = discharging). A hybrid's AC total is PV − charge + discharge,
261
+ * so real PV = AC + batteryPowerW. Without this, a battery discharging at night while
262
+ * the MPPT is unreadable would be reported as phantom PV production. `batteryPowerW`
263
+ * is undefined when no live battery source is available, in which case AC is used
264
+ * uncorrected (still better than reporting 0).
265
+ *
266
+ * The returned `pvPowerW` is always clamped to ≥ 0 — PV production is non-negative by
267
+ * definition and must never surface as a negative value on the data bus.
261
268
  */
262
- protected selectPvPowerW(dcStrings: EnyoDataBusInverterValuesV1String[], inverterData: SunspecInverterData): {
269
+ protected selectPvPowerW(dcStrings: EnyoDataBusInverterValuesV1String[], inverterData: SunspecInverterData, batteryPowerW?: number): {
263
270
  pvPowerW: number;
264
271
  source: string;
265
272
  };
266
273
  /**
267
- * Inverter total PV power from Model 101: DCW when strictly positive, else AC when
268
- * strictly positive, else 0. See {@link selectPvPowerW} for why positivity matters.
274
+ * Whether {@link selectPvPowerW} will source PV from the inverter total (rather than
275
+ * the summed MPPT strings) for the given string count. Mppt never does; Inverter
276
+ * always does; Auto does only when there are no strings. Exposed so `readData` can
277
+ * decide up front whether it needs to fetch battery power for the AC correction.
278
+ */
279
+ private wouldUseInverterTotal;
280
+ /**
281
+ * Inverter total PV power from Model 101: DCW when strictly positive, else AC (with
282
+ * battery power backed out) when the result is strictly positive, else 0. See
283
+ * {@link selectPvPowerW} for the battery correction and why positivity matters.
269
284
  */
270
285
  private inverterTotalPvPowerW;
271
286
  /**
@@ -346,6 +361,20 @@ export declare function detectFeaturesFromRegisters(batteryData: SunspecBatteryD
346
361
  * Exported as a free function so the preference logic can be unit-tested
347
362
  * without the full `SunspecBattery` scaffold.
348
363
  */
364
+ /**
365
+ * Sum battery charge/discharge power from Model 160 MPPT strings.
366
+ *
367
+ * - `StCha 3` → charging power (added, positive)
368
+ * - `StDisCha 4` → discharging power (subtracted)
369
+ *
370
+ * The result follows the battery-power sign convention used across this file:
371
+ * positive = charging, negative = discharging. Returns `undefined` when neither
372
+ * battery string is present, so callers can fall through to another source.
373
+ *
374
+ * Exported as a free function so it can be unit-tested and reused by both the
375
+ * SunspecBattery live-power path and the SunspecInverter AC-fallback correction.
376
+ */
377
+ export declare function sumMpptBatteryPowerW(mpptDataList: SunspecMPPTData[]): number | undefined;
349
378
  export declare function selectLiveBatteryPowerW(mpptBatteryPowerW: number | undefined, batteryData: SunspecBatteryData | null): number | undefined;
350
379
  export declare function filterFeaturesByCalibrationResult(detected: EnyoBatteryFeature[], result: CalibrationResult | undefined, controllable: readonly EnyoBatteryFeature[]): EnyoBatteryFeature[];
351
380
  /**
@@ -9,7 +9,7 @@ exports.getSdkVersion = getSdkVersion;
9
9
  /**
10
10
  * Current version of the enyo Energy App SDK.
11
11
  */
12
- exports.SDK_VERSION = '0.0.96';
12
+ exports.SDK_VERSION = '0.0.97';
13
13
  /**
14
14
  * Gets the current SDK version.
15
15
  * @returns The semantic version string of the SDK
@@ -5,7 +5,7 @@
5
5
  /**
6
6
  * Current version of the enyo Energy App SDK.
7
7
  */
8
- export declare const SDK_VERSION = "0.0.96";
8
+ export declare const SDK_VERSION = "0.0.97";
9
9
  /**
10
10
  * Gets the current SDK version.
11
11
  * @returns The semantic version string of the SDK
@@ -255,17 +255,32 @@ export declare class SunspecInverter extends BaseSunspecDevice {
255
255
  * Both inverter-total steps require a strictly positive value, so NOT_IMPLEMENTED
256
256
  * DCW (decodes to 0) falls through to AC, and a negative AC reading (e.g. a hybrid
257
257
  * importing from grid) falls through to 0 rather than reporting negative production.
258
- * Edge case: in Auto/Inverter a hybrid discharging its battery while its MPPT is
259
- * unreadable reports AC (incl. battery) rather than 0 — acceptable, and strictly
260
- * better than reporting 0.
258
+ *
259
+ * The AC fallback backs out battery power via `batteryPowerW` (signed: positive =
260
+ * charging, negative = discharging). A hybrid's AC total is PV − charge + discharge,
261
+ * so real PV = AC + batteryPowerW. Without this, a battery discharging at night while
262
+ * the MPPT is unreadable would be reported as phantom PV production. `batteryPowerW`
263
+ * is undefined when no live battery source is available, in which case AC is used
264
+ * uncorrected (still better than reporting 0).
265
+ *
266
+ * The returned `pvPowerW` is always clamped to ≥ 0 — PV production is non-negative by
267
+ * definition and must never surface as a negative value on the data bus.
261
268
  */
262
- protected selectPvPowerW(dcStrings: EnyoDataBusInverterValuesV1String[], inverterData: SunspecInverterData): {
269
+ protected selectPvPowerW(dcStrings: EnyoDataBusInverterValuesV1String[], inverterData: SunspecInverterData, batteryPowerW?: number): {
263
270
  pvPowerW: number;
264
271
  source: string;
265
272
  };
266
273
  /**
267
- * Inverter total PV power from Model 101: DCW when strictly positive, else AC when
268
- * strictly positive, else 0. See {@link selectPvPowerW} for why positivity matters.
274
+ * Whether {@link selectPvPowerW} will source PV from the inverter total (rather than
275
+ * the summed MPPT strings) for the given string count. Mppt never does; Inverter
276
+ * always does; Auto does only when there are no strings. Exposed so `readData` can
277
+ * decide up front whether it needs to fetch battery power for the AC correction.
278
+ */
279
+ private wouldUseInverterTotal;
280
+ /**
281
+ * Inverter total PV power from Model 101: DCW when strictly positive, else AC (with
282
+ * battery power backed out) when the result is strictly positive, else 0. See
283
+ * {@link selectPvPowerW} for the battery correction and why positivity matters.
269
284
  */
270
285
  private inverterTotalPvPowerW;
271
286
  /**
@@ -346,6 +361,20 @@ export declare function detectFeaturesFromRegisters(batteryData: SunspecBatteryD
346
361
  * Exported as a free function so the preference logic can be unit-tested
347
362
  * without the full `SunspecBattery` scaffold.
348
363
  */
364
+ /**
365
+ * Sum battery charge/discharge power from Model 160 MPPT strings.
366
+ *
367
+ * - `StCha 3` → charging power (added, positive)
368
+ * - `StDisCha 4` → discharging power (subtracted)
369
+ *
370
+ * The result follows the battery-power sign convention used across this file:
371
+ * positive = charging, negative = discharging. Returns `undefined` when neither
372
+ * battery string is present, so callers can fall through to another source.
373
+ *
374
+ * Exported as a free function so it can be unit-tested and reused by both the
375
+ * SunspecBattery live-power path and the SunspecInverter AC-fallback correction.
376
+ */
377
+ export declare function sumMpptBatteryPowerW(mpptDataList: SunspecMPPTData[]): number | undefined;
349
378
  export declare function selectLiveBatteryPowerW(mpptBatteryPowerW: number | undefined, batteryData: SunspecBatteryData | null): number | undefined;
350
379
  export declare function filterFeaturesByCalibrationResult(detected: EnyoBatteryFeature[], result: CalibrationResult | undefined, controllable: readonly EnyoBatteryFeature[]): EnyoBatteryFeature[];
351
380
  /**
@@ -489,7 +489,20 @@ export class SunspecInverter extends BaseSunspecDevice {
489
489
  // to Connected. No-op unless this device is still marked offline.
490
490
  await this.reassertConnectedIfRecovered();
491
491
  if (inverterData) {
492
- const { pvPowerW, source: pvPowerSource } = this.selectPvPowerW(dcStrings, inverterData);
492
+ // When PV is sourced from the inverter total (no usable MPPT strings),
493
+ // the AC fallback includes any battery charge/discharge on a hybrid.
494
+ // Fetch the live battery power (positive = charging, negative =
495
+ // discharging) so selectPvPowerW can back it out — otherwise a battery
496
+ // discharging at night is reported as phantom PV production.
497
+ let batteryPowerW;
498
+ if (this.wouldUseInverterTotal(dcStrings.length)) {
499
+ batteryPowerW = sumMpptBatteryPowerW(mpptDataList);
500
+ if (batteryPowerW === undefined) {
501
+ const batteryData = await this.sunspecClient.readBatteryData(this.unitId);
502
+ batteryPowerW = selectLiveBatteryPowerW(undefined, batteryData);
503
+ }
504
+ }
505
+ const { pvPowerW, source: pvPowerSource } = this.selectPvPowerW(dcStrings, inverterData, batteryPowerW);
493
506
  console.debug(`Got PV Power from ${pvPowerSource}: ${pvPowerW}W`);
494
507
  const inverterMessage = {
495
508
  id: randomUUID(),
@@ -827,29 +840,54 @@ export class SunspecInverter extends BaseSunspecDevice {
827
840
  * Both inverter-total steps require a strictly positive value, so NOT_IMPLEMENTED
828
841
  * DCW (decodes to 0) falls through to AC, and a negative AC reading (e.g. a hybrid
829
842
  * importing from grid) falls through to 0 rather than reporting negative production.
830
- * Edge case: in Auto/Inverter a hybrid discharging its battery while its MPPT is
831
- * unreadable reports AC (incl. battery) rather than 0 — acceptable, and strictly
832
- * better than reporting 0.
843
+ *
844
+ * The AC fallback backs out battery power via `batteryPowerW` (signed: positive =
845
+ * charging, negative = discharging). A hybrid's AC total is PV − charge + discharge,
846
+ * so real PV = AC + batteryPowerW. Without this, a battery discharging at night while
847
+ * the MPPT is unreadable would be reported as phantom PV production. `batteryPowerW`
848
+ * is undefined when no live battery source is available, in which case AC is used
849
+ * uncorrected (still better than reporting 0).
850
+ *
851
+ * The returned `pvPowerW` is always clamped to ≥ 0 — PV production is non-negative by
852
+ * definition and must never surface as a negative value on the data bus.
833
853
  */
834
- selectPvPowerW(dcStrings, inverterData) {
835
- const stringSumW = dcStrings.reduce((sum, s) => sum + (s.powerW || 0), 0);
836
- const useStrings = this.pvPowerSource === SunspecPvPowerSource.Mppt ||
837
- (this.pvPowerSource === SunspecPvPowerSource.Auto && dcStrings.length > 0);
838
- if (useStrings) {
839
- return { pvPowerW: stringSumW, source: 'DC strings' };
854
+ selectPvPowerW(dcStrings, inverterData, batteryPowerW) {
855
+ if (!this.wouldUseInverterTotal(dcStrings.length)) {
856
+ const stringSumW = dcStrings.reduce((sum, s) => sum + (s.powerW || 0), 0);
857
+ return { pvPowerW: Math.max(0, stringSumW), source: 'DC strings' };
840
858
  }
841
- return this.inverterTotalPvPowerW(inverterData);
859
+ return this.inverterTotalPvPowerW(inverterData, batteryPowerW);
860
+ }
861
+ /**
862
+ * Whether {@link selectPvPowerW} will source PV from the inverter total (rather than
863
+ * the summed MPPT strings) for the given string count. Mppt never does; Inverter
864
+ * always does; Auto does only when there are no strings. Exposed so `readData` can
865
+ * decide up front whether it needs to fetch battery power for the AC correction.
866
+ */
867
+ wouldUseInverterTotal(dcStringCount) {
868
+ if (this.pvPowerSource === SunspecPvPowerSource.Mppt)
869
+ return false;
870
+ if (this.pvPowerSource === SunspecPvPowerSource.Inverter)
871
+ return true;
872
+ return dcStringCount === 0; // Auto
842
873
  }
843
874
  /**
844
- * Inverter total PV power from Model 101: DCW when strictly positive, else AC when
845
- * strictly positive, else 0. See {@link selectPvPowerW} for why positivity matters.
875
+ * Inverter total PV power from Model 101: DCW when strictly positive, else AC (with
876
+ * battery power backed out) when the result is strictly positive, else 0. See
877
+ * {@link selectPvPowerW} for the battery correction and why positivity matters.
846
878
  */
847
- inverterTotalPvPowerW(inverterData) {
879
+ inverterTotalPvPowerW(inverterData, batteryPowerW) {
848
880
  if (typeof inverterData.dcPower === 'number' && inverterData.dcPower > 0) {
849
881
  return { pvPowerW: inverterData.dcPower, source: 'DC power (Model 101)' };
850
882
  }
851
883
  if (typeof inverterData.acPower === 'number' && inverterData.acPower > 0) {
852
- return { pvPowerW: inverterData.acPower, source: 'AC power (Model 101)' };
884
+ // AC = PV − charge + discharge on a hybrid; batteryPowerW is signed
885
+ // (positive = charging, negative = discharging), so PV = AC + batteryPowerW.
886
+ const correctedW = inverterData.acPower + (batteryPowerW ?? 0);
887
+ if (correctedW > 0) {
888
+ return { pvPowerW: correctedW, source: 'AC power (Model 101)' };
889
+ }
890
+ return { pvPowerW: 0, source: 'none' };
853
891
  }
854
892
  return { pvPowerW: 0, source: 'none' };
855
893
  }
@@ -1081,6 +1119,31 @@ export function detectFeaturesFromRegisters(batteryData) {
1081
1119
  * Exported as a free function so the preference logic can be unit-tested
1082
1120
  * without the full `SunspecBattery` scaffold.
1083
1121
  */
1122
+ /**
1123
+ * Sum battery charge/discharge power from Model 160 MPPT strings.
1124
+ *
1125
+ * - `StCha 3` → charging power (added, positive)
1126
+ * - `StDisCha 4` → discharging power (subtracted)
1127
+ *
1128
+ * The result follows the battery-power sign convention used across this file:
1129
+ * positive = charging, negative = discharging. Returns `undefined` when neither
1130
+ * battery string is present, so callers can fall through to another source.
1131
+ *
1132
+ * Exported as a free function so it can be unit-tested and reused by both the
1133
+ * SunspecBattery live-power path and the SunspecInverter AC-fallback correction.
1134
+ */
1135
+ export function sumMpptBatteryPowerW(mpptDataList) {
1136
+ let batteryPowerW;
1137
+ for (const mppt of mpptDataList) {
1138
+ if (mppt.stringId === 'StCha 3' && mppt.dcPower !== undefined) {
1139
+ batteryPowerW = (batteryPowerW ?? 0) + mppt.dcPower;
1140
+ }
1141
+ else if (mppt.stringId === 'StDisCha 4' && mppt.dcPower !== undefined) {
1142
+ batteryPowerW = (batteryPowerW ?? 0) - mppt.dcPower;
1143
+ }
1144
+ }
1145
+ return batteryPowerW;
1146
+ }
1084
1147
  export function selectLiveBatteryPowerW(mpptBatteryPowerW, batteryData) {
1085
1148
  if (mpptBatteryPowerW !== undefined) {
1086
1149
  return mpptBatteryPowerW;
@@ -1420,18 +1483,7 @@ export class SunspecBattery extends BaseSunspecDevice {
1420
1483
  * StDisCha 4: Battery discharging power (negative)
1421
1484
  */
1422
1485
  extractBatteryPowerFromMPPT(mpptDataList) {
1423
- let batteryPowerW;
1424
- mpptDataList.forEach((mppt) => {
1425
- if (mppt.stringId === 'StCha 3' && mppt.dcPower !== undefined) {
1426
- // Charging power is positive
1427
- batteryPowerW = (batteryPowerW || 0) + mppt.dcPower;
1428
- }
1429
- else if (mppt.stringId === 'StDisCha 4' && mppt.dcPower !== undefined) {
1430
- // Discharging power is negative
1431
- batteryPowerW = (batteryPowerW || 0) - mppt.dcPower;
1432
- }
1433
- });
1434
- return batteryPowerW;
1486
+ return sumMpptBatteryPowerW(mpptDataList);
1435
1487
  }
1436
1488
  /**
1437
1489
  * Determine battery state from MPPT strings
package/dist/version.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  /**
6
6
  * Current version of the enyo Energy App SDK.
7
7
  */
8
- export declare const SDK_VERSION = "0.0.96";
8
+ export declare const SDK_VERSION = "0.0.97";
9
9
  /**
10
10
  * Gets the current SDK version.
11
11
  * @returns The semantic version string of the SDK
package/dist/version.js CHANGED
@@ -5,7 +5,7 @@
5
5
  /**
6
6
  * Current version of the enyo Energy App SDK.
7
7
  */
8
- export const SDK_VERSION = '0.0.96';
8
+ export const SDK_VERSION = '0.0.97';
9
9
  /**
10
10
  * Gets the current SDK version.
11
11
  * @returns The semantic version string of the SDK
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@enyo-energy/sunspec-sdk",
3
- "version": "0.0.96",
3
+ "version": "0.0.97",
4
4
  "description": "enyo Energy Sunspec SDK",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",