@@ -1357,88 +1357,15 @@ export const formatConsumptionShort = (
13571357} ;
13581358
13591359export const calculateSolarConsumedGauge = (
1360- hasBattery : boolean ,
1361- data : EnergySumData
1360+ sum : EnergySumData ,
1361+ consumption : EnergyConsumptionData
13621362) : number | undefined => {
1363- if ( ! data . total . solar ) {
1363+ if ( ! sum . total . solar || consumption . total . solar_to_grid === undefined ) {
13641364 return undefined ;
13651365 }
1366- const { consumption, compareConsumption : _ } = computeConsumptionData (
1367- data ,
1368- undefined
1366+ return (
1367+ Math . max ( 0 , 1 - consumption . total . solar_to_grid / sum . total . solar ) * 100
13691368 ) ;
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 ;
14421369} ;
14431370
14441371/**
0 commit comments