A tag average over time

So like

avg = system.tag.queryTagHistroy(paths=["East Res/Water Age Hours"], startDate=startDate, endDate=endDate, returnSize=1,aggregationMode="SimpleAverage")

???

Because I have tried this as well.

I'm trying an expression tag now

your tag provider is not included in what you just posted. If your tag provider is [default] then put

"[default]East Res/Water Age Hours"
1 Like

So default is the tag provider?

Under Tag Browser is goes like this

#Tags
##East Res
###Water Age Hours

By default, haha, yes it is default. You can create more tag providers as you like for organizational purposes.

Go to Project → Properties → Project → General
This will show the Default Provider for Tag Settings

On the tag browser there is a selection for the tag provider, a default tag provider will look like this:

image

In a gateway scope you must include the tag provider in the path.

I don’t believe that you can use an expression tag to retrieve tag history.

You would have to use runScript(), probably not advisable though. (whispering so pturmel doesn't hear me)

2 Likes

sigh

I’m Just having now luck. I can run the script in the script console and can print out the number but I just cant seem to write it to a tag.

I even tried the hush hush way, says the tag has an error on line one where the = is after endDate

Also tried to add the script to a tag event and send it out to the memory tag (changing the pathing for Water Age and such by using the tag Add button of course and it says there is an error even with the write to tag # out.

If you run the script with the system.tag.writeBlocking() call from the script console, what happens?

Be sure to use the provider name in the path.

Ok so I get an Error saying this

Error writing to tag [‘East Res/Water Age Average’]
Tag Provider "east res’ not found.

system.tag.write([“East Res/Water Age Average”],avg.getValueAt(0,1))

Heh. (:

You didn't use the provider name.

system.tag.writeBlocking(['[Your Provider]East Res/Water Age Average'],[avg.getValueAt(0,1)])

That gives me this error

File “< buffer >”, line 5, in < module >
ValueError: com.google.common.collect.ComputationException: com.inductiveautomation.ignition.common.sqltags.parser.TagPathFormatException: Illegal character ‘[’ (Line 1 , Char 1)

Holy FFF I think We have it, had to remove the extra brackets around tag provider and tag path.

now = system.date.now()
startDate = system.date.addHours(now, -1)
endDate = now
avg = system.tag.queryTagHistory(paths=["[.]Water Age Days"], startDate=startDate, endDate=endDate, returnSize=1, aggregationMode=“SimpleAverage”)
system.tag.write("[default]Water Age Days",avg.getValueAt(0,1))

Testing now. Will let you know

It definitely works in the script console

nvm, the script in the console only returns a 2

Always

but yet print avg.getValueAt(0,1) is correct

That makes no sense

why does this return a 2 always
system.tag.write("[default]East Res/Water Age Average",avg.getValueAt (0,1))

but this will always return the average
print avg.getValueAt(0,1)

because the script console is printing the return value from system.tag.write()

A return value of 2 means the write is pending.

To see if the script is working you would watch the value of the tag you are writing to to see if it is correct.

Tag comes back null

What is the datatype of the Tag, is it just a pure memory tag?

Yes, I deleted it and created a new memory tag with a different name and it did not change with a tag script. However, when I ran it in the script console it posted a number. The tag that the script is on is the Water Age Hours and the number is frequently changing and I even tried an event script with the same issue.

Now whenever I run the event script (in the script console) with the gateway timer disabled and the tag event in Water Age Hours disabled it will flash the correct number then go right back to null

Create a Gateway Timer Script with whatever rate you would like. Use this script there, it should keep your tag value up to date.

The gateway event timer breaks it.

I set the timer to 10 second and if I run the script in the script console it will return a value on the tag like 47 then after 10 seconds it goes null.

This is my exact script, I cant think of any reason the syntax should be different for the gateway. Maybe you can see something I can’t. This is also the same script in the script console.

now = system.date.now()
startDate = system.date.addHours(now, -1)
endDate = now
avg = system.tag.queryTagHistory(paths=[“East Res/Water Age Days”], startDate=startDate, endDate=endDate, returnSize=1, aggregationMode=“SimpleAverage”)
system.tag.write("[default]East Res/Water Age Ave",avg.getValueAt (0,1))

On that note thank you all again. I wouldn’t have even gotten a working script in the console if it were not for you.