From fc9da115a56b276f21bfc0f9b71b64d7811f2c3e Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 19 Feb 2023 16:26:50 -0600 Subject: [PATCH] fix query so sqlalc does not get confused --- homeassistant/components/recorder/statistics.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/recorder/statistics.py b/homeassistant/components/recorder/statistics.py index c6e32e06888..584217ed035 100644 --- a/homeassistant/components/recorder/statistics.py +++ b/homeassistant/components/recorder/statistics.py @@ -2007,7 +2007,7 @@ def _statistics_at_time( # https://github.com/sqlalchemy/sqlalchemy/issues/9189 # pylint: disable-next=not-callable func.max(table.start_ts).label("max_start_ts"), - table.metadata_id, + table.metadata_id.label("max_metadata_id"), ) ) .filter(table.start_ts < start_time_ts) @@ -2019,7 +2019,7 @@ def _statistics_at_time( stmt += lambda q: q.join( most_recent_statistic_ids, table.start_ts == most_recent_statistic_ids.c.max_start_ts - and table.metadata_id == most_recent_statistic_ids.c.metadata_id, + and table.metadata_id == most_recent_statistic_ids.c.max_metadata_id, ) return cast(Sequence[Row], execute_stmt_lambda_element(session, stmt))