Daily Earliest Tag Value Extraction

Hello all,

I am trying to extract a counter value once the machine is ON, at the start of the first shift.
I used query tag (cast function and MIN value involved), but in some cases, it does not work.
So, I was wondering, what different approaches can be applied?

You could use a value change or quality change event on your ‘Machine ON’ tag. Something like

if currentValue.value == 'ON':  //only works when value was OFF and is now ON
  //Read the odometer count tag
  count = system.tag.read('counterTagPath').value  
  //Write count to memory tag for holding start count
  system.tag.write('dailyStartCountTag',count)

What about querying to the min timestamp of your data?

Hello, JordanCClark, how such query will looks like?

That depends, what does your table look like? :wink:
Also, what database are you using?

An example may look like:

SELECT count_value 
FROM my_table
WHERE t_stamp > '2020-05-21 06:00:00'
ORDER BY t_stamp
LIMIT 1

You could also use less than, if you want the last value before the start of shift.

SELECT count_value 
FROM my_table
WHERE t_stamp < '2020-05-21 06:00:00'
ORDER BY t_stamp DESC
LIMIT 1

Got.error "incorrect syntax near LIMIT…"Strange, because the syntax seems correct.

What kind of database? Some don’t have LIMIT, but use TOP n instead.

Oh, yes, it is SQL Express.

The proper syntax for SQL Server Express (my case) is:
SELECT TOP 1 count_value
FROM my_table
WHERE t_stamp < ‘2020-05-21 06:00:00’
ORDER BY t_stamp DESC,

but such query will not work for me, since it uses fixed date, and I need this query to be executed every time the machine has been started, on the current date.