# Compare Tag values

**URL:** <https://forum.inductiveautomation.com/t/compare-tag-values/71439>\
**Category:** Ignition\
**Created:** [March 22, 2023, 2:27pm UTC](https://forum.inductiveautomation.com/t/compare-tag-values/71439 "2023-03-22T14:27:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aman\_sharma](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@aman\_sharma](https://forum.inductiveautomation.com/u/aman_sharma)\
**Post date:** [March 22, 2023, 2:27pm UTC](https://forum.inductiveautomation.com/t/compare-tag-values/71439/1 "2023-03-22T14:27:46Z")

</div>

Hi,

I am getting data from postgreSql. basically i have created one query tag and getting the values.  
query is :  
SELECT "Sample"  
FROM "Samples"  
ORDER BY "Timestamp" desc LIMIT 6

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

Now i want to compare these values in such a way six points in a row are continuously increasing(or decreasing).

so i am writing the tag events script into this nelson rule tag :

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

but it didn't work.

Please guide me the steps how to do that or another way how to compare the value and writing into the tag.

---

<div class="post-metadata">

**Author:** ![bpreston](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@bpreston](https://forum.inductiveautomation.com/u/bpreston)\
**Post date:** [March 22, 2023, 2:44pm UTC](https://forum.inductiveautomation.com/t/compare-tag-values/71439/2 "2023-03-22T14:44:45Z")

</div>

Your doing a script but your trying to reference tags directly without reading them which will cause all of your "if" statements to fail. I'm also assuming you would want to write a 1 or another value to the "Comparison" tag if the if statement isn't true. My quick take on what I'm assuming your looking for based on the script you typed up is:

```auto
tr = ['[~]Nelson rule 3[%d]' % (x) for x in range(6)]
rv = system.tag.readBlocking(tr)
tw = ['[TAGPROVIDER]Comparison[%d]' % (x) for x in range(5)]
wv = []
for x in range(5):
	if rv[x] > rv[x+1]:
		wv.append(0)
	else:
		wv.append(1)
system.tag.writeBlocking(tw,wv)

```

---

<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:** [March 22, 2023, 2:49pm UTC](https://forum.inductiveautomation.com/t/compare-tag-values/71439/3 "2023-03-22T14:49:16Z")

</div>

I sounds as though you are trying to use [Nelson rules - Wikipedia](https://en.wikipedia.org/wiki/Nelson_rules) number 3.

Paste this script into Script Console and play around with it. It looks like you could do the job in two lines.

```auto
arr1 = [1, 2, 3, 4, 5, 6]
arr2 = [1, 2, 4, 3, 5, 6]
arr3 = [1, 2, 3, 3, 5, 6]

print "Is the series strictly increasing?"
print all(x < y for x, y in zip(arr1, arr1[1:]))
print all(x < y for x, y in zip(arr2, arr2[1:]))
print all(x < y for x, y in zip(arr3, arr3[1:]))

print "Is the series strictly decreasing?"
print all(x > y for x, y in zip(arr1, arr1[1:]))
print all(x > y for x, y in zip(arr2, arr2[1:]))
print all(x > y for x, y in zip(arr3, arr3[1:]))

print "Is the series strictly non-increasing?"
print all(x >= y for x, y in zip(arr1, arr1[1:]))
print all(x >= y for x, y in zip(arr2, arr2[1:]))
print all(x >= y for x, y in zip(arr3, arr3[1:]))

print "Is the series strictly non-decreasing?"
print all(x <= y for x, y in zip(arr1, arr1[1:]))
print all(x <= y for x, y in zip(arr2, arr2[1:]))
print all(x <= y for x, y in zip(arr3, arr3[1:]))

```

For more on this see [Python - How to check list monotonicity - Stack Overflow](https://stackoverflow.com/questions/4983258/python-how-to-check-list-monotonicity).

Note that you should paste code and not pictures of code in your question. Use the `</>` button to format it as code.

---

<div class="post-metadata">

**Author:** ![aman\_sharma](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@aman\_sharma](https://forum.inductiveautomation.com/u/aman_sharma)\
**Post date:** [March 23, 2023, 9:19am UTC](https://forum.inductiveautomation.com/t/compare-tag-values/71439/4 "2023-03-23T09:19:00Z")

</div>

thanks its working
