mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-28 03:36:44 +00:00
Add energy hourly calculations to CSV report (#25315)
This commit is contained in:
parent
9f6463eec0
commit
c178488aac
@ -22,11 +22,14 @@ import type {
|
|||||||
DeviceConsumptionEnergyPreference,
|
DeviceConsumptionEnergyPreference,
|
||||||
} from "../../data/energy";
|
} from "../../data/energy";
|
||||||
import {
|
import {
|
||||||
|
computeConsumptionData,
|
||||||
getEnergyDataCollection,
|
getEnergyDataCollection,
|
||||||
getEnergyGasUnit,
|
getEnergyGasUnit,
|
||||||
getEnergyWaterUnit,
|
getEnergyWaterUnit,
|
||||||
|
getSummedData,
|
||||||
} from "../../data/energy";
|
} from "../../data/energy";
|
||||||
import { fileDownload } from "../../util/file_download";
|
import { fileDownload } from "../../util/file_download";
|
||||||
|
import type { StatisticValue } from "../../data/recorder";
|
||||||
|
|
||||||
const ENERGY_LOVELACE_CONFIG: LovelaceConfig = {
|
const ENERGY_LOVELACE_CONFIG: LovelaceConfig = {
|
||||||
views: [
|
views: [
|
||||||
@ -177,18 +180,20 @@ class PanelEnergy extends LitElement {
|
|||||||
const csv: string[] = [];
|
const csv: string[] = [];
|
||||||
csv[0] = headers;
|
csv[0] = headers;
|
||||||
|
|
||||||
const processStat = function (stat: string, type: string, unit: string) {
|
const processCsvRow = function (
|
||||||
|
id: string,
|
||||||
|
type: string,
|
||||||
|
unit: string,
|
||||||
|
data: StatisticValue[]
|
||||||
|
) {
|
||||||
let n = 0;
|
let n = 0;
|
||||||
const row: string[] = [];
|
const row: string[] = [];
|
||||||
if (!stats[stat]) {
|
row.push(id);
|
||||||
return;
|
|
||||||
}
|
|
||||||
row.push(stat);
|
|
||||||
row.push(type);
|
row.push(type);
|
||||||
row.push(unit.normalize("NFKD"));
|
row.push(unit.normalize("NFKD"));
|
||||||
times.forEach((t) => {
|
times.forEach((t) => {
|
||||||
if (n < stats[stat].length && stats[stat][n].start === t) {
|
if (n < data.length && data[n].start === t) {
|
||||||
row.push((stats[stat][n].change ?? "").toString());
|
row.push((data[n].change ?? "").toString());
|
||||||
n++;
|
n++;
|
||||||
} else {
|
} else {
|
||||||
row.push("");
|
row.push("");
|
||||||
@ -197,6 +202,14 @@ class PanelEnergy extends LitElement {
|
|||||||
csv.push(row.join(",") + "\n");
|
csv.push(row.join(",") + "\n");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const processStat = function (stat: string, type: string, unit: string) {
|
||||||
|
if (!stats[stat]) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
processCsvRow(stat, type, unit, stats[stat]);
|
||||||
|
};
|
||||||
|
|
||||||
const currency = this.hass.config.currency;
|
const currency = this.hass.config.currency;
|
||||||
|
|
||||||
const printCategory = function (
|
const printCategory = function (
|
||||||
@ -335,6 +348,99 @@ class PanelEnergy extends LitElement {
|
|||||||
|
|
||||||
printCategory("device_consumption", devices, electricUnit);
|
printCategory("device_consumption", devices, electricUnit);
|
||||||
|
|
||||||
|
const { summedData, compareSummedData: _ } = getSummedData(
|
||||||
|
energyData.state
|
||||||
|
);
|
||||||
|
const { consumption, compareConsumption: __ } = computeConsumptionData(
|
||||||
|
summedData,
|
||||||
|
undefined
|
||||||
|
);
|
||||||
|
|
||||||
|
const processConsumptionData = function (
|
||||||
|
type: string,
|
||||||
|
unit: string,
|
||||||
|
data: Record<number, number>
|
||||||
|
) {
|
||||||
|
const data2: StatisticValue[] = [];
|
||||||
|
|
||||||
|
Object.entries(data).forEach(([t, value]) => {
|
||||||
|
data2.push({
|
||||||
|
start: Number(t),
|
||||||
|
end: NaN,
|
||||||
|
change: value,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
processCsvRow("", type, unit, data2);
|
||||||
|
};
|
||||||
|
|
||||||
|
const hasSolar = !!solar_productions.length;
|
||||||
|
const hasBattery = !!battery_ins.length;
|
||||||
|
const hasGridReturn = !!grid_productions.length;
|
||||||
|
const hasGridSource = !!grid_consumptions.length;
|
||||||
|
|
||||||
|
if (hasGridSource) {
|
||||||
|
processConsumptionData(
|
||||||
|
"calculated_consumed_grid",
|
||||||
|
electricUnit,
|
||||||
|
consumption.used_grid
|
||||||
|
);
|
||||||
|
if (hasBattery) {
|
||||||
|
processConsumptionData(
|
||||||
|
"calculated_grid_to_battery",
|
||||||
|
electricUnit,
|
||||||
|
consumption.grid_to_battery
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (hasGridReturn && hasBattery) {
|
||||||
|
processConsumptionData(
|
||||||
|
"calculated_battery_to_grid",
|
||||||
|
electricUnit,
|
||||||
|
consumption.battery_to_grid
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (hasBattery) {
|
||||||
|
processConsumptionData(
|
||||||
|
"calculated_consumed_battery",
|
||||||
|
electricUnit,
|
||||||
|
consumption.used_battery
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasSolar) {
|
||||||
|
processConsumptionData(
|
||||||
|
"calculated_consumed_solar",
|
||||||
|
electricUnit,
|
||||||
|
consumption.used_solar
|
||||||
|
);
|
||||||
|
if (hasBattery) {
|
||||||
|
processConsumptionData(
|
||||||
|
"calculated_solar_to_battery",
|
||||||
|
electricUnit,
|
||||||
|
consumption.solar_to_battery
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (hasGridReturn) {
|
||||||
|
processConsumptionData(
|
||||||
|
"calculated_solar_to_grid",
|
||||||
|
electricUnit,
|
||||||
|
consumption.solar_to_grid
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
(hasGridSource ? 1 : 0) + (hasSolar ? 1 : 0) + (hasBattery ? 1 : 0) >
|
||||||
|
1
|
||||||
|
) {
|
||||||
|
processConsumptionData(
|
||||||
|
"calculated_total_consumption",
|
||||||
|
electricUnit,
|
||||||
|
consumption.used_total
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const blob = new Blob(csv, {
|
const blob = new Blob(csv, {
|
||||||
type: "text/csv",
|
type: "text/csv",
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user