Temperature difference - historical analysis - question

I currently have a “temperature difference” sensor that displays the difference between two sensors - works great!

I have also created a statistical sensor that shows the mean temperature over the last 7 days:

  - platform: statistics
    name: "Average Temperature Difference"
    entity_id: sensor.temperature_difference
    state_characteristic: mean
    max_age:
      days: 7

This sensor also works well. The issue with this method is that it averages ALL data (day & night temp differences) which skews the data.

Now the question / challenge - how can I display the average temperature difference for a range of hours within that seven day period… EG: Only display the average for the historical temperature differences that are timestamped between the hours of 9:00 am and 7:00 pm. each of the seven days?

End state: Have analysis data that allows me to accurately set another automation that bumps down my AC when the temperature difference is greater than X.

This custom average sensor can define a start and end time, do this for a daily average.

You would then have to average this over a week. Use the core average sensor for this second sensor.

Thank you for the lead…I’ll play around with this.