Script Run Twice

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

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)

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.

1 Like

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

Here is the components:
image

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

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 …


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

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.

1 Like

Thank you Irose for your suggestion:

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

image

Try putting a print statement outside of your If statement.

system.perspective.print(message= currentValue)

Then tell us what your console says.

  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.

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:

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

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

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

Here´s the true or false condition

hello Irose

if I added previousValue.value I´ve this error

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)	

Drop the .value

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?

thanks josborn.

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

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

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

I’d try expanding this further.

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

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

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

you can see in this picture 2 values on true.

regards

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.

it works!!


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

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

Good to know, thank you.