Trouble using runScript

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

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?

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}+")")

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

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

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

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

I noticed you posted in the v7.7 forum, so I didn’t mention the new features of runScript available in v7.8. But you might consider using objectScript() instead, from the Simulation Aids module, if you need more flexibility.

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

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.

How do you setup alarming for rate of change?