Script Averaging calculations Part 2

hi, You all got me going with this script for an average of a three hour block of time:

how difficult would it be to get that to loop through the previous three days? So I can get the average of the previous three days between 0300 and 0600.
I made three of these scripts, one for each of the days, and then averaged those results but maybe I can do it in once shot?

I know there is a looping function but it’s beyond me. I wish I could learn to script but I just can’t seem to speak that language…

Thanks

now = system.date.now()
previousDay = system.date.addDays(now, -1)
time_3am_previous_day = system.date.setTime(previousDay, 3, 0, 0)
startDate = time_3am_previous_day
endDate = system.date.addHours(startDate, 3)

data = system.tag.queryTagCalculations(
paths=['[default]ST25/_25_FI_110X_Total'],
calculations=['Average'],
startDate=startDate,
endDate=endDate

)
return data.getValueAt(0,1)

Are you looking for a single value that's the average of the 3 days? Or are you looking for an average value for each of the prior 3 days?

A single value for the average of the previous three days.

But it isn't just a single value, really.

The way I understand this, is you are looking for the average of the average for each of the last three days during a 3 hour block on each day. Is that correct?

I believe this will do the trick. I added in some extra variables to make tweaking a little simpler

starthr = 3
hrstospan = 3
daystoget = 3
today = system.date.now()

avgSum=0

for day in range(1,daystoget+1):
	startDate = system.date.addDays(today, -day)
	startDT = system.date.setTime(startDate, starthr, 0, 0)
	endDT = system.date.addHours(startDT, hrstospan)
	
	data = system.tag.queryTagCalculations(
			paths=['[default]ST25/_25_FI_110X_Total'],
			calculations=['Average'],
			startDate=startDT,
			endDate=endDT
			)
	avgSum += data.getValueAt(0,1)
	
return avgSum/daystoget
	

that was how I hacked it together. I’m not sure mathematically, out of the bounds of my knowledge, if there would be a difference in an average of three averages or the average of all the values from that the 3day/3hour timer period. either way would work for me I think

Ok, a quick internet search says that the average of the averages and the average of all the raw datapoints will be the same if the sample sizes are all the same. In my case they should be so either way would work