queryTagHistory returning 0 for tag value

I have a tag whose value was last updated about five months ago and has remained unchanged since then. In a script, I’m trying to retrieve the tag’s value from 30 minutes ago using system.tag.queryTagHistory.

However, the query is returning 0 instead of the expected value, and I’m not sure why.

Here’s the script I’m using:

startTime= system.date.now()
endTime = system.date.addMinutes(startTime, 1)

dataSet = system.tag.queryTagHistory(
    paths=[my_tag_path],
    startDate=startTime,
    endDate=endTime,
    returnSize=1,
    aggregationMode="LastValue",
    returnFormat="Wide",
    includeBoundingValues=True
)

print dataSet.getValueAt(0, 1)

Given that the tag hasn’t changed in months, I expected the last known value (from well before the query window) to be returned. Am I misunderstanding how includeBoundingValues, returnSize, or LastValue behave in this scenario?

Any guidance would be appreciated.

You are misunderstanding.

  • The documentation of Last Value aggregation says zero will be returned for intervals that do not have a value.

  • includeBoundingValues specifically applies to Seeded Values, which are defined to be values outside the begin/end timestamps, but near them. Note the other requirements, too.

I don't think any interpretation would place "three months ago" as "near" a begin timestamp of now().

Requesting a one-minute interval starting now() pretty much guarantees there won't be a stored value to return.

Hmm that makes sense. Thanks for the clarification.

So in this case, includeBoundingValues won’t help because the last stored value is far outside the query window, and LastValue correctly returns 0 since there are no samples within that interval. I also tried other aggregation modes like Maximum and Average, but those behave the same way, which I suppose is due to the same logic (right?).

Given that, what is the correct way to retrieve the effective value of a tag at a specific point in time (e.g., what the value was 30 minutes ago) when the tag hasn’t changed in a long time? In other words, how should I query “the most recent value at or before a timestamp,” regardless of how far back it was recorded?

I think I’m still missing the intended pattern for this use case.

There really isn't such a thing.

You should change your time span to be -6 months to now() with return size = 1.

Better, reconfigure your tags to have a max time between samples that isn't infinite. For best results when charting, the max time between samples should be smaller than the smallest normal chart timespan.

1 Like

Interesting. This may be it. I hadn’t considered the max time between samples. I’ll look into updating the tag history settings.

Thanks for the help, I appreciate it.

And also set the type to Discrete so you don't get screwed by the Analog compression algorithm which will still not store a value for a non-changing value even at the max time between samples.

2 Likes