home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
69.04k stars 28.28k forks source link

Statistical aggregation calculation is wrong: for a weekly value doesn't account for the week start date #109119

Closed luissimoes closed 1 week ago

luissimoes commented 3 months ago

The problem

I have a sensor, which tracks the cumulative energy consumed. When creating a statistics card, and choosing "week" for the report, it calculates the energy from Monday to Sunday. In my case:

the calendar defines Sunday as start of the week the sensor has a CRON 00**7 so the sum should be from Sunday to Saturday -- this problem is confirmed as if I create a graph in Plotly it shows the same result - but not in Apex, where I can manipulate the days to be counted

Files: Result of the graph (wrong) and the sensor itself (resets correctly) (I am adding screenshots with the mouse hovering so that the values are visible) - it is also clear that the counting starts eg on the 8th instead of the 7th)

Screenshot 2024-01-29 at 15 44 09 Screenshot 2024-01-29 at 15 44 18 Screenshot 2024-01-29 at 15 44 25

The Sensor itself

Screenshot 2024-01-29 at 15 55 41 Screenshot 2024-01-29 at 15 56 03 Screenshot 2024-01-29 at 15 55 53

Here is how it should look like Expected: sum of the energy from Sunday to Saturday (as shown below, with a graph done with Apex, where the parameters were defined to counterbalance the problem)

Screenshot 2024-01-29 at 15 36 01

What version of Home Assistant Core has the issue?

core-2024.1.5

What was the last working version of Home Assistant Core?

never worked before in the previous versions I had

What type of installation are you running?

Home Assistant OS

Integration causing the issue

Statistics aggregation calculation

Link to integration documentation on our website

No response

Diagnostics information

No response

Example YAML snippet

Sensor - original
`
sensor.bitshake_smartmeterreader_mt681_total_in:
  unit_of_measurement: "kWh"
  device_class: energy
  state_class: total_increasing
`

Sensor - energy (utility)
mt1681_weekly_energy:
  unique_id: "mt1681-weeky"
  source: sensor.bitshake_smartmeterreader_mt681_total_in
  name: From netz weekly
  cron: "0 0 * * 7"

Info from States
state_class: total_increasing
source: sensor.bitshake_smartmeterreader_mt681_total_in
status: collecting
last_period: "75.417"
last_valid_state: "17485.588"
cron pattern: 0 0 * * 7
last_reset: "2024-01-27T23:00:00.023029+00:00"
unit_of_measurement: kWh
device_class: energy
icon: mdi:counter
friendly_name: From netz weekly

Anything in the logs that might be useful for us?

No response

Additional information

The calculation ignores the start of the week and just uses Monday as start.

I have tested this in iOS, iPad OS, Mac OS and using several browsers.

I had opened initially a bug against frontend (#19566) but it was mentioned there this was a core issue: " The frontend has a per-user control for preferred day of the week for showing calendars, but the backend statistics aggregation calculation for weeks is not configurable, it just uses simply the python time.weekday() function to compute the start of the week, so that's hardcoded to monday. "

issue-triage-workflows[bot] commented 2 weeks ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.