# Tag Event Scripts

**URL:** <https://forum.inductiveautomation.com/t/tag-event-scripts/39096>\
**Category:** Ignition\
**Tags:** scripting, ignition80\
**Created:** [September 27, 2020, 1:25am UTC](https://forum.inductiveautomation.com/t/tag-event-scripts/39096 "2020-09-27T01:25:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![448719222](https://avatars.discourse-cdn.com/v4/letter/4/71e660/32.png) [@448719222](https://forum.inductiveautomation.com/u/448719222)\
**Post date:** [September 27, 2020, 1:25am UTC](https://forum.inductiveautomation.com/t/tag-event-scripts/39096/1 "2020-09-27T01:25:07Z")

</div>

![微信图片_20200927091748](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/6/a63f88209a4a4ddfdae299f7f9a9437f6856c215.png) ![微信图片_20200927091759](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/8/b8719e630ceba4815abdf287e949790e8f716a55.png)  
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

 ![微信图片_20200927092441](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/0/50e793f6bd79d28c846a0a8bb095a980b3d62ecc.png)

---

<div class="post-metadata">

**Author:** ![448719222](https://avatars.discourse-cdn.com/v4/letter/4/71e660/32.png) [@448719222](https://forum.inductiveautomation.com/u/448719222)\
**Post date:** [September 27, 2020, 1:25am UTC](https://forum.inductiveautomation.com/t/tag-event-scripts/39096/2 "2020-09-27T01:25:58Z")

</div>

> [@448719222](#):
>
> 目的是存储标记的最后

this is script

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![448719222](https://avatars.discourse-cdn.com/v4/letter/4/71e660/32.png) [@448719222](https://forum.inductiveautomation.com/u/448719222)\
**Post date:** [September 27, 2020, 2:19am UTC](https://forum.inductiveautomation.com/t/tag-event-scripts/39096/3 "2020-09-27T02:19:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![448719222](https://avatars.discourse-cdn.com/v4/letter/4/71e660/32.png) [@448719222](https://forum.inductiveautomation.com/u/448719222)\
**Post date:** [September 27, 2020, 7:20am UTC](https://forum.inductiveautomation.com/t/tag-event-scripts/39096/4 "2020-09-27T07:20:34Z")

</div>

> [@448719222](#):
>
> 但是该函数同时被多次调用。

help,thank

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [September 27, 2020, 11:07am UTC](https://forum.inductiveautomation.com/t/tag-event-scripts/39096/5 "2020-09-27T11:07:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 27, 2020, 1:25pm UTC](https://forum.inductiveautomation.com/t/tag-event-scripts/39096/6 "2020-09-27T13:25:56Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![448719222](https://avatars.discourse-cdn.com/v4/letter/4/71e660/32.png) [@448719222](https://forum.inductiveautomation.com/u/448719222)\
**Post date:** [October 9, 2020, 12:54am UTC](https://forum.inductiveautomation.com/t/tag-event-scripts/39096/8 "2020-10-09T00:54:24Z")

</div>

thank
