From 21a29ed3a56dc010e6314a13842f2dc1bffda69d Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Tue, 27 Jul 2021 00:47:50 +0200 Subject: [PATCH] Statistic sums requires at least 2 values (#9616) --- src/data/history.ts | 16 ++-- .../hui-energy-carbon-consumed-gauge-card.ts | 73 ++++++++++--------- .../hui-energy-solar-consumed-gauge-card.ts | 4 +- .../lovelace/cards/hui-energy-usage-card.ts | 21 ++---- 4 files changed, 54 insertions(+), 60 deletions(-) diff --git a/src/data/history.ts b/src/data/history.ts index 71cc46e231..220c59aaf9 100644 --- a/src/data/history.ts +++ b/src/data/history.ts @@ -302,12 +302,9 @@ export const fetchStatistics = ( export const calculateStatisticSumGrowth = ( values: StatisticValue[] ): number | null => { - if (values.length === 0) { + if (values.length < 2) { return null; } - if (values.length === 1) { - return values[0].sum; - } const endSum = values[values.length - 1].sum; if (endSum === null) { return null; @@ -323,7 +320,7 @@ export const calculateStatisticsSumGrowth = ( data: Statistics, stats: string[] ): number | null => { - let totalGrowth = 0; + let totalGrowth: number | null = null; for (const stat of stats) { if (!(stat in data)) { @@ -332,10 +329,13 @@ export const calculateStatisticsSumGrowth = ( const statGrowth = calculateStatisticSumGrowth(data[stat]); if (statGrowth === null) { - return null; + continue; + } + if (totalGrowth === null) { + totalGrowth = statGrowth; + } else { + totalGrowth += statGrowth; } - - totalGrowth += statGrowth; } return totalGrowth; diff --git a/src/panels/lovelace/cards/hui-energy-carbon-consumed-gauge-card.ts b/src/panels/lovelace/cards/hui-energy-carbon-consumed-gauge-card.ts index 7a3c5f68ba..a5fb506d85 100644 --- a/src/panels/lovelace/cards/hui-energy-carbon-consumed-gauge-card.ts +++ b/src/panels/lovelace/cards/hui-energy-carbon-consumed-gauge-card.ts @@ -14,6 +14,7 @@ import { Statistics, } from "../../../data/history"; import type { HomeAssistant } from "../../../types"; +import { createEntityNotFoundWarning } from "../components/hui-warning"; import type { LovelaceCard } from "../types"; import { severityMap } from "./hui-gauge-card"; import type { EnergyCarbonGaugeCardConfig } from "./types"; @@ -61,7 +62,9 @@ class HuiEnergyCarbonGaugeCard extends LitElement implements LovelaceCard { const co2State = this.hass.states[this._co2SignalEntity]; if (!co2State) { - return html`No CO2 Signal entity found.`; + return html` + ${createEntityNotFoundWarning(this.hass, this._co2SignalEntity)} + `; } const co2percentage = Number(co2State.state); @@ -78,44 +81,46 @@ class HuiEnergyCarbonGaugeCard extends LitElement implements LovelaceCard { types.grid![0].flow_from.map((flow) => flow.stat_energy_from) ); - const totalSolarProduction = types.solar - ? calculateStatisticsSumGrowth( - this._stats, - types.solar.map((source) => source.stat_energy_from) - ) - : undefined; + let value: number | undefined; - const totalGridReturned = calculateStatisticsSumGrowth( - this._stats, - types.grid![0].flow_to.map((flow) => flow.stat_energy_to) - ); + if (totalGridConsumption) { + const totalSolarProduction = types.solar + ? calculateStatisticsSumGrowth( + this._stats, + types.solar.map((source) => source.stat_energy_from) + ) + : undefined; - if (totalGridConsumption === null) { - return html`Couldn't calculate the total grid consumption.`; + const totalGridReturned = calculateStatisticsSumGrowth( + this._stats, + types.grid![0].flow_to.map((flow) => flow.stat_energy_to) + ); + + const highCarbonEnergy = (totalGridConsumption * co2percentage) / 100; + + const totalEnergyConsumed = + totalGridConsumption + + (totalSolarProduction || 0) - + (totalGridReturned || 0); + + value = round((highCarbonEnergy / totalEnergyConsumed) * 100); } - const highCarbonEnergy = (totalGridConsumption * co2percentage) / 100; - - const totalEnergyConsumed = - totalGridConsumption + - (totalSolarProduction || 0) - - (totalGridReturned || 0); - - const value = round((highCarbonEnergy / totalEnergyConsumed) * 100); - return html` - - -
High-carbon energy consumed
+ ${value !== undefined + ? html` +
High-carbon energy consumed
` + : html`Consumed high-carbon energy couldn't be calculated`}
`; } diff --git a/src/panels/lovelace/cards/hui-energy-solar-consumed-gauge-card.ts b/src/panels/lovelace/cards/hui-energy-solar-consumed-gauge-card.ts index c5e9dab835..be3ad03e03 100644 --- a/src/panels/lovelace/cards/hui-energy-solar-consumed-gauge-card.ts +++ b/src/panels/lovelace/cards/hui-energy-solar-consumed-gauge-card.ts @@ -69,8 +69,8 @@ class HuiEnergySolarGaugeCard extends LitElement implements LovelaceCard { } return html` - ${value - ? html` 0; - const totalGridConsumption = calculateStatisticsSumGrowth( - this._stats, - types.grid![0].flow_from.map((flow) => flow.stat_energy_from) - ); - - if (totalGridConsumption === null) { - return html`Total consumption couldn't be calculated`; - } + const totalGridConsumption = + calculateStatisticsSumGrowth( + this._stats, + types.grid![0].flow_from.map((flow) => flow.stat_energy_from) + ) ?? 0; let totalSolarProduction: number | null = null; @@ -84,10 +81,6 @@ class HuiEnergyUsageCard extends LitElement implements LovelaceCard { this._stats, types.solar!.map((source) => source.stat_energy_from) ); - - if (totalSolarProduction === null) { - return html`Total production couldn't be calculated`; - } } let productionReturnedToGrid: number | null = null; @@ -97,10 +90,6 @@ class HuiEnergyUsageCard extends LitElement implements LovelaceCard { this._stats, types.grid![0].flow_to.map((flow) => flow.stat_energy_to) ); - - if (productionReturnedToGrid === undefined) { - return html`Production returned to grid couldn't be calculated`; - } } // total consumption = consumption_from_grid + solar_production - return_to_grid