Tag Event Scripts

微信图片_20200927091748 微信图片_20200927091759
I wrote a script, the purpose is to store the value of the last 5 changes of a tag, but now I found a strange phenomenon, when the measuring point changes once, the same value as the timestamp will be stored, help me

this is script

def setHistory_01(tag,value1,value2,timeStr):
	if value1.value==value2.value:
		return False
	path='[alarm_tag_01]history/DI/DI_0_1/'+tag
	dataSet=system.tag.read(path).value
	pyDataSet=system.dataset.toPyDataSet(dataSet)
	if len(pyDataSet)>4:
		dataSet=system.dataset.deleteRow(dataSet, 0)
		if len(pyDataSet)>5:
			dataSet=system.dataset.deleteRow(dataSet, 0)
	va=value1.value
	tt= system.date.toMillis(timeStr)/1000
	newRow=[va,tt]
	dataSet=system.dataset.addRow(dataSet, newRow)
	system.tag.write(path,dataSet)

The tag value is changed once, but the function is called more than once at the same time. Solve

help,thank

For starters, on this forum code should be placed inside three ` characters (or use the ‘preformatted text’ button) instead of put into quotes which doesn’t maintain formatting.

One potential issue I see is that you’re not filtering out ‘initialChange’ triggers. Therefore whenever you save the tag, restart the tag provider etc. your script will run which may not be what you want

In addition to running on tag edits or project edits (initialChange == True) as Nick noted, I vaguely recall there being a bug in tag editing leaving old code in place. If you aren’t running 8.0.16, you should upgrade. Also, you should edit your comments containing code to include the code formatting markers so it is readable. (Highlight all of your pasted code in the editor and click the </> button.)

thank