Statistic sums requires at least 2 values (#9616)

This commit is contained in:
Bram Kragten 2021-07-27 00:47:50 +02:00 committed by GitHub
parent a6312f4279
commit 21a29ed3a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 60 deletions

View File

@ -302,12 +302,9 @@ export const fetchStatistics = (
export const calculateStatisticSumGrowth = ( export const calculateStatisticSumGrowth = (
values: StatisticValue[] values: StatisticValue[]
): number | null => { ): number | null => {
if (values.length === 0) { if (values.length < 2) {
return null; return null;
} }
if (values.length === 1) {
return values[0].sum;
}
const endSum = values[values.length - 1].sum; const endSum = values[values.length - 1].sum;
if (endSum === null) { if (endSum === null) {
return null; return null;
@ -323,7 +320,7 @@ export const calculateStatisticsSumGrowth = (
data: Statistics, data: Statistics,
stats: string[] stats: string[]
): number | null => { ): number | null => {
let totalGrowth = 0; let totalGrowth: number | null = null;
for (const stat of stats) { for (const stat of stats) {
if (!(stat in data)) { if (!(stat in data)) {
@ -332,10 +329,13 @@ export const calculateStatisticsSumGrowth = (
const statGrowth = calculateStatisticSumGrowth(data[stat]); const statGrowth = calculateStatisticSumGrowth(data[stat]);
if (statGrowth === null) { if (statGrowth === null) {
return null; continue;
}
if (totalGrowth === null) {
totalGrowth = statGrowth;
} else {
totalGrowth += statGrowth;
} }
totalGrowth += statGrowth;
} }
return totalGrowth; return totalGrowth;

View File

@ -14,6 +14,7 @@ import {
Statistics, Statistics,
} from "../../../data/history"; } from "../../../data/history";
import type { HomeAssistant } from "../../../types"; import type { HomeAssistant } from "../../../types";
import { createEntityNotFoundWarning } from "../components/hui-warning";
import type { LovelaceCard } from "../types"; import type { LovelaceCard } from "../types";
import { severityMap } from "./hui-gauge-card"; import { severityMap } from "./hui-gauge-card";
import type { EnergyCarbonGaugeCardConfig } from "./types"; import type { EnergyCarbonGaugeCardConfig } from "./types";
@ -61,7 +62,9 @@ class HuiEnergyCarbonGaugeCard extends LitElement implements LovelaceCard {
const co2State = this.hass.states[this._co2SignalEntity]; const co2State = this.hass.states[this._co2SignalEntity];
if (!co2State) { if (!co2State) {
return html`No CO2 Signal entity found.`; return html`<hui-warning>
${createEntityNotFoundWarning(this.hass, this._co2SignalEntity)}
</hui-warning>`;
} }
const co2percentage = Number(co2State.state); 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) types.grid![0].flow_from.map((flow) => flow.stat_energy_from)
); );
const totalSolarProduction = types.solar let value: number | undefined;
? calculateStatisticsSumGrowth(
this._stats,
types.solar.map((source) => source.stat_energy_from)
)
: undefined;
const totalGridReturned = calculateStatisticsSumGrowth( if (totalGridConsumption) {
this._stats, const totalSolarProduction = types.solar
types.grid![0].flow_to.map((flow) => flow.stat_energy_to) ? calculateStatisticsSumGrowth(
); this._stats,
types.solar.map((source) => source.stat_energy_from)
)
: undefined;
if (totalGridConsumption === null) { const totalGridReturned = calculateStatisticsSumGrowth(
return html`Couldn't calculate the total grid consumption.`; 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` return html`
<ha-card> <ha-card
<ha-gauge >${value !== undefined
min="0" ? html` <ha-gauge
max="100" min="0"
.value=${value} max="100"
.locale=${this.hass!.locale} .value=${value}
label="%" .locale=${this.hass!.locale}
style=${styleMap({ label="%"
"--gauge-color": this._computeSeverity(64), style=${styleMap({
})} "--gauge-color": this._computeSeverity(64),
></ha-gauge> })}
<div class="name">High-carbon energy consumed</div> ></ha-gauge>
<div class="name">High-carbon energy consumed</div>`
: html`Consumed high-carbon energy couldn't be calculated`}
</ha-card> </ha-card>
`; `;
} }

View File

@ -69,8 +69,8 @@ class HuiEnergySolarGaugeCard extends LitElement implements LovelaceCard {
} }
return html` return html`
<ha-card> <ha-card>
${value ${value !== undefined
? html` <ha-gauge ? html`<ha-gauge
min="0" min="0"
max="100" max="100"
.value=${value} .value=${value}

View File

@ -68,14 +68,11 @@ class HuiEnergyUsageCard extends LitElement implements LovelaceCard {
const hasSolarProduction = types.solar !== undefined; const hasSolarProduction = types.solar !== undefined;
const hasReturnToGrid = hasConsumption && types.grid![0].flow_to.length > 0; const hasReturnToGrid = hasConsumption && types.grid![0].flow_to.length > 0;
const totalGridConsumption = calculateStatisticsSumGrowth( const totalGridConsumption =
this._stats, calculateStatisticsSumGrowth(
types.grid![0].flow_from.map((flow) => flow.stat_energy_from) this._stats,
); types.grid![0].flow_from.map((flow) => flow.stat_energy_from)
) ?? 0;
if (totalGridConsumption === null) {
return html`Total consumption couldn't be calculated`;
}
let totalSolarProduction: number | null = null; let totalSolarProduction: number | null = null;
@ -84,10 +81,6 @@ class HuiEnergyUsageCard extends LitElement implements LovelaceCard {
this._stats, this._stats,
types.solar!.map((source) => source.stat_energy_from) types.solar!.map((source) => source.stat_energy_from)
); );
if (totalSolarProduction === null) {
return html`Total production couldn't be calculated`;
}
} }
let productionReturnedToGrid: number | null = null; let productionReturnedToGrid: number | null = null;
@ -97,10 +90,6 @@ class HuiEnergyUsageCard extends LitElement implements LovelaceCard {
this._stats, this._stats,
types.grid![0].flow_to.map((flow) => flow.stat_energy_to) 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 // total consumption = consumption_from_grid + solar_production - return_to_grid