@enyo-energy/sunspec-sdk 1.12.0 → 1.12.1

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.
@@ -0,0 +1,67 @@
1
+ import { EnergyAppStorage } from "@enyo-energy/energy-app-sdk/dist/packages/energy-app-storage.js";
2
+ import type { SunspecBatteryControls, SunspecInverterControls } from "./sunspec-interfaces.js";
3
+ export type CalibrationDeviceType = "inverter" | "battery";
4
+ /**
5
+ * Persisted snapshot of a device's writable control registers, taken at the start of a
6
+ * calibration. Used to roll the device back when calibration ends.
7
+ *
8
+ * `modifiedFields` records which control-block field names were touched by *other* write
9
+ * commands (SetInverterFeedInLimitV1, StartStorageGridChargeV1, etc.) while the calibration
10
+ * was active — on stop, only those fields are written back, leaving fields the calibration
11
+ * itself didn't disturb alone.
12
+ */
13
+ export interface CalibrationSnapshot {
14
+ applianceId: string;
15
+ deviceType: CalibrationDeviceType;
16
+ unitId: number;
17
+ startedAtIso: string;
18
+ inverterControls?: SunspecInverterControls;
19
+ batteryControls?: SunspecBatteryControls;
20
+ modifiedFields: string[];
21
+ }
22
+ /** Calibration is capped at 5 minutes of wall time, including across restarts. */
23
+ export declare const CALIBRATION_AUTO_STOP_MS: number;
24
+ export type CalibrationRestoreReason = "stop" | "auto";
25
+ export type CalibrationRestoreCallback = (snapshot: CalibrationSnapshot, reason: CalibrationRestoreReason) => Promise<void>;
26
+ /**
27
+ * Per-appliance service that snapshots a SunSpec device's writable control registers when a
28
+ * calibration starts, tracks which registers other commands mutate while it is active, and
29
+ * restores those registers when calibration stops (either explicitly or via the 5-minute
30
+ * auto-stop timer).
31
+ *
32
+ * Snapshots are persisted via `EnergyAppStorage` so a crash/restart mid-calibration does not
33
+ * leave the device in a calibration state without a rollback. On `initialize()`, any stored
34
+ * snapshot is reloaded; the auto-stop deadline is computed from `startedAtIso` so total
35
+ * calibration time remains capped at `autoStopMs` across restarts.
36
+ */
37
+ export declare class CalibrationSnapshotService {
38
+ private readonly storage;
39
+ private readonly applianceId;
40
+ private readonly onRestore;
41
+ private readonly autoStopMs;
42
+ private snapshot?;
43
+ private autoStopTimer?;
44
+ constructor(storage: EnergyAppStorage, applianceId: string, onRestore: CalibrationRestoreCallback, autoStopMs?: number);
45
+ private storageKey;
46
+ initialize(): Promise<void>;
47
+ isCalibrating(): boolean;
48
+ getSnapshot(): CalibrationSnapshot | undefined;
49
+ startCalibration(input: {
50
+ deviceType: CalibrationDeviceType;
51
+ unitId: number;
52
+ inverterControls?: SunspecInverterControls;
53
+ batteryControls?: SunspecBatteryControls;
54
+ }): Promise<void>;
55
+ recordModification(fields: string[]): Promise<void>;
56
+ /**
57
+ * Clear the timer, remove the snapshot from memory + storage, and return the snapshot so
58
+ * the caller can restore the modified registers. Returns undefined if no calibration was
59
+ * active.
60
+ */
61
+ stopCalibration(): Promise<CalibrationSnapshot | undefined>;
62
+ private remainingMs;
63
+ private scheduleAutoStop;
64
+ private clearAutoStopTimer;
65
+ private fireAutoStop;
66
+ private persist;
67
+ }
@@ -0,0 +1,160 @@
1
+ /** Calibration is capped at 5 minutes of wall time, including across restarts. */
2
+ export const CALIBRATION_AUTO_STOP_MS = 5 * 60 * 1000;
3
+ /**
4
+ * Per-appliance service that snapshots a SunSpec device's writable control registers when a
5
+ * calibration starts, tracks which registers other commands mutate while it is active, and
6
+ * restores those registers when calibration stops (either explicitly or via the 5-minute
7
+ * auto-stop timer).
8
+ *
9
+ * Snapshots are persisted via `EnergyAppStorage` so a crash/restart mid-calibration does not
10
+ * leave the device in a calibration state without a rollback. On `initialize()`, any stored
11
+ * snapshot is reloaded; the auto-stop deadline is computed from `startedAtIso` so total
12
+ * calibration time remains capped at `autoStopMs` across restarts.
13
+ */
14
+ export class CalibrationSnapshotService {
15
+ storage;
16
+ applianceId;
17
+ onRestore;
18
+ autoStopMs;
19
+ snapshot;
20
+ autoStopTimer;
21
+ constructor(storage, applianceId, onRestore, autoStopMs = CALIBRATION_AUTO_STOP_MS) {
22
+ this.storage = storage;
23
+ this.applianceId = applianceId;
24
+ this.onRestore = onRestore;
25
+ this.autoStopMs = autoStopMs;
26
+ }
27
+ storageKey() {
28
+ return `sunspec-calibration-snapshot-${this.applianceId}`;
29
+ }
30
+ async initialize() {
31
+ try {
32
+ const loaded = await this.storage.load(this.storageKey());
33
+ if (!loaded) {
34
+ return;
35
+ }
36
+ this.snapshot = loaded;
37
+ console.log(`CalibrationSnapshotService ${this.applianceId}: loaded persisted snapshot (started ${loaded.startedAtIso}, modifiedFields=[${loaded.modifiedFields.join(', ')}])`);
38
+ const remainingMs = this.remainingMs(loaded);
39
+ if (remainingMs <= 0) {
40
+ console.warn(`CalibrationSnapshotService ${this.applianceId}: snapshot exceeded ${this.autoStopMs}ms deadline — auto-restoring immediately`);
41
+ await this.fireAutoStop();
42
+ }
43
+ else {
44
+ this.scheduleAutoStop(remainingMs);
45
+ }
46
+ }
47
+ catch (error) {
48
+ console.error(`CalibrationSnapshotService ${this.applianceId}: failed to load persisted snapshot: ${error}`);
49
+ }
50
+ }
51
+ isCalibrating() {
52
+ return this.snapshot !== undefined;
53
+ }
54
+ getSnapshot() {
55
+ return this.snapshot;
56
+ }
57
+ async startCalibration(input) {
58
+ this.clearAutoStopTimer();
59
+ this.snapshot = {
60
+ applianceId: this.applianceId,
61
+ deviceType: input.deviceType,
62
+ unitId: input.unitId,
63
+ startedAtIso: new Date().toISOString(),
64
+ inverterControls: input.inverterControls,
65
+ batteryControls: input.batteryControls,
66
+ modifiedFields: [],
67
+ };
68
+ await this.persist();
69
+ this.scheduleAutoStop(this.autoStopMs);
70
+ console.log(`CalibrationSnapshotService ${this.applianceId}: calibration started for ${input.deviceType} (auto-stop in ${this.autoStopMs}ms)`);
71
+ }
72
+ async recordModification(fields) {
73
+ if (!this.snapshot) {
74
+ return;
75
+ }
76
+ let added = false;
77
+ for (const field of fields) {
78
+ if (!this.snapshot.modifiedFields.includes(field)) {
79
+ this.snapshot.modifiedFields.push(field);
80
+ added = true;
81
+ }
82
+ }
83
+ if (added) {
84
+ await this.persist();
85
+ console.log(`CalibrationSnapshotService ${this.applianceId}: tracked modified fields [${this.snapshot.modifiedFields.join(', ')}]`);
86
+ }
87
+ }
88
+ /**
89
+ * Clear the timer, remove the snapshot from memory + storage, and return the snapshot so
90
+ * the caller can restore the modified registers. Returns undefined if no calibration was
91
+ * active.
92
+ */
93
+ async stopCalibration() {
94
+ this.clearAutoStopTimer();
95
+ const snap = this.snapshot;
96
+ this.snapshot = undefined;
97
+ if (snap) {
98
+ try {
99
+ await this.storage.remove(this.storageKey());
100
+ }
101
+ catch (error) {
102
+ console.error(`CalibrationSnapshotService ${this.applianceId}: failed to remove persisted snapshot: ${error}`);
103
+ }
104
+ }
105
+ return snap;
106
+ }
107
+ remainingMs(snap) {
108
+ const startedAtMs = Date.parse(snap.startedAtIso);
109
+ if (Number.isNaN(startedAtMs)) {
110
+ return this.autoStopMs;
111
+ }
112
+ return startedAtMs + this.autoStopMs - Date.now();
113
+ }
114
+ scheduleAutoStop(delayMs) {
115
+ this.clearAutoStopTimer();
116
+ this.autoStopTimer = setTimeout(() => {
117
+ void this.fireAutoStop();
118
+ }, delayMs);
119
+ if (typeof this.autoStopTimer.unref === "function") {
120
+ this.autoStopTimer.unref();
121
+ }
122
+ }
123
+ clearAutoStopTimer() {
124
+ if (this.autoStopTimer) {
125
+ clearTimeout(this.autoStopTimer);
126
+ this.autoStopTimer = undefined;
127
+ }
128
+ }
129
+ async fireAutoStop() {
130
+ const snap = this.snapshot;
131
+ this.snapshot = undefined;
132
+ this.clearAutoStopTimer();
133
+ if (!snap) {
134
+ return;
135
+ }
136
+ try {
137
+ await this.storage.remove(this.storageKey());
138
+ }
139
+ catch (error) {
140
+ console.error(`CalibrationSnapshotService ${this.applianceId}: failed to remove persisted snapshot during auto-stop: ${error}`);
141
+ }
142
+ try {
143
+ await this.onRestore(snap, "auto");
144
+ }
145
+ catch (error) {
146
+ console.error(`CalibrationSnapshotService ${this.applianceId}: auto-stop restore failed: ${error}`);
147
+ }
148
+ }
149
+ async persist() {
150
+ if (!this.snapshot) {
151
+ return;
152
+ }
153
+ try {
154
+ await this.storage.save(this.storageKey(), this.snapshot);
155
+ }
156
+ catch (error) {
157
+ console.error(`CalibrationSnapshotService ${this.applianceId}: failed to persist snapshot: ${error}`);
158
+ }
159
+ }
160
+ }
@@ -0,0 +1,164 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CalibrationSnapshotService = exports.CALIBRATION_AUTO_STOP_MS = void 0;
4
+ /** Calibration is capped at 5 minutes of wall time, including across restarts. */
5
+ exports.CALIBRATION_AUTO_STOP_MS = 5 * 60 * 1000;
6
+ /**
7
+ * Per-appliance service that snapshots a SunSpec device's writable control registers when a
8
+ * calibration starts, tracks which registers other commands mutate while it is active, and
9
+ * restores those registers when calibration stops (either explicitly or via the 5-minute
10
+ * auto-stop timer).
11
+ *
12
+ * Snapshots are persisted via `EnergyAppStorage` so a crash/restart mid-calibration does not
13
+ * leave the device in a calibration state without a rollback. On `initialize()`, any stored
14
+ * snapshot is reloaded; the auto-stop deadline is computed from `startedAtIso` so total
15
+ * calibration time remains capped at `autoStopMs` across restarts.
16
+ */
17
+ class CalibrationSnapshotService {
18
+ storage;
19
+ applianceId;
20
+ onRestore;
21
+ autoStopMs;
22
+ snapshot;
23
+ autoStopTimer;
24
+ constructor(storage, applianceId, onRestore, autoStopMs = exports.CALIBRATION_AUTO_STOP_MS) {
25
+ this.storage = storage;
26
+ this.applianceId = applianceId;
27
+ this.onRestore = onRestore;
28
+ this.autoStopMs = autoStopMs;
29
+ }
30
+ storageKey() {
31
+ return `sunspec-calibration-snapshot-${this.applianceId}`;
32
+ }
33
+ async initialize() {
34
+ try {
35
+ const loaded = await this.storage.load(this.storageKey());
36
+ if (!loaded) {
37
+ return;
38
+ }
39
+ this.snapshot = loaded;
40
+ console.log(`CalibrationSnapshotService ${this.applianceId}: loaded persisted snapshot (started ${loaded.startedAtIso}, modifiedFields=[${loaded.modifiedFields.join(', ')}])`);
41
+ const remainingMs = this.remainingMs(loaded);
42
+ if (remainingMs <= 0) {
43
+ console.warn(`CalibrationSnapshotService ${this.applianceId}: snapshot exceeded ${this.autoStopMs}ms deadline — auto-restoring immediately`);
44
+ await this.fireAutoStop();
45
+ }
46
+ else {
47
+ this.scheduleAutoStop(remainingMs);
48
+ }
49
+ }
50
+ catch (error) {
51
+ console.error(`CalibrationSnapshotService ${this.applianceId}: failed to load persisted snapshot: ${error}`);
52
+ }
53
+ }
54
+ isCalibrating() {
55
+ return this.snapshot !== undefined;
56
+ }
57
+ getSnapshot() {
58
+ return this.snapshot;
59
+ }
60
+ async startCalibration(input) {
61
+ this.clearAutoStopTimer();
62
+ this.snapshot = {
63
+ applianceId: this.applianceId,
64
+ deviceType: input.deviceType,
65
+ unitId: input.unitId,
66
+ startedAtIso: new Date().toISOString(),
67
+ inverterControls: input.inverterControls,
68
+ batteryControls: input.batteryControls,
69
+ modifiedFields: [],
70
+ };
71
+ await this.persist();
72
+ this.scheduleAutoStop(this.autoStopMs);
73
+ console.log(`CalibrationSnapshotService ${this.applianceId}: calibration started for ${input.deviceType} (auto-stop in ${this.autoStopMs}ms)`);
74
+ }
75
+ async recordModification(fields) {
76
+ if (!this.snapshot) {
77
+ return;
78
+ }
79
+ let added = false;
80
+ for (const field of fields) {
81
+ if (!this.snapshot.modifiedFields.includes(field)) {
82
+ this.snapshot.modifiedFields.push(field);
83
+ added = true;
84
+ }
85
+ }
86
+ if (added) {
87
+ await this.persist();
88
+ console.log(`CalibrationSnapshotService ${this.applianceId}: tracked modified fields [${this.snapshot.modifiedFields.join(', ')}]`);
89
+ }
90
+ }
91
+ /**
92
+ * Clear the timer, remove the snapshot from memory + storage, and return the snapshot so
93
+ * the caller can restore the modified registers. Returns undefined if no calibration was
94
+ * active.
95
+ */
96
+ async stopCalibration() {
97
+ this.clearAutoStopTimer();
98
+ const snap = this.snapshot;
99
+ this.snapshot = undefined;
100
+ if (snap) {
101
+ try {
102
+ await this.storage.remove(this.storageKey());
103
+ }
104
+ catch (error) {
105
+ console.error(`CalibrationSnapshotService ${this.applianceId}: failed to remove persisted snapshot: ${error}`);
106
+ }
107
+ }
108
+ return snap;
109
+ }
110
+ remainingMs(snap) {
111
+ const startedAtMs = Date.parse(snap.startedAtIso);
112
+ if (Number.isNaN(startedAtMs)) {
113
+ return this.autoStopMs;
114
+ }
115
+ return startedAtMs + this.autoStopMs - Date.now();
116
+ }
117
+ scheduleAutoStop(delayMs) {
118
+ this.clearAutoStopTimer();
119
+ this.autoStopTimer = setTimeout(() => {
120
+ void this.fireAutoStop();
121
+ }, delayMs);
122
+ if (typeof this.autoStopTimer.unref === "function") {
123
+ this.autoStopTimer.unref();
124
+ }
125
+ }
126
+ clearAutoStopTimer() {
127
+ if (this.autoStopTimer) {
128
+ clearTimeout(this.autoStopTimer);
129
+ this.autoStopTimer = undefined;
130
+ }
131
+ }
132
+ async fireAutoStop() {
133
+ const snap = this.snapshot;
134
+ this.snapshot = undefined;
135
+ this.clearAutoStopTimer();
136
+ if (!snap) {
137
+ return;
138
+ }
139
+ try {
140
+ await this.storage.remove(this.storageKey());
141
+ }
142
+ catch (error) {
143
+ console.error(`CalibrationSnapshotService ${this.applianceId}: failed to remove persisted snapshot during auto-stop: ${error}`);
144
+ }
145
+ try {
146
+ await this.onRestore(snap, "auto");
147
+ }
148
+ catch (error) {
149
+ console.error(`CalibrationSnapshotService ${this.applianceId}: auto-stop restore failed: ${error}`);
150
+ }
151
+ }
152
+ async persist() {
153
+ if (!this.snapshot) {
154
+ return;
155
+ }
156
+ try {
157
+ await this.storage.save(this.storageKey(), this.snapshot);
158
+ }
159
+ catch (error) {
160
+ console.error(`CalibrationSnapshotService ${this.applianceId}: failed to persist snapshot: ${error}`);
161
+ }
162
+ }
163
+ }
164
+ exports.CalibrationSnapshotService = CalibrationSnapshotService;
@@ -0,0 +1,67 @@
1
+ import { EnergyAppStorage } from "@enyo-energy/energy-app-sdk/dist/packages/energy-app-storage.js";
2
+ import type { SunspecBatteryControls, SunspecInverterControls } from "./sunspec-interfaces.cjs";
3
+ export type CalibrationDeviceType = "inverter" | "battery";
4
+ /**
5
+ * Persisted snapshot of a device's writable control registers, taken at the start of a
6
+ * calibration. Used to roll the device back when calibration ends.
7
+ *
8
+ * `modifiedFields` records which control-block field names were touched by *other* write
9
+ * commands (SetInverterFeedInLimitV1, StartStorageGridChargeV1, etc.) while the calibration
10
+ * was active — on stop, only those fields are written back, leaving fields the calibration
11
+ * itself didn't disturb alone.
12
+ */
13
+ export interface CalibrationSnapshot {
14
+ applianceId: string;
15
+ deviceType: CalibrationDeviceType;
16
+ unitId: number;
17
+ startedAtIso: string;
18
+ inverterControls?: SunspecInverterControls;
19
+ batteryControls?: SunspecBatteryControls;
20
+ modifiedFields: string[];
21
+ }
22
+ /** Calibration is capped at 5 minutes of wall time, including across restarts. */
23
+ export declare const CALIBRATION_AUTO_STOP_MS: number;
24
+ export type CalibrationRestoreReason = "stop" | "auto";
25
+ export type CalibrationRestoreCallback = (snapshot: CalibrationSnapshot, reason: CalibrationRestoreReason) => Promise<void>;
26
+ /**
27
+ * Per-appliance service that snapshots a SunSpec device's writable control registers when a
28
+ * calibration starts, tracks which registers other commands mutate while it is active, and
29
+ * restores those registers when calibration stops (either explicitly or via the 5-minute
30
+ * auto-stop timer).
31
+ *
32
+ * Snapshots are persisted via `EnergyAppStorage` so a crash/restart mid-calibration does not
33
+ * leave the device in a calibration state without a rollback. On `initialize()`, any stored
34
+ * snapshot is reloaded; the auto-stop deadline is computed from `startedAtIso` so total
35
+ * calibration time remains capped at `autoStopMs` across restarts.
36
+ */
37
+ export declare class CalibrationSnapshotService {
38
+ private readonly storage;
39
+ private readonly applianceId;
40
+ private readonly onRestore;
41
+ private readonly autoStopMs;
42
+ private snapshot?;
43
+ private autoStopTimer?;
44
+ constructor(storage: EnergyAppStorage, applianceId: string, onRestore: CalibrationRestoreCallback, autoStopMs?: number);
45
+ private storageKey;
46
+ initialize(): Promise<void>;
47
+ isCalibrating(): boolean;
48
+ getSnapshot(): CalibrationSnapshot | undefined;
49
+ startCalibration(input: {
50
+ deviceType: CalibrationDeviceType;
51
+ unitId: number;
52
+ inverterControls?: SunspecInverterControls;
53
+ batteryControls?: SunspecBatteryControls;
54
+ }): Promise<void>;
55
+ recordModification(fields: string[]): Promise<void>;
56
+ /**
57
+ * Clear the timer, remove the snapshot from memory + storage, and return the snapshot so
58
+ * the caller can restore the modified registers. Returns undefined if no calibration was
59
+ * active.
60
+ */
61
+ stopCalibration(): Promise<CalibrationSnapshot | undefined>;
62
+ private remainingMs;
63
+ private scheduleAutoStop;
64
+ private clearAutoStopTimer;
65
+ private fireAutoStop;
66
+ private persist;
67
+ }
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SunspecCustomDevice = exports.SunspecMeter = exports.SunspecBattery = exports.SunspecInverter = exports.BaseSunspecDevice = void 0;
4
4
  exports.detectFeaturesFromRegisters = detectFeaturesFromRegisters;
