Skip to content

Commit

Permalink
Add battery charging/discharging/percentage accessories
Browse files Browse the repository at this point in the history
  • Loading branch information
longzheng committed Mar 8, 2024
1 parent 61eca2b commit 9b85122
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
48 changes: 38 additions & 10 deletions src/fronius-accessory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { AccessoryPlugin, HAP, Logging, Service } from 'homebridge';
import { FroniusApi } from './fronius-api';

export type Metering = 'Import' | 'Export' | 'Load' | 'PV' | 'Battery';
export type Metering =
| 'Import'
| 'Export'
| 'Load'
| 'PV'
| 'Battery charging'
| 'Battery discharging'
| 'Battery %';

export class FroniusAccessory implements AccessoryPlugin {
private readonly log: Logging;
Expand Down Expand Up @@ -113,6 +120,15 @@ export class FroniusAccessory implements AccessoryPlugin {
const data = await this.froniusApi.getInverterData();

if (data) {
// P_Akku should be positive when discharging and negative when charging
const batteryValue = data.Site.P_Akku ?? 0;
const batteryState =
batteryValue < 0
? 'charging'
: batteryValue > 0
? 'discharging'
: 'idle';

switch (this.metering) {
case 'Export':
case 'Import': {
Expand All @@ -122,9 +138,11 @@ export class FroniusAccessory implements AccessoryPlugin {
const isImport = this.metering === 'Import';

this.onValue =
(isImport ? autonomyValue : selfConsumptionValue) < 100; // on/off is calculated whether autonomy/selfConsumption is less than 100
// on/off is calculated whether autonomy/selfConsumption is less than 100
(isImport ? autonomyValue : selfConsumptionValue) < 100;
this.brightnessValue =
100 - (isImport ? autonomyValue : selfConsumptionValue); // percentage of import/export is calculated from 100 - autonomy/selfConsumption
// percentage of import/export is calculated from 100 - autonomy/selfConsumption
100 - (isImport ? autonomyValue : selfConsumptionValue);
this.luxValue = isImport
? gridValue > 0
? gridValue
Expand Down Expand Up @@ -153,16 +171,26 @@ export class FroniusAccessory implements AccessoryPlugin {
this.luxValue = pvValue ?? 0;
break;
}
case 'Battery': {
const akkuValue = Math.abs(data.Site.P_Akku ?? 0);

case 'Battery %': {
// if the site has multiple inverters, average all the inverter SOCs
const socs = Object.values(data.Inverters).map(inv => inv.SOC ?? 0);
const socs = Object.values(data.Inverters).map((inv) => inv.SOC ?? 0);
const socAvg = socs.reduce((a, b) => a + b, 0) / socs.length;

this.brightnessValue = socAvg;
this.onValue = akkuValue > 0; // if P_Akku is positive -> using energy out of Battery, turn on
this.luxValue = akkuValue;
this.onValue = socAvg > 0;
break;
}
case 'Battery charging': {
const isCharging = batteryState === 'charging';
this.brightnessValue = isCharging ? 100 : 0;
this.onValue = isCharging;
this.luxValue = Math.abs(batteryValue);
break;
}
case 'Battery discharging': {
const isDischarging = batteryState === 'discharging';
this.brightnessValue = isDischarging ? 100 : 0;
this.onValue = isDischarging;
this.luxValue = Math.abs(batteryValue);
break;
}
}
Expand Down
16 changes: 15 additions & 1 deletion src/fronius-platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,21 @@ class FroniusInverterLightsStaticPlatform implements StaticPlatformPlugin {
new FroniusAccessory(
hap,
this.log,
'Battery',
'Battery charging',
this.froniusApi,
this.pollInterval,
),
new FroniusAccessory(
hap,
this.log,
'Battery discharging',
this.froniusApi,
this.pollInterval,
),
new FroniusAccessory(
hap,
this.log,
'Battery %',
this.froniusApi,
this.pollInterval,
),
Expand Down

0 comments on commit 9b85122

Please sign in to comment.