# Script Averaging calculations Part 2

**URL:** <https://forum.inductiveautomation.com/t/script-averaging-calculations-part-2/113323>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [January 15, 2026, 5:08pm UTC](https://forum.inductiveautomation.com/t/script-averaging-calculations-part-2/113323 "2026-01-15T17:08:51Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bigtalljv](https://avatars.discourse-cdn.com/v4/letter/b/e19adc/32.png) [@Bigtalljv](https://forum.inductiveautomation.com/u/Bigtalljv)\
**Post date:** [January 15, 2026, 5:08pm UTC](https://forum.inductiveautomation.com/t/script-averaging-calculations-part-2/113323/1 "2026-01-15T17:08:51Z")

</div>

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

```python
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)

```

---

<div class="post-metadata">

**Author:** ![tgarrison](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tgarrison/32/80965_2.png) [@tgarrison](https://forum.inductiveautomation.com/u/tgarrison)\
**Post date:** [January 15, 2026, 8:24pm UTC](https://forum.inductiveautomation.com/t/script-averaging-calculations-part-2/113323/2 "2026-01-15T20:24:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Bigtalljv](https://avatars.discourse-cdn.com/v4/letter/b/e19adc/32.png) [@Bigtalljv](https://forum.inductiveautomation.com/u/Bigtalljv)\
**Post date:** [January 15, 2026, 8:31pm UTC](https://forum.inductiveautomation.com/t/script-averaging-calculations-part-2/113323/3 "2026-01-15T20:31:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [January 15, 2026, 8:34pm UTC](https://forum.inductiveautomation.com/t/script-averaging-calculations-part-2/113323/4 "2026-01-15T20:34:39Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![tgarrison](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tgarrison/32/80965_2.png) [@tgarrison](https://forum.inductiveautomation.com/u/tgarrison)\
**Post date:** [January 15, 2026, 8:48pm UTC](https://forum.inductiveautomation.com/t/script-averaging-calculations-part-2/113323/5 "2026-01-15T20:48:01Z")

</div>

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

```python
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
	

```

---

<div class="post-metadata">

**Author:** ![Bigtalljv](https://avatars.discourse-cdn.com/v4/letter/b/e19adc/32.png) [@Bigtalljv](https://forum.inductiveautomation.com/u/Bigtalljv)\
**Post date:** [January 15, 2026, 8:52pm UTC](https://forum.inductiveautomation.com/t/script-averaging-calculations-part-2/113323/6 "2026-01-15T20:52:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Bigtalljv](https://avatars.discourse-cdn.com/v4/letter/b/e19adc/32.png) [@Bigtalljv](https://forum.inductiveautomation.com/u/Bigtalljv)\
**Post date:** [January 16, 2026, 12:14am UTC](https://forum.inductiveautomation.com/t/script-averaging-calculations-part-2/113323/7 "2026-01-16T00:14:55Z")

</div>

Thanks! I think it’s close, but I get different results from my other averages. If I switch the command to system.tag.queryTagHistory should the For process still work? I only get 1 days worth of data when I do that.

---

<div class="post-metadata">

**Author:** ![tgarrison](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tgarrison/32/80965_2.png) [@tgarrison](https://forum.inductiveautomation.com/u/tgarrison)\
**Post date:** [January 16, 2026, 1:40pm UTC](https://forum.inductiveautomation.com/t/script-averaging-calculations-part-2/113323/8 "2026-01-16T13:40:00Z")

</div>

You'll need to figure out the math. But you should be able to use the same basic routine with whatever method you use for calculating the average.

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [January 16, 2026, 6:05pm UTC](https://forum.inductiveautomation.com/t/script-averaging-calculations-part-2/113323/9 "2026-01-16T18:05:05Z")

</div>

You are manually querying a database. Does that mean you do not have tag historian?

You could start by editing the function to take a date parameter as an input (default `none` uses `system.date.now()` ) and use that date as your “now” in the script. Then just call the same function 3 times for the last 3 days and compute the average in the script.

```python
def averageWindow(dateStart = none, span = 3)
	if dateStart is none:
		now = system.date.now()
	else:
		now = dateStart
	previousDay = system.date.addDays(now, -1)
	time_3am_previous_day = system.date.setTime(previousDay, span, 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)

```

```python
#range could be 0-2 depending on when this runs
avgs = [averageWindow(system.date.addDays(system.date.now(), -1*day)) for day in range(1,3)]

avg = sum(avgs)/len(avgs)

```

Can also pass the path in and make this script much more useful

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [January 16, 2026, 11:37pm UTC](https://forum.inductiveautomation.com/t/script-averaging-calculations-part-2/113323/10 "2026-01-16T23:37:16Z")

</div>

You might want to cast one of the operands to float if the sum is not already one.

---

<div class="post-metadata">

**Author:** ![Bigtalljv](https://avatars.discourse-cdn.com/v4/letter/b/e19adc/32.png) [@Bigtalljv](https://forum.inductiveautomation.com/u/Bigtalljv)\
**Post date:** [January 20, 2026, 5:51pm UTC](https://forum.inductiveautomation.com/t/script-averaging-calculations-part-2/113323/11 "2026-01-20T17:51:15Z")

</div>

I do have the tags in historian; I think I went this way because I didn’t understand what I was doing. How does having a tag history change the approach?

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [January 20, 2026, 5:56pm UTC](https://forum.inductiveautomation.com/t/script-averaging-calculations-part-2/113323/12 "2026-01-20T17:56:53Z")

</div>

No need to manually query at that point. Use a tag history binding with the selected date range and select average as the aggregation mode.

---

<div class="post-metadata">

**Author:** ![Bigtalljv](https://avatars.discourse-cdn.com/v4/letter/b/e19adc/32.png) [@Bigtalljv](https://forum.inductiveautomation.com/u/Bigtalljv)\
**Post date:** [January 20, 2026, 6:16pm UTC](https://forum.inductiveautomation.com/t/script-averaging-calculations-part-2/113323/13 "2026-01-20T18:16:20Z")

</div>

oh, that looks easier. any thoughts on how to deal with the three day, three hour time block? I can’t really do that in one tag history query can I?

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [January 20, 2026, 6:28pm UTC](https://forum.inductiveautomation.com/t/script-averaging-calculations-part-2/113323/14 "2026-01-20T18:28:20Z")

</div>

Use custom properties to hold the “selected” dates and use expressions to determine start and end. Bind the custom props to the date inputs in the tag history binding:

(use average instead of closest value)

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/5/c55089359be031ea0f834af621b6f577fc3fd387.png)

---

<div class="post-metadata">

**Author:** ![Bigtalljv](https://avatars.discourse-cdn.com/v4/letter/b/e19adc/32.png) [@Bigtalljv](https://forum.inductiveautomation.com/u/Bigtalljv)\
**Post date:** [January 20, 2026, 6:46pm UTC](https://forum.inductiveautomation.com/t/script-averaging-calculations-part-2/113323/15 "2026-01-20T18:46:06Z")

</div>

Thanks, but you’re going to need to back up a step, where are you in that screen? custom property to what?

---

<div class="post-metadata">

**Author:** ![Bigtalljv](https://avatars.discourse-cdn.com/v4/letter/b/e19adc/32.png) [@Bigtalljv](https://forum.inductiveautomation.com/u/Bigtalljv)\
**Post date:** [January 20, 2026, 6:52pm UTC](https://forum.inductiveautomation.com/t/script-averaging-calculations-part-2/113323/17 "2026-01-20T18:52:37Z")

</div>

perspective…

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [January 20, 2026, 6:53pm UTC](https://forum.inductiveautomation.com/t/script-averaging-calculations-part-2/113323/18 "2026-01-20T18:53:33Z")

</div>

Please update the title. The process will be much the same.

Create 4 custom properties on the view (start, end, data, avg).

start:

```python
dateArithmetic(now(),-3,"day")

```

end:

```python
now()

```

data:

tag history binding for your tag using the start and end date specified

avg (pseudocode):

```python
mean({data},1)

```

You may want to change the argument for now() if you don’t want it updating so often. Or use a script when the view opens to set start and end instead of using a binding. If you’re swapping between hours/days you’ll need some sort of additional logic. If you’re displaying both you can duplicate the process for the other range(s).

---

<div class="post-metadata">

**Author:** ![Bigtalljv](https://avatars.discourse-cdn.com/v4/letter/b/e19adc/32.png) [@Bigtalljv](https://forum.inductiveautomation.com/u/Bigtalljv)\
**Post date:** [January 20, 2026, 6:59pm UTC](https://forum.inductiveautomation.com/t/script-averaging-calculations-part-2/113323/19 "2026-01-20T18:59:01Z")

</div>

Gotcha, I understand. I’m kind of back to where I started but not needing the scripting I don’t really understand.

I don’t see how to edit the title in the forum…

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [January 20, 2026, 7:04pm UTC](https://forum.inductiveautomation.com/t/script-averaging-calculations-part-2/113323/20 "2026-01-20T19:04:22Z")

</div>

There's a pencil that will appear if you hover over the title. I added the perspective tag for you.

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [January 20, 2026, 7:05pm UTC](https://forum.inductiveautomation.com/t/script-averaging-calculations-part-2/113323/21 "2026-01-20T19:05:35Z")

</div>

Add 3 (we can use a transform since this is perspective so that removes a step, no need for the “data” value) custom values in the custom props section on the right.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/8/18a338ac83201fe2b4bb2a9b0919e1913d92b4c2.png)

Bind to `start` and `end` as specified above.

Fill out tag history binding and add transform for the `avg` prop binding:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/2/82b6d0cbd5389138f775afa0bdf83faf50b787f6.png)

[Next page](https://forum.inductiveautomation.com/t/script-averaging-calculations-part-2/113323.md?page=2)
