Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions src/data/energy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,16 +449,9 @@ const getEnergyData = async (
const allStatIDs = [...energyStatIds, ...waterStatIds, ...powerStatIds];

const dayDifference = differenceInDays(end || new Date(), start);
const period =
isFirstDayOfMonth(start) &&
(!end || isLastDayOfMonth(end)) &&
dayDifference > 35
? "month"
: dayDifference > 2
? "day"
: "hour";
const finePeriod =
dayDifference > 64 ? "day" : dayDifference > 8 ? "hour" : "5minute";

const period = getSuggestedPeriod(start, end);
const finePeriod = getSuggestedPeriod(start, end, true);

const statsMetadata: Record<string, StatisticsMetaData> = {};
const statsMetadataArray = allStatIDs.length
Expand Down Expand Up @@ -589,7 +582,7 @@ const getEnergyData = async (
consumptionStatIDs,
co2SignalEntity,
end,
dayDifference > 35 ? "month" : dayDifference > 2 ? "day" : "hour"
period
);
if (compare) {
_fossilEnergyConsumptionCompare = getFossilEnergyConsumption(
Expand All @@ -598,7 +591,7 @@ const getEnergyData = async (
consumptionStatIDs,
co2SignalEntity,
endCompare,
dayDifference > 35 ? "month" : dayDifference > 2 ? "day" : "hour"
period
);
}
}
Expand Down Expand Up @@ -1427,3 +1420,22 @@ export const formatPowerShort = (
units[unitIndex]
);
};

export function getSuggestedPeriod(
start: Date,
end?: Date,
fine = false
): "5minute" | "hour" | "day" | "month" {
const dayDifference = differenceInDays(end || new Date(), start);

if (fine) {
return dayDifference > 64 ? "day" : dayDifference > 8 ? "hour" : "5minute";
}
return isFirstDayOfMonth(start) &&
(!end || isLastDayOfMonth(end)) &&
dayDifference > 35
? "month"
: dayDifference > 2
? "day"
: "hour";
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { formatTime } from "../../../../../common/datetime/format_time";
import type { ECOption } from "../../../../../resources/echarts/echarts";
import { filterXSS } from "../../../../../common/util/xss";
import type { StatisticPeriod } from "../../../../../data/recorder";
import { getSuggestedPeriod } from "../../../../../data/energy";

export function getSuggestedMax(period: StatisticPeriod, end: Date): number {
let suggestedMax = new Date(end);
Expand All @@ -56,10 +57,6 @@ export function getSuggestedMax(period: StatisticPeriod, end: Date): number {
return suggestedMax.getTime();
}

export function getSuggestedPeriod(dayDifference: number): StatisticPeriod {
return dayDifference > 35 ? "month" : dayDifference > 2 ? "day" : "hour";
}

function createYAxisLabelFormatter(locale: FrontendLocaleData) {
let previousValue: number | undefined;

Expand Down Expand Up @@ -95,7 +92,7 @@ export function getCommonOptions(
type: "time",
min: start,
max: getSuggestedMax(
detailedDailyData ? "5minute" : getSuggestedPeriod(dayDifference),
getSuggestedPeriod(start, end, detailedDailyData),
end
),
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { differenceInDays, endOfToday, isToday, startOfToday } from "date-fns";
import { endOfToday, isToday, startOfToday } from "date-fns";
import type { HassConfig, UnsubscribeFunc } from "home-assistant-js-websocket";
import type { PropertyValues } from "lit";
import { css, html, LitElement, nothing } from "lit";
Expand All @@ -18,6 +18,7 @@ import type {
import {
getEnergyDataCollection,
getEnergySolarForecasts,
getSuggestedPeriod,
} from "../../../../data/energy";
import type { Statistics, StatisticsMetaData } from "../../../../data/recorder";
import { getStatisticLabel } from "../../../../data/recorder";
Expand Down Expand Up @@ -354,7 +355,7 @@ export class HuiEnergySolarGraphCard
) {
const data: LineSeriesOption[] = [];

const dayDifference = differenceInDays(end || new Date(), start);
const period = getSuggestedPeriod(start, end);

// Process solar forecast data.
solarSources.forEach((source) => {
Expand All @@ -370,10 +371,10 @@ export class HuiEnergySolarGraphCard
if (dateObj < start || (end && dateObj > end)) {
return;
}
if (dayDifference > 35) {
if (period === "month") {
dateObj.setDate(1);
}
if (dayDifference > 2) {
if (period === "month" || period === "day") {
dateObj.setHours(0, 0, 0, 0);
} else {
dateObj.setMinutes(0, 0, 0);
Expand Down
14 changes: 6 additions & 8 deletions src/panels/lovelace/cards/hui-statistics-graph-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { createSearchParam } from "../../../common/url/search-params";
import "../../../components/ha-card";
import "../../../components/ha-icon-next";
import "../../../components/ha-tooltip";
import { getEnergyDataCollection } from "../../../data/energy";
import {
getEnergyDataCollection,
getSuggestedPeriod,
} from "../../../data/energy";
import type {
Statistics,
StatisticsMetaData,
Expand All @@ -26,10 +29,7 @@ import { hasConfigOrEntitiesChanged } from "../common/has-changed";
import { processConfigEntities } from "../common/process-config-entities";
import type { EntityConfig } from "../entity-rows/types";
import type { LovelaceCard, LovelaceGridOptions } from "../types";
import {
getSuggestedMax,
getSuggestedPeriod,
} from "./energy/common/energy-chart-options";
import { getSuggestedMax } from "./energy/common/energy-chart-options";
import type { StatisticsGraphCardConfig } from "./types";

export const DEFAULT_DAYS_TO_SHOW = 30;
Expand Down Expand Up @@ -268,9 +268,7 @@ export class HuiStatisticsGraphCard extends LitElement implements LovelaceCard {
return (
this._config?.period ??
(this._energyStart && this._energyEnd
? getSuggestedPeriod(
differenceInDays(this._energyEnd, this._energyStart)
)
? getSuggestedPeriod(this._energyStart, this._energyEnd)
: undefined)
);
}
Expand Down
Loading