# Evaluate Expression Language in Script

**URL:** <https://forum.inductiveautomation.com/t/evaluate-expression-language-in-script/15012>\
**Category:** Ignition\
**Created:** [August 8, 2017, 3:29pm UTC](https://forum.inductiveautomation.com/t/evaluate-expression-language-in-script/15012 "2017-08-08T15:29:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Caleb\_Bohon](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/caleb_bohon/32/3293_2.png) [@Caleb\_Bohon](https://forum.inductiveautomation.com/u/Caleb_Bohon)\
**Post date:** [August 8, 2017, 3:29pm UTC](https://forum.inductiveautomation.com/t/evaluate-expression-language-in-script/15012/1 "2017-08-08T15:29:11Z")

</div>

I’m trying to write a script that creates a list of alarms setup on an Ignition System.

So far I’ve got it iterating through all the Tags then through all Alarms, but when I print the alarms, many of my bindings show up as expressions (which is fine because that’s what I have) but I would like to evaluate the expressions.

I don’t see a good way to do this. Does anyone know of one?

If not and you would like similar functionality could you vote for my feature request:

[http://ideas.inductiveautomation.com/forums/255378-ignition-features-and-ideas/suggestions/20458549-create-a-script-function-to-evaluate-an-expression](http://ideas.inductiveautomation.com/forums/255378-ignition-features-and-ideas/suggestions/20458549-create-a-script-function-to-evaluate-an-expression)

Thanks!

---

<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:** [August 8, 2017, 6:24pm UTC](https://forum.inductiveautomation.com/t/evaluate-expression-language-in-script/15012/2 "2017-08-08T18:24:46Z")

</div>

I’m pretty sure the expression parser only exists in the designer. Bindings are stored and activated from the serialized objects of the expression after parsing.

---

<div class="post-metadata">

**Author:** ![Caleb\_Bohon](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/caleb_bohon/32/3293_2.png) [@Caleb\_Bohon](https://forum.inductiveautomation.com/u/Caleb_Bohon)\
**Post date:** [August 8, 2017, 11:40pm UTC](https://forum.inductiveautomation.com/t/evaluate-expression-language-in-script/15012/3 "2017-08-08T23:40:05Z")

</div>

Is there actually a tool that does the expression parsing that I can access?  
I’m not seeing anything.

---

<div class="post-metadata">

**Author:** ![JMcguigan](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@JMcguigan](https://forum.inductiveautomation.com/u/JMcguigan)\
**Post date:** [August 14, 2017, 5:45pm UTC](https://forum.inductiveautomation.com/t/evaluate-expression-language-in-script/15012/4 "2017-08-14T17:45:12Z")

</div>

We needed to do this on a project, so we created a test expression tag that we would write expressions into through scripting and then read the result out of. It’s not a perfect solution but it worked well enough for our application.

---

<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:** [August 15, 2017, 4:05pm UTC](https://forum.inductiveautomation.com/t/evaluate-expression-language-in-script/15012/5 "2017-08-15T16:05:51Z")

</div>

This means the gateway has the parser class available. You might have to go spelunking in gateway.jar. /-:

---

<div class="post-metadata">

**Author:** ![Caleb\_Bohon](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/caleb_bohon/32/3293_2.png) [@Caleb\_Bohon](https://forum.inductiveautomation.com/u/Caleb_Bohon)\
**Post date:** [August 15, 2017, 4:09pm UTC](https://forum.inductiveautomation.com/t/evaluate-expression-language-in-script/15012/6 "2017-08-15T16:09:39Z")

</div>

@JMcguigan I don’t think that will work for us, because many of the expressions have relative tag paths.

@pturmel Thats useful to know. Thanks for clarifying. Should I be able to use that from a normal script or would I have to create an Ignition module for it?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 15, 2017, 4:35pm UTC](https://forum.inductiveautomation.com/t/evaluate-expression-language-in-script/15012/7 "2017-08-15T16:35:30Z")

</div>

This is as far as I got - so close, and yet so far:

```python
from com.inductiveautomation.ignition.common.expressions.parsing import ELParserHarness
ELParserHarness().parse("1+1", None).execute()

```

The trick is acquiring an ‘ExpressionParseContext’ for the second argument to `parse()` - which I’m fairly certain requires a custom module. Without that, expression functions don’t work.

Also - a disclaimer: This is not officially supported or encouraged.

---

<div class="post-metadata">

**Author:** ![Caleb\_Bohon](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/caleb_bohon/32/3293_2.png) [@Caleb\_Bohon](https://forum.inductiveautomation.com/u/Caleb_Bohon)\
**Post date:** [August 13, 2018, 2:28pm UTC](https://forum.inductiveautomation.com/t/evaluate-expression-language-in-script/15012/8 "2018-08-13T14:28:28Z")

</div>

It seems the link to my feature request broke. Here is a new link:

[http://ideas.inductiveautomation.com/ignition-features-and-ideas/p/create-a-script-function-to-evaluate-an-expression](http://ideas.inductiveautomation.com/ignition-features-and-ideas/p/create-a-script-function-to-evaluate-an-expression)

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [August 13, 2018, 9:31pm UTC](https://forum.inductiveautomation.com/t/evaluate-expression-language-in-script/15012/9 "2018-08-13T21:31:34Z")

</div>

Here is something to get you started:

In shared.expression, place the following:

```auto
from com.inductiveautomation.ignition.client.expressions import ClientFunctionFactory
from com.inductiveautomation.ignition.common.expressions import ExpressionParseContext,BoundTagExpression,TagListener,DefaultFunctionFactory
from com.inductiveautomation.ignition.common.expressions.parsing import ELParserHarness
from com.inductiveautomation.ignition.common.sqltags.parser import TagPathParser
from com.inductiveautomation.ignition.common.sqltags.model.types import DataQuality

class MyExpressionParseContext(ExpressionParseContext):

	def createBoundExpression(self, path):
		try:
			tagPath = TagPathParser.parse(path)
			exp = BoundTagExpression(tagPath)
			v = system.tag.read(path)
			exp.setTagListenerDelegate(TagListener(tagPath, v))
			return exp;
		except:
			return None;

	def getFunctionFactory(self):
		return ClientFunctionFactory(DefaultFunctionFactory.getSharedInstance())
		
parser = ELParserHarness()
parseContext = MyExpressionParseContext()

def parse(expression):
	p = parser.parse(expression,parseContext)
	if p is not None:
		return p.execute().value
	return None

```

Now, in somewhere where scripts run, place the following as an example

```auto
print shared.expressions.parse("{[System]Gateway/Performance/Memory Usage}/1024/1024")

```

Now, this is a basic implementation. Because parse contexts are so different, you will probably have to extend something other than ExpressionParseContext. Maybe AlarmExpressionParseContext. But i am lazy, so you will have to figure that part out. 😉
