From 8e4e22b6f86ce8efaadf2b6fb942fa5cc1c719e9 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Tue, 24 May 2022 17:20:16 +0200 Subject: [PATCH] Add compare to energy sources table (#12762) --- .../energy/hui-energy-sources-table-card.ts | 295 ++++++++++++++++++ src/translations/en.json | 2 + 2 files changed, 297 insertions(+) diff --git a/src/panels/lovelace/cards/energy/hui-energy-sources-table-card.ts b/src/panels/lovelace/cards/energy/hui-energy-sources-table-card.ts index e3bce86105..eee7891937 100644 --- a/src/panels/lovelace/cards/energy/hui-energy-sources-table-card.ts +++ b/src/panels/lovelace/cards/energy/hui-energy-sources-table-card.ts @@ -83,6 +83,13 @@ export class HuiEnergySourcesTableCard let totalGas = 0; let totalGasCost = 0; + let totalGridCompare = 0; + let totalGridCostCompare = 0; + let totalSolarCompare = 0; + let totalBatteryCompare = 0; + let totalGasCompare = 0; + let totalGasCostCompare = 0; + const types = energySourcesByType(this._data.prefs); const computedStyles = getComputedStyle(this); @@ -123,6 +130,8 @@ export class HuiEnergySourcesTableCard const gasUnit = getEnergyGasUnit(this.hass, this._data.prefs) || ""; + const compare = this._data.statsCompare !== undefined; + return html` ${this._config.title ? html`

${this._config.title}

` @@ -142,6 +151,28 @@ export class HuiEnergySourcesTableCard "ui.panel.lovelace.cards.energy.energy_sources_table.source" )} + ${compare + ? html` + ${this.hass.localize( + "ui.panel.lovelace.cards.energy.energy_sources_table.previous_energy" + )} + + ${showCosts + ? html` + ${this.hass.localize( + "ui.panel.lovelace.cards.energy.energy_sources_table.previous_cost" + )} + ` + : ""}` + : ""} 0 ? this.hass.themes.darkMode @@ -198,6 +237,16 @@ export class HuiEnergySourcesTableCard ? computeStateName(entity) : source.stat_energy_from} + ${compare + ? html` + ${formatNumber(compareEnergy, this.hass.locale)} kWh + + ${showCosts + ? html`` + : ""}` + : ""} @@ -214,6 +263,17 @@ export class HuiEnergySourcesTableCard Solar total + ${compare + ? html` + ${formatNumber(totalSolarCompare, this.hass.locale)} + kWh + + ${showCosts + ? html`` + : ""}` + : ""} @@ -237,6 +297,20 @@ export class HuiEnergySourcesTableCard ) || 0; totalBattery += energyFrom - energyTo; + const energyFromCompare = + (compare && + calculateStatisticSumGrowth( + this._data!.statsCompare[source.stat_energy_from] + )) || + 0; + const energyToCompare = + (compare && + calculateStatisticSumGrowth( + this._data!.statsCompare[source.stat_energy_to] + )) || + 0; + totalBatteryCompare += energyFromCompare - energyToCompare; + const modifiedFromColor = idx > 0 ? this.hass.themes.darkMode @@ -271,6 +345,17 @@ export class HuiEnergySourcesTableCard ? computeStateName(entityFrom) : source.stat_energy_from} + ${compare + ? html` + ${formatNumber(energyFromCompare, this.hass.locale)} + kWh + + ${showCosts + ? html`` + : ""}` + : ""} @@ -295,6 +380,20 @@ export class HuiEnergySourcesTableCard ? computeStateName(entityTo) : source.stat_energy_from} + ${compare + ? html` + ${formatNumber( + energyToCompare * -1, + this.hass.locale + )} + kWh + + ${showCosts + ? html`` + : ""}` + : ""} @@ -313,6 +412,20 @@ export class HuiEnergySourcesTableCard "ui.panel.lovelace.cards.energy.energy_sources_table.battery_total" )} + ${compare + ? html` + ${formatNumber( + totalBatteryCompare, + this.hass.locale + )} + kWh + + ${showCosts + ? html`` + : ""}` + : ""} @@ -331,6 +444,15 @@ export class HuiEnergySourcesTableCard this._data!.stats[flow.stat_energy_from] ) || 0; totalGrid += energy; + + const compareEnergy = + (compare && + calculateStatisticSumGrowth( + this._data!.statsCompare[flow.stat_energy_from] + )) || + 0; + totalGridCompare += compareEnergy; + const cost_stat = flow.stat_cost || this._data!.info.cost_sensors[flow.stat_energy_from]; @@ -343,6 +465,16 @@ export class HuiEnergySourcesTableCard totalGridCost += cost; } + const costCompare = + compare && cost_stat + ? calculateStatisticSumGrowth( + this._data!.statsCompare[cost_stat] + ) || 0 + : null; + if (costCompare !== null) { + totalGridCostCompare += costCompare; + } + const modifiedColor = idx > 0 ? this.hass.themes.darkMode @@ -368,6 +500,29 @@ export class HuiEnergySourcesTableCard ? computeStateName(entity) : flow.stat_energy_from} + ${compare + ? html` + ${formatNumber(compareEnergy, this.hass.locale)} kWh + + ${showCosts + ? html` + ${costCompare !== null + ? formatNumber( + costCompare, + this.hass.locale, + { + style: "currency", + currency: this.hass.config.currency!, + } + ) + : ""} + ` + : ""}` + : ""} @@ -406,6 +561,24 @@ export class HuiEnergySourcesTableCard totalGridCost += cost; } + const energyCompare = + ((compare && + calculateStatisticSumGrowth( + this._data!.statsCompare[flow.stat_energy_to] + )) || + 0) * -1; + totalGridCompare += energyCompare; + + const costCompare = + compare && cost_stat + ? (calculateStatisticSumGrowth( + this._data!.statsCompare[cost_stat] + ) || 0) * -1 + : null; + if (costCompare !== null) { + totalGridCostCompare += costCompare; + } + const modifiedColor = idx > 0 ? this.hass.themes.darkMode @@ -429,6 +602,29 @@ export class HuiEnergySourcesTableCard ${entity ? computeStateName(entity) : flow.stat_energy_to} + ${compare + ? html` + ${formatNumber(energyCompare, this.hass.locale)} kWh + + ${showCosts + ? html` + ${costCompare !== null + ? formatNumber( + costCompare, + this.hass.locale, + { + style: "currency", + currency: this.hass.config.currency!, + } + ) + : ""} + ` + : ""}` + : ""} @@ -457,6 +653,28 @@ export class HuiEnergySourcesTableCard "ui.panel.lovelace.cards.energy.energy_sources_table.grid_total" )} + ${compare + ? html` + ${formatNumber(totalGridCompare, this.hass.locale)} + kWh + + ${showCosts + ? html` + ${formatNumber( + totalGridCostCompare, + this.hass.locale, + { + style: "currency", + currency: this.hass.config.currency!, + } + )} + ` + : ""}` + : ""} @@ -482,6 +700,14 @@ export class HuiEnergySourcesTableCard ) || 0; totalGas += energy; + const energyCompare = + (compare && + calculateStatisticSumGrowth( + this._data!.statsCompare[source.stat_energy_from] + )) || + 0; + totalGasCompare += energyCompare; + const cost_stat = source.stat_cost || this._data!.info.cost_sensors[source.stat_energy_from]; @@ -493,6 +719,16 @@ export class HuiEnergySourcesTableCard totalGasCost += cost; } + const costCompare = + compare && cost_stat + ? calculateStatisticSumGrowth( + this._data!.statsCompare[cost_stat] + ) || 0 + : null; + if (costCompare !== null) { + totalGasCostCompare += costCompare; + } + const modifiedColor = idx > 0 ? this.hass.themes.darkMode @@ -518,6 +754,26 @@ export class HuiEnergySourcesTableCard ? computeStateName(entity) : source.stat_energy_from} + ${compare + ? html` + ${formatNumber(energyCompare, this.hass.locale)} + ${gasUnit} + + ${showCosts + ? html` + ${costCompare !== null + ? formatNumber(costCompare, this.hass.locale, { + style: "currency", + currency: this.hass.config.currency!, + }) + : ""} + ` + : ""}` + : ""} @@ -545,6 +801,28 @@ export class HuiEnergySourcesTableCard "ui.panel.lovelace.cards.energy.energy_sources_table.gas_total" )} + ${compare + ? html` + ${formatNumber(totalGasCompare, this.hass.locale)} + ${gasUnit} + + ${showCosts + ? html` + ${formatNumber( + totalGasCostCompare, + this.hass.locale, + { + style: "currency", + currency: this.hass.config.currency!, + } + )} + ` + : ""}` + : ""} @@ -570,6 +848,23 @@ export class HuiEnergySourcesTableCard "ui.panel.lovelace.cards.energy.energy_sources_table.total_costs" )} + ${compare + ? html` + ${formatNumber( + totalGasCostCompare + totalGridCostCompare, + this.hass.locale, + { + style: "currency", + currency: this.hass.config.currency!, + } + )} + + ${showCosts + ? html`` + : ""}` + : ""}