diff --git a/src/panels/lovelace/cards/energy/hui-energy-sankey-card.ts b/src/panels/lovelace/cards/energy/hui-energy-sankey-card.ts index 0fe49658cf..a23b6dbc3e 100644 --- a/src/panels/lovelace/cards/energy/hui-energy-sankey-card.ts +++ b/src/panels/lovelace/cards/energy/hui-energy-sankey-card.ts @@ -123,30 +123,6 @@ class HuiEnergySankeyCard }); } - // Add battery source if available - if (types.battery) { - const totalBatteryOut = - calculateStatisticsSumGrowth( - this._data.stats, - types.battery.map((source) => source.stat_energy_from) - ) || 0; - - nodes.push({ - id: "battery", - label: this.hass.localize( - "ui.panel.lovelace.cards.energy.energy_distribution.battery" - ), - value: totalBatteryOut, - tooltip: `${formatNumber(totalBatteryOut, this.hass.locale)} kWh`, - color: computedStyle.getPropertyValue("--energy-battery-out-color"), - index: 0, - }); - links.push({ - source: "battery", - target: "home", - }); - } - // Add solar if available if (types.solar) { const totalSolarProduction = @@ -172,26 +148,51 @@ class HuiEnergySankeyCard }); } - // Calculate total home consumption from all source nodes + // Calculate total home consumption from all producers homeNode.value = nodes .filter((node) => node.index === 0) .reduce((sum, node) => sum + (node.value || 0), 0); - // Add battery sink if available if (types.battery) { + // Add battery source + const totalBatteryOut = + calculateStatisticsSumGrowth( + this._data.stats, + types.battery.map((source) => source.stat_energy_from) + ) || 0; const totalBatteryIn = calculateStatisticsSumGrowth( this._data.stats, types.battery.map((source) => source.stat_energy_to) ) || 0; + const netBattery = totalBatteryOut - totalBatteryIn; + const netBatteryOut = Math.max(netBattery, 0); + const netBatteryIn = Math.max(-netBattery, 0); + homeNode.value += netBattery; + nodes.push({ + id: "battery", + label: this.hass.localize( + "ui.panel.lovelace.cards.energy.energy_distribution.battery" + ), + value: netBatteryOut, + tooltip: `${formatNumber(netBatteryOut, this.hass.locale)} kWh`, + color: computedStyle.getPropertyValue("--energy-battery-out-color"), + index: 0, + }); + links.push({ + source: "battery", + target: "home", + }); + + // Add battery sink nodes.push({ id: "battery_in", label: this.hass.localize( "ui.panel.lovelace.cards.energy.energy_distribution.battery" ), - value: totalBatteryIn, - tooltip: `${formatNumber(totalBatteryIn, this.hass.locale)} kWh`, + value: netBatteryIn, + tooltip: `${formatNumber(netBatteryIn, this.hass.locale)} kWh`, color: computedStyle.getPropertyValue("--energy-battery-in-color"), index: 1, }); @@ -204,8 +205,6 @@ class HuiEnergySankeyCard }); } }); - - homeNode.value -= totalBatteryIn; } // Add grid return if available