# Trouble using runScript

**URL:** <https://forum.inductiveautomation.com/t/trouble-using-runscript/12854>\
**Category:** Ignition\
**Created:** [March 28, 2017, 8:02pm UTC](https://forum.inductiveautomation.com/t/trouble-using-runscript/12854 "2017-03-28T20:02:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![waker1der](https://avatars.discourse-cdn.com/v4/letter/w/dbc845/32.png) [@waker1der](https://forum.inductiveautomation.com/u/waker1der)\
**Post date:** [March 28, 2017, 8:02pm UTC](https://forum.inductiveautomation.com/t/trouble-using-runscript/12854/1 "2017-03-28T20:02:35Z")

</div>

Hi I am trying to use runScript in an expression tag that is part of a UDT.

the UDT has 3 query tags that pull values from my DB. All values are int.

I want to work out rate of change between two of the values using a script so if the rate is negative I can handle the value.

my project script called rateOfChange is as follows.

`def getChange(current, previous):
    if current == previous:
        return 100.0
    change = current - previous
    try:
    	percentage_change = (change / float(old_value))
    	if multiply:
     		percentage_change = percentage_change * 100
       	return percentage_change
    except ZeroDivisionError:
    	return None`

then in my expression tag I have added the following

```auto
runScript("project.rateOfChange.get_change({[.]5min},{[.]10min})",150000)
```

so the inputs in to my function are the tag values for 5min and 10min tags. I have also tried this without the “” but I get the following error

`Last Error Cause	com.inductiveautomation.ignition.common.expressions.ExpressionException: Error parsing script for runScript() expression: SyntaxError: ("no viable alternative at input '.'", ('<expression:runScript>', 1, 44, '__RESULT = project.rateOfChange.getChange({[.]5min},{[.]10min})\n'))
	ERROR\n'))
	ERROR`

can anyone help?

---

<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:** [March 28, 2017, 8:17pm UTC](https://forum.inductiveautomation.com/t/trouble-using-runscript/12854/2 "2017-03-28T20:17:29Z")

</div>

Three changes are needed:

1. You must construct a string for runScript()'s first parameter that is valid python by itself.
2. You cannot use project.\* scripts in expression tags, other than client tags. You’ll have to put the function you need in the shared.\* scripts.
3. Expression tags run at their scan class. The “15000” would have been ignored.

Something like this:`runScript("shared.rateOfChange.get_change("+{[.]5min}+","+{[.]10min}+")")`

---

<div class="post-metadata">

**Author:** ![waker1der](https://avatars.discourse-cdn.com/v4/letter/w/dbc845/32.png) [@waker1der](https://forum.inductiveautomation.com/u/waker1der)\
**Post date:** [March 29, 2017, 7:52am UTC](https://forum.inductiveautomation.com/t/trouble-using-runscript/12854/3 "2017-03-29T07:52:27Z")

</div>

thank you, i didn’t realise I had to build the string like that.

and yes my script was jargon 😊 all works now, thank you for the help

---

<div class="post-metadata">

**Author:** ![waker1der](https://avatars.discourse-cdn.com/v4/letter/w/dbc845/32.png) [@waker1der](https://forum.inductiveautomation.com/u/waker1der)\
**Post date:** [March 29, 2017, 8:15am UTC](https://forum.inductiveautomation.com/t/trouble-using-runscript/12854/4 "2017-03-29T08:15:41Z")

</div>

for anyone interested i wrote the following for my rate of change script

```auto
def getChange(x1,y1,x2,y2):
    if y1 == y2:
        return 0
    else:
    	try:
    		rate = (float(y2-y1)/float(x2-x1))
    		return rate
    	except ZeroDivisionError:
    		return 0
```

---

<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:** [March 29, 2017, 1:44pm UTC](https://forum.inductiveautomation.com/t/trouble-using-runscript/12854/5 "2017-03-29T13:44:39Z")

</div>

I noticed you posted in the v7.7 forum, so I didn’t mention the [new features of runScript](https://inductiveautomation.com/downloads/changelog/410) available in v7.8. But you might consider using [objectScript()](https://www.automation-pros.com/simaids/doc/expression.objectscript.html) instead, from the [Simulation Aids](https://inductiveautomation.com/moduleshowcase/module/automation-professionals-llc-simulation-aids) module, if you need more flexibility.

---

<div class="post-metadata">

**Author:** ![jose.granero](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jose.granero/32/11190_2.png) [@jose.granero](https://forum.inductiveautomation.com/u/jose.granero)\
**Post date:** [September 15, 2017, 10:55am UTC](https://forum.inductiveautomation.com/t/trouble-using-runscript/12854/6 "2017-09-15T10:55:48Z")

</div>

Hi Phil,

I’ve tried to use runScript in an expression tag part of a UDT also contained in another UDT but haven’t been able to make it work. I’ve even simplified the script so that it just returned a UDT tag with the same result. Any ideas will be welcome.

I’m usind V9.3

---

<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:** [September 15, 2017, 12:34pm UTC](https://forum.inductiveautomation.com/t/trouble-using-runscript/12854/7 "2017-09-15T12:34:18Z")

</div>

> [@jose.granero](#):
>
> ... but haven't been able to make it work.

I wouldn't expect runScript to be much help in tag expressions. objectScript() might be more helpful, but you'll have to share the details. I only use UDTs to automate tag construction, not in bindings or expressions, so I'm not sure what underlying java types would be needed to return a UDT from a script expression.

---

<div class="post-metadata">

**Author:** ![snoopyflyingace](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@snoopyflyingace](https://forum.inductiveautomation.com/u/snoopyflyingace)\
**Post date:** [October 17, 2017, 10:49pm UTC](https://forum.inductiveautomation.com/t/trouble-using-runscript/12854/8 "2017-10-17T22:49:22Z")

</div>

How do you setup alarming for rate of change?
