@enyo-energy/sunspec-sdk 0.0.104 → 0.0.105

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.
@@ -22,8 +22,9 @@ class SunspecBatteryScheduleHandler extends storage_schedule_handler_js_1.Storag
22
22
  * Sticky pre-schedule snapshot, captured once on the first `onInit` and
23
23
  * held across every subsequent schedule-to-schedule replacement. Cleared
24
24
  * only when a real rollback fires (`mode: auto` from the data bus or
25
- * `dispose`). Lets the eventual restore write the *true* baseline rather
26
- * than the last-active-entry register set that the device happens to be
25
+ * `dispose`). Supplies the `wChaMax` denominator for the percentage math
26
+ * and the charge source to hand back on release — taken from the *original*
27
+ * pre-schedule read rather than the register set the device happens to be
27
28
  * in at the moment of replacement.
28
29
  */
29
30
  originalBaseline;
@@ -35,6 +36,11 @@ class SunspecBatteryScheduleHandler extends storage_schedule_handler_js_1.Storag
35
36
  * write the snapshot back during a replacement.
36
37
  */
37
38
  suppressNextRollbackWrite = false;
39
+ /**
40
+ * Mirrors the base class's private `disposed` flag so the auto-release
41
+ * path can tell "released and gone" from "released and still live".
42
+ */
43
+ isDisposed = false;
38
44
  constructor(opts) {
39
45
  super(opts);
40
46
  this.sunspecClient = opts.sunspecClient;
@@ -52,11 +58,15 @@ class SunspecBatteryScheduleHandler extends storage_schedule_handler_js_1.Storag
52
58
  * normally there.
53
59
  */
54
60
  async applyMessage(msg) {
55
- if (msg.applianceId === this.applianceIdForLog
61
+ const isForThisAppliance = msg.applianceId === this.applianceIdForLog;
62
+ if (isForThisAppliance
56
63
  && msg.data.mode === enyo_data_bus_value_js_1.EnyoStorageScheduleModeEnum.Schedule
57
64
  && this.getActiveEntry() !== undefined) {
58
65
  this.suppressNextRollbackWrite = true;
59
66
  }
67
+ // Captured before the base class runs: with a schedule active, its
68
+ // `doReleaseSchedule → onRollback` already performs the auto release.
69
+ const hadActiveSchedule = this.getActiveEntry() !== undefined;
60
70
  try {
61
71
  await super.applyMessage(msg);
62
72
  }
@@ -66,6 +76,30 @@ class SunspecBatteryScheduleHandler extends storage_schedule_handler_js_1.Storag
66
76
  // otherwise stay set and silently swallow the next real rollback.
67
77
  this.suppressNextRollbackWrite = false;
68
78
  }
79
+ if (isForThisAppliance
80
+ && msg.data.mode === enyo_data_bus_value_js_1.EnyoStorageScheduleModeEnum.Auto
81
+ && !hadActiveSchedule
82
+ && !this.isDisposed) {
83
+ // `mode: auto` with no schedule running is a no-op in the base
84
+ // library (`doReleaseSchedule` returns early when idle), so nothing
85
+ // would reach the device. That is not harmless: Model 124 setpoints
86
+ // latch — an interrupted schedule (modbus drop, package restart)
87
+ // leaves StorCtl_Mod and InWRte set, and with InOutWRte_RvrtTms = 0
88
+ // the inverter never reverts on its own. Seen in production as a
89
+ // battery pinned at ~2 % charge rate for hours while the energy
90
+ // manager sent `auto` every 40 s and got an `Accepted` back.
91
+ //
92
+ // Writing the release unconditionally is safe because it is
93
+ // idempotent: it is exactly the register set that means
94
+ // "no external control".
95
+ await this.releaseToAuto().catch(err => {
96
+ console.error(`SunspecBatteryScheduleHandler ${this.applianceIdForLog}: auto release failed: ${err}`);
97
+ });
98
+ }
99
+ }
100
+ dispose() {
101
+ this.isDisposed = true;
102
+ super.dispose();
69
103
  }
70
104
  async onInit(_active) {
71
105
  if (this.originalBaseline !== undefined) {
@@ -104,7 +138,7 @@ class SunspecBatteryScheduleHandler extends storage_schedule_handler_js_1.Storag
104
138
  console.error(`SunspecBatteryScheduleHandler ${this.applianceIdForLog}: onChange write failed for entry ${active.indexInSchedule}: ${err}`);
105
139
  });
106
140
  }
