diff --git a/src/data/history.ts b/src/data/history.ts index b688650603..94e821aa3d 100644 --- a/src/data/history.ts +++ b/src/data/history.ts @@ -464,6 +464,15 @@ export const convertStatisticsToHistory = ( return statisticsHistory; }; +export const limitedHistoryFromStateObj = ( + state: HassEntity +): EntityHistoryState[] => [ + { + s: state.state, + a: state.attributes, + lu: new Date(state.last_updated).getTime() / 1000, + }, +]; export const computeHistory = ( hass: HomeAssistant, stateHistory: HistoryStates, @@ -484,13 +493,9 @@ export const computeHistory = ( if (entity in stateHistory) { localStateHistory[entity] = stateHistory[entity]; } else if (hass.states[entity]) { - localStateHistory[entity] = [ - { - s: hass.states[entity].state, - a: hass.states[entity].attributes, - lu: new Date(hass.states[entity].last_updated).getTime() / 1000, - }, - ]; + localStateHistory[entity] = limitedHistoryFromStateObj( + hass.states[entity] + ); } }); diff --git a/src/panels/lovelace/card-features/hui-trend-graph-card-feature.ts b/src/panels/lovelace/card-features/hui-trend-graph-card-feature.ts index 5ba1c815b0..2329ef0e0f 100644 --- a/src/panels/lovelace/card-features/hui-trend-graph-card-feature.ts +++ b/src/panels/lovelace/card-features/hui-trend-graph-card-feature.ts @@ -4,7 +4,10 @@ import { isComponentLoaded } from "../../../common/config/is_component_loaded"; import { computeDomain } from "../../../common/entity/compute_domain"; import { isNumericFromAttributes } from "../../../common/number/format_number"; import "../../../components/ha-spinner"; -import { subscribeHistoryStatesTimeWindow } from "../../../data/history"; +import { + limitedHistoryFromStateObj, + subscribeHistoryStatesTimeWindow, +} from "../../../data/history"; import { SubscribeMixin } from "../../../mixins/subscribe-mixin"; import type { HomeAssistant } from "../../../types"; import { coordinatesMinimalResponseCompressedState } from "../common/graph/coordinates"; @@ -127,6 +130,14 @@ class HuiHistoryChartCardFeature return subscribeHistoryStatesTimeWindow( this.hass!, (historyStates) => { + const entityId = this.context!.entity_id!; + let history = historyStates[entityId]; + if (!history?.length) { + const stateObj = this.hass!.states[entityId]; + if (stateObj) { + history = limitedHistoryFromStateObj(stateObj); + } + } // sample to 1 point per hour for low detail or 1 point per 5 pixels for high detail const maxDetails = detail ? Math.max(10, this.clientWidth / 5, hourToShow) @@ -134,7 +145,7 @@ class HuiHistoryChartCardFeature const useMean = !detail; const { points, yAxisOrigin } = coordinatesMinimalResponseCompressedState( - historyStates[this.context!.entity_id!], + history, this.clientWidth, this.clientHeight, maxDetails, diff --git a/src/panels/lovelace/header-footer/hui-graph-header-footer.ts b/src/panels/lovelace/header-footer/hui-graph-header-footer.ts index 44e4d6b613..7bffffdc73 100644 --- a/src/panels/lovelace/header-footer/hui-graph-header-footer.ts +++ b/src/panels/lovelace/header-footer/hui-graph-header-footer.ts @@ -7,7 +7,10 @@ import { fireEvent } from "../../../common/dom/fire_event"; import { computeDomain } from "../../../common/entity/compute_domain"; import "../../../components/ha-spinner"; import type { HistoryStates } from "../../../data/history"; -import { subscribeHistoryStatesTimeWindow } from "../../../data/history"; +import { + limitedHistoryFromStateObj, + subscribeHistoryStatesTimeWindow, +} from "../../../data/history"; import type { HomeAssistant } from "../../../types"; import { findEntities } from "../common/find-entities"; import { coordinatesMinimalResponseCompressedState } from "../common/graph/coordinates"; @@ -165,6 +168,13 @@ export class HuiGraphHeaderFooter return; } this._history = combinedHistory; + if (!this._history[this._config.entity]?.length) { + const stateObj = this.hass!.states[this._config.entity]; + if (stateObj) { + this._history[this._config.entity] = + limitedHistoryFromStateObj(stateObj); + } + } this._computeCoordinates(); }, this._config.hours_to_show!,