Help getting query tag to select from desired timestamp

Hi, I’m using a MySQL database connected to an Ignition 8.1.47 gateway and am having trouble with a Query tag inside of my perspective project.

This query tag is supposed to be equal to the value of another logged tag from 2 minutes ago so that I can compare the 2 values to measure rate of change. The query tag updates its values and is reading from the correct tag but only seems to be able to read from ~30 seconds ago, not 2+ minutes. The query is the following:

SELECT   
   d.floatvalue AS value_numeric,   
   d.t_stamp AS source_tstamp
FROM sqlth_te te 
JOIN sqlt_data_1_2025_10 d ON d.tagid = te.id
WHERE te.tagpath LIKE '%AI_LIT02002/HMI/rActiveValue%' 
   AND d.t_stamp <= TIMESTAMP(DATE_SUB(NOW(), INTERVAL 2 MINUTE))
ORDER BY d.t_stamp DESC
LIMIT 1;

In this case, the tag whose historized value I want to read has the path AI_LIT02002/HMI/rActiveValue.

From reading other forum posts, I suspect I am doing something wrong with DATE_SUB, but I cannot figure out what.

The MySQL database and Ignition gateway are both hosted on the same machine.

Any help is appreciated!

Why are you querying the Historian tables directly instead of using the system functions?

I don't know why you're not getting the value from the time you expect, but if this is intended to be more than temporary, then it will fall apart once the historian moves into a different partition table.

1 Like

This was the first way I thought of to get the historized value from some time ago, what system functions are you referring to specifically?

Also this:

I suspect that the TIMESTAMP() function is not required.
Anyway, the queryTagHistory function is the way to go.

Or perhaps system.query.TagCalculations which can do the math for you.

Edit: Irose quick on the draw :slight_smile:

Thank you all for the help! Will try QueryTagHistory and QueryTagCalculations and post results here.