Hourly statistics for values over a set span of time(days)

I have been looking around but have not had any luck finding what I am looking for, basically I want to create a rolling average that is done over say 1 week or 1 month etc but changes based on the time of day.

for example if it is just the last 2 days to use then @12:00 it would take the value 1 and 2 days ago @ or nearest to 12:00 and in priority calculate ave min max

I am thinking this will be useful to forecast the values ahead of us or simply to compare… eg what did the energy profile look like last year at this time? or last month? how much have prices for electricity increased since last year? etc… basically, to answer “are we doing better or worse than a span that is seen as comparable?”

something I have done in excel looks like this that I was hoping to do in home assistant a comparison of min/max and average of these statics and overlay it on the data today (blue, hand drawn :slight_smile: ) :

have you all seen something like this done in home assistant?

seems if this does not already exist it looks to be requiring some sql ?

your help is appreciated!
if you have seen something you think can help with this please send it over!
if you have ideas how to best go about it - let me know!

ok, well with the help of AI (sorry… ) I seem to have an SQL query that I am testing via the integration that takes the sensor (in my case one I trigger for solar production hourly)

it fills every hour with the average including the current value

WITH config AS (
    -- Configuration variables
    SELECT 
        7 AS x_days_to_average_over,  -- Change this to set the number of days
        'input_number.pv_value_this_hour_for_sql_queries' AS target_entity -- Change this to set the entity
),
hourly_max_last_x_days AS (
    -- Get the maximum value for each hour over the last X days
    SELECT 
        strftime('%Y-%m-%d %H:00:00', datetime(s.last_updated_ts, 'unixepoch', 'localtime')) AS hour_timestamp,
        MAX(CAST(s.state AS FLOAT)) AS max_value
    FROM states s
    JOIN states_meta sm ON s.metadata_id = sm.metadata_id
    JOIN config c ON 1=1  -- Use the config variables
    WHERE sm.entity_id = c.target_entity
    AND s.state NOT IN ('unknown', '', 'unavailable')
    AND s.last_updated_ts >= strftime('%s', datetime('now', printf('-%d days', c.x_days_to_average_over - 1)))  
    GROUP BY hour_timestamp
),
hourly_averages AS (
    -- Compute the X-day rolling average for each hour
    SELECT 
        strftime('%H', hour_timestamp) AS hour_of_day,
        AVG(max_value) AS rolling_x_day_avg
    FROM hourly_max_last_x_days
    WHERE strftime('%H', hour_timestamp) = strftime('%H', datetime('now', 'localtime')) -- Match exact hour
    GROUP BY hour_of_day
),
current_value_cte AS (
    -- Get the latest current value for the target entity
    SELECT 
        CAST(s.state AS FLOAT) AS current_value
    FROM states s
    JOIN states_meta sm ON s.metadata_id = sm.metadata_id
    JOIN config c ON 1=1
    WHERE sm.entity_id = c.target_entity
    AND s.state NOT IN ('unknown', '', 'unavailable')
    ORDER BY s.last_updated_ts DESC
    LIMIT 1
)
-- Final selection with the current value, config values, and rolling average
SELECT 
    ha.hour_of_day AS measurement_hour,
    ha.rolling_x_day_avg,
    cv.current_value,
    c.x_days_to_average_over,
    c.target_entity
FROM hourly_averages ha
JOIN current_value_cte cv ON 1=1
JOIN config c ON 1=1;

additionally, in configuring the integration I have entered/choosen:
column : rolling_x_day_avg
state class: Measurement
I setup an entity that it follows which updates only hourly in part to keep the system from being overloaded to do it constantly (with that said, I see I have triggered it at weird times and that then is a point it follows giving more points in the average presumably for the period given which I think can cause some strangeness in the data if it does not have points on other days… so I guess be consistent).

so after running a 24hrs I have this (pink line) on a 3 day sensor, I then shifted it -24h (-1d) to not include todays data in the average :

  • in hindsight I should have not done it on an hourly delta not a accumulated daily sensor

My hope is that it will not be too hard on the system as the value that triggers it (?) is updated only hourly although I am running this for 3, 7, 10 day hourly running averages to figure out what is interesting and to debug (hidden is the same I am doing for forecasts as well… so it multiples quickly… ) :upside_down_face:

Nope this
Is exactly how you should do it. Doubt the system even feels what you did you’re briefly updating stats once an hour. That’s the same thing the energy dashboard does and why you have to wait an hour for it to update too…

You’re fine glad it works!

1 Like