Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Statistics Graphs card showing only "Loading statistics..." after 2023.3.3 update #89489

Closed
erikgeurts opened this issue Mar 10, 2023 · 14 comments · Fixed by #89650
Closed

Statistics Graphs card showing only "Loading statistics..." after 2023.3.3 update #89489

erikgeurts opened this issue Mar 10, 2023 · 14 comments · Fixed by #89650

Comments

@erikgeurts
Copy link

The problem

I updated to 2023.3.3 late last night Europe/Amsterdam time (very soon after it was released) and now I find that the Statistics Graph card is having issues displaying the stats. This happens to all cards of this type. Example:
image

The entity itself is working well and reporting, also in the History:
image

I did not change anything to the way the cards are defined, example yaml below. The issue occurs in the web app, both local and over a Cloudflared tunnel (and thus also on my Android app).

The first occurence of the log entry I found (and 148 similar items) was indeed a few seconds after the update to 2023.3.3. The log entries appear to me (non coder) to point to a bug. I haven't looked at the code and the indicated line numbers, I'm not sufficiently familiar with Python.

What version of Home Assistant Core has the issue?

2023.3.3

What was the last working version of Home Assistant Core?

2023.3.2

What type of installation are you running?

Home Assistant OS

Integration causing the issue

n/a (I think)

Link to integration documentation on our website

No response

Diagnostics information

No response

Example YAML snippet

chart_type: bar
period: hour
days_to_show: 1
type: statistics-graph
entities:
  - sensor.p1_meter_3c39e728d3a6_total_power_import
  - sensor.p1_meter_3c39e728d3a6_total_power_export
stat_types:
  - change
title: Last 24 hours

Anything in the logs that might be useful for us?

Logger: homeassistant.components.websocket_api.http.connection
Source: components/recorder/statistics.py:2076
Integration: Home Assistant WebSocket API (documentation, issues)
First occurred: March 9, 2023 at 23:10:16 (148 occurrences)
Last logged: 09:07:38

