# Script to compare and return dataset values

**URL:** <https://forum.inductiveautomation.com/t/script-to-compare-and-return-dataset-values/47462>\
**Category:** Ignition\
**Created:** [June 4, 2021, 3:05pm UTC](https://forum.inductiveautomation.com/t/script-to-compare-and-return-dataset-values/47462 "2021-06-04T15:05:00Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![joelle.rennick](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@joelle.rennick](https://forum.inductiveautomation.com/u/joelle.rennick)\
**Post date:** [June 4, 2021, 3:05pm UTC](https://forum.inductiveautomation.com/t/script-to-compare-and-return-dataset-values/47462/1 "2021-06-04T15:05:01Z")

</div>

Hello,

I am trying to write a script that will run every 15 seconds on one popup screen only. I have a memory tag called Interlock that is a dataset. The columns are called InterlockedTags (this column provides the tag path), StatusForTrue (this tag is a static boolean that says whether true or false is the desired condition of the tag), and ColourNum (this is an int which will be used to turn the rows yellow if the condition is bad and grey if it is good). I am trying to write a script that will take the tag path from the InterlockedTags column and compare the actual status of that tag to the value in the StatusForTrue tag. If they are the same, I want it to return a 0 to the ColourNum column and if they are not the same I want to to return a 1 to ColourNum.

Can anyone help me decide what the best way to implement this is and where the script should actually go? I am not very familiar with the scripting language.

Thank you!

---

<div class="post-metadata">

**Author:** ![zacht](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@zacht](https://forum.inductiveautomation.com/u/zacht)\
**Post date:** [June 4, 2021, 3:14pm UTC](https://forum.inductiveautomation.com/t/script-to-compare-and-return-dataset-values/47462/2 "2021-06-04T15:14:09Z")

</div>

Are you using Vision or Perspective for graphics? If in Vision and you really only want to run the script when a particular window (popup in this case) is open you can use a timer component and add a script like this to the propertyChange listener. Using the default timer configuration in 8.1.5 this script executes every 10 seconds. you can change the bound to 15 to achieve 15 second delay.

I would still set your logic up in a script library and call it from the timer property change script. Doing it this way you can reuse the logic in another area if you need to.

```auto
if event.newValue==0:
	print 'Timer Test'

```

---

<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:** [June 4, 2021, 4:12pm UTC](https://forum.inductiveautomation.com/t/script-to-compare-and-return-dataset-values/47462/3 "2021-06-04T16:12:24Z")

</div>

If there are multiple clients with this popup open, you don’t really want then to fight over writing to the dataset. It would be more efficient to call it from a gateway timer script.

---

<div class="post-metadata">

**Author:** ![joelle.rennick](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@joelle.rennick](https://forum.inductiveautomation.com/u/joelle.rennick)\
**Post date:** [June 4, 2021, 5:43pm UTC](https://forum.inductiveautomation.com/t/script-to-compare-and-return-dataset-values/47462/4 "2021-06-04T17:43:26Z")

</div>

Yes that is true as there may be multiple instances of a similar popup open at the same time. Thank you

---

<div class="post-metadata">

**Author:** ![joelle.rennick](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@joelle.rennick](https://forum.inductiveautomation.com/u/joelle.rennick)\
**Post date:** [June 4, 2021, 5:44pm UTC](https://forum.inductiveautomation.com/t/script-to-compare-and-return-dataset-values/47462/5 "2021-06-04T17:44:25Z")

</div>

I am using vision. Do you know if there is a system. function that I can use to compare the information in a specific cell to the information in a tag?

---

<div class="post-metadata">

**Author:** ![zacht](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@zacht](https://forum.inductiveautomation.com/u/zacht)\
**Post date:** [June 4, 2021, 5:49pm UTC](https://forum.inductiveautomation.com/t/script-to-compare-and-return-dataset-values/47462/6 "2021-06-04T17:49:45Z")

</div>

It’s not a system.\* function, but once you have a dataset you can use the getDataAt() method to inspect a particular cell.

Example:

```auto
data = event.source.parent.getComponent('Table').data
print data.getValueAt(2,2) # row, column; zero based indexing

```

---

<div class="post-metadata">

**Author:** ![joelle.rennick](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@joelle.rennick](https://forum.inductiveautomation.com/u/joelle.rennick)\
**Post date:** [June 4, 2021, 6:55pm UTC](https://forum.inductiveautomation.com/t/script-to-compare-and-return-dataset-values/47462/7 "2021-06-04T18:55:44Z")

</div>

Awesome, thank you. Do you know what the best way to get tag information based on the information in a cell? For example, if the cell is a string of the tag path to a boolean tag, how can I return a 0 or 1 just by taking the string tag path?

---

<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:** [June 4, 2021, 7:22pm UTC](https://forum.inductiveautomation.com/t/script-to-compare-and-return-dataset-values/47462/8 "2021-06-04T19:22:54Z")

</div>

You can get column data as a list. Use that list with `system.tag.readBlocking` to get all your values at once.

I’d also recommend converting to a PyDataSet to make it convenient to iterate through the data.

```auto
def myFunc(datasetPath):
	# Read the dataset tag. Convert to PyDataSet for easier iteration.
	dataIn = system.dataset.toPyDataSet(system.tag.readBlocking([datasetPath])[0].value)

	# Get column names. Coerce it to a list to use the same names in the new dataset.
	headers = list(dataIn.getColumnNames())
	
	# Get tag paths as a list
	tagList = dataIn.getColumnAsList(headers.index('InterlockedTags'))

	# Read the tags
	tagValues = [tag.value for tag in system.tag.readBlocking(tagList)]

	# Construct the new dataset.
	data = []
	for row, actualValue in zip(dataIn, tagValues): 
		data.append([row['InterlockedTags'], row['StatusForTrue'], int(row['StatusForTrue'] != actualValue)])
	
	# Write back to the original tag
	system.tag.writeBlocking([datasetPath],[system.dataset.toDataSet(headers, data)])

myFunc('[default]path/to/Interlock')

```

---

<div class="post-metadata">

**Author:** ![joelle.rennick](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@joelle.rennick](https://forum.inductiveautomation.com/u/joelle.rennick)\
**Post date:** [June 7, 2021, 2:47pm UTC](https://forum.inductiveautomation.com/t/script-to-compare-and-return-dataset-values/47462/9 "2021-06-07T14:47:06Z")

</div>

Thanks Jordan. I am not really sure how or where to implement this… The code I have come up with so far is shown below. I am not sure if I should put it in the table scripting, on the popup window itself, or somewhere else. Do you have any ideas?

rowNum = 0 #Need to get this value. 0 is placeholder value.  
numOfRow = 4 #Number of total rows (this will vary depending on the tagpath). Need to get this value. 4 is placeholder value.

for rowNum in range (0, numOfRow, 1):  
data = event.source.parent.getComponent(‘Interlock’).data  
pathToRead = system.tag.read(“InterlockedTags”).value.geValueAt(rowNum, 0) #String containing the tag path  
trueStatusCheck = data.getValueAt(rowNum, 1) #Boolean which shows the desired status  
if pathToRead == trueStatusCheck:  
colourNum = 1 #ColourNum is the third colum and I would like to make it control the colour of the row in the future.  
else:  
colourNum = 0

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [June 7, 2021, 3:04pm UTC](https://forum.inductiveautomation.com/t/script-to-compare-and-return-dataset-values/47462/10 "2021-06-07T15:04:47Z")

</div>

You need to encase your code in a preformatted block

 ![Screenshot_20210608-003550_Chrome](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/c/1c70fa38bb113e8314c96473b0e53cea774695d6.jpeg)

---

<div class="post-metadata">

**Author:** ![joelle.rennick](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@joelle.rennick](https://forum.inductiveautomation.com/u/joelle.rennick)\
**Post date:** [June 7, 2021, 3:05pm UTC](https://forum.inductiveautomation.com/t/script-to-compare-and-return-dataset-values/47462/11 "2021-06-07T15:05:28Z")

</div>

```auto
rowNum = 0 #Need to get this value. 0 is placeholder value.
numOfRow = 4 #Number of total rows (this will vary depending on the tagpath). Need to get this value. 4 is placeholder value.

for rowNum in range (0, numOfRow, 1):
	data = event.source.parent.getComponent('Interlock').data
	pathToRead = system.tag.read("InterlockedTags").value.geValueAt(rowNum, 0) #String containing the tag path
	trueStatusCheck = data.getValueAt(rowNum, 1) #Boolean which shows the desired status
	if pathToRead == trueStatusCheck:
		colourNum = 1 #ColourNum is the third colum and I would like to make it control the colour of the row in the future.
	else:
		colourNum = 0

```

Sorry about that!

---

<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:** [June 7, 2021, 3:15pm UTC](https://forum.inductiveautomation.com/t/script-to-compare-and-return-dataset-values/47462/12 "2021-06-07T15:15:50Z")

</div>

Sorry, at meetings this morning. Can’t give this much attention. I’ll post something in a bit.

---

<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:** [June 8, 2021, 9:55am UTC](https://forum.inductiveautomation.com/t/script-to-compare-and-return-dataset-values/47462/13 "2021-06-08T09:55:47Z")

</div>

The attached file may be iported into your project. It contains the script I posted above, and a getway timer script to call it every 15 seconds. update tagName in the gateway script to reflect where your tag is.

[interlock\_2021-06-08\_0549.zip](https://forum.inductiveautomation.com/uploads/short-url/4mzmMrXXMH80Vq7QQFXxCCpnoJZ.zip) (2.3 KB)

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/4/44578773b89e68a88fd84adfc316cfc3d7143520.png)  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/c/6c51e129256f6b48953f49fe9298b34603aba3ca.png)

---

<div class="post-metadata">

**Author:** ![joelle.rennick](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@joelle.rennick](https://forum.inductiveautomation.com/u/joelle.rennick)\
**Post date:** [June 8, 2021, 4:05pm UTC](https://forum.inductiveautomation.com/t/script-to-compare-and-return-dataset-values/47462/14 "2021-06-08T16:05:29Z")

</div>

Thank you very much! This makes sense now. I am getting an error when I run this code saying that “TypeError: toPyDataSet(): 1st arg can’t be coerced to com.inductiveautomation.ignition.common.Dataset” for line 3. Do you know what might fix this?

---

<div class="post-metadata">

**Author:** ![WillMT10](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@WillMT10](https://forum.inductiveautomation.com/u/WillMT10)\
**Post date:** [June 8, 2021, 4:21pm UTC](https://forum.inductiveautomation.com/t/script-to-compare-and-return-dataset-values/47462/15 "2021-06-08T16:21:47Z")

</div>

Looks like a couple typos on line 3. Should be:

```auto
dataIn = system.dataset.toPyDataSet(system.tag.readBlocking([datasetPath])[0].value)

```

---

<div class="post-metadata">

**Author:** ![joelle.rennick](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@joelle.rennick](https://forum.inductiveautomation.com/u/joelle.rennick)\
**Post date:** [June 8, 2021, 5:18pm UTC](https://forum.inductiveautomation.com/t/script-to-compare-and-return-dataset-values/47462/16 "2021-06-08T17:18:18Z")

</div>

That change to line 3 and making actualValue and actualVal variables match in line 16 and 17 fixed all the problems and the script works great.

Thanks again to everyone!!!

---

<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:** [June 8, 2021, 5:23pm UTC](https://forum.inductiveautomation.com/t/script-to-compare-and-return-dataset-values/47462/17 "2021-06-08T17:23:11Z")

</div>

Thanks, post edited.
