Relative Tag Paths with queryTagHistory Function

Hello, I am attempting to call queryTagHistory() within the Value Changed script of TagA, to find the history of TagB which resides in the same folder.

This logic will be applied to other tag trees so it needs to reference the relative tag path "[.]TagB" when getting historical data.

This seems simple enough, but I'm wondering if maybe I am doing something wrong syntactically. Here is the script for Value Changed for tag A

endTime = system.date.now()
startTime = system.date.addMinutes(endTime, -1)
dataSet = system.tag.queryTagHistory(paths=["[.]TagB"], startDate=startTime, endDate=endTime, returnSize=1, aggregationMode="Range", returnFormat='Wide')

logger.info(str(dataSet.getValueAt(0,1)))

The logger returns "None" in this instance. But, when I use the very same relative tag path in readBlocking(), the tag data is returned.

tagValue = system.tag.readBlocking(["[.]TagB"])
logger.info(str(tagValue))

Here, the logger returns [[1234567, Good, Thu May 09 14:11:33 CDT 2024 (1715281893803)]] so I know it is getting the right value.

I also tested my same queryTagHistory call with the full path name for TagB instead of the relative path name and I get the correct historical data I am looking for.

dataSetFullPath = system.tag.queryTagHistory(paths=["[provider]path1/path2/path3/TagB"], startDate=startTime, endDate=endTime, returnSize=1, aggregationMode="Range", returnFormat='Wide')

logger.info(str(dataSetFullPath.getValueAt(0,1)))

Accurately yields a valid number.

Would anyone be able to weigh in on why system.tag.queryTagHistory(paths=["[.]TagB"], ... would not work but system.tag.readBlocking(["[.]TagB"]) does? Thank you in advance

That is an extremely bad idea. Tag valueChange scripts must execute very quickly--single digit millis or less--or you can bog down all tag events. History queries are definitely not acceptable. (Nor any other database operations, nor any tag/opc operations that reach out over the network.)

What do you need to do this for, really?

2 Likes

TagA is an expression tag that would have a fixed execution rate of 1 min, its job is to get the value of historized TagB from 1 minute ago and compare it to the current value.

I am not necessarily dead set on using the Value Changed, I was more or less just testing out that I could get the value of TagB from 1 minute ago and do some scripting on that value, which led me here. Any suggestions you may have on how to accomplish this would be appreciated.

I recommend using a scheduled event that runs at the top of the minute. Have its script pull the appropriate history in one go for all the tags that need this kind of processing.

1 Like

If I use a scheduled event, that doesn't solve my problem for needing to write out the entire tag path, correct? This is logic that will need to be expandable to several paths even within the same project, so the path would need to be relative.

1 Like

No, the script would have a constant list of all of the absolute tag paths.

Would this be the reason that the relative tag path would not work for queryTagHistory?

system.tag.queryTagHistory(paths=["[.]TagB"], ...

Is this syntax not acceptable for the function?
It works fine with the full tag path and system.tag.readBlocking(["[.]TagB"]) shows that it is a valid relative path.

I believe @pturmel is saying that he is completely against using the queryTagHistory function at all in valueChanged event. Even if it did work, your next post on the forums would be "Why is my gateway performing so badly/slowly". He's suggesting other ways of accomplishing what you're wanting to do.

With that being said, there's alternate ways to get the full path without using relative referencing since the tagPath is passed in as a parameter of the event/function, but again, take @pturmel's advice as he's very experienced at what he does. Outside of the Inductive Automation staff, he's the top contributor to these forums.

That's no problem, I can find a different way of going about this. I was more or less still curious about my original question and why this call wasn't working. Either way, I'd take yours and @pturmel 's advice here.

Thank you

This. Relative paths are only applicable in tag events and expression tags, neither of which is an acceptable place to call history.

1 Like