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 =
totalGridConsumption +
(totalSolarProduction || 0) -
(totalGridReturned || 0);
Math.max(0, (totalSolarProduction || 0) - (totalGridReturned || 0));
value = round((highCarbonEnergy / totalEnergyConsumed) * 100);
value = round((1 - highCarbonEnergy / totalEnergyConsumed) * 100);
}
return html`

View File

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