Calculate Running Average based on day of month

I have some sensors that give me daily totals of certain values (run hours per day, sunlight hours per day, cycles per day, etc.). I would like to create a running monthly average by adding each day to the previous total and then dividing by the day of the month. I’ve made several unsuccessful attempts but below is what I am attempting. Is this possible? Is there a better way?

Here’s what I’m attempting as a template:

Capture daily value - this works
Capture the day of the month - this works
Create an input_number to store the monthly sum of the daily values - this works
but below is where I’m not sure how to proceed:

If day of month is 1
Read Daily Value and Store in input_number.
Monthly Average is input_number/1
If day of month is >1
Read input_number and add Daily Value
Store this new number in input_number as running sum
Monthly Average is input_number value/Day of Month

The reading from an input_number, calculating works but writing back to an input_number is not working from a template.
Is this possible?