# Double trigger on tag Value Changed script

**URL:** <https://forum.inductiveautomation.com/t/double-trigger-on-tag-value-changed-script/79418>\
**Category:** Ignition\
**Created:** [September 20, 2023, 8:11pm UTC](https://forum.inductiveautomation.com/t/double-trigger-on-tag-value-changed-script/79418 "2023-09-20T20:11:05Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![pjgonzales](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@pjgonzales](https://forum.inductiveautomation.com/u/pjgonzales)\
**Post date:** [September 20, 2023, 8:11pm UTC](https://forum.inductiveautomation.com/t/double-trigger-on-tag-value-changed-script/79418/1 "2023-09-20T20:11:05Z")

</div>

I have a boolean tag that occasionally gets set TRUE from a PLC. The tag has a value change script that should run when its value becomes TRUE but for some reason its running the script when it goes TRUE and again when it goes FALSE. I tested it by turning it into a memory boolean and toggling it myself. The script begins as such:

```auto
if not initialChange:
     if currentValue.value == True:
          "area where scripting magick happens"

```

I'm a little lost as to why it would trigger when the tag goes into the FALSE state, and any input would be much appreciated.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [September 20, 2023, 8:22pm UTC](https://forum.inductiveautomation.com/t/double-trigger-on-tag-value-changed-script/79418/2 "2023-09-20T20:22:02Z")

</div>

Can you prove that's actually what's happening?

Add print statements to the "magick":

```python
if not initialChange:
     if currentValue.value == True:
          print "initialChange=%s currentValue.value=%s" % (initialChange, currentValue.value)
           # area where scripting magick happens 

```

Also, copy all of your script into an external editor and make sure it's all tabs or all spaces. There was another thread with weird behavior recently that was caused by a mix of tabs/spaces in the script.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [September 20, 2023, 8:26pm UTC](https://forum.inductiveautomation.com/t/double-trigger-on-tag-value-changed-script/79418/3 "2023-09-20T20:26:38Z")

</div>

And don't forget that you can just use,  
`if currentValue.value:`  
This will return a logical `True` or `False` and the logic will respond correctly. For False testing use,  
`if not currentValue.value:`

---

<div class="post-metadata">

**Author:** ![pjgonzales](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@pjgonzales](https://forum.inductiveautomation.com/u/pjgonzales)\
**Post date:** [September 20, 2023, 8:28pm UTC](https://forum.inductiveautomation.com/t/double-trigger-on-tag-value-changed-script/79418/4 "2023-09-20T20:28:59Z")

</div>

It's all tabs, I've gone through and checked multiple times.  
I have posted the entire script below, minus the "magick" part, which was just supposed to be a placeholder. After running calculations it's writing an adjusted value back to a single tag for now, during my testing phase, and when I set the boolean tag to TRUE I can see the value in the other tag change. If I set the boolean tag to FALSE, I see the value in the other tag change a second time.

```auto
	if not initialChange:
		if currentValue.value == True:
			for i in range(0,19):
				for x in range(1,96):
					value = system.tag.read("[.]ARRAYS/ARRAY "+str(i)+"/MEMBER "+str(x))
					system.tag.write("[.]DATA["+str(i)+","+str(x-1)+"]", value)
			
			for i in range(0,95):
			    values = []
			    for x in range(0,19):
			        calc = system.tag.read("[.]DATA["+str(x)+","+str(i)+"]")
			        values.append(calc.value)
			    cValue = system.math.median(values)
			    system.tag.write("[.]MEDIANS[0,"+str(i)+"]", cValue)
		
		Enable = True
		if Enable == True:
		
			for i in range(1,97):
				Mode = True
				medianValue = system.tag.read("[default]PACKAGING/LINE 6/CAMERA/CAMERA DATA/MEDIANS[0,"+str(i-1)+"]").value
				flowSP = system.tag.readBlocking("[default]PACKAGING/LINE 6/HUEFT/OVERFILL PER VALVE/VALVE"+str(i)+"/SP_TIMER_FLOWRATE")[0].value
				AutoMode = True
	
				if Mode == True and AutoMode == True:
					if medianValue >= 980:
						adjustedFlow = flowSP + 12
					elif 980 > medianValue >= 920:
						adjustedFlow = flowSP + 6
					elif 920 > medianValue >= 880:
						adjustedFlow = flowSP + 3
					elif 720 > medianValue > 700:
						adjustedFlow = flowSP - 3
					elif 700 > medianValue > 640:
						adjustedFlow = flowSP - 6
					elif 620 >= medianValue >= 0:
						adjustedFlow = flowSP - 12
					elif medianValue == -1:
						adjustedFlow = flowSP
		
	# system.tag.write("[default]PACKAGING/LINE 6/HUEFT/OVERFILL PER VALVE/VALVE"+str(i)+"/SP_TIMER_FLOWRATE", flowSP)
				system.tag.write("[default]Scratchpad/testFlow["+str(i-1)+"]", adjustedFlow)
				if i == 1:
					system.tag.write("[default]PACKAGING/LINE 6/HUEFT/OVERFILL PER VALVE/VALVE1/SP_TIMER_FLOWRATE", adjustedFlow)

```

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [September 20, 2023, 8:34pm UTC](https://forum.inductiveautomation.com/t/double-trigger-on-tag-value-changed-script/79418/5 "2023-09-20T20:34:38Z")

</div>

> [@pjgonzales](#):
>
> It's all tabs, I've gone through and checked multiple times.

When I paste your code into Notepad++ I can see that lines 8 to 14 have three tabs and then multiples of four spaces. I don't think that's your problem though.

---

<div class="post-metadata">

**Author:** ![pjgonzales](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@pjgonzales](https://forum.inductiveautomation.com/u/pjgonzales)\
**Post date:** [September 20, 2023, 8:43pm UTC](https://forum.inductiveautomation.com/t/double-trigger-on-tag-value-changed-script/79418/6 "2023-09-20T20:43:50Z")

</div>

Naturally, just because I said I had checked, the gremlins added spaces.

---

<div class="post-metadata">

**Author:** ![pjgonzales](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@pjgonzales](https://forum.inductiveautomation.com/u/pjgonzales)\
**Post date:** [September 20, 2023, 9:38pm UTC](https://forum.inductiveautomation.com/t/double-trigger-on-tag-value-changed-script/79418/7 "2023-09-20T21:38:49Z")

</div>

Because this script is in a tag event, where would I find the print output? I see nothing in the console or the log files.

---

<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 20, 2023, 9:43pm UTC](https://forum.inductiveautomation.com/t/double-trigger-on-tag-value-changed-script/79418/8 "2023-09-20T21:43:56Z")

</div>

In the wrapper log. (Don't use `print` in gateway scope. Use a logger.)

---

<div class="post-metadata">

**Author:** ![pjgonzales](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@pjgonzales](https://forum.inductiveautomation.com/u/pjgonzales)\
**Post date:** [September 20, 2023, 11:08pm UTC](https://forum.inductiveautomation.com/t/double-trigger-on-tag-value-changed-script/79418/9 "2023-09-20T23:08:53Z")

</div>

Well, I got it working by adding an evaluation of the currentValue to the script at line 45:

```auto
					if i == 1 and currentValue.value == True:
						system.tag.write("[default]PACKAGING/LINE 6/HUEFT/OVERFILL PER VALVE/VALVE1/SP_TIMER_FLOWRATE", adjustedFlow)

```

I can't tell if its triggering the script twice and only writing once or if its actually triggering the script only one time, but I don't see the result of my calculations being added to the tag twice. Doesn't make sense because I would think that the initial evaluation of the currentValue would be all that was needed, but, here we are. 🤔

---

<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 20, 2023, 11:20pm UTC](https://forum.inductiveautomation.com/t/double-trigger-on-tag-value-changed-script/79418/10 "2023-09-20T23:20:56Z")

</div>

> [@pjgonzales](#):
>
> I can't tell if its

> [@pturmel](#):
>
> Use a logger.

Yes, really. `system.util.getLogger(...)`.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [September 20, 2023, 11:25pm UTC](https://forum.inductiveautomation.com/t/double-trigger-on-tag-value-changed-script/79418/11 "2023-09-20T23:25:32Z")

</div>

Look closer at your logic / indentation:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/a/da7faf45425c59816500cf71a4141750b3e3c24e.png)
