Calculate average of three max values of hourly power sensor

I have a sensor which stores power (kWh) used per hour.

I would like to get the max value per date in current month, then calculate the average of the three max values (unique dates) per month.
Here is a python script showing what I’d like to accomplish:

max_value_per_date = []
for date in january:
	date_values = []
	for hour in date:


three_max_values_in_month_dates = max_value_per_date.sort()[-3:]
average = three_max_values_in_month_dates  / 3
return average

Anyone know if this is possible and how I calculate this in home assistant?