Return Last N records from Tag Historian Query

Is there any way to retrieve last N records from tag historian tables? I would like to retrieve the data as is without applying any aggregations.
One alternative is to query the tables directly but as the tables are partitioned month wise there isn’t way to find which table has the relevant data.
The data is recorded only on change.

Many Thanks

There is no nicely built in way to do it.

Your best bet is to pick a range of time, do a query on that time range, and see how many results you get. If it’s N or more, truncate it to N and you’re done. If it’s less than N, do another query for the prior time period and accumulate more results.

I will warn you that Ignition Historian has many optimizations built in that do things like skip storing data and interpolating results in ways I didn’t expect. It may lead to madness if you’re not careful. I recommend reading ALL of this manual page and being sure you understand what you’re getting into and that you’re configuring the tags properly.

1 Like

Thanks for the suggestion. I wish Ignition provided a feature to retrieve the last N records regardless of the time interval.

Not completely free of time interval, but you can just use system.tag.queryTagHistory() with the returnSize set to 5. So long as you have recorded 5 events with in an interval you'll get the Top 5 records returned.

#returns the top 5 records within the last 8 hours
ds0 = system.tag.queryTagHistory(tagPaths,returnSize=5, noInterpolation = True)

#returns the top 5 records within the last day
ds1 = system.tag.queryTagHistory(tagPaths, system.date.addDays(system.date.now(),-1),returnSize = 5, noInterpolation = True)

Note, use noInterpolation to insure you only get real values.

I know it isn't exactly what you were looking for, but at some point if a value hasn't changed in x amount of time it really becomes irrelevant. I would suggest setting the Max Time between records in the historical settings to insure that you record the value at least once for that interval.

That doesn't get the top 5 results. That splits the 8 hours into 5 even time intervals, and uses whatever the default aggregation mode is to reduce all the (non-interpolated) samples in each interval down to one. The docs don't say what the default aggregation mode is, but I suspect "Average". I can run your sample on an integer-based tag in my system and I get back floating-point results. It's doing some kind of aggregation.