A tag average over time

I provided a link very early in this topic:

https://docs.inductiveautomation.com/display/DOC81/system.tag.queryTagHistory

The simplest way to figure out what you need to supply in that function is to drag your tag onto an EasyChart and look at the contents of the tagPens property.

I need an average for a set time for Water Age Hours, so that it can also be stored in the historian and then added to graphs and a number visible on the screen

Thats partly of how I got to where I am but it’s still coming up with a tag error
Says the value is NULL

syntax error

Might be time to ask support to look over your shoulder. Some subtle nuance of your setup isn’t coming across.

Well I thank you for all the help everyone has given me thus far!

From what I understand, you already have the data stored in the historian, getting the average is just a matter of calculating it.

Adding the average to a graph is a little more difficult, but can still be accomplished, however, if you are truly wanting to do that then perhaps keeping track of the average at the device level is a better approach, then create a tag to pull that in and use the historian to store the value.

Showing a number on a screen would then take care of itself.

I do have one question, can this be put in an expression tag is does it have to be scripted under tag events?

In the script console I’m getting the error

Traceback (most recent call last):
File ‘’, line 4, in
NameError: name ‘startTime’ in not defined

Because you copied from the user manual but didn't define them (or defined them differently)

startDate = system.date.now()
endDate = system.date.addHours(startDate, -1)
avg = system.tag.queryTagHistory(paths=[’{[.]Water Age Hours}’], startDate, endDate, returnSize=1, aggregationMode=“SimpleAverage”)
system.tag.writeBlocking([’’],[avg[0][1]])

Try this

startDate = system.date.now()
endDate = system.date.addHours(startDate, -1)
avg = system.tag.queryTagHistory(paths=['{[.]Water Age Hours}'], startDate=startDate, endDate=endDate, returnSize=1, aggregationMode="SimpleAverage")
#system.tag.writeBlocking(['<InsertPathtoYourTag>'],[avg[0][1]])
print avg[0][1]

TypeError: 'com.inductiveautomation.ignition.common.BasicDataset' object is unscriptable

Odd I’m getting some number from the gateway even though that gave me an error.

change print avg[0][1] to print avg. Post what it prints.

Okay I think I fixed it.

now = system.date.now()
startDate = system.date.addHours(now, -1)
endDate = now
avg = system.tag.queryTagHistory(paths=['{[.]Water Age Hours}'], startDate=startDate, endDate=endDate, returnSize=1, aggregationMode="SimpleAverage")
#system.tag.writeBlocking(['<InsertPathtoYourTag>'],[avg[0][2]])
print avg[0][2]

Dataset [1R ? 2C] without [0][2]
Object is unsubscriptable with [0][2]

You can’t use square bracket notation with a dataset. Instead you need to use the getValueAt function.

endDate = system.date.now()
startDate = system.date.addHours(endDate,-1)
avg = system.tag.queryTagHistroy(paths=['{[.]Water Age Hours}'], startDate=startDate, endDate=endDate, returnSize=1,aggregationMode="SimpleAverage")
print avg.getValueAt(0,1)
1 Like

I get a number here!

15

It won’t always be 15 right as long as the average over the hour changes right?

#EDIT

IT WORKS IN THE SCRPT CONSOLE

Now I just got to get it to write to a memory tag from a gateway script?

right now the tag is returning null

Same script should work for writing to a memory tag.

endDate = system.date.now()
startDate = system.date.addHours(endDate, -1)
avg = system.tag.queryTagHistory(paths='{[.]Water Age Hours}'],startDate=startDate,endDate=endDate, returnSize =1,aggregationMode='SimpleAverage')
system.tag.writeBlocking(['Path to MemoryTag'],[avg.getValueAt(0,1)])

Maybe it doesn't like being a gateway script. I'm still getting Null in the memory tag.

I'm trying different syntax to see if the fixes it. Nothing so far.

You probably need to add a tag provider in the tag path, and use the full path.

"[default]My/Tag/Path"