Skip to content

Commit

Permalink
Add option to report Victron Battery at interval even without level c…
Browse files Browse the repository at this point in the history
…hange.
  • Loading branch information
theimo1221 committed Oct 3, 2024
1 parent 4de756e commit b1031bd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
## **WORK IN PROGRESS**
* (theimo1221) Update packages
-->

## **WORK IN PROGRESS**

* (theimo1221) Add option to report Victron Battery at interval even without level change.

## 3.0.0-alpha.88 (2024-09-07)

* (theimo1221) Make Victron AC Block configurable
Expand Down
6 changes: 6 additions & 0 deletions src/models/deviceSettings/victronDeviceSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { DeviceSettings } from './deviceSettings';
import { Utils } from '../../server';

export class VictronDeviceSettings extends DeviceSettings {
/**
* The default time interval in minutes for battery-change reporting regardless of battery level.
* -1 = disabled
*/
public batteryReportingInterval: number = 30;
/**
* The maximum wattage that the battery can deliver to the house
* @default 1700
Expand Down Expand Up @@ -62,6 +67,7 @@ export class VictronDeviceSettings extends DeviceSettings {

public fromPartialObject(data: Partial<VictronDeviceSettings>): void {
this.maxBatteryLoadWattage = data.maxBatteryLoadWattage ?? this.maxBatteryLoadWattage;
this.batteryReportingInterval = data.batteryReportingInterval ?? this.batteryReportingInterval;
this.hasBattery = data.hasBattery ?? this.hasBattery;
this.hasGrid = data.hasGrid ?? this.hasGrid;
this.hasSolar = data.hasSolar ?? this.hasSolar;
Expand Down
8 changes: 7 additions & 1 deletion src/server/services/victron/victron-device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class VictronDevice implements iEnergyManager, iBatteryDevice {
private _lastBatteryLevel: number = -1;
private _batteryLevelCallbacks: Array<(action: BatteryLevelChangeAction) => void> = [];
private _excessEnergy: number = 0;
private _lastBatteryChangeReportMs: number = 0;

public constructor(opts: VictronMqttConnectionOptions) {
this.settings = new VictronDeviceSettings();
Expand Down Expand Up @@ -333,9 +334,14 @@ export class VictronDevice implements iEnergyManager, iBatteryDevice {

private checkForBatteryChange(): void {
const newLevel: number = this.battery;
if (newLevel == this._lastBatteryLevel) {
if (
newLevel == this._lastBatteryLevel &&
(this.settings.batteryReportingInterval < 0 ||
Utils.nowMS() - this._lastBatteryChangeReportMs < this.settings.batteryReportingInterval * 60 * 1000)
) {
return;
}
this._lastBatteryChangeReportMs = Utils.nowMS();
for (const cb of this._batteryLevelCallbacks) {
cb(new BatteryLevelChangeAction(this));
}
Expand Down

0 comments on commit b1031bd

Please sign in to comment.