107
- onRollback(registers) {
141
+ onRollback(_registers) {
108
142
  if (this.suppressNextRollbackWrite) {
109
143
  // Schedule-to-schedule replacement. Do NOT write the snapshot
110
144
  // back; the new schedule's first `onChange` will own the
@@ -122,19 +156,41 @@ class SunspecBatteryScheduleHandler extends storage_schedule_handler_js_1.Storag
122
156
  // that follows — so a concurrent `onInit` queued behind this one
123
157
  // (impossible today, but defensive) cannot observe a half-cleared
124
158
  // state.
159
+ const chargeSource = this.originalBaseline?.chaGriSet;
125
160
  this.originalBaseline = undefined;
126
161
  this.installedWChaMax = undefined;
127
- void (async () => {
128
- try {
129
- await this.sunspecClient.writeBatteryControls(this.unitId, registers);
130
- await this.getSnapshotService()?.recordModification([
131
- 'storCtlMod', 'chaGriSet', 'wChaMax', 'inWRte', 'outWRte',
132
- ]);
133
- }
134
- catch (err) {
135
- console.error(`SunspecBatteryScheduleHandler ${this.applianceIdForLog}: onRollback failed: ${err}`);
136
- }
137
- })();
162
+ void this.releaseToAuto(chargeSource).catch(err => {
163
+ console.error(`SunspecBatteryScheduleHandler ${this.applianceIdForLog}: onRollback failed: ${err}`);
164
+ });
165
+ }
166
+ /**
167
+ * Hand the battery back to its own logic: clear StorCtl_Mod and open both
168
+ * rate setpoints back up to 100 %.
169
+ *
170
+ * This deliberately does *not* replay the snapshot captured at `onInit`.
171
+ * That snapshot is whatever the registers happened to hold when the
172
+ * schedule started — and if a previous schedule had already stranded a
173
+ * limit there, restoring it re-latches the limit and the battery can never
174
+ * climb out. The only correct end state for "we are no longer controlling
175
+ * this battery" is the one written here, and it is the same set of
176
+ * registers a schedule entry touches, so nothing else is left dangling.
177
+ *
178
+ * `wChaMax` is not restored because a schedule never writes it — the
179
+ * percentage math divides against it instead.
180
+ *
181
+ * @param chargeSource - `chaGriSet` to restore; falls back to PV-only,
182
+ * since grid charging is only ever switched on by a Charge entry.
183
+ */
184
+ async releaseToAuto(chargeSource) {
185
+ await this.sunspecClient.writeBatteryControls(this.unitId, {
186
+ storCtlMod: sunspec_interfaces_js_1.SunspecStorageControlMode.NONE,
187
+ chaGriSet: chargeSource ?? sunspec_interfaces_js_1.SunspecChargeSource.PV,
188
+ inWRte: 100,
189
+ outWRte: 100,
190
+ });
191
+ await this.getSnapshotService()?.recordModification([
192
+ 'storCtlMod', 'chaGriSet', 'inWRte', 'outWRte',
193
+ ]);
138
194
  }
139
195
  onError(err) {
140
196
  this.onErrorCallback?.(err);
@@ -142,19 +198,11 @@ class SunspecBatteryScheduleHandler extends storage_schedule_handler_js_1.Storag
142
198
  async applyEntry(active) {
143
199
  const { direction, powerW } = active.entry;
144
200
  if (direction === enyo_data_bus_value_js_1.EnyoStorageScheduleDirectionEnum.Idle) {
145
- // Idle entry: hand the battery back to its own autonomous logic
146
- // by restoring the snapshotted pre-schedule registers (same write
147
- // set as the `mode: auto` rollback path). The schedule itself
148
- // keeps running, so a subsequent Charge / Discharge entry can
149
- // resume external control.
150
- const baselineRegisters = this.originalBaseline;
151
- if (!baselineRegisters) {
152
- throw new Error(`no usable baseline for idle entry (originalBaseline=undefined)`);
153
- }
154
- await this.sunspecClient.writeBatteryControls(this.unitId, baselineRegisters);
155
- await this.getSnapshotService()?.recordModification([
156
- 'storCtlMod', 'chaGriSet', 'wChaMax', 'inWRte', 'outWRte',
157
- ]);
201
+ // Idle entry: hand the battery back to its own autonomous logic —
202
+ // the same release the `mode: auto` path performs. The schedule
203
+ // itself keeps running, so a subsequent Charge / Discharge entry
204
+ // can resume external control.
205
+ await this.releaseToAuto(this.originalBaseline?.chaGriSet);
158
206
  return;
159
207
  }
160
208
  const baseline = this.installedWChaMax;
@@ -19,9 +19,10 @@ export interface BatteryControlsClient {
19
19
  */
20
20
  export type BatteryModificationRecorder = Pick<SnapshotService<SunspecBatteryControls>, 'recordModification'>;
21
21
  /**
22
- * Snapshot of the writable Model 124 registers taken at schedule install time
23
- * and handed back to `onRollback` when the schedule completes, is replaced, or
24
- * is restored after a process restart. JSON-serialisable because
22
+ * Snapshot of the writable Model 124 registers taken at schedule install time.
23
+ * The base library hands it back to `onRollback`, but the release path does not
24
+ * replay it — see `releaseToAuto`. It survives as the record of the installed
25
+ * `wChaMax` and the pre-schedule charge source. JSON-serialisable because
25
26
  * `EnergyAppStorage` round-trips it.
26
27
  *
27
28
  * The snapshotted `wChaMax` doubles as the denominator for the `inWRte` /
@@ -63,8 +64,9 @@ export declare class SunspecBatteryScheduleHandler extends StorageScheduleHandle
63
64
  * Sticky pre-schedule snapshot, captured once on the first `onInit` and
64
65
  * held across every subsequent schedule-to-schedule replacement. Cleared
65
66
  * only when a real rollback fires (`mode: auto` from the data bus or
66
- * `dispose`). Lets the eventual restore write the *true* baseline rather
67
- * than the last-active-entry register set that the device happens to be
67
+ * `dispose`). Supplies the `wChaMax` denominator for the percentage math
68
+ * and the charge source to hand back on release — taken from the *original*
69
+ * pre-schedule read rather than the register set the device happens to be
68
70
  * in at the moment of replacement.
69
71
  */
70
72
  private originalBaseline?;
@@ -76,6 +78,11 @@ export declare class SunspecBatteryScheduleHandler extends StorageScheduleHandle
76
78
  * write the snapshot back during a replacement.
77
79
  */
78
80
  private suppressNextRollbackWrite;
81
+ /**
82
+ * Mirrors the base class's private `disposed` flag so the auto-release
83
+ * path can tell "released and gone" from "released and still live".
84
+ */
85
+ private isDisposed;
79
86
  constructor(opts: SunspecBatteryScheduleHandlerOptions);
80
87
  /**
81
88
  * Override the base-class data-bus router so a schedule-to-schedule
@@ -86,9 +93,29 @@ export declare class SunspecBatteryScheduleHandler extends StorageScheduleHandle
86
93
  * normally there.
87
94
  */
88
95
  applyMessage(msg: EnyoDataBusSetStorageScheduleV1): Promise<void>;
96
+ dispose(): void;
89
97
  protected onInit(_active: ActiveStorageScheduleEntry): Promise<SunspecScheduleRegisters>;
90
98
  protected onChange(active: ActiveStorageScheduleEntry, _previous: ActiveStorageScheduleEntry | undefined): void;
91
- protected onRollback(registers: SunspecScheduleRegisters): void;
99
+ protected onRollback(_registers: SunspecScheduleRegisters): void;
100
+ /**
101
+ * Hand the battery back to its own logic: clear StorCtl_Mod and open both
102
+ * rate setpoints back up to 100 %.
103
+ *
104
+ * This deliberately does *not* replay the snapshot captured at `onInit`.
105
+ * That snapshot is whatever the registers happened to hold when the
106
+ * schedule started — and if a previous schedule had already stranded a
107
+ * limit there, restoring it re-latches the limit and the battery can never
108
+ * climb out. The only correct end state for "we are no longer controlling
109
+ * this battery" is the one written here, and it is the same set of
110
+ * registers a schedule entry touches, so nothing else is left dangling.
111
+ *
112
+ * `wChaMax` is not restored because a schedule never writes it — the
113
+ * percentage math divides against it instead.
114
+ *
115
+ * @param chargeSource - `chaGriSet` to restore; falls back to PV-only,
116
+ * since grid charging is only ever switched on by a Charge entry.
117
+ */
118
+ private releaseToAuto;
92
119
  protected onError(err: Error): void;
93
120
  private applyEntry;
94
121
  }
@@ -5,6 +5,7 @@ exports.detectFeaturesFromRegisters = detectFeaturesFromRegisters;
5
5
  exports.sumMpptBatteryPowerW = sumMpptBatteryPowerW;
6
6
  exports.selectLiveBatteryPowerW = selectLiveBatteryPowerW;
7
7
  exports.filterFeaturesByCalibrationResult = filterFeaturesByCalibrationResult;
8
+ exports.deriveStorageMode = deriveStorageMode;
8
9
  const sunspec_interfaces_js_1 = require("./sunspec-interfaces.cjs");
9
10
  const node_crypto_1 = require("node:crypto");
10
11
  const enyo_appliance_js_1 = require("@enyo-energy/energy-app-sdk/dist/types/enyo-appliance.js");
@@ -1434,6 +1435,37 @@ function filterFeaturesByCalibrationResult(detected, result, controllable) {
1434
1435
  /**
1435
1436
  * Sunspec Battery implementation
1436
1437
  */
1438
+ /**
1439
+ * Derive the storage mode from Model 124's StorCtl_Mod bitfield.
1440
+ *
1441
+ * The bits mark *which rate setpoint the device is enforcing* — bit 0 for
1442
+ * InWRte, bit 1 for OutWRte — not which direction is permitted. So no bit set
1443
+ * is the autonomous mode, and both bits set is the most externally controlled
1444
+ * state there is, which is the opposite of what the naming suggests. Reading
1445
+ * them the other way round meant a battery running its own logic reported
1446
+ * `holding`, a fully clamped one reported `auto`, and no appliance could ever
1447
+ * report that it had been handed back to itself.
1448
+ *
1449
+ * @param controls - Model 124 control registers, or null when unreadable.
1450
+ * @returns The mode, or null when StorCtl_Mod could not be read.
1451
+ */
1452
+ function deriveStorageMode(controls) {
1453
+ if (!controls || controls.storCtlMod === undefined) {
1454
+ return null;
1455
+ }
1456
+ const chargeLimited = (controls.storCtlMod & 0x0001) !== 0;
1457
+ const dischargeLimited = (controls.storCtlMod & 0x0002) !== 0;
1458
+ if (!chargeLimited && !dischargeLimited) {
1459
+ return sunspec_interfaces_js_1.SunspecStorageMode.AUTO;
1460
+ }
1461
+ // Both directions clamped to zero — the battery is parked, not idling.
1462
+ if (chargeLimited && dischargeLimited && controls.inWRte === 0 && controls.outWRte === 0) {
1463
+ return sunspec_interfaces_js_1.SunspecStorageMode.HOLDING;
1464
+ }
1465
+ return dischargeLimited && !chargeLimited
1466
+ ? sunspec_interfaces_js_1.SunspecStorageMode.DISCHARGE
1467
+ : sunspec_interfaces_js_1.SunspecStorageMode.CHARGE;
1468
+ }
1437
1469
  class SunspecBattery extends BaseSunspecDevice {
1438
1470
  featureMode;
1439
1471
  capabilities;
@@ -1588,7 +1620,7 @@ class SunspecBattery extends BaseSunspecDevice {
1588
1620
  battery: {
1589
1621
  connectedToApplianceId: inverterApplianceId,
1590
1622
  storageMode: this.mapToEnyoStorageMode(storageMode),
1591
- maxChargingPowerW: batteryData && batteryData.wChaMax !== undefined && batteryData.inWRte !== undefined ? batteryData.wChaMax * batteryData.inWRte : undefined,
1623
+ maxChargingPowerW: batteryData?.wChaMax,
1592
1624
  maxDischargePowerW: batteryData && batteryData.wChaMax !== undefined && batteryData.outWRte !== undefined ? batteryData.wChaMax * batteryData.outWRte : undefined,
1593
1625
  activeChargeLimitW: activeChargeLimitW,
1594
1626
  activeDischargeLimitW: activeDischargeLimitW,
@@ -1616,7 +1648,7 @@ class SunspecBattery extends BaseSunspecDevice {
1616
1648
  ...existingAppliance?.battery,
1617
1649
  connectedToApplianceId: inverterApplianceId,
1618
1650
  storageMode: this.mapToEnyoStorageMode(storageMode),
1619
- maxChargingPowerW: batteryData && batteryData.wChaMax !== undefined && batteryData.inWRte !== undefined ? batteryData.wChaMax * batteryData.inWRte : undefined,
1651
+ maxChargingPowerW: batteryData?.wChaMax,
1620
1652
  maxDischargePowerW: batteryData && batteryData.wChaMax !== undefined && batteryData.outWRte !== undefined ? batteryData.wChaMax * batteryData.outWRte : undefined,
1621
1653
  activeChargeLimitW: activeChargeLimitW,
1622
1654
  activeDischargeLimitW: activeDischargeLimitW,
@@ -1775,7 +1807,7 @@ class SunspecBattery extends BaseSunspecDevice {
1775
1807
  battery: {
1776
1808
  ...appliance?.battery,
1777
1809
  storageMode: this.mapToEnyoStorageMode(storageMode),
1778
- maxChargingPowerW: batteryData && batteryData.wChaMax !== undefined && batteryData.inWRte !== undefined ? batteryData.wChaMax * batteryData.inWRte : undefined,
1810
+ maxChargingPowerW: batteryData?.wChaMax,
1779
1811
  maxDischargePowerW: batteryData && batteryData.wChaMax !== undefined && batteryData.outWRte !== undefined ? batteryData.wChaMax * batteryData.outWRte : undefined,
1780
1812
  activeChargeLimitW: this.computeActiveChargeLimitW(batteryData),
1781
1813
  activeDischargeLimitW: this.computeActiveDischargeLimitW(batteryData),
@@ -1979,23 +2011,7 @@ class SunspecBattery extends BaseSunspecDevice {
1979
2011
  return (batteryData.wChaMax * batteryData.outWRte) / 100;
1980
2012
  }
1981
2013
  determineStorageMode(controls) {
1982
- if (!controls || controls.storCtlMod === undefined) {
1983
- return null;
1984
- }
1985
- const chargeEnabled = (controls.storCtlMod & 0x0001) !== 0;
1986
- const dischargeEnabled = (controls.storCtlMod & 0x0002) !== 0;
1987
- if (chargeEnabled && dischargeEnabled) {
1988
- return sunspec_interfaces_js_1.SunspecStorageMode.AUTO;
1989
- }
1990
- else if (chargeEnabled && !dischargeEnabled) {
1991
- return sunspec_interfaces_js_1.SunspecStorageMode.CHARGE;
1992
- }
1993
- else if (!chargeEnabled && dischargeEnabled) {
1994
- return sunspec_interfaces_js_1.SunspecStorageMode.DISCHARGE;
1995
- }
1996
- else {
1997
- return sunspec_interfaces_js_1.SunspecStorageMode.HOLDING;
1998
- }
2014
+ return deriveStorageMode(controls);
1999
2015
  }
2000
2016
  /**
2001
2017
  * Check if grid charging is enabled
@@ -510,6 +510,21 @@ export declare function filterFeaturesByCalibrationResult(detected: EnyoBatteryF
510
510
  /**
511
511
  * Sunspec Battery implementation
512
512
  */
513
+ /**
514
+ * Derive the storage mode from Model 124's StorCtl_Mod bitfield.
515
+ *
516
+ * The bits mark *which rate setpoint the device is enforcing* — bit 0 for
517
+ * InWRte, bit 1 for OutWRte — not which direction is permitted. So no bit set
518
+ * is the autonomous mode, and both bits set is the most externally controlled
519
+ * state there is, which is the opposite of what the naming suggests. Reading
520
+ * them the other way round meant a battery running its own logic reported
521
+ * `holding`, a fully clamped one reported `auto`, and no appliance could ever
522
+ * report that it had been handed back to itself.
523
+ *
524
+ * @param controls - Model 124 control registers, or null when unreadable.
525
+ * @returns The mode, or null when StorCtl_Mod could not be read.
526
+ */
527
+ export declare function deriveStorageMode(controls: SunspecBatteryControls | null): SunspecStorageMode | null;
513
528
  export declare class SunspecBattery extends BaseSunspecDevice {
514
529
  private readonly featureMode;
515
530
  private readonly capabilities;
@@ -216,8 +216,15 @@ var SunspecBatteryEvent1;
216
216
  */
217
217
  var SunspecStorageControlMode;
218
218
  (function (SunspecStorageControlMode) {
219
+ /**
220
+ * No bit set — no external setpoint is active and the battery runs its own
221
+ * autonomous logic. This, not `CHARGE | DISCHARGE`, is what "auto" means on
222
+ * Model 124: the bits say *which rate setpoint is being enforced*, so
223
+ * clearing them is what hands control back to the device.
224
+ */
225
+ SunspecStorageControlMode[SunspecStorageControlMode["NONE"] = 0] = "NONE";
219
226
  SunspecStorageControlMode[SunspecStorageControlMode["CHARGE"] = 1] = "CHARGE";
220
- SunspecStorageControlMode[SunspecStorageControlMode["DISCHARGE"] = 2] = "DISCHARGE"; // Bit 1 - Enable discharge mode (allow external discharge control)
227
+ SunspecStorageControlMode[SunspecStorageControlMode["DISCHARGE"] = 2] = "DISCHARGE"; // Bit 1 - the OutWRte discharge-rate setpoint is active
221
228
  // Note: Higher bits may control other modes like autonomous/self-consumption
222
229
  // but these are device-specific and not standardized in SunSpec
223
230
  })(SunspecStorageControlMode || (exports.SunspecStorageControlMode = SunspecStorageControlMode = {}));
@@ -262,7 +269,7 @@ var SunspecStorageMode;
262
269
  SunspecStorageMode["CHARGE"] = "charge";
263
270
  SunspecStorageMode["DISCHARGE"] = "discharge";
264
271
  SunspecStorageMode["HOLDING"] = "holding";
265
- SunspecStorageMode["AUTO"] = "auto"; // Both charge and discharge allowed
272
+ SunspecStorageMode["AUTO"] = "auto"; // No external setpoint active (StorCtl_Mod = 0)
266
273
  })(SunspecStorageMode || (exports.SunspecStorageMode = SunspecStorageMode = {}));
267
274
  /**
268
275
  * Battery control structure for writing to Model 124
@@ -503,7 +503,14 @@ export declare enum SunspecBatteryEvent1 {
503
503
  * - To ensure external control works properly, the appropriate bits must be set
504
504
  */
505
505
  export declare enum SunspecStorageControlMode {
506
- CHARGE = 1,// Bit 0 - Enable charge mode (allow external charge control)
506
+ /**
507
+ * No bit set — no external setpoint is active and the battery runs its own
508
+ * autonomous logic. This, not `CHARGE | DISCHARGE`, is what "auto" means on
509
+ * Model 124: the bits say *which rate setpoint is being enforced*, so
510
+ * clearing them is what hands control back to the device.
511
+ */
512
+ NONE = 0,
513
+ CHARGE = 1,// Bit 0 - the InWRte charge-rate setpoint is active
507
514
  DISCHARGE = 2
508
515
  }
509
516
  /**
@@ -778,9 +785,9 @@ export declare enum SunspecChargeSource {
778
785
  * Storage modes for simplified battery control
779
786
  */
780
787
  export declare enum SunspecStorageMode {
781
- CHARGE = "charge",// Allow charging only
782
- DISCHARGE = "discharge",// Allow discharging only
783
- HOLDING = "holding",// No charge or discharge (holding mode)
788
+ CHARGE = "charge",// Charging is externally rate-limited via InWRte
789
+ DISCHARGE = "discharge",// Discharging is externally rate-limited via OutWRte
790
+ HOLDING = "holding",// Both directions clamped to 0 % — battery is parked
784
791
  AUTO = "auto"
785
792
  }
786
793
  /**
@@ -2277,30 +2277,43 @@ class SunspecModbusClient {
2277
2277
  * Set battery storage mode (simplified interface)
2278
2278
  */
2279
2279
  async setStorageMode(unitId, mode) {
2280
- let storCtlMod;
2280
+ // StorCtl_Mod says *which rate setpoint is enforced*, not what the
2281
+ // battery is allowed to do — so releasing control means clearing the
2282
+ // bits, and the rate registers have to be written alongside them.
2283
+ // Setting the bits without touching InWRte/OutWRte would enforce
2284
+ // whatever percentage was left behind by an earlier command.
2285
+ let controls;
2281
2286
  switch (mode) {
2282
2287
  case sunspec_interfaces_js_1.SunspecStorageMode.CHARGE:
2283
- // Enable charge only (bit 0 = 1, bit 1 = 0)
2284
- storCtlMod = sunspec_interfaces_js_1.SunspecStorageControlMode.CHARGE;
2288
+ // Enforce the charge-rate setpoint only (bit 0 = 1, bit 1 = 0).
2289
+ controls = { storCtlMod: sunspec_interfaces_js_1.SunspecStorageControlMode.CHARGE };
2285
2290
  break;
2286
2291
  case sunspec_interfaces_js_1.SunspecStorageMode.DISCHARGE:
2287
- // Enable discharge only (bit 0 = 0, bit 1 = 1)
2288
- storCtlMod = sunspec_interfaces_js_1.SunspecStorageControlMode.DISCHARGE;
2292
+ // Enforce the discharge-rate setpoint only (bit 0 = 0, bit 1 = 1).
2293
+ controls = { storCtlMod: sunspec_interfaces_js_1.SunspecStorageControlMode.DISCHARGE };
2289
2294
  break;
2290
2295
  case sunspec_interfaces_js_1.SunspecStorageMode.HOLDING:
2291
- // Disable both charge and discharge (bits 0 and 1 = 0)
2292
- storCtlMod = 0;
2296
+ // Park the battery: both setpoints enforced, both clamped to 0 %.
2297
+ controls = {
2298
+ storCtlMod: sunspec_interfaces_js_1.SunspecStorageControlMode.CHARGE | sunspec_interfaces_js_1.SunspecStorageControlMode.DISCHARGE,
2299
+ inWRte: 0,
2300
+ outWRte: 0,
2301
+ };
2293
2302
  break;
2294
2303
  case sunspec_interfaces_js_1.SunspecStorageMode.AUTO:
2295
- // Enable both charge and discharge (bits 0 and 1 = 1)
2296
- storCtlMod = sunspec_interfaces_js_1.SunspecStorageControlMode.CHARGE | sunspec_interfaces_js_1.SunspecStorageControlMode.DISCHARGE;
2304
+ // Hand control back: no setpoint enforced, both rates wide open.
2305
+ controls = {
2306
+ storCtlMod: sunspec_interfaces_js_1.SunspecStorageControlMode.NONE,
2307
+ inWRte: 100,
2308
+ outWRte: 100,
2309
+ };
2297
2310
  break;
2298
2311
  default:
2299
2312
  console.error(`Invalid storage mode: ${mode}`);
2300
2313
  return false;
2301
2314
  }
2302
- console.log(`Setting storage mode to ${mode} (control bits: 0x${storCtlMod.toString(16)})`);
2303
- return this.writeBatteryControls(unitId, { storCtlMod });
2315
+ console.log(`Setting storage mode to ${mode} (${JSON.stringify(controls)})`);
2316
+ return this.writeBatteryControls(unitId, controls);
2304
2317
  }
2305
2318
  /**
2306
2319
  * Enable or disable grid charging
@@ -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.104';
12
+ exports.SDK_VERSION = '0.0.105';
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.104";
8
+ export declare const SDK_VERSION = "0.0.105";
9
9
  /**
10
10
  * Gets the current SDK version.
11
11
  * @returns The semantic version string of the SDK
@@ -19,9 +19,10 @@ export interface BatteryControlsClient {
19
19
  */
20
20
  export type BatteryModificationRecorder = Pick<SnapshotService<SunspecBatteryControls>, 'recordModification'>;
21
21
  /**
22
- * Snapshot of the writable Model 124 registers taken at schedule install time
23
- * and handed back to `onRollback` when the schedule completes, is replaced, or
24
- * is restored after a process restart. JSON-serialisable because
22
+ * Snapshot of the writable Model 124 registers taken at schedule install time.
23
+ * The base library hands it back to `onRollback`, but the release path does not
24
+ * replay it — see `releaseToAuto`. It survives as the record of the installed
25
+ * `wChaMax` and the pre-schedule charge source. JSON-serialisable because
25
26
  * `EnergyAppStorage` round-trips it.
26
27
  *
27
28
  * The snapshotted `wChaMax` doubles as the denominator for the `inWRte` /
@@ -63,8 +64,9 @@ export declare class SunspecBatteryScheduleHandler extends StorageScheduleHandle
63
64
  * Sticky pre-schedule snapshot, captured once on the first `onInit` and
64
65
  * held across every subsequent schedule-to-schedule replacement. Cleared
65
66
  * only when a real rollback fires (`mode: auto` from the data bus or
66
- * `dispose`). Lets the eventual restore write the *true* baseline rather
67
- * than the last-active-entry register set that the device happens to be
67
+ * `dispose`). Supplies the `wChaMax` denominator for the percentage math
68
+ * and the charge source to hand back on release — taken from the *original*
69
+ * pre-schedule read rather than the register set the device happens to be
68
70
  * in at the moment of replacement.
69
71
  */
70
72
  private originalBaseline?;
@@ -76,6 +78,11 @@ export declare class SunspecBatteryScheduleHandler extends StorageScheduleHandle
76
78
  * write the snapshot back during a replacement.
77
79
  */
78
80
  private suppressNextRollbackWrite;
81
+ /**
82
+ * Mirrors the base class's private `disposed` flag so the auto-release
83
+ * path can tell "released and gone" from "released and still live".
84
+ */
85
+ private isDisposed;
79
86
  constructor(opts: SunspecBatteryScheduleHandlerOptions);
80
87
  /**
81
88
  * Override the base-class data-bus router so a schedule-to-schedule
@@ -86,9 +93,29 @@ export declare class SunspecBatteryScheduleHandler extends StorageScheduleHandle
86
93
  * normally there.
87
94
  */
88
95
  applyMessage(msg: EnyoDataBusSetStorageScheduleV1): Promise<void>;
96
+ dispose(): void;
89
97
  protected onInit(_active: ActiveStorageScheduleEntry): Promise<SunspecScheduleRegisters>;
90
98
  protected onChange(active: ActiveStorageScheduleEntry, _previous: ActiveStorageScheduleEntry | undefined): void;
91
- protected onRollback(registers: SunspecScheduleRegisters): void;
99
+ protected onRollback(_registers: SunspecScheduleRegisters): void;
100
+ /**
101
+ * Hand the battery back to its own logic: clear StorCtl_Mod and open both
102
+ * rate setpoints back up to 100 %.
103
+ *
104
+ * This deliberately does *not* replay the snapshot captured at `onInit`.
105
+ * That snapshot is whatever the registers happened to hold when the
106
+ * schedule started — and if a previous schedule had already stranded a
107
+ * limit there, restoring it re-latches the limit and the battery can never
108
+ * climb out. The only correct end state for "we are no longer controlling
109
+ * this battery" is the one written here, and it is the same set of
110
+ * registers a schedule entry touches, so nothing else is left dangling.
111
+ *
112
+ * `wChaMax` is not restored because a schedule never writes it — the
113
+ * percentage math divides against it instead.
114
+ *
115
+ * @param chargeSource - `chaGriSet` to restore; falls back to PV-only,
116
+ * since grid charging is only ever switched on by a Charge entry.
117
+ */
118
+ private releaseToAuto;
92
119
  protected onError(err: Error): void;
93
120
  private applyEntry;
94
121
  }
@@ -19,8 +19,9 @@ export class SunspecBatteryScheduleHandler extends StorageScheduleHandler {
19
19
  * Sticky pre-schedule snapshot, captured once on the first `onInit` and
20
20
  * held across every subsequent schedule-to-schedule replacement. Cleared
21
21
  * only when a real rollback fires (`mode: auto` from the data bus or
22
- * `dispose`). Lets the eventual restore write the *true* baseline rather
23
- * than the last-active-entry register set that the device happens to be
22
+ * `dispose`). Supplies the `wChaMax` denominator for the percentage math
23
+ * and the charge source to hand back on release — taken from the *original*
24
+ * pre-schedule read rather than the register set the device happens to be
24
25
  * in at the moment of replacement.
25
26
  */
26
27
  originalBaseline;
@@ -32,6 +33,11 @@ export class SunspecBatteryScheduleHandler extends StorageScheduleHandler {
32
33
  * write the snapshot back during a replacement.
33
34
  */
34
35
  suppressNextRollbackWrite = false;
36
+ /**
37
+ * Mirrors the base class's private `disposed` flag so the auto-release
38
+ * path can tell "released and gone" from "released and still live".
39
+ */
40
+ isDisposed = false;
35
41
  constructor(opts) {
36
42
  super(opts);
37
43
  this.sunspecClient = opts.sunspecClient;
@@ -49,11 +55,15 @@ export class SunspecBatteryScheduleHandler extends StorageScheduleHandler {
49
55
  * normally there.
50
56
  */
51
57
  async applyMessage(msg) {
52
- if (msg.applianceId === this.applianceIdForLog
58
+ const isForThisAppliance = msg.applianceId === this.applianceIdForLog;
59
+ if (isForThisAppliance
53
60
  && msg.data.mode === EnyoStorageScheduleModeEnum.Schedule
54
61
  && this.getActiveEntry() !== undefined) {
55
62
  this.suppressNextRollbackWrite = true;
56
63
  }
64
+ // Captured before the base class runs: with a schedule active, its
65
+ // `doReleaseSchedule → onRollback` already performs the auto release.
66
+ const hadActiveSchedule = this.getActiveEntry() !== undefined;
57
67
  try {
58
68
  await super.applyMessage(msg);
59
69
  }
@@ -63,6 +73,30 @@ export class SunspecBatteryScheduleHandler extends StorageScheduleHandler {
63
73
  // otherwise stay set and silently swallow the next real rollback.
64
74
  this.suppressNextRollbackWrite = false;
65
75
  }
76
+ if (isForThisAppliance
77
+ && msg.data.mode === EnyoStorageScheduleModeEnum.Auto
78
+ && !hadActiveSchedule
79
+ && !this.isDisposed) {
80
+ // `mode: auto` with no schedule running is a no-op in the base
81
+ // library (`doReleaseSchedule` returns early when idle), so nothing
82
+ // would reach the device. That is not harmless: Model 124 setpoints
83
+ // latch — an interrupted schedule (modbus drop, package restart)
84
+ // leaves StorCtl_Mod and InWRte set, and with InOutWRte_RvrtTms = 0
85
+ // the inverter never reverts on its own. Seen in production as a
86
+ // battery pinned at ~2 % charge rate for hours while the energy
87
+ // manager sent `auto` every 40 s and got an `Accepted` back.
88
+ //
89
+ // Writing the release unconditionally is safe because it is
90
+ // idempotent: it is exactly the register set that means
91
+ // "no external control".
92
+ await this.releaseToAuto().catch(err => {
93
+ console.error(`SunspecBatteryScheduleHandler ${this.applianceIdForLog}: auto release failed: ${err}`);
94
+ });
95
+ }
96
+ }
97
+ dispose() {
98
+ this.isDisposed = true;
99
+ super.dispose();
66
100
  }
67
101
  async onInit(_active) {
68
102
  if (this.originalBaseline !== undefined) {
@@ -101,7 +135,7 @@ export class SunspecBatteryScheduleHandler extends StorageScheduleHandler {
101
135
  console.error(`SunspecBatteryScheduleHandler ${this.applianceIdForLog}: onChange write failed for entry ${active.indexInSchedule}: ${err}`);
102
136
  });
103
137
  }
104
- onRollback(registers) {
138
+ onRollback(_registers) {
105
139
  if (this.suppressNextRollbackWrite) {
106
140
  // Schedule-to-schedule replacement. Do NOT write the snapshot
107
141
  // back; the new schedule's first `onChange` will own the
@@ -119,19 +153,41 @@ export class SunspecBatteryScheduleHandler extends StorageScheduleHandler {
119
153
  // that follows — so a concurrent `onInit` queued behind this one
120
154
  // (impossible today, but defensive) cannot observe a half-cleared
121
155
  // state.
156
+ const chargeSource = this.originalBaseline?.chaGriSet;
122
157
  this.originalBaseline = undefined;
123
158
  this.installedWChaMax = undefined;
124
- void (async () => {
125
- try {
126
- await this.sunspecClient.writeBatteryControls(this.unitId, registers);
127
- await this.getSnapshotService()?.recordModification([
128
- 'storCtlMod', 'chaGriSet', 'wChaMax', 'inWRte', 'outWRte',
129
- ]);
130
- }
131
- catch (err) {
132
- console.error(`SunspecBatteryScheduleHandler ${this.applianceIdForLog}: onRollback failed: ${err}`);
133
- }
134
- })();
159
+ void this.releaseToAuto(chargeSource).catch(err => {
160
+ console.error(`SunspecBatteryScheduleHandler ${this.applianceIdForLog}: onRollback failed: ${err}`);
161
+ });
162
+ }
163
+ /**
164
+ * Hand the battery back to its own logic: clear StorCtl_Mod and open both
165
+ * rate setpoints back up to 100 %.
166
+ *
167
+ * This deliberately does *not* replay the snapshot captured at `onInit`.
168
+ * That snapshot is whatever the registers happened to hold when the
169
+ * schedule started — and if a previous schedule had already stranded a
170
+ * limit there, restoring it re-latches the limit and the battery can never
171
+ * climb out. The only correct end state for "we are no longer controlling
172
+ * this battery" is the one written here, and it is the same set of
173
+ * registers a schedule entry touches, so nothing else is left dangling.
174
+ *
175
+ * `wChaMax` is not restored because a schedule never writes it — the
176
+ * percentage math divides against it instead.
177
+ *
178
+ * @param chargeSource - `chaGriSet` to restore; falls back to PV-only,
179
+ * since grid charging is only ever switched on by a Charge entry.
180
+ */
181
+ async releaseToAuto(chargeSource) {
182
+ await this.sunspecClient.writeBatteryControls(this.unitId, {
183
+ storCtlMod: SunspecStorageControlMode.NONE,
184
+ chaGriSet: chargeSource ?? SunspecChargeSource.PV,
185
+ inWRte: 100,
186
+ outWRte: 100,
187
+ });
188
+ await this.getSnapshotService()?.recordModification([
189
+ 'storCtlMod', 'chaGriSet', 'inWRte', 'outWRte',
190
+ ]);
135
191
  }
136
192
  onError(err) {
137
193
  this.onErrorCallback?.(err);
@@ -139,19 +195,11 @@ export class SunspecBatteryScheduleHandler extends StorageScheduleHandler {
139
195
  async applyEntry(active) {
140
196
  const { direction, powerW } = active.entry;
141
197
  if (direction === EnyoStorageScheduleDirectionEnum.Idle) {
142
- // Idle entry: hand the battery back to its own autonomous logic
143
- // by restoring the snapshotted pre-schedule registers (same write
144
- // set as the `mode: auto` rollback path). The schedule itself
145
- // keeps running, so a subsequent Charge / Discharge entry can
146
- // resume external control.
147
- const baselineRegisters = this.originalBaseline;
148
- if (!baselineRegisters) {
149
- throw new Error(`no usable baseline for idle entry (originalBaseline=undefined)`);
150
- }
151
- await this.sunspecClient.writeBatteryControls(this.unitId, baselineRegisters);
152
- await this.getSnapshotService()?.recordModification([
153
- 'storCtlMod', 'chaGriSet', 'wChaMax', 'inWRte', 'outWRte',
154
- ]);
198
+ // Idle entry: hand the battery back to its own autonomous logic —
199
+ // the same release the `mode: auto` path performs. The schedule
200
+ // itself keeps running, so a subsequent Charge / Discharge entry
201
+ // can resume external control.
202
+ await this.releaseToAuto(this.originalBaseline?.chaGriSet);
155
203
  return;
156
204
  }
157
205
  const baseline = this.installedWChaMax;
@@ -510,6 +510,21 @@ export declare function filterFeaturesByCalibrationResult(detected: EnyoBatteryF
510
510
  /**
511
511
  * Sunspec Battery implementation
512
512
  */
513
+ /**
514
+ * Derive the storage mode from Model 124's StorCtl_Mod bitfield.
515
+ *
516
+ * The bits mark *which rate setpoint the device is enforcing* — bit 0 for
517
+ * InWRte, bit 1 for OutWRte — not which direction is permitted. So no bit set
518
+ * is the autonomous mode, and both bits set is the most externally controlled
519
+ * state there is, which is the opposite of what the naming suggests. Reading
520
+ * them the other way round meant a battery running its own logic reported
521
+ * `holding`, a fully clamped one reported `auto`, and no appliance could ever
522
+ * report that it had been handed back to itself.
523
+ *
524
+ * @param controls - Model 124 control registers, or null when unreadable.
525
+ * @returns The mode, or null when StorCtl_Mod could not be read.
526
+ */
527
+ export declare function deriveStorageMode(controls: SunspecBatteryControls | null): SunspecStorageMode | null;
513
528
  export declare class SunspecBattery extends BaseSunspecDevice {
514
529
  private readonly featureMode;
515
530
  private readonly capabilities;
@@ -1425,6 +1425,37 @@ export function filterFeaturesByCalibrationResult(detected, result, controllable
1425
1425
  /**
1426
1426
  * Sunspec Battery implementation
1427
1427
  */
1428
+ /**
1429
+ * Derive the storage mode from Model 124's StorCtl_Mod bitfield.
1430
+ *
1431
+ * The bits mark *which rate setpoint the device is enforcing* — bit 0 for
1432
+ * InWRte, bit 1 for OutWRte — not which direction is permitted. So no bit set
1433
+ * is the autonomous mode, and both bits set is the most externally controlled
1434
+ * state there is, which is the opposite of what the naming suggests. Reading
1435
+ * them the other way round meant a battery running its own logic reported
1436
+ * `holding`, a fully clamped one reported `auto`, and no appliance could ever
1437
+ * report that it had been handed back to itself.
1438
+ *
1439
+ * @param controls - Model 124 control registers, or null when unreadable.
1440
+ * @returns The mode, or null when StorCtl_Mod could not be read.
1441
+ */
1442
+ export function deriveStorageMode(controls) {
1443
+ if (!controls || controls.storCtlMod === undefined) {
1444
+ return null;
1445
+ }
1446
+ const chargeLimited = (controls.storCtlMod & 0x0001) !== 0;
1447
+ const dischargeLimited = (controls.storCtlMod & 0x0002) !== 0;
1448
+ if (!chargeLimited && !dischargeLimited) {
1449
+ return SunspecStorageMode.AUTO;
1450
+ }
1451
+ // Both directions clamped to zero — the battery is parked, not idling.
1452
+ if (chargeLimited && dischargeLimited && controls.inWRte === 0 && controls.outWRte === 0) {
1453
+ return SunspecStorageMode.HOLDING;
1454
+ }
1455
+ return dischargeLimited && !chargeLimited
1456
+ ? SunspecStorageMode.DISCHARGE
1457
+ : SunspecStorageMode.CHARGE;
1458
+ }
1428
1459
  export class SunspecBattery extends BaseSunspecDevice {
1429
1460
  featureMode;
1430
1461
  capabilities;
@@ -1579,7 +1610,7 @@ export class SunspecBattery extends BaseSunspecDevice {
1579
1610
  battery: {
1580
1611
  connectedToApplianceId: inverterApplianceId,
1581
1612
  storageMode: this.mapToEnyoStorageMode(storageMode),
1582
- maxChargingPowerW: batteryData && batteryData.wChaMax !== undefined && batteryData.inWRte !== undefined ? batteryData.wChaMax * batteryData.inWRte : undefined,
1613
+ maxChargingPowerW: batteryData?.wChaMax,
1583
1614
  maxDischargePowerW: batteryData && batteryData.wChaMax !== undefined && batteryData.outWRte !== undefined ? batteryData.wChaMax * batteryData.outWRte : undefined,
1584
1615
  activeChargeLimitW: activeChargeLimitW,
1585
1616
  activeDischargeLimitW: activeDischargeLimitW,
@@ -1607,7 +1638,7 @@ export class SunspecBattery extends BaseSunspecDevice {
1607
1638
  ...existingAppliance?.battery,
1608
1639
  connectedToApplianceId: inverterApplianceId,
1609
1640
  storageMode: this.mapToEnyoStorageMode(storageMode),
1610
- maxChargingPowerW: batteryData && batteryData.wChaMax !== undefined && batteryData.inWRte !== undefined ? batteryData.wChaMax * batteryData.inWRte : undefined,
1641
+ maxChargingPowerW: batteryData?.wChaMax,
1611
1642
  maxDischargePowerW: batteryData && batteryData.wChaMax !== undefined && batteryData.outWRte !== undefined ? batteryData.wChaMax * batteryData.outWRte : undefined,
1612
1643
  activeChargeLimitW: activeChargeLimitW,
1613
1644
  activeDischargeLimitW: activeDischargeLimitW,
@@ -1766,7 +1797,7 @@ export class SunspecBattery extends BaseSunspecDevice {
1766
1797
  battery: {
1767
1798
  ...appliance?.battery,
1768
1799
  storageMode: this.mapToEnyoStorageMode(storageMode),
1769
- maxChargingPowerW: batteryData && batteryData.wChaMax !== undefined && batteryData.inWRte !== undefined ? batteryData.wChaMax * batteryData.inWRte : undefined,
1800
+ maxChargingPowerW: batteryData?.wChaMax,
1770
1801
  maxDischargePowerW: batteryData && batteryData.wChaMax !== undefined && batteryData.outWRte !== undefined ? batteryData.wChaMax * batteryData.outWRte : undefined,
1771
1802
  activeChargeLimitW: this.computeActiveChargeLimitW(batteryData),
1772
1803
  activeDischargeLimitW: this.computeActiveDischargeLimitW(batteryData),
@@ -1970,23 +2001,7 @@ export class SunspecBattery extends BaseSunspecDevice {
1970
2001
  return (batteryData.wChaMax * batteryData.outWRte) / 100;
1971
2002
  }
1972
2003
  determineStorageMode(controls) {
1973
- if (!controls || controls.storCtlMod === undefined) {
1974
- return null;
1975
- }
1976
- const chargeEnabled = (controls.storCtlMod & 0x0001) !== 0;
1977
- const dischargeEnabled = (controls.storCtlMod & 0x0002) !== 0;
1978
- if (chargeEnabled && dischargeEnabled) {
1979
- return SunspecStorageMode.AUTO;
1980
- }
1981
- else if (chargeEnabled && !dischargeEnabled) {
1982
- return SunspecStorageMode.CHARGE;
1983
- }
1984
- else if (!chargeEnabled && dischargeEnabled) {
1985
- return SunspecStorageMode.DISCHARGE;
1986
- }
1987
- else {
1988
- return SunspecStorageMode.HOLDING;
1989
- }
2004
+ return deriveStorageMode(controls);
1990
2005
  }
1991
2006
  /**
1992
2007
  * Check if grid charging is enabled
@@ -503,7 +503,14 @@ export declare enum SunspecBatteryEvent1 {
503
503
  * - To ensure external control works properly, the appropriate bits must be set
504
504
  */
505
505
  export declare enum SunspecStorageControlMode {
506
- CHARGE = 1,// Bit 0 - Enable charge mode (allow external charge control)
506
+ /**
507
+ * No bit set — no external setpoint is active and the battery runs its own
508
+ * autonomous logic. This, not `CHARGE | DISCHARGE`, is what "auto" means on
509
+ * Model 124: the bits say *which rate setpoint is being enforced*, so
510
+ * clearing them is what hands control back to the device.
511
+ */
512
+ NONE = 0,
513
+ CHARGE = 1,// Bit 0 - the InWRte charge-rate setpoint is active
507
514
  DISCHARGE = 2
508
515
  }
509
516
  /**
@@ -778,9 +785,9 @@ export declare enum SunspecChargeSource {
778
785
  * Storage modes for simplified battery control
779
786
  */
780
787
  export declare enum SunspecStorageMode {
781
- CHARGE = "charge",// Allow charging only
782
- DISCHARGE = "discharge",// Allow discharging only
783
- HOLDING = "holding",// No charge or discharge (holding mode)
788
+ CHARGE = "charge",// Charging is externally rate-limited via InWRte
789
+ DISCHARGE = "discharge",// Discharging is externally rate-limited via OutWRte
790
+ HOLDING = "holding",// Both directions clamped to 0 % — battery is parked
784
791
  AUTO = "auto"
785
792
  }
786
793
  /**
@@ -213,8 +213,15 @@ export var SunspecBatteryEvent1;
213
213
  */
214
214
  export var SunspecStorageControlMode;
215
215
  (function (SunspecStorageControlMode) {
216
+ /**
217
+ * No bit set — no external setpoint is active and the battery runs its own
218
+ * autonomous logic. This, not `CHARGE | DISCHARGE`, is what "auto" means on
219
+ * Model 124: the bits say *which rate setpoint is being enforced*, so
220
+ * clearing them is what hands control back to the device.
221
+ */
222
+ SunspecStorageControlMode[SunspecStorageControlMode["NONE"] = 0] = "NONE";
216
223
  SunspecStorageControlMode[SunspecStorageControlMode["CHARGE"] = 1] = "CHARGE";
217
- SunspecStorageControlMode[SunspecStorageControlMode["DISCHARGE"] = 2] = "DISCHARGE"; // Bit 1 - Enable discharge mode (allow external discharge control)
224
+ SunspecStorageControlMode[SunspecStorageControlMode["DISCHARGE"] = 2] = "DISCHARGE"; // Bit 1 - the OutWRte discharge-rate setpoint is active
218
225
  // Note: Higher bits may control other modes like autonomous/self-consumption
219
226
  // but these are device-specific and not standardized in SunSpec
220
227
  })(SunspecStorageControlMode || (SunspecStorageControlMode = {}));
@@ -259,7 +266,7 @@ export var SunspecStorageMode;
259
266
  SunspecStorageMode["CHARGE"] = "charge";
260
267
  SunspecStorageMode["DISCHARGE"] = "discharge";
261
268
  SunspecStorageMode["HOLDING"] = "holding";
262
- SunspecStorageMode["AUTO"] = "auto"; // Both charge and discharge allowed
269
+ SunspecStorageMode["AUTO"] = "auto"; // No external setpoint active (StorCtl_Mod = 0)
263
270
  })(SunspecStorageMode || (SunspecStorageMode = {}));
264
271
  /**
265
272
  * Battery control structure for writing to Model 124
@@ -2270,30 +2270,43 @@ export class SunspecModbusClient {
2270
2270
  * Set battery storage mode (simplified interface)
2271
2271
  */
2272
2272
  async setStorageMode(unitId, mode) {
2273
- let storCtlMod;
2273
+ // StorCtl_Mod says *which rate setpoint is enforced*, not what the
2274
+ // battery is allowed to do — so releasing control means clearing the
2275
+ // bits, and the rate registers have to be written alongside them.
2276
+ // Setting the bits without touching InWRte/OutWRte would enforce
2277
+ // whatever percentage was left behind by an earlier command.
2278
+ let controls;
2274
2279
  switch (mode) {
2275
2280
  case SunspecStorageMode.CHARGE:
2276
- // Enable charge only (bit 0 = 1, bit 1 = 0)
2277
- storCtlMod = SunspecStorageControlMode.CHARGE;
2281
+ // Enforce the charge-rate setpoint only (bit 0 = 1, bit 1 = 0).
2282
+ controls = { storCtlMod: SunspecStorageControlMode.CHARGE };
2278
2283
  break;
2279
2284
  case SunspecStorageMode.DISCHARGE:
2280
- // Enable discharge only (bit 0 = 0, bit 1 = 1)
2281
- storCtlMod = SunspecStorageControlMode.DISCHARGE;
2285
+ // Enforce the discharge-rate setpoint only (bit 0 = 0, bit 1 = 1).
2286
+ controls = { storCtlMod: SunspecStorageControlMode.DISCHARGE };
2282
2287
  break;
2283
2288
  case SunspecStorageMode.HOLDING:
2284
- // Disable both charge and discharge (bits 0 and 1 = 0)
2285
- storCtlMod = 0;
2289
+ // Park the battery: both setpoints enforced, both clamped to 0 %.
2290
+ controls = {
2291
+ storCtlMod: SunspecStorageControlMode.CHARGE | SunspecStorageControlMode.DISCHARGE,
2292
+ inWRte: 0,
2293
+ outWRte: 0,
2294
+ };
2286
2295
  break;
2287
2296
  case SunspecStorageMode.AUTO:
2288
- // Enable both charge and discharge (bits 0 and 1 = 1)
2289
- storCtlMod = SunspecStorageControlMode.CHARGE | SunspecStorageControlMode.DISCHARGE;
2297
+ // Hand control back: no setpoint enforced, both rates wide open.
2298
+ controls = {
2299
+ storCtlMod: SunspecStorageControlMode.NONE,
2300
+ inWRte: 100,
2301
+ outWRte: 100,
2302
+ };
2290
2303
  break;
2291
2304
  default:
2292
2305
  console.error(`Invalid storage mode: ${mode}`);
2293
2306
  return false;
2294
2307
  }
2295
- console.log(`Setting storage mode to ${mode} (control bits: 0x${storCtlMod.toString(16)})`);
2296
- return this.writeBatteryControls(unitId, { storCtlMod });
2308
+ console.log(`Setting storage mode to ${mode} (${JSON.stringify(controls)})`);
2309
+ return this.writeBatteryControls(unitId, controls);
2297
2310
  }
2298
2311
  /**
2299
2312
  * Enable or disable grid charging
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.104";
8
+ export declare const SDK_VERSION = "0.0.105";
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.104';
8
+ export const SDK_VERSION = '0.0.105';
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.104",
3
+ "version": "0.0.105",
4
4
  "description": "enyo Energy Sunspec SDK",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",