5
5
  exports.ratedDischargePowerW = ratedDischargePowerW;
6
+ exports.mpptStorageStringKind = mpptStorageStringKind;
6
7
  exports.sumMpptBatteryPowerW = sumMpptBatteryPowerW;
7
8
  exports.selectLiveBatteryPowerW = selectLiveBatteryPowerW;
8
9
  exports.filterFeaturesByCalibrationResult = filterFeaturesByCalibrationResult;
@@ -1203,13 +1204,14 @@ class SunspecInverter extends BaseSunspecDevice {
1203
1204
  }
1204
1205
  /**
1205
1206
  * Map MPPT data to DC string structure for data bus
1206
- * Filters out battery-related strings (StCha 3, StDisCha 4)
1207
+ * Filters out battery-related strings (StCha n, StDisCha n+1; see
1208
+ * {@link mpptStorageStringKind})
1207
1209
  */
1208
1210
  mapMPPTToStrings(mpptDataList) {
1209
1211
  const result = [];
1210
1212
  mpptDataList.forEach((mppt, index) => {
1211
1213
  // Skip battery-related strings - they will be handled separately
1212
- if (mppt.stringId === 'StCha 3' || mppt.stringId === 'StDisCha 4') {
1214
+ if (mpptStorageStringKind(mppt.stringId) !== undefined) {
1213
1215
  return;
1214
1216
  }
1215
1217
  // Only include strings with valid data
@@ -1428,7 +1430,7 @@ function ratedDischargePowerW(batteryData) {
1428
1430
  /**
1429
1431
  * Pick the live battery power, in preference order:
1430
1432
  *
1431
- * 1. Model 160 (MPPT) `StCha 3` / `StDisCha 4` — direct DC-link reading,
1433
+ * 1. Model 160 (MPPT) `StCha n` / `StDisCha n+1` — direct DC-link reading,
1432
1434
  * passed in as the already-summed `mpptBatteryPowerW`.
1433
1435
  * 2. Model 802 `w` (offset 47) — surfaced as `chargePower` / `dischargePower`
1434
1436
  * by the Model 802 path of `SunspecModbusClient.readBatteryData`.
@@ -1442,11 +1444,33 @@ function ratedDischargePowerW(batteryData) {
1442
1444
  * Exported as a free function so the preference logic can be unit-tested
1443
1445
  * without the full `SunspecBattery` scaffold.
1444
1446
  */
1447
+ /**
1448
+ * Classify a Model 160 module by its `IDStr` as a DC-coupled battery string.
1449
+ *
1450
+ * Fronius hybrids list the battery as two extra Model 160 modules after the PV
1451
+ * MPPTs: `StCha <n>` (charge power) and `StDisCha <n+1>` (discharge power). The
1452
+ * number is the module position, so it depends on how many PV trackers the
1453
+ * device has: a GEN24 Plus (2 MPPTs) reports `StCha 3` / `StDisCha 4`, a
1454
+ * Verto (3 MPPTs) `StCha 4` / `StDisCha 5`. Match the prefix, never the full
1455
+ * label, or a battery module is counted as a PV string.
1456
+ *
1457
+ * Returns `undefined` for a PV string.
1458
+ */
1459
+ function mpptStorageStringKind(stringId) {
1460
+ const id = stringId?.trim() ?? '';
1461
+ if (/^StCha\b/i.test(id)) {
1462
+ return 'charge';
1463
+ }
1464
+ if (/^StDisCha\b/i.test(id)) {
1465
+ return 'discharge';
1466
+ }
1467
+ return undefined;
1468
+ }
1445
1469
  /**
1446
1470
  * Sum battery charge/discharge power from Model 160 MPPT strings.
1447
1471
  *
1448
- * - `StCha 3` → charging power (added, positive)
1449
- * - `StDisCha 4` → discharging power (subtracted)
1472
+ * - `StCha n` → charging power (added, positive)
1473
+ * - `StDisCha n+1` → discharging power (subtracted)
1450
1474
  *
1451
1475
  * The result follows the battery-power sign convention used across this file:
1452
1476
  * positive = charging, negative = discharging. Returns `undefined` when neither
@@ -1458,10 +1482,11 @@ function ratedDischargePowerW(batteryData) {
1458
1482
  function sumMpptBatteryPowerW(mpptDataList) {
1459
1483
  let batteryPowerW;
1460
1484
  for (const mppt of mpptDataList) {
1461
- if (mppt.stringId === 'StCha 3' && mppt.dcPower !== undefined) {
1485
+ const kind = mpptStorageStringKind(mppt.stringId);
1486
+ if (kind === 'charge' && mppt.dcPower !== undefined) {
1462
1487
  batteryPowerW = (batteryPowerW ?? 0) + mppt.dcPower;
1463
1488
  }
1464
- else if (mppt.stringId === 'StDisCha 4' && mppt.dcPower !== undefined) {
1489
+ else if (kind === 'discharge' && mppt.dcPower !== undefined) {
1465
1490
  batteryPowerW = (batteryPowerW ?? 0) - mppt.dcPower;
1466
1491
  }
1467
1492
  }
@@ -1972,8 +1997,8 @@ class SunspecBattery extends BaseSunspecDevice {
1972
1997
  }
1973
1998
  /**
1974
1999
  * Extract battery power from MPPT strings with specific IDs
1975
- * StCha 3: Battery charging power (positive)
1976
- * StDisCha 4: Battery discharging power (negative)
2000
+ * StCha n: Battery charging power (positive)
2001
+ * StDisCha n+1: Battery discharging power (negative)
1977
2002
  */
1978
2003
  extractBatteryPowerFromMPPT(mpptDataList) {
1979
2004
  return sumMpptBatteryPowerW(mpptDataList);
@@ -1985,10 +2010,11 @@ class SunspecBattery extends BaseSunspecDevice {
1985
2010
  let hasCharge = false;
1986
2011
  let hasDischarge = false;
1987
2012
  mpptDataList.forEach((mppt) => {
1988
- if (mppt.stringId === 'StCha 3' && mppt.dcPower !== undefined && mppt.dcPower > 0) {
2013
+ const kind = mpptStorageStringKind(mppt.stringId);
2014
+ if (kind === 'charge' && mppt.dcPower !== undefined && mppt.dcPower > 0) {
1989
2015
  hasCharge = true;
1990
2016
  }
1991
- else if (mppt.stringId === 'StDisCha 4' && mppt.dcPower !== undefined && mppt.dcPower > 0) {
2017
+ else if (kind === 'discharge' && mppt.dcPower !== undefined && mppt.dcPower > 0) {
1992
2018
  hasDischarge = true;
1993
2019
  }
1994
2020
  });
@@ -422,7 +422,8 @@ export declare class SunspecInverter extends BaseSunspecDevice {
422
422
  private sumDcStringsW;
423
423
  /**
424
424
  * Map MPPT data to DC string structure for data bus
425
- * Filters out battery-related strings (StCha 3, StDisCha 4)
425
+ * Filters out battery-related strings (StCha n, StDisCha n+1; see
426
+ * {@link mpptStorageStringKind})
426
427
  */
427
428
  protected mapMPPTToStrings(mpptDataList: SunspecMPPTData[]): EnyoDataBusInverterValuesV1String[];
428
429
  private mapMPPTOperatingState;
@@ -494,7 +495,7 @@ export declare function ratedDischargePowerW(batteryData: SunspecBatteryData | n
494
495
  /**
495
496
  * Pick the live battery power, in preference order:
496
497
  *
497
- * 1. Model 160 (MPPT) `StCha 3` / `StDisCha 4` — direct DC-link reading,
498
+ * 1. Model 160 (MPPT) `StCha n` / `StDisCha n+1` — direct DC-link reading,
498
499
  * passed in as the already-summed `mpptBatteryPowerW`.
499
500
  * 2. Model 802 `w` (offset 47) — surfaced as `chargePower` / `dischargePower`
500
501
  * by the Model 802 path of `SunspecModbusClient.readBatteryData`.
@@ -508,11 +509,24 @@ export declare function ratedDischargePowerW(batteryData: SunspecBatteryData | n
508
509
  * Exported as a free function so the preference logic can be unit-tested
509
510
  * without the full `SunspecBattery` scaffold.
510
511
  */
512
+ /**
513
+ * Classify a Model 160 module by its `IDStr` as a DC-coupled battery string.
514
+ *
515
+ * Fronius hybrids list the battery as two extra Model 160 modules after the PV
516
+ * MPPTs: `StCha <n>` (charge power) and `StDisCha <n+1>` (discharge power). The
517
+ * number is the module position, so it depends on how many PV trackers the
518
+ * device has: a GEN24 Plus (2 MPPTs) reports `StCha 3` / `StDisCha 4`, a
519
+ * Verto (3 MPPTs) `StCha 4` / `StDisCha 5`. Match the prefix, never the full
520
+ * label, or a battery module is counted as a PV string.
521
+ *
522
+ * Returns `undefined` for a PV string.
523
+ */
524
+ export declare function mpptStorageStringKind(stringId: string | undefined): 'charge' | 'discharge' | undefined;
511
525
  /**
512
526
  * Sum battery charge/discharge power from Model 160 MPPT strings.
513
527
  *
514
- * - `StCha 3` → charging power (added, positive)
515
- * - `StDisCha 4` → discharging power (subtracted)
528
+ * - `StCha n` → charging power (added, positive)
529
+ * - `StDisCha n+1` → discharging power (subtracted)
516
530
  *
517
531
  * The result follows the battery-power sign convention used across this file:
518
532
  * positive = charging, negative = discharging. Returns `undefined` when neither
@@ -677,8 +691,8 @@ export declare class SunspecBattery extends BaseSunspecDevice {
677
691
  readData(clockId: string, resolution: EnyoDataBusMessageResolution): Promise<EnyoDataBusMessage[]>;
678
692
  /**
679
693
  * Extract battery power from MPPT strings with specific IDs
680
- * StCha 3: Battery charging power (positive)
681
- * StDisCha 4: Battery discharging power (negative)
694
+ * StCha n: Battery charging power (positive)
695
+ * StDisCha n+1: Battery discharging power (negative)
682
696
  */
683
697
  private extractBatteryPowerFromMPPT;
684
698
  /**
@@ -332,7 +332,7 @@ var SunspecPvPowerSource;
332
332
  * consumer can see which telemetry produced the number.
333
333
  *
334
334
  * - `MpptStrings`: summed per-string DC power from Model 160 (battery strings
335
- * `StCha 3` / `StDisCha 4` excluded). Already pure PV — never battery-adjusted.
335
+ * `StCha n` / `StDisCha n+1` excluded). Already pure PV — never battery-adjusted.
336
336
  * - `InverterDc`: the inverter's total DC power (Model 101 `DCW`). Pure PV — never
337
337
  * battery-adjusted.
338
338
  * - `InverterAc`: the inverter's total AC power (Model 101 `W`). On a hybrid this
@@ -892,7 +892,7 @@ export declare enum SunspecPvPowerSource {
892
892
  * consumer can see which telemetry produced the number.
893
893
  *
894
894
  * - `MpptStrings`: summed per-string DC power from Model 160 (battery strings
895
- * `StCha 3` / `StDisCha 4` excluded). Already pure PV — never battery-adjusted.
895
+ * `StCha n` / `StDisCha n+1` excluded). Already pure PV — never battery-adjusted.
896
896
  * - `InverterDc`: the inverter's total DC power (Model 101 `DCW`). Pure PV — never
897
897
  * battery-adjusted.
898
898
  * - `InverterAc`: the inverter's total AC power (Model 101 `W`). On a hybrid this
@@ -932,7 +932,7 @@ export interface SunspecInverterReading {
932
932
  inverterPowerWRaw: number;
933
933
  /**
934
934
  * Summed per-string MPPT DC power (W) from Model 160 (battery strings
935
- * `StCha 3` / `StDisCha 4` excluded), clamped to ≥ 0. Always computed and
935
+ * `StCha n` / `StDisCha n+1` excluded), clamped to ≥ 0. Always computed and
936
936
  * reported independently of `powerOrigin` — `0` when no PV strings are readable.
937
937
  */
938
938
  inverterPowerWDcStrings: number;
@@ -2494,7 +2494,7 @@ class SunspecModbusClient {
2494
2494
  // a live measurement — and produces e.g. 5000 W of "charging"
2495
2495
  // for an idle battery with inWRte = 100 % and a 5 kW nameplate.
2496
2496
  // Live battery power must come from a real source: Model 160
2497
- // StCha 3 / StDisCha 4 (see SunspecBattery.extractBatteryPower
2497
+ // StCha n / StDisCha n+1 (see SunspecBattery.extractBatteryPower
2498
2498
  // FromMPPT) or Model 802 `w` at offset 47 (the Model 802 branch
2499
2499
  // of this method, just below). Model-124-only devices should
2500
2500
  // publish "no signal" rather than the commanded cap.
@@ -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 = '1.12.0';
12
+ exports.SDK_VERSION = '1.12.1';
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 = "1.12.0";
8
+ export declare const SDK_VERSION = "1.12.1";
9
9
  /**
10
10
  * Gets the current SDK version.
11
11
  * @returns The semantic version string of the SDK
@@ -422,7 +422,8 @@ export declare class SunspecInverter extends BaseSunspecDevice {
422
422
  private sumDcStringsW;
423
423
  /**
424
424
  * Map MPPT data to DC string structure for data bus
425
- * Filters out battery-related strings (StCha 3, StDisCha 4)
425
+ * Filters out battery-related strings (StCha n, StDisCha n+1; see
426
+ * {@link mpptStorageStringKind})
426
427
  */
427
428
  protected mapMPPTToStrings(mpptDataList: SunspecMPPTData[]): EnyoDataBusInverterValuesV1String[];
428
429
  private mapMPPTOperatingState;
@@ -494,7 +495,7 @@ export declare function ratedDischargePowerW(batteryData: SunspecBatteryData | n
494
495
  /**
495
496
  * Pick the live battery power, in preference order:
496
497
  *
497
- * 1. Model 160 (MPPT) `StCha 3` / `StDisCha 4` — direct DC-link reading,
498
+ * 1. Model 160 (MPPT) `StCha n` / `StDisCha n+1` — direct DC-link reading,
498
499
  * passed in as the already-summed `mpptBatteryPowerW`.
499
500
  * 2. Model 802 `w` (offset 47) — surfaced as `chargePower` / `dischargePower`
500
501
  * by the Model 802 path of `SunspecModbusClient.readBatteryData`.
@@ -508,11 +509,24 @@ export declare function ratedDischargePowerW(batteryData: SunspecBatteryData | n
508
509
  * Exported as a free function so the preference logic can be unit-tested
509
510
  * without the full `SunspecBattery` scaffold.
510
511
  */
512
+ /**
513
+ * Classify a Model 160 module by its `IDStr` as a DC-coupled battery string.
514
+ *
515
+ * Fronius hybrids list the battery as two extra Model 160 modules after the PV
516
+ * MPPTs: `StCha <n>` (charge power) and `StDisCha <n+1>` (discharge power). The
517
+ * number is the module position, so it depends on how many PV trackers the
518
+ * device has: a GEN24 Plus (2 MPPTs) reports `StCha 3` / `StDisCha 4`, a
519
+ * Verto (3 MPPTs) `StCha 4` / `StDisCha 5`. Match the prefix, never the full
520
+ * label, or a battery module is counted as a PV string.
521
+ *
522
+ * Returns `undefined` for a PV string.
523
+ */
524
+ export declare function mpptStorageStringKind(stringId: string | undefined): 'charge' | 'discharge' | undefined;
511
525
  /**
512
526
  * Sum battery charge/discharge power from Model 160 MPPT strings.
513
527
  *
514
- * - `StCha 3` → charging power (added, positive)
515
- * - `StDisCha 4` → discharging power (subtracted)
528
+ * - `StCha n` → charging power (added, positive)
529
+ * - `StDisCha n+1` → discharging power (subtracted)
516
530
  *
517
531
  * The result follows the battery-power sign convention used across this file:
518
532
  * positive = charging, negative = discharging. Returns `undefined` when neither
@@ -677,8 +691,8 @@ export declare class SunspecBattery extends BaseSunspecDevice {
677
691
  readData(clockId: string, resolution: EnyoDataBusMessageResolution): Promise<EnyoDataBusMessage[]>;
678
692
  /**
679
693
  * Extract battery power from MPPT strings with specific IDs
680
- * StCha 3: Battery charging power (positive)
681
- * StDisCha 4: Battery discharging power (negative)
694
+ * StCha n: Battery charging power (positive)
695
+ * StDisCha n+1: Battery discharging power (negative)
682
696
  */
683
697
  private extractBatteryPowerFromMPPT;
684
698
  /**
@@ -1193,13 +1193,14 @@ export class SunspecInverter extends BaseSunspecDevice {
1193
1193
  }
1194
1194
  /**
1195
1195
  * Map MPPT data to DC string structure for data bus
1196
- * Filters out battery-related strings (StCha 3, StDisCha 4)
1196
+ * Filters out battery-related strings (StCha n, StDisCha n+1; see
1197
+ * {@link mpptStorageStringKind})
1197
1198
  */
1198
1199
  mapMPPTToStrings(mpptDataList) {
1199
1200
  const result = [];
1200
1201
  mpptDataList.forEach((mppt, index) => {
1201
1202
  // Skip battery-related strings - they will be handled separately
1202
- if (mppt.stringId === 'StCha 3' || mppt.stringId === 'StDisCha 4') {
1203
+ if (mpptStorageStringKind(mppt.stringId) !== undefined) {
1203
1204
  return;
1204
1205
  }
1205
1206
  // Only include strings with valid data
@@ -1417,7 +1418,7 @@ export function ratedDischargePowerW(batteryData) {
1417
1418
  /**
1418
1419
  * Pick the live battery power, in preference order:
1419
1420
  *
1420
- * 1. Model 160 (MPPT) `StCha 3` / `StDisCha 4` — direct DC-link reading,
1421
+ * 1. Model 160 (MPPT) `StCha n` / `StDisCha n+1` — direct DC-link reading,
1421
1422
  * passed in as the already-summed `mpptBatteryPowerW`.
1422
1423
  * 2. Model 802 `w` (offset 47) — surfaced as `chargePower` / `dischargePower`
1423
1424
  * by the Model 802 path of `SunspecModbusClient.readBatteryData`.
@@ -1431,11 +1432,33 @@ export function ratedDischargePowerW(batteryData) {
1431
1432
  * Exported as a free function so the preference logic can be unit-tested
1432
1433
  * without the full `SunspecBattery` scaffold.
1433
1434
  */
1435
+ /**
1436
+ * Classify a Model 160 module by its `IDStr` as a DC-coupled battery string.
1437
+ *
1438
+ * Fronius hybrids list the battery as two extra Model 160 modules after the PV
1439
+ * MPPTs: `StCha <n>` (charge power) and `StDisCha <n+1>` (discharge power). The
1440
+ * number is the module position, so it depends on how many PV trackers the
1441
+ * device has: a GEN24 Plus (2 MPPTs) reports `StCha 3` / `StDisCha 4`, a
1442
+ * Verto (3 MPPTs) `StCha 4` / `StDisCha 5`. Match the prefix, never the full
1443
+ * label, or a battery module is counted as a PV string.
1444
+ *
1445
+ * Returns `undefined` for a PV string.
1446
+ */
1447
+ export function mpptStorageStringKind(stringId) {
1448
+ const id = stringId?.trim() ?? '';
1449
+ if (/^StCha\b/i.test(id)) {
1450
+ return 'charge';
1451
+ }
1452
+ if (/^StDisCha\b/i.test(id)) {
1453
+ return 'discharge';
1454
+ }
1455
+ return undefined;
1456
+ }
1434
1457
  /**
1435
1458
  * Sum battery charge/discharge power from Model 160 MPPT strings.
1436
1459
  *
1437
- * - `StCha 3` → charging power (added, positive)
1438
- * - `StDisCha 4` → discharging power (subtracted)
1460
+ * - `StCha n` → charging power (added, positive)
1461
+ * - `StDisCha n+1` → discharging power (subtracted)
1439
1462
  *
1440
1463
  * The result follows the battery-power sign convention used across this file:
1441
1464
  * positive = charging, negative = discharging. Returns `undefined` when neither
@@ -1447,10 +1470,11 @@ export function ratedDischargePowerW(batteryData) {
1447
1470
  export function sumMpptBatteryPowerW(mpptDataList) {
1448
1471
  let batteryPowerW;
1449
1472
  for (const mppt of mpptDataList) {
1450
- if (mppt.stringId === 'StCha 3' && mppt.dcPower !== undefined) {
1473
+ const kind = mpptStorageStringKind(mppt.stringId);
1474
+ if (kind === 'charge' && mppt.dcPower !== undefined) {
1451
1475
  batteryPowerW = (batteryPowerW ?? 0) + mppt.dcPower;
1452
1476
  }
1453
- else if (mppt.stringId === 'StDisCha 4' && mppt.dcPower !== undefined) {
1477
+ else if (kind === 'discharge' && mppt.dcPower !== undefined) {
1454
1478
  batteryPowerW = (batteryPowerW ?? 0) - mppt.dcPower;
1455
1479
  }
1456
1480
  }
@@ -1961,8 +1985,8 @@ export class SunspecBattery extends BaseSunspecDevice {
1961
1985
  }
1962
1986
  /**
1963
1987
  * Extract battery power from MPPT strings with specific IDs
1964
- * StCha 3: Battery charging power (positive)
1965
- * StDisCha 4: Battery discharging power (negative)
1988
+ * StCha n: Battery charging power (positive)
1989
+ * StDisCha n+1: Battery discharging power (negative)
1966
1990
  */
1967
1991
  extractBatteryPowerFromMPPT(mpptDataList) {
1968
1992
  return sumMpptBatteryPowerW(mpptDataList);
@@ -1974,10 +1998,11 @@ export class SunspecBattery extends BaseSunspecDevice {
1974
1998
  let hasCharge = false;
1975
1999
  let hasDischarge = false;
1976
2000
  mpptDataList.forEach((mppt) => {
1977
- if (mppt.stringId === 'StCha 3' && mppt.dcPower !== undefined && mppt.dcPower > 0) {
2001
+ const kind = mpptStorageStringKind(mppt.stringId);
2002
+ if (kind === 'charge' && mppt.dcPower !== undefined && mppt.dcPower > 0) {
1978
2003
  hasCharge = true;
1979
2004
  }
1980
- else if (mppt.stringId === 'StDisCha 4' && mppt.dcPower !== undefined && mppt.dcPower > 0) {
2005
+ else if (kind === 'discharge' && mppt.dcPower !== undefined && mppt.dcPower > 0) {
1981
2006
  hasDischarge = true;
1982
2007
  }
1983
2008
  });
@@ -892,7 +892,7 @@ export declare enum SunspecPvPowerSource {
892
892
  * consumer can see which telemetry produced the number.
893
893
  *
894
894
  * - `MpptStrings`: summed per-string DC power from Model 160 (battery strings
895
- * `StCha 3` / `StDisCha 4` excluded). Already pure PV — never battery-adjusted.
895
+ * `StCha n` / `StDisCha n+1` excluded). Already pure PV — never battery-adjusted.
896
896
  * - `InverterDc`: the inverter's total DC power (Model 101 `DCW`). Pure PV — never
897
897
  * battery-adjusted.
898
898
  * - `InverterAc`: the inverter's total AC power (Model 101 `W`). On a hybrid this
@@ -932,7 +932,7 @@ export interface SunspecInverterReading {
932
932
  inverterPowerWRaw: number;
933
933
  /**
934
934
  * Summed per-string MPPT DC power (W) from Model 160 (battery strings
935
- * `StCha 3` / `StDisCha 4` excluded), clamped to ≥ 0. Always computed and
935
+ * `StCha n` / `StDisCha n+1` excluded), clamped to ≥ 0. Always computed and
936
936
  * reported independently of `powerOrigin` — `0` when no PV strings are readable.
937
937
  */
938
938
  inverterPowerWDcStrings: number;
@@ -329,7 +329,7 @@ export var SunspecPvPowerSource;
329
329
  * consumer can see which telemetry produced the number.
330
330
  *
331
331
  * - `MpptStrings`: summed per-string DC power from Model 160 (battery strings
332
- * `StCha 3` / `StDisCha 4` excluded). Already pure PV — never battery-adjusted.
332
+ * `StCha n` / `StDisCha n+1` excluded). Already pure PV — never battery-adjusted.
333
333
  * - `InverterDc`: the inverter's total DC power (Model 101 `DCW`). Pure PV — never
334
334
  * battery-adjusted.
335
335
  * - `InverterAc`: the inverter's total AC power (Model 101 `W`). On a hybrid this
@@ -2485,7 +2485,7 @@ export class SunspecModbusClient {
2485
2485
  // a live measurement — and produces e.g. 5000 W of "charging"
2486
2486
  // for an idle battery with inWRte = 100 % and a 5 kW nameplate.
2487
2487
  // Live battery power must come from a real source: Model 160
2488
- // StCha 3 / StDisCha 4 (see SunspecBattery.extractBatteryPower
2488
+ // StCha n / StDisCha n+1 (see SunspecBattery.extractBatteryPower
2489
2489
  // FromMPPT) or Model 802 `w` at offset 47 (the Model 802 branch
2490
2490
  // of this method, just below). Model-124-only devices should
2491
2491
  // publish "no signal" rather than the commanded cap.
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 = "1.12.0";
8
+ export declare const SDK_VERSION = "1.12.1";
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 = '1.12.0';
8
+ export const SDK_VERSION = '1.12.1';
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": "1.12.0",
3
+ "version": "1.12.1",
4
4
  "description": "enyo Energy Sunspec SDK",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -42,7 +42,7 @@
42
42
  },
43
43
  "dependencies": {
44
44
  "@enyo-energy/appliance-calibration": "0.0.3",
45
- "@enyo-energy/energy-app-sdk": "1.64.0"
45
+ "@enyo-energy/energy-app-sdk": "1.70.0"
46
46
  },
47
47
  "volta": {
48
48
  "node": "22.17.0"