Fix energy calculations (#9647)

* Fix calculations

* max.. not min...
This commit is contained in:
Bram Kragten 2021-07-29 20:29:49 +02:00 committed by GitHub
parent 1bd6392a4c
commit d4cbdab4a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 24 deletions

View File

@ -102,10 +102,9 @@ class HuiEnergyCarbonGaugeCard extends LitElement implements LovelaceCard {
const totalEnergyConsumed = const totalEnergyConsumed =
totalGridConsumption + totalGridConsumption +
(totalSolarProduction || 0) - Math.max(0, (totalSolarProduction || 0) - (totalGridReturned || 0));
(totalGridReturned || 0);
value = round((highCarbonEnergy / totalEnergyConsumed) * 100); value = round((1 - highCarbonEnergy / totalEnergyConsumed) * 100);
} }
return html` return html`

View File

@ -104,17 +104,17 @@ class HuiEnergyDistrubutionCard extends LitElement implements LovelaceCard {
) || 0; ) || 0;
} }
const totalConsumption = const solarConsumption = Math.max(
totalGridConsumption + 0,
(totalSolarProduction || 0) - (totalSolarProduction || 0) - (productionReturnedToGrid || 0)
(productionReturnedToGrid || 0); );
const totalHomeConsumption = totalGridConsumption + solarConsumption;
let homeSolarCircumference: number | undefined; let homeSolarCircumference: number | undefined;
if (hasSolarProduction) { if (hasSolarProduction) {
const homePctSolar = homeSolarCircumference =
((totalSolarProduction || 0) - (productionReturnedToGrid || 0)) / CIRCLE_CIRCUMFERENCE * (solarConsumption / totalHomeConsumption);
totalConsumption;
homeSolarCircumference = CIRCLE_CIRCUMFERENCE * homePctSolar;
} }
let lowCarbonConsumption: number | undefined; let lowCarbonConsumption: number | undefined;
@ -142,10 +142,8 @@ class HuiEnergyDistrubutionCard extends LitElement implements LovelaceCard {
if (highCarbonConsumption !== null) { if (highCarbonConsumption !== null) {
lowCarbonConsumption = totalGridConsumption - highCarbonConsumption; lowCarbonConsumption = totalGridConsumption - highCarbonConsumption;
const homePctGridHighCarbon = highCarbonConsumption / totalConsumption;
homeHighCarbonCircumference = homeHighCarbonCircumference =
CIRCLE_CIRCUMFERENCE * homePctGridHighCarbon; CIRCLE_CIRCUMFERENCE * (highCarbonConsumption / totalHomeConsumption);
homeLowCarbonCircumference = homeLowCarbonCircumference =
CIRCLE_CIRCUMFERENCE - CIRCLE_CIRCUMFERENCE -
@ -154,15 +152,6 @@ class HuiEnergyDistrubutionCard extends LitElement implements LovelaceCard {
} }
} }
homeSolarCircumference = CIRCLE_CIRCUMFERENCE * 0.1;
homeHighCarbonCircumference = CIRCLE_CIRCUMFERENCE * 0.8;
homeLowCarbonCircumference =
CIRCLE_CIRCUMFERENCE -
(homeSolarCircumference || 0) -
homeHighCarbonCircumference;
return html` return html`
<ha-card .header=${this._config.title}> <ha-card .header=${this._config.title}>
<div class="card-content"> <div class="card-content">
@ -252,7 +241,7 @@ class HuiEnergyDistrubutionCard extends LitElement implements LovelaceCard {
})}" })}"
> >
<ha-svg-icon .path="${mdiHome}"></ha-svg-icon> <ha-svg-icon .path="${mdiHome}"></ha-svg-icon>
${formatNumber(totalConsumption, this.hass.locale, { ${formatNumber(totalHomeConsumption, this.hass.locale, {
maximumFractionDigits: 1, maximumFractionDigits: 1,
})} })}
kWh kWh