# Tag value change Scripting Help

**URL:** <https://forum.inductiveautomation.com/t/tag-value-change-scripting-help/88259>\
**Category:** Ignition\
**Tags:** ignition81, perspective\
**Created:** [April 2, 2024, 8:50pm UTC](https://forum.inductiveautomation.com/t/tag-value-change-scripting-help/88259 "2024-04-02T20:50:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Craig](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@Ryan\_Craig](https://forum.inductiveautomation.com/u/Ryan_Craig)\
**Post date:** [April 2, 2024, 8:50pm UTC](https://forum.inductiveautomation.com/t/tag-value-change-scripting-help/88259/1 "2024-04-02T20:50:15Z")

</div>

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

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

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

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

Am I missing something obvious within the code?

---

<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:** [April 2, 2024, 8:58pm UTC](https://forum.inductiveautomation.com/t/tag-value-change-scripting-help/88259/2 "2024-04-02T20:58:45Z")

</div>

> [@Ryan\_Craig](#):
>
> `Prgrm = system.tag.readBlocking(programTagPath)`

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

> [@Ryan\_Craig](#):
>
> ```auto
> #Write based off Program Number
> if Prgrm == '3':
> 
> ```

---

<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:** [April 2, 2024, 9:09pm UTC](https://forum.inductiveautomation.com/t/tag-value-change-scripting-help/88259/3 "2024-04-02T21:09:57Z")

</div>

Other tips:

1. Do all your tag reads in one readBlocking call. It's faster.
2. [readBlocking](https://docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-tag/system-tag-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.

```auto
    #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](https://docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-tag/system-tag-writeBlocking) also expects lists for the tagpaths and values.

```auto
	# Flip boolean
    tags = ['[default]Colder Products Company/Roseville/BurstTester/Results/Get_Results_Trigger']
    values = [0]
	system.tag.writeBlocking(tags, values)

```

---

<div class="post-metadata">

**Author:** ![Ryan\_Craig](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@Ryan\_Craig](https://forum.inductiveautomation.com/u/Ryan_Craig)\
**Post date:** [April 3, 2024, 5:43pm UTC](https://forum.inductiveautomation.com/t/tag-value-change-scripting-help/88259/4 "2024-04-03T17:43:08Z")

</div>

> [@Ryan\_Craig](#):
>
> `if Prgrm == '3':`

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

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

```

---

<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:** [April 3, 2024, 5:54pm UTC](https://forum.inductiveautomation.com/t/tag-value-change-scripting-help/88259/5 "2024-04-03T17:54:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rbabcock](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/rbabcock/32/13425_2.png) [@rbabcock](https://forum.inductiveautomation.com/u/rbabcock)\
**Post date:** [April 3, 2024, 5:55pm UTC](https://forum.inductiveautomation.com/t/tag-value-change-scripting-help/88259/6 "2024-04-03T17:55:45Z")

</div>

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:

```auto
#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:

```auto
#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]

```
