Hi every one,
I use this code to store "data" (which is a real-time calculated value) as a history tag (tag5):
paths = ["folder/tag5"]
system.tag.storeTagHistory(histProv, tagProv, paths, data)
Then I try to query the created history tag's values using this:
endTime = system.date.now()
startTime = system.date.addHours(endTime, -24)
Hist_Tag_Paths = ["[tagProv]folder/tag5"]
Cols = ["data_query"]
result = system.tag.queryTagHistory(paths=Hist_Tag_Paths,
startDate=startTime, endDate=endTime, returnSize=0, aggregationMode="Average", returnFormat='Wide', columnNames = Cols)
I have also added tag5 as a pen to a PowerChart and I can see its values plotted as a graph, but the "result" returned by system.tag.queryTagHistory
is an empty dataset.
Does anyone have any idea why system.tag.queryTagHistory
can't find this history tag?
Thanks
Does the tag exist? And have history turned on?
(.storeTagHistory()
is not intended to be the primary path to store history, but is a tool to backfill missing history for a tag that otherwise records normally.)
This was the issue. I enabled the tag's history and now I can get the values using system.tag.queryTagHistory
.
Thank you for your help @pturmel
Another issue I’ve noticed with this implementation is that when the Designer or any session is not open, the script does not seem to historize data to tag5 properly — likely because it is not being executed at all.
The script is designed to run in real time as a transform on an expression binding to a main view parameter (see attached screenshot). It calculates the "data" value using system.tag.queryTagCalculations
based on another history tag’s values, and then stores the result to tag5 as described earlier.
Any idea regarding this is appreciated
Correct, if its a binding on a view then it's not being run if there is no session. It also means it's being run on EVERY session that is open. Tha's probably filling your history with a bunch of duplicates.
Make an expression tag that calculates this value and enable history on that tag. Or if the calculation is too complex for an expression, make a gateway timer event and do the calculation in there.
Thanks for your response, Ryan.
I initially wanted to implement the calculation as an expression tag, but my calculation requires taking the average of historical values over a predefined period. I couldn’t find a way to do this with an expression tag, as it seems expression tags don’t support historical queries — or at least I'm not aware of a way to accomplish it.
Using a gateway timer script is a great idea. Thank you for the suggestion!