diff --git a/homeassistant/components/recorder/queries.py b/homeassistant/components/recorder/queries.py index 8ca7bef2691..34e9ec32f99 100644 --- a/homeassistant/components/recorder/queries.py +++ b/homeassistant/components/recorder/queries.py @@ -640,9 +640,9 @@ def find_states_to_purge( def find_oldest_state() -> StatementLambdaElement: """Find the last_updated_ts of the oldest state.""" return lambda_stmt( - lambda: select(States.last_updated_ts).where( - States.state_id.in_(select(func.min(States.state_id))) - ) + lambda: select(States.last_updated_ts) + .order_by(States.last_updated_ts.asc()) + .limit(1) )