Tag value change Scripting Help

I am writing a script and having trouble getting it to work correctly.

def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	#Get Variable tag paths for writing values
	TagPath1 = '[default]Colder Products Company/Roseville/BurstTester/Test1Val'
	Var1 = system.tag.readBlocking(TagPath1)
	
	TagPath2 = '[default]Colder Products Company/Roseville/BurstTester/Test2Val'
	Var2 = system.tag.readBlocking(TagPath2)
	
	programTagPath = '[default]Colder Products Company/Roseville/BurstTester/ProgramNum/Current_Program_Number.value'
	Prgrm = system.tag.readBlocking(programTagPath)
	
	# Flip boolean
	system.tag.writeBlocking('[default]Colder Products Company/Roseville/BurstTester/Results/Get_Results_Trigger', 0)
	
	#Write based off Program Number
	if Prgrm == '3':
	
	# Write variable value to another tag for linked test (Test 3)
		if currentValue.value != 0 and currentValue.value < 3 and currentValue.value != Var2:
			system.tag.writeBlocking('[default]Colder Products Company/Roseville/BurstTester/Test1Val', currentValue.value)
		
		elif currentValue.value != 0 and currentValue.value != Var1:
			system.tag.writeBlocking('[default]Colder Products Company/Roseville/BurstTester/Test2Val', currentValue.value)
		
	elif Prgrm == '2' and currentValue.value != 0:
		system.tag.writeBlocking('[default]Colder Products Company/Roseville/BurstTester/Test1Val', currentValue.value)
		
	elif Prgrm == '1' and currentValue.value != 0:
		system.tag.writeBlocking('[default]Colder Products Company/Roseville/BurstTester/Test2Val', currentValue.value)	

I was able to get it to work when it looked like this:

def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	#Get Variable tag paths for writing values
	TagPath1 = '[default]Colder Products Company/Roseville/BurstTester/Test1Val'
	Var1 = system.tag.readBlocking(TagPath1)
	
	TagPath2 = '[default]Colder Products Company/Roseville/BurstTester/Test2Val'
	Var2 = system.tag.readBlocking(TagPath2)
	
	programTagPath = '[default]Colder Products Company/Roseville/BurstTester/ProgramNum/Current_Program_Number.value'
	Prgrm = system.tag.readBlocking(programTagPath)
	
	# Flip boolean
	system.tag.writeBlocking('[default]Colder Products Company/Roseville/BurstTester/Results/Get_Results_Trigger', 0)
	
	
	# Write variable value to another tag for linked test (Test 3)
	if currentValue.value != 0 and currentValue.value < 3 and currentValue.value != Var2:
			system.tag.writeBlocking('[default]Colder Products Company/Roseville/BurstTester/Test1Val', currentValue.value)
		
	elif currentValue.value != 0 and currentValue.value != Var1:
			system.tag.writeBlocking('[default]Colder Products Company/Roseville/BurstTester/Test2Val', currentValue.value)
		

But as soon as I add the if statement to check on a tag value, it does not seem to work. I checked to make sure that the script was able to read from the tag, and it was able to correctly:

Am I missing something obvious within the code?

The result of this call is a QualifiedValue, but you don't reference the inner value in your if statement:

2 Likes

Other tips:

  1. Do all your tag reads in one readBlocking call. It's faster.
  2. readBlocking is expecting a list of tag paths. It works (for now) with one value but I wouldn't count on it in future.
  3. You can assign multiple variables from the list in one statement.
    #Get Variable tag paths for writing values
    TagPaths = [
        '[default]Colder Products Company/Roseville/BurstTester/Test1Val',
        '[default]Colder Products Company/Roseville/BurstTester/Test2Val',
        '[default]Colder Products Company/Roseville/BurstTester/ProgramNum/Current_Program_Number.value'
    ]
   	Var1, Var2, Prgrm = system.tag.readBlocking(TagPaths)
  1. writeBlocking also expects lists for the tagpaths and values.
	# Flip boolean
    tags = ['[default]Colder Products Company/Roseville/BurstTester/Results/Get_Results_Trigger']
    values = [0]
	system.tag.writeBlocking(tags, values)

I tried this and the program is still acting incorrectly. Here is what it looks like now:

def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	#Get Variable tag paths for writing values
	TagPath1 = '[default]Colder Products Company/Roseville/BurstTester/Test1Val'
	Var1 = system.tag.readBlocking(TagPath1)
	
	TagPath2 = '[default]Colder Products Company/Roseville/BurstTester/Test2Val'
	Var2 = system.tag.readBlocking(TagPath2)
	
	programTagPath = '[default]Colder Products Company/Roseville/BurstTester/ProgramNum/Current_Program_Number.value'
	Prgrm = system.tag.readBlocking(programTagPath)
	
	# Flip boolean
	system.tag.writeBlocking('[default]Colder Products Company/Roseville/BurstTester/Results/Get_Results_Trigger', 0)
	
	#Write based off Program Number
	if Prgrm == '3':
	
	# Write variable value to another tag for linked test (Test 3)
		if currentValue.value != 0 and currentValue.value < 3 and currentValue.value != Var2:
			system.tag.writeBlocking('[default]Colder Products Company/Roseville/BurstTester/Test1Val', currentValue.value)
		
		elif currentValue.value != 0 and currentValue.value != Var1:
			system.tag.writeBlocking('[default]Colder Products Company/Roseville/BurstTester/Test2Val', currentValue.value)
		
	elif Prgrm == '2' and currentValue.value != 0:
		system.tag.writeBlocking('[default]Colder Products Company/Roseville/BurstTester/Test1Val', currentValue.value)
		
	elif Prgrm == '1' and currentValue.value != 0:
		system.tag.writeBlocking('[default]Colder Products Company/Roseville/BurstTester/Test2Val', currentValue.value)

Kevin was pointing out that Prgrm is a qualified value of the form [value, quality, timestamp].
You need
if Prgrm.value == '3':

readBlocking returns a list of qualified values, regardless of whether you feed it a list of tag paths or not.
Your expression:
if Prgrm == '3':
should be:
if Prgrm[0].value == '3':

As for what @Transistor is saying, you should be doing one call to read your tags:

#Get Variable tag paths for writing values
TagPaths = [
    '[default]Colder Products Company/Roseville/BurstTester/Test1Val',
    '[default]Colder Products Company/Roseville/BurstTester/Test2Val',
    '[default]Colder Products Company/Roseville/BurstTester/ProgramNum/Current_Program_Number.value'
]
Var1, Var2, Prgrm = system.tag.readBlocking(TagPaths)

I, personally, would do something more like this to make it easier to use your values:

#Get Variable tag paths for writing values
TagPaths = [
    '[default]Colder Products Company/Roseville/BurstTester/Test1Val',
    '[default]Colder Products Company/Roseville/BurstTester/Test2Val',
    '[default]Colder Products Company/Roseville/BurstTester/ProgramNum/Current_Program_Number.value'
]
res = system.tag.readBlocking(TagPaths)
Var1, Var2, Prgrm = [i.value for i in res]
3 Likes