[140131671014176] Error handling message: Unknown error (unknown_error) <username redacted> from <IP address redacted> (Mozilla/5.0 (Linux; Android 13; Pixel 5 Build/TQ1A.230205.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/110.0.5481.154 Mobile Safari/537.36 Home Assistant/2023.1.1-3124 (Android 13; Pixel 5))
[140131670678960] Error handling message: Unknown error (unknown_error) <username redacted> from <IP address redacted> (Mozilla/5.0 (Linux; Android 13; Pixel 5 Build/TQ1A.230205.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/110.0.5481.154 Mobile Safari/537.36 Home Assistant/2023.1.1-3124 (Android 13; Pixel 5))
[140131506440320] Error handling message: Unknown error (unknown_error) <username redacted> from <IP address redacted> (Mozilla/5.0 (Linux; Android 13; Pixel 5 Build/TQ1A.230205.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/110.0.5481.154 Mobile Safari/537.36 Home Assistant/2023.1.1-3124 (Android 13; Pixel 5))
[140131507289152] Error handling message: Unknown error (unknown_error) <username redacted> from <IP address redacted> (Mozilla/5.0 (Linux; Android 13; Pixel 5 Build/TQ1A.230205.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/110.0.5481.154 Mobile Safari/537.36 Home Assistant/2023.1.1-3124 (Android 13; Pixel 5))
[140131523464208] Error handling message: Unknown error (unknown_error) <username redacted> from 192.168.86.50 (Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/decorators.py", line 26, in _handle_async_response
    await func(hass, connection, msg)
  File "/usr/src/homeassistant/homeassistant/components/recorder/websocket_api.py", line 238, in ws_get_statistics_during_period
    await ws_handle_get_statistics_during_period(hass, connection, msg)
  File "/usr/src/homeassistant/homeassistant/components/recorder/websocket_api.py", line 205, in ws_handle_get_statistics_during_period
    await get_instance(hass).async_add_executor_job(
  File "/usr/local/lib/python3.10/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/components/recorder/websocket_api.py", line 160, in _ws_get_statistics_during_period
    result = statistics_during_period(
  File "/usr/src/homeassistant/homeassistant/components/recorder/statistics.py", line 1818, in statistics_during_period
    return _statistics_during_period_with_session(
  File "/usr/src/homeassistant/homeassistant/components/recorder/statistics.py", line 1768, in _statistics_during_period_with_session
    return _sorted_statistics_to_dict(
  File "/usr/src/homeassistant/homeassistant/components/recorder/statistics.py", line 2076, in _sorted_statistics_to_dict
    seen_statistic_ids.add(metadata[meta_id]["statistic_id"])
KeyError: 5

Additional information

No response

@erikgeurts
Copy link
Author

I was fooling around a bit with the 'broken' card on one of my dashboards, and on a whim I changed the reporting interval from Hour to 5 Minutes, and on another from Day to 5 Minutes, and to my surprise, on the same card that was just displaying "Loading statistics...", suddenly a graph appeared again.

It's not very useful to have a 30 day overview with 5 minute intervals, but it seems to be an indication that something goes wrong when aggregating the 'raw' numbers into the higher levels of aggregation.

@home-assistant
Copy link

Hey there @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (recorder) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of recorder can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Renames the issue.
  • @home-assistant reopen Reopen the issue.
  • @home-assistant unassign recorder Removes the current integration label and assignees on the issue, add the integration domain after the command.

(message by CodeOwnersMention)


recorder documentation
recorder source
(message by IssueLinks)

@bdraco
Copy link
Member

bdraco commented Mar 11, 2023

Probably the same root cause as #89170

@bdraco
Copy link
Member

bdraco commented Mar 11, 2023

We will push a new version with sqlalchemy 2.0.6 when it's released which has sqlalchemy/sqlalchemy#9461 fixed

@erikgeurts
Copy link
Author

Thanks for the note, good to know there is a fix on the horizon. Feel free to let me know if I can help with testing. Either way, I will report back here if the issue is fixed after a future release with that new version of sqlalchemy included.

@erikgeurts
Copy link
Author

Much to my surprise, the affected Statistics Graphs cards have suddenly started to work again. I haven't made any changes to them, so I don't fully understand how this can have happened.

The only thing that I can think about that may have contributed to this, is that I had to do a restart of Home Assistant for an entirely unrelated reason.

Perhaps it was a data-related thing all along, some kind of data migration that didn't start or didn't complete.

Perhaps I should have tried if restarting would have fixed it in the first place, but I'm fairly new to Home Assistant, and as such somewhat apprehensive to do anything that might have unintended consequences.

I apologize to anyone whose time I may have wasted.

@bdraco
Copy link
Member

bdraco commented Mar 12, 2023

It's a entirely expected that it only happens sometimes since it's a concurrency problem with sqlalchemy. You have to have some bad luck with two or more queries of the same type happening to run at the same time with different branching and the query cache gets in a bad state.

@mietzen
Copy link

mietzen commented Mar 13, 2023

I got the same Issue, some statistics work some doesn't. After restarting the host some of the not working statics worked again but others don't. I guess this proves @bdraco theory

@lmagyar
Copy link
Contributor

lmagyar commented Mar 13, 2023

Testing #89170 I've run into this problem, but with a different HA log error. It seems to me, that there can be a pure core issue also, not only sqlalchemy, but this is only a guess on my side. If sqlalchemy can explain this, forget about my comment.

With core 2023.3.3 I have no visible errors, even after multiple days and 10+ restarts.

I've restored core 2023.3.2, and I've got this "never loading statistics" error.

There are strange errors in the HA log on each browser refresh:

  • these can be there even after a core restart when everything seems later working, and later these errors are not repeated, only a few error during startup
  • when I restart core from a non-problematic state into another non-problematic state, these errors disappear
  • so it seems a previous problematic core state has some effect on the next non-problematic core state during startup, really strange
  • note the TypeError: '<' not supported between instances of 'str' and 'NoneType'
2023-03-13 12:13:30.406 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [1591644136] Error handling message: Unknown error (unknown_error) Admin from 192.168.1.100 (Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/connection.py", line 117, in async_handle
    handler(self.hass, self, schema(msg))
  File "/usr/local/lib/python3.10/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.10/site-packages/voluptuous/schema_builder.py", line 595, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/usr/local/lib/python3.10/site-packages/voluptuous/schema_builder.py", line 387, in validate_mapping
    cval = cvalue(key_path, value)
  File "/usr/local/lib/python3.10/site-packages/voluptuous/schema_builder.py", line 818, in validate_callable
    return schema(data)
  File "/usr/local/lib/python3.10/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.10/site-packages/voluptuous/schema_builder.py", line 595, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/usr/local/lib/python3.10/site-packages/voluptuous/schema_builder.py", line 387, in validate_mapping
    cval = cvalue(key_path, value)
  File "/usr/local/lib/python3.10/site-packages/voluptuous/schema_builder.py", line 818, in validate_callable
    return schema(data)
  File "/usr/local/lib/python3.10/site-packages/voluptuous/validators.py", line 755, in __call__
    or 'value must be one of {}'.format(sorted(self.container)))
TypeError: '<' not supported between instances of 'str' and 'NoneType'

And the queries for these statistics graphs never reach the database:

Click to open
  • there are 6 graphs: 3 different sensor groups, each with a 30 day mean and a 180 day mean/min/max chart, so 2x3 charts
  • the 3 sensor groups metadata ids start with 1, 5, 8
  • though only 4 charts are loaded, you never see a query with WHERE ... IN (8, ...)
  • but previously I saw other statistics charts being unable to load, the failure is undeterministic (certainly)
230313 12:04:46	    68 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.living_room_temperature_average', 'sensor.working_room_temperature_average', 'sensor.dining_room_temperature_average', 'sensor.bedroom_temperature_average')
		    68 Query	COMMIT
		    68 Query	ROLLBACK
		    69 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.average_target_temperature', 'sensor.average_current_temperature', 'sensor.average_outside_temperature', 'sensor.spare_temperature_average', 'sensor.average_wind_speed', 'sensor.forward_water_temperature')
		    65 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.living_room_humidity_average', 'sensor.working_room_humidity_average', 'sensor.dining_room_humidity_average', 'sensor.bedroom_humidity_average', 'sensor.spare_humidity_average')
		    67 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.living_room_temperature_average', 'sensor.working_room_temperature_average', 'sensor.dining_room_temperature_average', 'sensor.bedroom_temperature_average')
		    65 Query	COMMIT
		    69 Query	COMMIT
		    69 Query	ROLLBACK
		    66 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.average_target_temperature', 'sensor.average_current_temperature', 'sensor.average_outside_temperature', 'sensor.spare_temperature_average', 'sensor.average_wind_speed', 'sensor.forward_water_temperature')
		    65 Query	ROLLBACK
		    66 Query	COMMIT
		    68 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.living_room_humidity_average', 'sensor.working_room_humidity_average', 'sensor.dining_room_humidity_average', 'sensor.bedroom_humidity_average', 'sensor.spare_humidity_average')
		    67 Query	COMMIT
		    68 Query	COMMIT
		    66 Query	ROLLBACK
		    67 Query	ROLLBACK
		    68 Query	ROLLBACK
		    65 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.living_room_temperature_average', 'sensor.working_room_temperature_average', 'sensor.dining_room_temperature_average', 'sensor.bedroom_temperature_average')
		    65 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.mean 
FROM statistics 
WHERE statistics.start_ts >= 1676109891.379e0 AND statistics.metadata_id IN (1, 2, 3, 4) ORDER BY statistics.metadata_id, statistics.start_ts
		    69 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.average_target_temperature', 'sensor.average_current_temperature', 'sensor.average_outside_temperature', 'sensor.spare_temperature_average', 'sensor.average_wind_speed', 'sensor.forward_water_temperature')
		    66 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.average_target_temperature', 'sensor.average_current_temperature', 'sensor.average_outside_temperature', 'sensor.spare_temperature_average', 'sensor.average_wind_speed', 'sensor.forward_water_temperature')
		    69 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.mean 
FROM statistics 
WHERE statistics.start_ts >= 1676109891.402e0 AND statistics.metadata_id IN (5, 6, 7, 12, 167, 168) ORDER BY statistics.metadata_id, statistics.start_ts
		    67 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.living_room_temperature_average', 'sensor.working_room_temperature_average', 'sensor.dining_room_temperature_average', 'sensor.bedroom_temperature_average')
		    67 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.max, statistics.mean, statistics.min 
FROM statistics 
WHERE statistics.start_ts >= 1663149891.414e0 AND statistics.metadata_id IN (1, 2, 3, 4) ORDER BY statistics.metadata_id, statistics.start_ts
		    66 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.max, statistics.mean, statistics.min 
FROM statistics 
WHERE statistics.start_ts >= 1663149891.413e0 AND statistics.metadata_id IN (5, 6, 7, 12, 167, 168) ORDER BY statistics.metadata_id, statistics.start_ts
		    65 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.mean 
FROM statistics INNER JOIN (SELECT max(statistics.start_ts) AS max_start_ts, statistics.metadata_id AS max_metadata_id 
FROM statistics 
WHERE statistics.start_ts < 1676109891.379e0 AND statistics.metadata_id IN (1, 2, 3, 4) GROUP BY statistics.metadata_id) AS anon_1 ON statistics.start_ts = anon_1.max_start_ts AND statistics.metadata_id = anon_1.max_metadata_id
		    69 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.mean 
FROM statistics INNER JOIN (SELECT max(statistics.start_ts) AS max_start_ts, statistics.metadata_id AS max_metadata_id 
FROM statistics 
WHERE statistics.start_ts < 1676109891.402e0 AND statistics.metadata_id IN (5, 6, 167, 7, 168, 12) GROUP BY statistics.metadata_id) AS anon_1 ON statistics.start_ts = anon_1.max_start_ts AND statistics.metadata_id = anon_1.max_metadata_id
230313 12:04:47	    69 Query	COMMIT
		    69 Query	ROLLBACK
		    65 Query	COMMIT
		    65 Query	ROLLBACK
230313 12:04:49	    68 Query	SELECT 1
230313 12:04:50	    66 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.max, statistics.mean, statistics.min 
FROM statistics INNER JOIN (SELECT max(statistics.start_ts) AS max_start_ts, statistics.metadata_id AS max_metadata_id 
FROM statistics 
WHERE statistics.start_ts < 1663149891.413e0 AND statistics.metadata_id IN (5, 6, 167, 7, 168, 12) GROUP BY statistics.metadata_id) AS anon_1 ON statistics.start_ts = anon_1.max_start_ts AND statistics.metadata_id = anon_1.max_metadata_id
		    67 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.max, statistics.mean, statistics.min 
FROM statistics INNER JOIN (SELECT max(statistics.start_ts) AS max_start_ts, statistics.metadata_id AS max_metadata_id 
FROM statistics 
WHERE statistics.start_ts < 1663149891.414e0 AND statistics.metadata_id IN (1, 2, 3, 4) GROUP BY statistics.metadata_id) AS anon_1 ON statistics.start_ts = anon_1.max_start_ts AND statistics.metadata_id = anon_1.max_metadata_id
230313 12:04:52	    66 Query	COMMIT
		    67 Query	COMMIT
		    66 Query	ROLLBACK
		    67 Query	ROLLBACK
230313 12:05:10	    68 Query	SELECT statistics_runs.run_id AS statistics_runs_run_id, statistics_runs.start AS statistics_runs_start 
FROM statistics_runs 
WHERE statistics_runs.start = '2023-03-13 11:00:00' 
 LIMIT 1
		    68 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.bedroom_humidity_average', 'sensor.bedroom_temperature_average', 'sensor.average_target_temperature', 'sensor.average_current_temperature', 'sensor.average_outside_temperature', 'sensor.average_wind_speed', 'sensor.forward_water_temperature', 'sensor.dining_room_temperature_average', 'sensor.dining_room_humidity_average', 'sensor.living_room_temperature_average', 'sensor.living_room_humidity_average', 'sensor.working_room_temperature_average', 'sensor.working_room_humidity_average', 'sensor.spare_temperature_average', 'sensor.spare_humidity_average')
		    68 Query	SELECT states.entity_id, states.state, NULL AS last_changed_ts, states.last_updated_ts, states.attributes, state_attributes.shared_attrs 
FROM states LEFT OUTER JOIN state_attributes ON states.attributes_id = state_attributes.attributes_id 
WHERE (states.entity_id LIKE 'humidifier.%' OR states.entity_id LIKE 'water_heater.%' OR states.entity_id LIKE 'device_tracker.%' OR states.entity_id LIKE 'climate.%' OR states.entity_id LIKE 'thermostat.%' OR states.last_changed_ts = states.last_updated_ts OR states.last_changed_ts IS NULL) AND states.entity_id IN ('sensor.bedroom_humidity_average', 'sensor.bedroom_temperature_average', 'sensor.average_target_temperature', 'sensor.average_current_temperature', 'sensor.average_outside_temperature', 'sensor.average_wind_speed', 'sensor.forward_water_temperature', 'sensor.dining_room_temperature_average', 'sensor.dining_room_humidity_average', 'sensor.living_room_temperature_average', 'sensor.living_room_humidity_average', 'sensor.working_room_temperature_average', 'sensor.working_room_humidity_average', 'sensor.spare_temperature_average', 'sensor.spare_humidity_average') AND states.last_updated_ts > 1678705199.999999e0 AND states.last_updated_ts < 1678705500.0e0 ORDER BY states.entity_id, states.last_updated_ts
		    68 Query	SELECT states.entity_id, states.state, states.last_changed_ts, states.last_updated_ts, states.attributes, state_attributes.shared_attrs 
FROM states INNER JOIN (SELECT states.entity_id AS max_entity_id, max(states.last_updated_ts) AS max_last_updated 
FROM states 
WHERE states.last_updated_ts >= 1678704345.699451e0 AND states.last_updated_ts < 1678705199.999999e0 AND states.entity_id IN ('sensor.bedroom_humidity_average', 'sensor.bedroom_temperature_average', 'sensor.average_target_temperature', 'sensor.average_current_temperature', 'sensor.average_outside_temperature', 'sensor.average_wind_speed', 'sensor.forward_water_temperature', 'sensor.dining_room_temperature_average', 'sensor.dining_room_humidity_average', 'sensor.living_room_temperature_average', 'sensor.living_room_humidity_average', 'sensor.working_room_temperature_average', 'sensor.working_room_humidity_average', 'sensor.spare_temperature_average', 'sensor.spare_humidity_average') GROUP BY states.entity_id) AS anon_1 ON states.entity_id = anon_1.max_entity_id AND states.last_updated_ts = anon_1.max_last_updated LEFT OUTER JOIN state_attributes ON states.attributes_id = state_attributes.attributes_id
		    68 Query	SELECT statistics_short_term.metadata_id, statistics_short_term.start_ts, statistics_short_term.mean, statistics_short_term.min, statistics_short_term.max, statistics_short_term.last_reset_ts, statistics_short_term.state, statistics_short_term.sum 
FROM statistics_short_term INNER JOIN (SELECT statistics_short_term.metadata_id AS metadata_id, max(statistics_short_term.start_ts) AS start_max 
FROM statistics_short_term 
WHERE statistics_short_term.metadata_id IN (NULL) AND (1 != 1) GROUP BY statistics_short_term.metadata_id) AS anon_1 ON statistics_short_term.metadata_id = anon_1.metadata_id AND statistics_short_term.start_ts = anon_1.start_max
		    68 Query	COMMIT
		    68 Query	ROLLBACK
		    69 Query	INSERT INTO statistics_runs (start) VALUES ('2023-03-13 11:00:00') RETURNING statistics_runs.run_id
		    69 Query	INSERT INTO statistics_short_term (created, created_ts, metadata_id, start, start_ts, mean, min, max, last_reset, last_reset_ts, state, sum) VALUES (NULL, 1678705510.5635657e0, 1, NULL, 1678705200.0e0, 23.553936285666666e0, 23.5e0, 23.6e0, NULL, NULL, NULL, NULL), (NULL, 1678705510.5644927e0, 5, NULL, 1678705200.0e0, 21.9e0, 21.9e0, 21.9e0, NULL, NULL, NULL, NULL), (NULL, 1678705510.5650072e0, 6, NULL, 1678705200.0e0, 22.578468463666663e0, 22.5e0, 22.8e0, NULL, NULL, NULL, NULL), (NULL, 1678705510.5654995e0, 7, NULL, 1678705200.0e0, 10.4e0, 10.4e0, 10.4e0, NULL, NULL, NULL, NULL), (NULL, 1678705510.56599e0, 167, NULL, 1678705200.0e0, 18.4e0, 18.4e0, 18.4e0, NULL, NULL, NULL, NULL), (NULL, 1678705510.5665646e0, 168, NULL, 1678705200.0e0, 50.3e0, 50.3e0, 50.3e0, NULL, NULL, NULL, NULL), (NULL, 1678705510.5670567e0, 2, NULL, 1678705200.0e0, 24.81227269066667e0, 24.8e0, 24.9e0, NULL, NULL, NULL, NULL), (NULL, 1678705510.5676098e0, 3, NULL, 1678705200.0e0, 22.8e0, 22.8e0, 22.8e0, NULL, NULL, NULL, NULL), (NULL, 1678705510.5681021e0, 4, NULL, 1678705200.0e0, 22.795035271333333e0, 22.7e0, 22.8e0, NULL, NULL, NULL, NULL), (NULL, 1678705510.5685935e0, 12, NULL, 1678705200.0e0, 12.2e0, 12.2e0, 12.2e0, NULL, NULL, NULL, NULL) RETURNING statistics_short_term.id
		    69 Query	COMMIT
		    69 Query	ROLLBACK
230313 12:05:15	    65 Query	INSERT INTO events (event_type, event_data, origin, origin_idx, time_fired, time_fired_ts, context_id, context_user_id, context_parent_id, data_id) VALUES ('recorder_5min_statistics_generated', NULL, NULL, 0, NULL, 1678705510.600939e0, '01GVDAAJ685A7B7J3570C941WY', NULL, NULL, NULL) RETURNING events.event_id
		    65 Query	COMMIT
		    65 Query	ROLLBACK
230313 12:05:19	    66 Query	SELECT 1

Below is a query log, when there are no problems (it seems I pressed refresh 2 times, so everything is duplicated, sorry):

Click to open
230313 12:20:33	    73 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.living_room_temperature_average', 'sensor.working_room_temperature_average', 'sensor.dining_room_temperature_average', 'sensor.bedroom_temperature_average')
		    76 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.average_target_temperature', 'sensor.average_current_temperature', 'sensor.average_outside_temperature', 'sensor.spare_temperature_average', 'sensor.average_wind_speed', 'sensor.forward_water_temperature')
		    73 Query	COMMIT
		    72 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.living_room_humidity_average', 'sensor.working_room_humidity_average', 'sensor.dining_room_humidity_average', 'sensor.bedroom_humidity_average', 'sensor.spare_humidity_average')
		    75 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.living_room_temperature_average', 'sensor.working_room_temperature_average', 'sensor.dining_room_temperature_average', 'sensor.bedroom_temperature_average')
		    76 Query	COMMIT
		    75 Query	COMMIT
		    72 Query	COMMIT
		    76 Query	ROLLBACK
		    72 Query	ROLLBACK
		    76 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.average_target_temperature', 'sensor.average_current_temperature', 'sensor.average_outside_temperature', 'sensor.spare_temperature_average', 'sensor.average_wind_speed', 'sensor.forward_water_temperature')
		    72 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.living_room_humidity_average', 'sensor.working_room_humidity_average', 'sensor.dining_room_humidity_average', 'sensor.bedroom_humidity_average', 'sensor.spare_humidity_average')
		    73 Query	ROLLBACK
		    76 Query	COMMIT
		    75 Query	ROLLBACK
		    76 Query	ROLLBACK
		    72 Query	COMMIT
		    72 Query	ROLLBACK
		    73 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.living_room_temperature_average', 'sensor.working_room_temperature_average', 'sensor.dining_room_temperature_average', 'sensor.bedroom_temperature_average')
		    73 Query	COMMIT
		    73 Query	ROLLBACK
		    76 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.living_room_humidity_average', 'sensor.working_room_humidity_average', 'sensor.dining_room_humidity_average', 'sensor.bedroom_humidity_average', 'sensor.spare_humidity_average')
		    72 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.living_room_temperature_average', 'sensor.working_room_temperature_average', 'sensor.dining_room_temperature_average', 'sensor.bedroom_temperature_average')
		    73 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.average_target_temperature', 'sensor.average_current_temperature', 'sensor.average_outside_temperature', 'sensor.spare_temperature_average', 'sensor.average_wind_speed', 'sensor.forward_water_temperature')
		    76 Query	COMMIT
		    72 Query	COMMIT
		    75 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.average_target_temperature', 'sensor.average_current_temperature', 'sensor.average_outside_temperature', 'sensor.spare_temperature_average', 'sensor.average_wind_speed', 'sensor.forward_water_temperature')
		    72 Query	ROLLBACK
		    76 Query	ROLLBACK
		    73 Query	COMMIT
		    72 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.living_room_humidity_average', 'sensor.working_room_humidity_average', 'sensor.dining_room_humidity_average', 'sensor.bedroom_humidity_average', 'sensor.spare_humidity_average')
		    73 Query	ROLLBACK
		    72 Query	COMMIT
		    75 Query	COMMIT
		    72 Query	ROLLBACK
		    75 Query	ROLLBACK
		    76 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.average_target_temperature', 'sensor.average_current_temperature', 'sensor.average_outside_temperature', 'sensor.spare_temperature_average', 'sensor.average_wind_speed', 'sensor.forward_water_temperature')
		    73 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.living_room_humidity_average', 'sensor.working_room_humidity_average', 'sensor.dining_room_humidity_average', 'sensor.bedroom_humidity_average', 'sensor.spare_humidity_average')
		    76 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.mean 
FROM statistics 
WHERE statistics.start_ts >= 1676110844.623e0 AND statistics.metadata_id IN (5, 6, 7, 12, 167, 168) ORDER BY statistics.metadata_id, statistics.start_ts
		    73 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.mean 
FROM statistics 
WHERE statistics.start_ts >= 1676110844.623e0 AND statistics.metadata_id IN (8, 14, 16, 13, 9) ORDER BY statistics.metadata_id, statistics.start_ts
		    72 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.living_room_temperature_average', 'sensor.working_room_temperature_average', 'sensor.dining_room_temperature_average', 'sensor.bedroom_temperature_average')
		    75 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.living_room_temperature_average', 'sensor.working_room_temperature_average', 'sensor.dining_room_temperature_average', 'sensor.bedroom_temperature_average')
		    72 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.mean 
FROM statistics 
WHERE statistics.start_ts >= 1676110844.645e0 AND statistics.metadata_id IN (1, 2, 3, 4) ORDER BY statistics.metadata_id, statistics.start_ts
		    75 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.max, statistics.mean, statistics.min 
FROM statistics 
WHERE statistics.start_ts >= 1663150844.646e0 AND statistics.metadata_id IN (1, 2, 3, 4) ORDER BY statistics.metadata_id, statistics.start_ts
		    72 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.mean 
FROM statistics INNER JOIN (SELECT max(statistics.start_ts) AS max_start_ts, statistics.metadata_id AS max_metadata_id 
FROM statistics 
WHERE statistics.start_ts < 1676110844.645e0 AND statistics.metadata_id IN (1, 2, 3, 4) GROUP BY statistics.metadata_id) AS anon_1 ON statistics.start_ts = anon_1.max_start_ts AND statistics.metadata_id = anon_1.max_metadata_id
230313 12:20:34	    72 Query	COMMIT
		    72 Query	ROLLBACK
		    76 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.mean 
FROM statistics INNER JOIN (SELECT max(statistics.start_ts) AS max_start_ts, statistics.metadata_id AS max_metadata_id 
FROM statistics 
WHERE statistics.start_ts < 1676110844.623e0 AND statistics.metadata_id IN (5, 6, 167, 7, 168, 12) GROUP BY statistics.metadata_id) AS anon_1 ON statistics.start_ts = anon_1.max_start_ts AND statistics.metadata_id = anon_1.max_metadata_id
		    73 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.mean 
FROM statistics INNER JOIN (SELECT max(statistics.start_ts) AS max_start_ts, statistics.metadata_id AS max_metadata_id 
FROM statistics 
WHERE statistics.start_ts < 1676110844.623e0 AND statistics.metadata_id IN (8, 9, 13, 14, 16) GROUP BY statistics.metadata_id) AS anon_1 ON statistics.start_ts = anon_1.max_start_ts AND statistics.metadata_id = anon_1.max_metadata_id
		    72 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.average_target_temperature', 'sensor.average_current_temperature', 'sensor.average_outside_temperature', 'sensor.spare_temperature_average', 'sensor.average_wind_speed', 'sensor.forward_water_temperature')
		    76 Query	COMMIT
		    76 Query	ROLLBACK
		    72 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.max, statistics.mean, statistics.min 
FROM statistics 
WHERE statistics.start_ts >= 1663150844.646e0 AND statistics.metadata_id IN (5, 6, 7, 12, 167, 168) ORDER BY statistics.metadata_id, statistics.start_ts
		    73 Query	COMMIT
		    73 Query	ROLLBACK
		    76 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.living_room_humidity_average', 'sensor.working_room_humidity_average', 'sensor.dining_room_humidity_average', 'sensor.bedroom_humidity_average', 'sensor.spare_humidity_average')
		    76 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.max, statistics.mean, statistics.min 
FROM statistics 
WHERE statistics.start_ts >= 1663150844.647e0 AND statistics.metadata_id IN (8, 14, 16, 13, 9) ORDER BY statistics.metadata_id, statistics.start_ts
		    73 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.living_room_temperature_average', 'sensor.working_room_temperature_average', 'sensor.dining_room_temperature_average', 'sensor.bedroom_temperature_average')
		    73 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.mean 
FROM statistics 
WHERE statistics.start_ts >= 1676110844.649e0 AND statistics.metadata_id IN (1, 2, 3, 4) ORDER BY statistics.metadata_id, statistics.start_ts
230313 12:20:36	    73 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.mean 
FROM statistics INNER JOIN (SELECT max(statistics.start_ts) AS max_start_ts, statistics.metadata_id AS max_metadata_id 
FROM statistics 
WHERE statistics.start_ts < 1676110844.649e0 AND statistics.metadata_id IN (1, 2, 3, 4) GROUP BY statistics.metadata_id) AS anon_1 ON statistics.start_ts = anon_1.max_start_ts AND statistics.metadata_id = anon_1.max_metadata_id
		    73 Query	COMMIT
		    73 Query	ROLLBACK
		    73 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.living_room_temperature_average', 'sensor.working_room_temperature_average', 'sensor.dining_room_temperature_average', 'sensor.bedroom_temperature_average')
		    73 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.max, statistics.mean, statistics.min 
FROM statistics 
WHERE statistics.start_ts >= 1663150844.65e0 AND statistics.metadata_id IN (1, 2, 3, 4) ORDER BY statistics.metadata_id, statistics.start_ts
230313 12:20:38	    75 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.max, statistics.mean, statistics.min 
FROM statistics INNER JOIN (SELECT max(statistics.start_ts) AS max_start_ts, statistics.metadata_id AS max_metadata_id 
FROM statistics 
WHERE statistics.start_ts < 1663150844.646e0 AND statistics.metadata_id IN (1, 2, 3, 4) GROUP BY statistics.metadata_id) AS anon_1 ON statistics.start_ts = anon_1.max_start_ts AND statistics.metadata_id = anon_1.max_metadata_id
230313 12:20:41	    72 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.max, statistics.mean, statistics.min 
FROM statistics INNER JOIN (SELECT max(statistics.start_ts) AS max_start_ts, statistics.metadata_id AS max_metadata_id 
FROM statistics 
WHERE statistics.start_ts < 1663150844.646e0 AND statistics.metadata_id IN (5, 6, 167, 7, 168, 12) GROUP BY statistics.metadata_id) AS anon_1 ON statistics.start_ts = anon_1.max_start_ts AND statistics.metadata_id = anon_1.max_metadata_id
230313 12:20:42	    76 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.max, statistics.mean, statistics.min 
FROM statistics INNER JOIN (SELECT max(statistics.start_ts) AS max_start_ts, statistics.metadata_id AS max_metadata_id 
FROM statistics 
WHERE statistics.start_ts < 1663150844.647e0 AND statistics.metadata_id IN (8, 9, 13, 14, 16) GROUP BY statistics.metadata_id) AS anon_1 ON statistics.start_ts = anon_1.max_start_ts AND statistics.metadata_id = anon_1.max_metadata_id
		    73 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.max, statistics.mean, statistics.min 
FROM statistics INNER JOIN (SELECT max(statistics.start_ts) AS max_start_ts, statistics.metadata_id AS max_metadata_id 
FROM statistics 
WHERE statistics.start_ts < 1663150844.65e0 AND statistics.metadata_id IN (1, 2, 3, 4) GROUP BY statistics.metadata_id) AS anon_1 ON statistics.start_ts = anon_1.max_start_ts AND statistics.metadata_id = anon_1.max_metadata_id
230313 12:20:43	    75 Query	COMMIT
		    75 Query	ROLLBACK
		    75 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.living_room_humidity_average', 'sensor.working_room_humidity_average', 'sensor.dining_room_humidity_average', 'sensor.bedroom_humidity_average', 'sensor.spare_humidity_average')
		    75 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.mean 
FROM statistics 
WHERE statistics.start_ts >= 1676110844.651e0 AND statistics.metadata_id IN (8, 14, 16, 13, 9) ORDER BY statistics.metadata_id, statistics.start_ts
230313 12:20:44	    75 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.mean 
FROM statistics INNER JOIN (SELECT max(statistics.start_ts) AS max_start_ts, statistics.metadata_id AS max_metadata_id 
FROM statistics 
WHERE statistics.start_ts < 1676110844.651e0 AND statistics.metadata_id IN (8, 9, 13, 14, 16) GROUP BY statistics.metadata_id) AS anon_1 ON statistics.start_ts = anon_1.max_start_ts AND statistics.metadata_id = anon_1.max_metadata_id
230313 12:20:45	    75 Query	COMMIT
		    75 Query	ROLLBACK
		    75 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.average_target_temperature', 'sensor.average_current_temperature', 'sensor.average_outside_temperature', 'sensor.spare_temperature_average', 'sensor.average_wind_speed', 'sensor.forward_water_temperature')
		    75 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.max, statistics.mean, statistics.min 
FROM statistics 
WHERE statistics.start_ts >= 1663150844.651e0 AND statistics.metadata_id IN (5, 6, 7, 12, 167, 168) ORDER BY statistics.metadata_id, statistics.start_ts
		    72 Query	COMMIT
		    72 Query	ROLLBACK
		    72 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.living_room_humidity_average', 'sensor.working_room_humidity_average', 'sensor.dining_room_humidity_average', 'sensor.bedroom_humidity_average', 'sensor.spare_humidity_average')
		    72 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.max, statistics.mean, statistics.min 
FROM statistics 
WHERE statistics.start_ts >= 1663150844.652e0 AND statistics.metadata_id IN (8, 14, 16, 13, 9) ORDER BY statistics.metadata_id, statistics.start_ts
230313 12:20:46	    74 Query	INSERT INTO states (entity_id, state, attributes, event_id, last_changed, last_changed_ts, last_updated, last_updated_ts, old_state_id, attributes_id, context_id, context_user_id, context_parent_id, origin_idx) VALUES ('sensor.dining_room_humidity_average', '36.0', NULL, NULL, NULL, NULL, NULL, 1678706443.280858e0, 14163054, 3227684, '01GVDB710G1173H08NCMYB8GN3', NULL, NULL, 0) RETURNING states.state_id
		    74 Query	COMMIT
		    74 Query	ROLLBACK
		    73 Query	COMMIT
		    73 Query	ROLLBACK
		    74 Query	SELECT statistics_meta.id, statistics_meta.statistic_id, statistics_meta.source, statistics_meta.unit_of_measurement, statistics_meta.has_mean, statistics_meta.has_sum, statistics_meta.name 
FROM statistics_meta 
WHERE statistics_meta.statistic_id IN ('sensor.average_target_temperature', 'sensor.average_current_temperature', 'sensor.average_outside_temperature', 'sensor.spare_temperature_average', 'sensor.average_wind_speed', 'sensor.forward_water_temperature')
		    74 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.mean 
FROM statistics 
WHERE statistics.start_ts >= 1676110844.652e0 AND statistics.metadata_id IN (5, 6, 7, 12, 167, 168) ORDER BY statistics.metadata_id, statistics.start_ts
230313 12:20:47	    76 Query	COMMIT
		    76 Query	ROLLBACK
		    74 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.mean 
FROM statistics INNER JOIN (SELECT max(statistics.start_ts) AS max_start_ts, statistics.metadata_id AS max_metadata_id 
FROM statistics 
WHERE statistics.start_ts < 1676110844.652e0 AND statistics.metadata_id IN (5, 6, 167, 7, 168, 12) GROUP BY statistics.metadata_id) AS anon_1 ON statistics.start_ts = anon_1.max_start_ts AND statistics.metadata_id = anon_1.max_metadata_id
		    74 Query	COMMIT
		    74 Query	ROLLBACK
230313 12:20:49	    75 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.max, statistics.mean, statistics.min 
FROM statistics INNER JOIN (SELECT max(statistics.start_ts) AS max_start_ts, statistics.metadata_id AS max_metadata_id 
FROM statistics 
WHERE statistics.start_ts < 1663150844.651e0 AND statistics.metadata_id IN (5, 6, 167, 7, 168, 12) GROUP BY statistics.metadata_id) AS anon_1 ON statistics.start_ts = anon_1.max_start_ts AND statistics.metadata_id = anon_1.max_metadata_id
230313 12:20:50	    73 Query	SELECT 1
		    72 Query	SELECT statistics.metadata_id, statistics.start_ts, statistics.max, statistics.mean, statistics.min 
FROM statistics INNER JOIN (SELECT max(statistics.start_ts) AS max_start_ts, statistics.metadata_id AS max_metadata_id 
FROM statistics 
WHERE statistics.start_ts < 1663150844.652e0 AND statistics.metadata_id IN (8, 9, 13, 14, 16) GROUP BY statistics.metadata_id) AS anon_1 ON statistics.start_ts = anon_1.max_start_ts AND statistics.metadata_id = anon_1.max_metadata_id
230313 12:20:51	    73 Query	INSERT INTO states (entity_id, state, attributes, event_id, last_changed, last_changed_ts, last_updated, last_updated_ts, old_state_id, attributes_id, context_id, context_user_id, context_parent_id, origin_idx) VALUES ('sensor.living_room_humidity_average', '38.3', NULL, NULL, NULL, NULL, NULL, 1678706449.227221e0, 14162993, 3227681, '01GVDB76TBE48F42NM03B9647B', NULL, NULL, 0) RETURNING states.state_id
		    73 Query	COMMIT
		    73 Query	ROLLBACK
230313 12:20:52	    75 Query	COMMIT
		    75 Query	ROLLBACK
		    72 Query	COMMIT
		    72 Query	ROLLBACK

Additional investigation:

I see that each graph has 2 data fetching select query, one with an INNER JOIN statement, and this INNER JOIN contains a < operator like the HA log error mentioned above:

I've added some indentation manually.

SELECT statistics.metadata_id, statistics.start_ts, statistics.mean 
FROM statistics 
WHERE statistics.start_ts >= 1676110844.645e0 AND statistics.metadata_id IN (1, 2, 3, 4) ORDER BY statistics.metadata_id, statistics.start_ts

SELECT statistics.metadata_id, statistics.start_ts, statistics.mean 
FROM statistics INNER JOIN (
    SELECT max(statistics.start_ts) AS max_start_ts, statistics.metadata_id AS max_metadata_id 
    FROM statistics 
    WHERE statistics.start_ts < 1676110844.645e0 AND statistics.metadata_id IN (1, 2, 3, 4) GROUP BY statistics.metadata_id) AS anon_1 
    ON statistics.start_ts = anon_1.max_start_ts AND statistics.metadata_id = anon_1.max_metadata_id

@bdraco
Copy link
Member

bdraco commented Mar 13, 2023

All of the above could be (likely) caused by the concurrency issue with sqlalchemy so I think we need to ask if we can get a 2.0.6 release sooner.

@mietzen
Copy link

mietzen commented Mar 13, 2023

I just downgraded to 2023.3.1, statistics are working again.

@balloob
Copy link
Member

balloob commented Mar 13, 2023

SQLAlchemy 2.0.6 upgrade will be part of HA 2023.3.4, which will be released today.

@bdraco
Copy link
Member

bdraco commented Mar 13, 2023

The bump to sqlalchemy 2.0.6 via #89650 should fix this in 2023.3.4. Once its released, if there is still a problem after upgrading, please let us know and we will get this issue reopened.

@erikgeurts
Copy link
Author

Even though my statistics graphs card started working again after a restart while still on 2023.3.3, I was a bit apprehensive to update to 2023.3.4. But I'm happy to report that the graphs still work on 2023.3.4 as well.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants