Historian On Change Tag with Consecutive Duplicates

Hello,

I have a string OPC tag that is configured with ‘Sample Mode’ set to ‘On Change’.

Firstly, can anyone tell me why it is it that if I query the historian for this tag, I see consecutive duplicates?

Second, how do I query the historian for the last value logged (and its timestamp)?
dataset = system.tag.queryTagHistory(paths=tagPaths, startDate = startTime, returnSize=-1, aggregationMode="LastValue", noInterpolation = 0, ignoreBadQuality = 1)
is returning all logged data from startTime until now rather than just the last one…

stime = '4/3/2020 13:44:00'
stime = system.date.parse(str(stime),"MM/dd/yyyy HH:mm")
startTime = system.date.format(stime,"E MMM d HH:mm:ss z yyyy")
tags=['Path/To/My/Bool/Tag']
ret = system.tag.queryTagHistory(paths=tags, startDate = startTime, returnSize=1, aggregationMode="LastValue", noInterpolation = 1, ignoreBadQuality = 1)

Try setting your return size to 1 and noInterpolation to 1 as well.
The above just worked fine for me.

Thanks, comparing the result with the latest historian sqlt_data_X_XXXX_XX table, your query looks like its returning the latest logged value, unfortunately, the timestamp is the same as what you set the query start time to rather than when it actually got logged...

Id really like to understand why I am getting consecutive duplicates in the first place… The fact that this is happening is causing me to try figure out a janky work around to what I am trying to accomplish.

I think what is happening is that the tag has its last value stored in history then the device loses connection (On Change triggered), it then logs the same value but with bad quality then the device reconnects, on change is triggered again and it logs the same value with good quality again. I will verify if this is the case on Monday.

I cam across this http://forum.inductiveautomation.com/t/table-tag-history-and-bad-quality/4319

But even if I could filter out the bad quality, I will still have consecutive duplicates each time the device loses connection and reconnects (just one less).

This morning I created a tag history binding to the tag in question on a table component and verified my above post.

As a work around, I have turned off tag history and put this tag event script on the tag on value changed so that it only logs if the value of the tag changes:

	try:		
		if currentValue.value != previousValue.value:
			histProv = "NewConnection"
			tagProv = "default"
			paths = [tagPath]      
			values = [currentValue.value]
			system.tag.storeTagHistory(histProv, tagProv, paths, values)
	except:
		pass

Will check tomorrow to see if it is working as I expect.