Tag Query History: get the last record before start datetime

given startDatetime = 1:00 pm and endDatetime = 5:00 pm, I am querying Machine State using system.tag.queryTagHistory
Data returned like so:
value='Run' at timestamp 1:30pm
value='Stop' at timestamp 1:50pm
etc.

How can I get the last row(record) before 1:30pm?

You can't, really. I was very surprised at the lack of interpolation / extrapolation offered in tag history queries to the point of finding it useless for many applications. You can set maximum time between samples in tag history properties but can't specify when the occur.

Take control! Create your own data table, create a gateway scheduled task to record tag value on the hour or whatever suits.

1 Like

Thanks.
I was querying for last 24 hours machine state.
I move my startDatetime 7 days back to the past to capture more records and get the last record before the initial startDatetime.

But really, what if there was no entry for the past 7 days.. Oh well, I can just put Unknown state when this happens.

Maybe I'm wrong.

But would system.tag.queryTagHistory with a return size of 1 and an aggregate mode of LastValue not return the last value in a time range. Then set end date to whatever time you want?

I am still not very familiar with tag historian querying.
In my case the data are discrete, which is a string such as "Run", "Stop", "Standby".

And from querying it looks like it logs one row per state change.

I tried to set startDatetime and endDatetime to a time where it is between records and query returns nothing.