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 = (
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;

View File

@ -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`<hui-warning>
${createEntityNotFoundWarning(this.hass, this._co2SignalEntity)}
</hui-warning>`;
}
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`
<ha-card>
<ha-gauge
min="0"
max="100"
.value=${value}
.locale=${this.hass!.locale}
label="%"
style=${styleMap({
"--gauge-color": this._computeSeverity(64),
})}
></ha-gauge>
<div class="name">High-carbon energy consumed</div>
<ha-card
>${value !== undefined
? html` <ha-gauge
min="0"
max="100"
.value=${value}
.locale=${this.hass!.locale}
label="%"
style=${styleMap({
"--gauge-color": this._computeSeverity(64),
})}
></ha-gauge>
<div class="name">High-carbon energy consumed</div>`
: html`Consumed high-carbon energy couldn't be calculated`}
</ha-card>
`;
}

View File

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

View File

@ -68,14 +68,11 @@ class HuiEnergyUsageCard extends LitElement implements LovelaceCard {
const hasSolarProduction = types.solar !== undefined;
const hasReturnToGrid = hasConsumption && types.grid![0].flow_to.length > 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