# Ignition Python Tables

**URL:** <https://forum.inductiveautomation.com/t/ignition-python-tables/42089>\
**Category:** Ignition\
**Created:** [January 9, 2021, 10:37am UTC](https://forum.inductiveautomation.com/t/ignition-python-tables/42089 "2021-01-09T10:37:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gijovarghese8](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/gijovarghese8/32/20571_2.png) [@gijovarghese8](https://forum.inductiveautomation.com/u/gijovarghese8)\
**Post date:** [January 9, 2021, 10:37am UTC](https://forum.inductiveautomation.com/t/ignition-python-tables/42089/1 "2021-01-09T10:37:52Z")

</div>

Hello,

I need help with how to select a value from another column if a value in another column is great than some value. for example

Machine P1 P2  
1 150 145  
2 220 275  
3 201 186

how do I write a script to return the machine number if value in P2 is greater than 270?

Thank you for your help.

Regards.

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [January 9, 2021, 1:22pm UTC](https://forum.inductiveautomation.com/t/ignition-python-tables/42089/2 "2021-01-09T13:22:20Z")

</div>

Ignition version? If 8+, Vision or Perspective?

Python table, you mean Power Table etc in vision? Or data in SQL DB? Which SQL?

Until you answer the above, the below is a guess.

Create a tag type SQL, pseudo-code query:

```auto
Select machineNum from db.table
where P2 > '270'

```

Output as per your example dataset will be ‘2’

Of course, that is a hardcoded value of 270, make it a variable and link it to your input parameter

---

<div class="post-metadata">

**Author:** ![gijovarghese8](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/gijovarghese8/32/20571_2.png) [@gijovarghese8](https://forum.inductiveautomation.com/u/gijovarghese8)\
**Post date:** [January 9, 2021, 1:57pm UTC](https://forum.inductiveautomation.com/t/ignition-python-tables/42089/3 "2021-01-09T13:57:09Z")

</div>

Good Morning,

Thank you for your response.

.the way I have it set up right now, is I created an expression tag and have a SQL query pulling the data. and I am using Table to show me the data. however next I want to notify me when a value gets above 270.

so far I only have

if system.tag.read(Notification/Parameters/“Send Email”).value == True  
table = event.source.parent.getComponent(‘Table’)  
data = system.dataset.toPyDataSet(Table.data)

Next, i would like to look at column 2 and if any of the values are above 270 i would like to get the machine number from column 0

Thank you

Gijo

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [January 9, 2021, 2:05pm UTC](https://forum.inductiveautomation.com/t/ignition-python-tables/42089/4 "2021-01-09T14:05:43Z")

</div>

You didn’t answer all my questions, please do.

Use the code button (\</\> symbol) when pasting Jython/SQL/error code here.

---

<div class="post-metadata">

**Author:** ![gijovarghese8](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/gijovarghese8/32/20571_2.png) [@gijovarghese8](https://forum.inductiveautomation.com/u/gijovarghese8)\
**Post date:** [January 9, 2021, 2:19pm UTC](https://forum.inductiveautomation.com/t/ignition-python-tables/42089/5 "2021-01-09T14:19:53Z")

</div>

i apologize.  
i am using ignition 7.9.10. i am not sure what vision or perspective means. i am getting the data from MYSQL and then into a table. the table works, it is showing the data that is being pulled through SQL.

---

<div class="post-metadata">

**Author:** ![gijovarghese8](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/gijovarghese8/32/20571_2.png) [@gijovarghese8](https://forum.inductiveautomation.com/u/gijovarghese8)\
**Post date:** [January 9, 2021, 2:21pm UTC](https://forum.inductiveautomation.com/t/ignition-python-tables/42089/6 "2021-01-09T14:21:23Z")

</div>

```auto
			if system.tag.read("Email Notifications/Parameters/Send Email when Primary Pall filters are getting OLD").value == True 
			table = event.source.parent.getComponent('Table')
			data = system.dataset.toPyDataSet(Table.data) # this converts the dataset into a py dataset
			
			

```

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [January 9, 2021, 3:30pm UTC](https://forum.inductiveautomation.com/t/ignition-python-tables/42089/7 "2021-01-09T15:30:34Z")

</div>

> [@gijovarghese8](#):
>
> i am using ignition 7.9.10. i am not sure what vision or perspective means.

Perspective only applies to 8.0.0 and above.

I think you clicked the wrong button, because your formatting should be preserved (tabs and white space, this is important in Jython):

```auto
if system.tag.read("Email Notifications/Parameters/Send Email when Primary Pall filters are getting OLD").value == True 
   table = event.source.parent.getComponent('Table')
   data = system.dataset.toPyDataSet(Table.data) # this converts the dataset into a py dataset
			

```

Python (Jython) if statements need a colon then a tab on the next line, see example below. Go to Tools/script console and paste in and hit execute

```auto
for x in range(0,9):
	if x > 5:
		print x
	else: 
		print "<5"

```

> the way I have it set up right now, is I created an expression tag and have a SQL query pulling the data

I think you need to spend a bit of time on Inductive University, make sure to change the version to 7.9

I would have your table get the data from MySQL. If all you want to show then is one row, write the SQL query to only return this.

Or if you want the table to show all data, then select \*

If you want the machine number shown, for example as a numeric label, create a numeric label and bind this to a SQL tag that has something similar to my first reply pseudo-code.

---

<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:** [January 9, 2021, 4:49pm UTC](https://forum.inductiveautomation.com/t/ignition-python-tables/42089/8 "2021-01-09T16:49:31Z")

</div>

> [@Matrix\_Engineering](#):
>
> Select machineNum  
> from db.table  
> where P2 \> '270'

3rd line should be:  
`WHERE P2 > 270`

(no quotes around the integer value)

If you _did_ want to process the table though instead of running another query:

```auto
if system.tag.read("Email Notifications/Parameters/Send Email when Primary Pall filters are getting OLD").value == True 
   table = event.source.parent.getComponent('Table')
   data = system.dataset.toPyDataSet(Table.data) 
   P1_Old = [] 
   for row in data if row['P2'] > 270:
      P1_Old.append('Machine %s P1 %s [%s]' % (row['Machine'], row['P1'], row['P2']) 
   badFiltersCSV = ', '.join(P1_Old)
   # do something with the above... 

```

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [January 9, 2021, 4:55pm UTC](https://forum.inductiveautomation.com/t/ignition-python-tables/42089/9 "2021-01-09T16:55:22Z")

</div>

Been a long day 🙂

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 9, 2021, 9:19pm UTC](https://forum.inductiveautomation.com/t/ignition-python-tables/42089/10 "2021-01-09T21:19:18Z")

</div>

> [@nminchin](#):
>
> If you _did_ want to process the table though instead of running another query:

AHEM:

In an expression binding:

```auto
view("SELECT machineNum Where P2>270", {Root Container.path.to.dataset.property})

```

From my _ **free** _ Simulation Aids module. (:

---

<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:** [January 11, 2021, 10:40am UTC](https://forum.inductiveautomation.com/t/ignition-python-tables/42089/11 "2021-01-11T10:40:40Z")

</div>

Using a list comprehension:

```python
if system.tag.read("Email Notifications/Parameters/Send Email when Primary Pall filters are getting OLD").value == True 
   table = event.source.parent.getComponent('Table')
   dataIn = system.dataset.toPyDataSet(Table.data)
   
   headers = dataIn.getColumnNames()
   dataOut = [list(row) for row in dataIn if row['P2'] > 270]

   datasetOut = system.dataset.toDataSet(headers, dataOut)

```

---

<div class="post-metadata">

**Author:** ![gijovarghese8](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/gijovarghese8/32/20571_2.png) [@gijovarghese8](https://forum.inductiveautomation.com/u/gijovarghese8)\
**Post date:** [January 11, 2021, 1:23pm UTC](https://forum.inductiveautomation.com/t/ignition-python-tables/42089/12 "2021-01-11T13:23:01Z")

</div>

Good Morning Jordan,

So when selecting the row. i need to use the row name and not P2 correct?

Thank you

Gijo

---

<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:** [January 11, 2021, 2:40pm UTC](https://forum.inductiveautomation.com/t/ignition-python-tables/42089/13 "2021-01-11T14:40:03Z")

</div>

Hi Gijo,

The example I gave will make a new ‘filtered’ dataset containing rows where P2 \> 270. Not sure what you are meaning by selecting a row.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [January 11, 2021, 2:55pm UTC](https://forum.inductiveautomation.com/t/ignition-python-tables/42089/14 "2021-01-11T14:55:26Z")

</div>

In Jordan’s code, this line:

```auto
dataOut = [list(row) for row in dataIn if row['P2'] > 270]

```

is equivelant to:

```auto
dataOut = []
for row in dataIn:
     if row['P2'] > 270:
          dataOut.append(list(row))

```

Since you have converted your dataIn dataset to a PyDataset you can use this syntax to reference the value in the column of a row by name. So, “row[‘P2’]” says get the value of the current row at column ‘P2’

---

<div class="post-metadata">

**Author:** ![gijovarghese8](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/gijovarghese8/32/20571_2.png) [@gijovarghese8](https://forum.inductiveautomation.com/u/gijovarghese8)\
**Post date:** [January 11, 2021, 2:59pm UTC](https://forum.inductiveautomation.com/t/ignition-python-tables/42089/15 "2021-01-11T14:59:49Z")

</div>

Hello Jordan,

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/f/5f7831529bf7edfb8dddac35ddfe2a7875672027.png)  
so my SQL query pulls data into a table and I want to be notified of the tower#(Row0) where the PRI CMD got above a limit(i am still debating on what is a safe upper limit), I choose 270 just as a placeholder. I want ignition to notify me via email the tower number and the “PRI CMD” value. I have been staring at this for a while, not getting any emails. i have also attached a copy of what i have so far.

```auto
			if system.tag.read("Email Notifications/Parameters/Send Email when Primary Pall filters are getting OLD").value == True 
				table = event.source.parent.getComponent('Table')
				data = system.dataset.toPyDataSet(Table.data) # this converts the dataset into a py dataset
			
				headers = dataIn.getColumnNames()
			   	dataOut = [list(row) for row in dataIn if row['Pri CMD'] > 260]
			   	datasetOut = system.dataset.toDataSet(headers, dataOut)

			     
			else:  
			   
			loggerGatewayScripts = system.util.getLogger("Symation.Email_Logger")
				
			sSubject = "Primary Pall Filter getting clogged"
			sBody = "please schedule to repalce these Primary Pall filters. " + datasetOut
			sSubject=str(system.tag.read("Tower Config/Tower Number").value) + " "+sSubject
			sBody="Tower " + str(system.tag.read("Tower Config/Tower Number").value)+ "\n" + sBody
			sRecipients = ["gvarghese@ofsoptics.com"] 
	
			loggerGatewayScripts.infof("Sending Line Idle Email: %s",sSubject)
			system.net.sendEmail("smtp.corp.ofsoptics.com:25","gvarghese@ofsoptics.com",sSubject,sBody,0,sRecipients)
		except:
			loggerGatewayScripts = system.util.getLogger("Symation.Email_Logger")
			loggerGatewayScripts.infof("Error sending Clogged Primary Pall Filter email")

```

---

<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:** [January 11, 2021, 3:07pm UTC](https://forum.inductiveautomation.com/t/ignition-python-tables/42089/16 "2021-01-11T15:07:31Z")

</div>

Where is this script located? How is it triggered?

---

<div class="post-metadata">

**Author:** ![gijovarghese8](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/gijovarghese8/32/20571_2.png) [@gijovarghese8](https://forum.inductiveautomation.com/u/gijovarghese8)\
**Post date:** [January 11, 2021, 3:14pm UTC](https://forum.inductiveautomation.com/t/ignition-python-tables/42089/17 "2021-01-11T15:14:36Z")

</div>

Sorry, idk what i answered

i have a memory tag active. and this should execute when that memory tag is activated.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [January 11, 2021, 5:17pm UTC](https://forum.inductiveautomation.com/t/ignition-python-tables/42089/18 "2021-01-11T17:17:14Z")

</div>

There is an error in the script as it is in your post. If this script is in a tag’s valueChanged event as you have it posted, then it is probably not executing. Are you seeing any messages in your logger? You can look at the Tag Diagnostics to see if the script is throwing an exception or not.

You need to delete the else: or you need to provide something that should be executed after the else. Also, you have an except, but no ‘Try’, is this the entire script?

I don’t think that you will get what you expect in the body with just concatenating datasetOut to the body. You will need to loop through the filtered dataset and concatenate the information that way.  
Something like the following is what I would do:

```auto
try:
	if system.tag.read("Email Notifications/Parameters/Send Email when Primary Pall filters are getting OLD").value == True: 
		table = event.source.parent.getComponent('Table')
		data = system.dataset.toPyDataSet(Table.data) # this converts the dataset into a py dataset
			
		headers = dataIn.getColumnNames()
	    dataOut = [list(row) for row in dataIn if row['Pri CMD'] > 260]
	    datasetOut = system.dataset.toDataSet(headers, dataOut)

		loggerGatewayScripts = system.util.getLogger("Symation.Email_Logger")
				
		sSubject = "Primary Pall Filter getting clogged"
        sBody = "please schedule to repalce these Primary Pall filter"
        for row in datasetOut:
		    sBody += "\n Tower: %i, Pri Cmd: %i" % (row['Tower'],row['Pri CMD'])
		sRecipients = ["gvarghese@ofsoptics.com"] 
	
		loggerGatewayScripts.infof("Sending Line Idle Email: %s",sSubject)
		system.net.sendEmail("smtp.corp.ofsoptics.com:25","gvarghese@ofsoptics.com",sSubject,sBody,0,sRecipients)
except:
	loggerGatewayScripts = system.util.getLogger("Symation.Email_Logger")
	loggerGatewayScripts.infof("Error sending Clogged Primary Pall Filter email")
```

---

<div class="post-metadata">

**Author:** ![gijovarghese8](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/gijovarghese8/32/20571_2.png) [@gijovarghese8](https://forum.inductiveautomation.com/u/gijovarghese8)\
**Post date:** [January 12, 2021, 4:57pm UTC](https://forum.inductiveautomation.com/t/ignition-python-tables/42089/19 "2021-01-12T16:57:45Z")

</div>

So i had an error in the script i was missing “:” at the end of my IF statement. I got that fixed. and have no more errors. i updated the script, i am now seeing stuff updating on the logger. but for some reason, the script do not get past the Initialchange: line.

```auto
	loggerGatewayScripts = system.util.getLogger("Symation.Email_Logger")
	loggerGatewayScripts.infof("Primary Pall Filter Script Executed!")
	if not initialChange:
		loggerGatewayScripts.infof("Primary Pall Filter Script Executed!")
		try:
			if system.tag.read("Email Notifications/Parameters/Send Email when Primary Pall filters are getting OLD").value == True: 
				table = event.source.parent.getComponent('Table')
				loggerGatewayScripts.infof("Primary Pall Filter Script inside first if statement")
				data = system.dataset.toPyDataSet(Table.data) # this converts the dataset into a py dataset
			
				headers = dataIn.getColumnNames()
			   	dataOut = [list(row) for row in dataIn if row['Pri CMD'] > 260]
			   	datasetOut = system.dataset.toDataSet(headers, dataOut)
			   	if len(dataOut) > 0:		   	
					sSubject = "Primary Pall Filter getting clogged"
					sBody = "please schedule to repalce these Primary Pall filters. " + datasetOut
					sSubject=str(system.tag.read("Tower Config/Tower Number").value) + " "+sSubject
					sBody="Tower " + str(system.tag.read("Tower Config/Tower Number").value)+ "\n" + sBody
					sRecipients = ["gvarghese@ofsoptics.com"] 
				
					loggerGatewayScripts.infof("Sending Line Idle Email: %s",sSubject)
					system.net.sendEmail("smtp.corp.ofsoptics.com:25","gvarghese@ofsoptics.com",sSubject,sBody,0,sRecipients)
		except:
		
			loggerGatewayScripts.infof("Error sending Clogged Primary Pall Filter email")

```

---

<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:** [January 12, 2021, 6:06pm UTC](https://forum.inductiveautomation.com/t/ignition-python-tables/42089/20 "2021-01-12T18:06:22Z")

</div>

> [@gijovarghese8](#):
>
> data = system.dataset.toPyDataSet(Table.data)

Should be a lowercase 't' in 'Table' based on your variable name 'table' two lines above.

Also, this script will never work in a Tag Event Script, or any Gateway Script. You cannot access components (such as 'Table') from a Tag/Gateway script (gateway vs client scope). You will need to setup a different means to pass your data into this script (separate dataset tag, modify script to retrieve source data, etc).

[Next page](https://forum.inductiveautomation.com/t/ignition-python-tables/42089.md?page=2)
