Skip to content

Commit 880b169

Browse files
committed
use a simpler calculation in the SolarGaugeCard
In the tooltip, the formula is described simple as 100% - (total grid export / total solar production) This commit uses this formula instead of the complicated calculations with the stored battery power. That formula reduced the self consumption ratio if the batteries are not 100% efficient.
1 parent 8605c23 commit 880b169

File tree

3 files changed

+86
-315
lines changed

3 files changed

+86
-315
lines changed

src/data/energy.ts

Lines changed: 12 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,88 +1357,22 @@ export const formatConsumptionShort = (
13571357
};
13581358

13591359
export const calculateSolarConsumedGauge = (
1360-
hasBattery: boolean,
1361-
data: EnergySumData
1360+
sum: EnergySumData,
1361+
consumption: EnergyConsumptionData
13621362
): number | undefined => {
1363-
if (!data.total.solar) {
1363+
if (
1364+
!sum.total.solar ||
1365+
sum.total.to_grid === undefined ||
1366+
consumption.total.battery_to_grid === undefined
1367+
) {
13641368
return undefined;
13651369
}
1366-
const { consumption, compareConsumption: _ } = computeConsumptionData(
1367-
data,
1368-
undefined
1370+
return (
1371+
(1 -
1372+
(sum.total.to_grid - consumption.total.battery_to_grid) /
1373+
sum.total.solar) *
1374+
100
13691375
);
1370-
if (!hasBattery) {
1371-
const solarProduction = data.total.solar;
1372-
return (consumption.total.used_solar / solarProduction) * 100;
1373-
}
1374-
1375-
let solarConsumed = 0;
1376-
let solarReturned = 0;
1377-
const batteryLifo: { type: "solar" | "grid"; value: number }[] = [];
1378-
1379-
// Here we will attempt to track consumed solar energy, as it routes through the battery and ultimately to consumption or grid.
1380-
// At each timestamp we will track energy added to the battery (and its source), and we will drain this in Last-in/First-out order.
1381-
// Energy leaving the battery when the stack is empty will just be ignored, as we cannot determine where it came from.
1382-
// This is likely energy stored during a previous period.
1383-
1384-
data.timestamps.forEach((t) => {
1385-
solarConsumed += consumption.used_solar[t] ?? 0;
1386-
solarReturned += consumption.solar_to_grid[t] ?? 0;
1387-
1388-
if (consumption.grid_to_battery[t]) {
1389-
batteryLifo.push({
1390-
type: "grid",
1391-
value: consumption.grid_to_battery[t],
1392-
});
1393-
}
1394-
if (consumption.solar_to_battery[t]) {
1395-
batteryLifo.push({
1396-
type: "solar",
1397-
value: consumption.solar_to_battery[t],
1398-
});
1399-
}
1400-
1401-
let batteryToGrid = consumption.battery_to_grid[t] ?? 0;
1402-
let usedBattery = consumption.used_battery[t] ?? 0;
1403-
1404-
const drainBattery = function (amount: number): {
1405-
energy: number;
1406-
type: "solar" | "grid";
1407-
} {
1408-
const lastLifo = batteryLifo[batteryLifo.length - 1];
1409-
const type = lastLifo.type;
1410-
if (amount >= lastLifo.value) {
1411-
const energy = lastLifo.value;
1412-
batteryLifo.pop();
1413-
return { energy, type };
1414-
}
1415-
lastLifo.value -= amount;
1416-
return { energy: amount, type };
1417-
};
1418-
1419-
while (usedBattery > 0 && batteryLifo.length) {
1420-
const { energy, type } = drainBattery(usedBattery);
1421-
if (type === "solar") {
1422-
solarConsumed += energy;
1423-
}
1424-
usedBattery -= energy;
1425-
}
1426-
1427-
while (batteryToGrid > 0 && batteryLifo.length) {
1428-
const { energy, type } = drainBattery(batteryToGrid);
1429-
if (type === "solar") {
1430-
solarReturned += energy;
1431-
}
1432-
batteryToGrid -= energy;
1433-
}
1434-
});
1435-
1436-
const totalProduction = solarConsumed + solarReturned;
1437-
const hasSolarProduction = !!totalProduction;
1438-
if (hasSolarProduction) {
1439-
return (solarConsumed / totalProduction) * 100;
1440-
}
1441-
return undefined;
14421376
};
14431377

14441378
/**

src/panels/lovelace/cards/energy/hui-energy-solar-consumed-gauge-card.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import "../../../../components/ha-gauge";
99
import "../../../../components/ha-svg-icon";
1010
import type { EnergyData } from "../../../../data/energy";
1111
import {
12+
computeConsumptionData,
1213
calculateSolarConsumedGauge,
1314
getEnergyDataCollection,
1415
getSummedData,
@@ -102,8 +103,11 @@ class HuiEnergySolarGaugeCard
102103

103104
let value: number | undefined;
104105
if (productionReturnedToGrid !== null) {
105-
const hasBattery = !!summedData.to_battery || !!summedData.from_battery;
106-
value = calculateSolarConsumedGauge(hasBattery, summedData);
106+
const { consumption, compareConsumption: __ } = computeConsumptionData(
107+
summedData,
108+
undefined
109+
);
110+
value = calculateSolarConsumedGauge(summedData, consumption);
107111
}
108112

109113
return html`

0 commit comments

Comments
 (0)