From 39f550cf9f9647adfba5718f8145681579518798 Mon Sep 17 00:00:00 2001 From: Petar Petrov Date: Fri, 19 Dec 2025 19:47:18 +0200 Subject: [PATCH] Fix datetime handling in energy charts (#28345) * Fix datetime handling in energy charts * PR comment * Add detailedDailyData parameter to getSuggestedMax and update getCommonOptions * refactor --------- Co-authored-by: Bram Kragten --- .../energy/common/energy-chart-options.ts | 15 ++++++++--- .../energy/hui-power-sources-graph-card.ts | 25 +++++++++++-------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/panels/lovelace/cards/energy/common/energy-chart-options.ts b/src/panels/lovelace/cards/energy/common/energy-chart-options.ts index d49ac755ae..a2ce47a412 100644 --- a/src/panels/lovelace/cards/energy/common/energy-chart-options.ts +++ b/src/panels/lovelace/cards/energy/common/energy-chart-options.ts @@ -31,7 +31,11 @@ import { formatTime } from "../../../../../common/datetime/format_time"; import type { ECOption } from "../../../../../resources/echarts/echarts"; import { filterXSS } from "../../../../../common/util/xss"; -export function getSuggestedMax(dayDifference: number, end: Date): number { +export function getSuggestedMax( + dayDifference: number, + end: Date, + detailedDailyData = false +): number { let suggestedMax = new Date(end); // Sometimes around DST we get a time of 0:59 instead of 23:59 as expected. @@ -40,7 +44,9 @@ export function getSuggestedMax(dayDifference: number, end: Date): number { suggestedMax = subHours(suggestedMax, 1); } - suggestedMax.setMinutes(0, 0, 0); + if (!detailedDailyData) { + suggestedMax.setMinutes(0, 0, 0); + } if (dayDifference > 35) { suggestedMax.setDate(1); } @@ -77,7 +83,8 @@ export function getCommonOptions( unit?: string, compareStart?: Date, compareEnd?: Date, - formatTotal?: (total: number) => string + formatTotal?: (total: number) => string, + detailedDailyData = false ): ECOption { const dayDifference = differenceInDays(end, start); @@ -89,7 +96,7 @@ export function getCommonOptions( xAxis: { type: "time", min: start, - max: getSuggestedMax(dayDifference, end), + max: getSuggestedMax(dayDifference, end, detailedDailyData), }, yAxis: { type: "value", diff --git a/src/panels/lovelace/cards/energy/hui-power-sources-graph-card.ts b/src/panels/lovelace/cards/energy/hui-power-sources-graph-card.ts index cf63066b45..f7ea069f8f 100644 --- a/src/panels/lovelace/cards/energy/hui-power-sources-graph-card.ts +++ b/src/panels/lovelace/cards/energy/hui-power-sources-graph-card.ts @@ -1,4 +1,4 @@ -import { endOfToday, isToday, startOfToday } from "date-fns"; +import { endOfToday, isSameDay, 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"; @@ -132,7 +132,9 @@ export class HuiPowerSourcesGraphCard config, "kW", compareStart, - compareEnd + compareEnd, + undefined, + true ), legend: { show: this._config?.show_legend !== false, @@ -210,14 +212,17 @@ export class HuiPowerSourcesGraphCard const { positive, negative } = this._processData( statIds[key].stats.map((id: string) => { const stats = energyData.stats[id] ?? []; - const currentStateWatts = getPowerFromState(this.hass.states[id]); - if (currentStateWatts !== undefined) { - // getPowerFromState returns power in W; convert to kW for this graph - stats.push({ - start: now, - end: now, - mean: currentStateWatts / 1000, - }); + if (isSameDay(now, this._start) && isSameDay(now, this._end)) { + // Append current state if we are showing today + const currentStateWatts = getPowerFromState(this.hass.states[id]); + if (currentStateWatts !== undefined) { + // getPowerFromState returns power in W; convert to kW for this graph + stats.push({ + start: now, + end: now, + mean: currentStateWatts / 1000, + }); + } } return stats; })