Skip to content

Commit

Permalink
Merge pull request #14 from mban88/patch-3
Browse files Browse the repository at this point in the history
Added Battery Values
  • Loading branch information
longzheng committed Jun 24, 2023
2 parents 5122627 + 447efab commit 43f0eb0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
24 changes: 18 additions & 6 deletions src/fronius-accessory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AccessoryPlugin, HAP, Logging, Service } from 'homebridge';
import { FroniusApi } from './fronius-api';

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

export class FroniusAccessory implements AccessoryPlugin {
private readonly log: Logging;
Expand Down Expand Up @@ -116,9 +116,9 @@ export class FroniusAccessory implements AccessoryPlugin {
switch (this.metering) {
case 'Export':
case 'Import': {
const gridValue = data.P_Grid;
const autonomyValue = data.rel_Autonomy;
const selfConsumptionValue = data.rel_SelfConsumption || 100;
const gridValue = data.Site.P_Grid;
const autonomyValue = data.Site.rel_Autonomy;
const selfConsumptionValue = data.Site.rel_SelfConsumption || 100;
const isImport = this.metering === 'Import';

this.onValue =
Expand All @@ -135,14 +135,14 @@ export class FroniusAccessory implements AccessoryPlugin {
break;
}
case 'Load': {
const loadValue = Math.abs(data.P_Load);
const loadValue = Math.abs(data.Site.P_Load);
this.brightnessValue = 100;
this.onValue = loadValue > 0;
this.luxValue = loadValue;
break;
}
case 'PV': {
const pvValue = data.P_PV;
const pvValue = data.Site.P_PV;
this.brightnessValue = this.pvMaxPower
? Math.min(
((pvValue ?? 0) / this.pvMaxPower) * 100, // calculate PV output as a percentage of PV max power
Expand All @@ -153,6 +153,18 @@ export class FroniusAccessory implements AccessoryPlugin {
this.luxValue = pvValue ?? 0;
break;
}
case 'Battery': {
const akkuValue = Math.abs(data.Site.P_Akku ?? 0);

// if the site has multiple inverters, average all the inverter SOCs
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;
break;
}
}
} else {
this.onValue = new Error('Error fetching value');
Expand Down
4 changes: 2 additions & 2 deletions src/fronius-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class FroniusApi {
private readonly http: AxiosInstance;
private readonly inverterIp: string;
private readonly log: Logging;
private request: Promise<Site | null> | undefined; // cache the current request to prevent concurrent requests
private request: Promise<Data | null> | undefined; // cache the current request to prevent concurrent requests

constructor(inverterIp: string, log: Logging) {
this.inverterIp = inverterIp;
Expand Down Expand Up @@ -41,7 +41,7 @@ export class FroniusApi {
this.request = undefined;

if (response.status === 200) {
return resolve(response.data.Body.Data.Site);
return resolve(response.data.Body.Data);
} else {
this.log.error(`Received invalid status code: ${response.status}`);

Expand Down

0 comments on commit 43f0eb0

Please sign in to comment.