# Script Run Twice

**URL:** <https://forum.inductiveautomation.com/t/script-run-twice/57565>\
**Category:** Ignition\
**Tags:** ignition80\
**Created:** [March 23, 2022, 10:03pm UTC](https://forum.inductiveautomation.com/t/script-run-twice/57565 "2022-03-23T22:03:55Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![splata](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@splata](https://forum.inductiveautomation.com/u/splata)\
**Post date:** [March 23, 2022, 10:03pm UTC](https://forum.inductiveautomation.com/t/script-run-twice/57565/1 "2022-03-23T22:03:55Z")

</div>

hello.

I have an application running well , when i push a button this copy the values of some field text and number fields in a NamedQuery variables , this values are inserted in a table in a database. to here everything war running fine but , I need automate the same application with a simple script and I observed that is running twice,I did put this script in a transform of a custom variable when this variable change the value to “True” the script transform is executed , I tested in the script console the same script an running only 1 time

```auto
Code="+52"
Num=Code+"4420000000" # I replace this number
Name="Alerta App i4.0: "+"Rutina "+" ha iniciado"
Status=0
Params={'Numero':Num,'Texto':Name,'Status':Status}
system.db.runNamedQuery("Twilio/Insert",Params)

```

---

<div class="post-metadata">

**Author:** ![josborn](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@josborn](https://forum.inductiveautomation.com/u/josborn)\
**Post date:** [March 24, 2022, 2:01am UTC](https://forum.inductiveautomation.com/t/script-run-twice/57565/2 "2022-03-24T02:01:18Z")

</div>

Can you show us where you are running this code, i am not seeing where you are telling to not run when the value is False.

---

<div class="post-metadata">

**Author:** ![splata](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@splata](https://forum.inductiveautomation.com/u/splata)\
**Post date:** [March 24, 2022, 5:03pm UTC](https://forum.inductiveautomation.com/t/script-run-twice/57565/3 "2022-03-24T17:03:14Z")

</div>

Sure.  
here´s the custom properties  
“URenRutina” is binding with a boolean condition “if robot is running:”

“EnWhats” is a boolean condition if the fields are filled correctly.

ScriptCount: is an integer that I use to find out how many time run the script

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/f/ef3da31b0d166db438c53da328b770010b3e426d.png)

Here is the components:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/1/d1a8b69561633ee6f8a034192b49356b9a84fa48.png)

in the binding of “URenRutina” I made this transform script

```auto
def transform(self, value, quality, timestamp):
	"""
	Transform the incoming value and return a result.

	Arguments:
		self: A reference to the component this binding is configured on.
		value: The incoming value from the binding or the previous transform.
		quality: The quality code of the incoming value.
		timestamp: The timestamp of the incoming value as a java.util.Date
	"""
	
	if self.getChild("root").getChild("CoordinateContainer").getChild("FlexRepeaterAndRoutines").getChild("Routines").getChild("MurrElectronik").getChild("PhotoCell").getChild("ToggleRoutinePhoto").props.selected:
				RutinaN="Fotoelectrico en BlockIO Impact 67 Murrelectronik"
	if self.getChild("root").getChild("CoordinateContainer").getChild("FlexRepeaterAndRoutines").getChild("Routines").getChild("MurrElectronik").getChild("Inductive").getChild("ToggleRoutineInd").props.selected:
				RutinaN="Inductivo en BlockIO Impact67 Murrelectronik"
	if self.getChild("root").getChild("CoordinateContainer").getChild("FlexRepeaterAndRoutines").getChild("Routines").getChild("Sick").getChild("Photocell").getChild("RoutineSick").props.selected:
				RutinaN="Fotoelectrico en BlockIO SIG200"
	if self.getChild("root").getChild("CoordinateContainer").getChild("FlexRepeaterAndRoutines").getChild("Routines").getChild("Coretigo").getChild("Encoder").getChild("Encoder_0").props.selected:
				RutinaN="Encoder en BlockIO Coretigo"
	if self.getChild("root").getChild("CoordinateContainer").getChild("FlexRepeaterAndRoutines").getChild("Routines").getChild("Coretigo").getChild("Inductive").getChild("Inductive Rob").props.selected:
				RutinaN="Inductivo en BlockIO Coretigo"
	if self.getChild("root").getChild("CoordinateContainer").getChild("FlexRepeaterAndRoutines").getChild("Routines").getChild("Opto").getChild("Buttons").getChild("ToggleSwitch_4").props.selected:
				RutinaN="Pulsar botones en Opto 22"
	
		
	if value and self.custom.EnWhats:
				
				Code=self.getChild("root").getChild("CoordinateContainer").getChild("FlexRepeaterAndRoutines").getChild("Routines").getChild("Messages").getChild("FixTemplate").getChild("Number").getChild("Dropdown").props.value
				Num=Code+str(self.getChild("root").getChild("CoordinateContainer").getChild("FlexRepeaterAndRoutines").getChild("Routines").getChild("Messages").getChild("FixTemplate").getChild("Number").getChild("NumericEntryField").props.value)
				Name="Alerta App i4.0: "+"Rutina "+RutinaN+" ha iniciado"
				Status=0
				Params={'Numero':Num,'Texto':Name,'Status':Status}
				system.db.runNamedQuery("Twilio/Insert",Params)
				self.custom.ScriptCount=self.custom.ScriptCount+1
			
				
	return value

```

the last code related to variables of my application but if I make a change for a fixed number and text (next code)the result is the same , in the table receive 2 rows with the same data and the custom property “ScriptCount” value get moving 2,4,6 …

```auto

Num="+524420000000" # 
Name="Alerta App i4.0: Rutina ha iniciado"
Status=0
Params={'Numero':Num,'Texto':Name,'Status':Status}
system.db.runNamedQuery("Twilio/Insert",Params)

```

if I run this code in the Script Console this only run one time. the row 251 did run in the script console  
and the next 252,253,524,255 was running in with the transform script

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

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [March 24, 2022, 5:51pm UTC](https://forum.inductiveautomation.com/t/script-run-twice/57565/4 "2022-03-24T17:51:57Z")

</div>

You should be running this script in a change event, rather than in a script transform on the binding.

Right Click on the `URenRutina` property and then click on Add Change Script…

Move this script from the transform to the change script. There you can use the arguments to filter the script as appropriate.

---

<div class="post-metadata">

**Author:** ![splata](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@splata](https://forum.inductiveautomation.com/u/splata)\
**Post date:** [March 24, 2022, 6:50pm UTC](https://forum.inductiveautomation.com/t/script-run-twice/57565/5 "2022-03-24T18:50:07Z")

</div>

Thank you Irose for your suggestion:

it was my first option to find the solution but with the same results

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/6/b62f9e5f79e34a25f32a005e1daccb6a4a04595d.png)

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/0/f0f1b05023d7647549fe1b4240f55c77eafb25df.png)

---

<div class="post-metadata">

**Author:** ![josborn](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@josborn](https://forum.inductiveautomation.com/u/josborn)\
**Post date:** [March 24, 2022, 7:11pm UTC](https://forum.inductiveautomation.com/t/script-run-twice/57565/6 "2022-03-24T19:11:39Z")

</div>

Try putting a print statement outside of your If statement.

```auto
system.perspective.print(message= currentValue)

```

Then tell us what your console says.

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [March 24, 2022, 7:35pm UTC](https://forum.inductiveautomation.com/t/script-run-twice/57565/7 "2022-03-24T19:35:04Z")

</div>

1. Verify you haven’t replicated that code to run in multiple places. Some users will often configure scripts in one Action and then realize later on that the script should be in a different Action - but they often forget to remove the original implementation. Use the Find/Replace tool to search for your “Alerta App i4.0” text and see if it is in place somewhere else.
2. Show us the _full_ binding, instead of just the transform; there’s a very real possibility your binding is causing this issue instead of the transform.

---

<div class="post-metadata">

**Author:** ![splata](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@splata](https://forum.inductiveautomation.com/u/splata)\
**Post date:** [March 24, 2022, 7:48pm UTC](https://forum.inductiveautomation.com/t/script-run-twice/57565/8 "2022-03-24T19:48:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [March 24, 2022, 8:04pm UTC](https://forum.inductiveautomation.com/t/script-run-twice/57565/9 "2022-03-24T20:04:12Z")

</div>

I wouldn’t expect a valueChanged script to trigger on a quality change.

Are you filtering to insure a value change has actually occurred?

Something like:

```python
if currentValue.value != previousValue.value:
    #do work here

```

---

<div class="post-metadata">

**Author:** ![splata](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@splata](https://forum.inductiveautomation.com/u/splata)\
**Post date:** [March 24, 2022, 8:11pm UTC](https://forum.inductiveautomation.com/t/script-run-twice/57565/10 "2022-03-24T20:11:24Z")

</div>

> [@cmallonee](#):
>
> Alerta App i4.0

here´s the search with scope of "All views "

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/5/c514395a5204be1256b3edb7eb20d64b47236232.png)

I create other new project and run this script when a memory boolean tag change his value and de behavior is the same, i remember a post with a similar issue and the solution it was restart the gateway. [SimilarIssue](https://forum.inductiveautomation.com/t/tag-event-script-value-changed-firing-twice/17709)

Here´s the true or false condition

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/0/20b5ccf005171c08129855e9551061cccdf74a30.png)

---

<div class="post-metadata">

**Author:** ![splata](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@splata](https://forum.inductiveautomation.com/u/splata)\
**Post date:** [March 24, 2022, 8:27pm UTC](https://forum.inductiveautomation.com/t/script-run-twice/57565/11 "2022-03-24T20:27:56Z")

</div>

hello Irose

if I added previousValue.value I´ve this error

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/c/bc20b532442b3d3c6433df3ca9d57c622333bccd.png)

```auto
if currentValue.value != previousValue.value and self.custom.EnWhats:
		Code=self.getChild("root").getChild("CoordinateContainer").getChild("FlexRepeaterAndRoutines").getChild("Routines").getChild("Messages").getChild("FixTemplate").getChild("Number").getChild("Dropdown").props.value
		Num=Code+str(self.getChild("root").getChild("CoordinateContainer").getChild("FlexRepeaterAndRoutines").getChild("Routines").getChild("Messages").getChild("FixTemplate").getChild("Number").getChild("NumericEntryField").props.value)
		Name="Alerta App i4.0: "+"Rutina "+RutinaN+" ha iniciado"
		Status=0
		Params={'Numero':Num,'Texto':Name,'Status':Status}
		system.db.runNamedQuery("Twilio/Insert",Params)
		self.custom.ScriptCount=self.custom.ScriptCount+1
		system.perspective.print(message= currentValue)	

```

---

<div class="post-metadata">

**Author:** ![josborn](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@josborn](https://forum.inductiveautomation.com/u/josborn)\
**Post date:** [March 24, 2022, 8:33pm UTC](https://forum.inductiveautomation.com/t/script-run-twice/57565/12 "2022-03-24T20:33:53Z")

</div>

Drop the .value

```auto
currentValue != previousValue

```

But you still need to check that current value is True.

Edit: Nvm, @lrose was correct.  
How strange, my tests failed with the same error, but only the first time after making a change to the script. All tests after the first worked fine. A bug maybe?

---

<div class="post-metadata">

**Author:** ![splata](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@splata](https://forum.inductiveautomation.com/u/splata)\
**Post date:** [March 24, 2022, 8:59pm UTC](https://forum.inductiveautomation.com/t/script-run-twice/57565/13 "2022-03-24T20:59:49Z")

</div>

thanks josborn.

now I haven´t error but the script running twice continue.

---

<div class="post-metadata">

**Author:** ![josborn](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@josborn](https://forum.inductiveautomation.com/u/josborn)\
**Post date:** [March 24, 2022, 9:02pm UTC](https://forum.inductiveautomation.com/t/script-run-twice/57565/14 "2022-03-24T21:02:53Z")

</div>

Do you still have your print statement in your code?  
You can work around the error like this:

```auto
if previousValue and currentValue.value and self.custom.EnWhats:

```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 24, 2022, 9:19pm UTC](https://forum.inductiveautomation.com/t/script-run-twice/57565/15 "2022-03-24T21:19:13Z")

</div>

I’d try expanding this further.

```auto
binEnc(
       {root/CoordinateContainer/FlexRepeaterAndRoutines/Routines/MurrElectronik/PhotoCell/ToggleRoutinePhoto.props.selected},
       {root/CoordinateContainer/FlexRepeaterAndRoutines/Routines/MurrElectronik/Inductive/ToggleRoutineInd.props.selected},
       {root/CoordinateContainer/FlexRepeaterAndRoutines/Routines/Sick/PhotoCell/RoutineSick.props.selected},
       {root/CoordinateContainer/FlexRepeaterAndRoutines/Routines/Coretigo/Encoder/Encoder_0.props.selected},
       {root/CoordinateContainer/FlexRepeaterAndRoutines/Routines/Coretigo/Inductive/Inductive Rob.props.selected},
       {root/CoordinateContainer/FlexRepeaterAndRoutines/Routines/Opto/Buttons/ToggleSwitch_4.props.selected}
      )

```

Then see if the encoded value is changing. The expression may be firing because of changes in different bits

---

<div class="post-metadata">

**Author:** ![splata](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@splata](https://forum.inductiveautomation.com/u/splata)\
**Post date:** [March 24, 2022, 9:23pm UTC](https://forum.inductiveautomation.com/t/script-run-twice/57565/16 "2022-03-24T21:23:57Z")

</div>

hello josborn.

following the test with the console I found a difference the same script ,I paste the same code in a event of button and observe the console just 1 register and only 1 register in the DB.

this is the test with the code in a event in a button

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

this is the test with the code in a transform or tag change value.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/6/867403acb44b4de46f502878151b2b3359a9d20a.png)

you can see in this picture 2 values on true.

regards

---

<div class="post-metadata">

**Author:** ![josborn](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@josborn](https://forum.inductiveautomation.com/u/josborn)\
**Post date:** [March 24, 2022, 9:29pm UTC](https://forum.inductiveautomation.com/t/script-run-twice/57565/17 "2022-03-24T21:29:25Z")

</div>

> [@lrose](#):
>
> ```auto
> if currentValue.value != previousValue.value:
> #do work here
> 
> ```

I would think this would do the trick, it looks like the quality is causing the tag change event to fire. Also it may be time to contact support, if you can let them see your project live they may have a better time helping.

---

<div class="post-metadata">

**Author:** ![splata](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@splata](https://forum.inductiveautomation.com/u/splata)\
**Post date:** [March 24, 2022, 9:31pm UTC](https://forum.inductiveautomation.com/t/script-run-twice/57565/18 "2022-03-24T21:31:23Z")

</div>

it works!!

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/c/2cc4ad59f2087388cc01634a57fdd391a7d773ec.png)  
just 1 register in the console and DB.

but I don´t understand why I’ve 2 true values one of them “Good\_unspecidied” and other “Good”.

Thankyou JordanCClark

---

<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:** [March 24, 2022, 9:43pm UTC](https://forum.inductiveautomation.com/t/script-run-twice/57565/19 "2022-03-24T21:43:07Z")

</div>

There’s no `previousValue` when `initialChange` is true. If you blindly dereference to get `.value` you get a null pointer error.

---

<div class="post-metadata">

**Author:** ![josborn](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@josborn](https://forum.inductiveautomation.com/u/josborn)\
**Post date:** [March 24, 2022, 9:47pm UTC](https://forum.inductiveautomation.com/t/script-run-twice/57565/20 "2022-03-24T21:47:30Z")

</div>

Good to know, thank you.

[Next page](https://forum.inductiveautomation.com/t/script-run-twice/57565.md?page=2)
