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.
water2
April 21, 2021, 1:38pm
22
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
water2
April 21, 2021, 1:39pm
23
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.
water2
April 21, 2021, 1:41pm
25
Well I thank you for all the help everyone has given me thus far!
lrose
April 21, 2021, 1:43pm
26
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.
water2
April 21, 2021, 1:47pm
27
I do have one question, can this be put in an expression tag is does it have to be scripted under tag events?
water2
April 21, 2021, 2:25pm
28
In the script console I’m getting the error
Traceback (most recent call last):
File ‘’, line 4, in
NameError: name ‘startTime’ in not defined
water2:
startDate = system.date.now()
endDate = system.date.addHours(startDate, -1)
avg = system.tag.queryTagHistory(paths=[’{[.]Water Age Hours}’], startDate=startTime, endDate=endTime, returnSize=1, aggregationMode=“SimpleAverage”)
system.tag.writeBlocking([’’],[avg[0][1]])
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]
water2
April 21, 2021, 2:59pm
31
dkhayes117:
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
water2
April 21, 2021, 3:00pm
32
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]
water2
April 22, 2021, 11:36am
35
dkhayes117:
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]
lrose
April 22, 2021, 12:07pm
36
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
water2
April 22, 2021, 12:21pm
37
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
lrose
April 22, 2021, 12:31pm
38
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)])
water2
April 22, 2021, 12:36pm
39
Louis Rose:
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")
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"