Expression Tag - Error_ExpressionEval

I have the script with throw a error_expressioneval. Runs fine in the script console.
Not sure where is the script error. Attached screenshot of expression tag.

def GenPE():

# Define variables
path1 = 'xx/ATS-01-PR-A/POS'
path2 = 'xx/MPE'
path3 = 'xx/MPE_Previous'
path4 = 'xx/PE'
path5 = 'xx/RUN'
tag1 = system.tag.readBlocking(path1)
tag2 = system.tag.readBlocking(path2)
tag3 = system.tag.readBlocking(path3)
tag4 = system.tag.readBlocking(path4)
tag5 = system.tag.readBlocking(path5)
atspos=tag1[0]
mpe=tag2[0]
mpe_prev=tag3[0]
gpe=tag4[0]
run=tag5[0]
pediff=0
if atspos.value==2 and run.value==0:
	pediff = mpe.value - mpe_prev.value
	gpe.value = pediff + gpe.value
	system.tag.writeBlocking(path4, gpe)
system.tag.writeBlocking(path3, mpe)
return gpe.value

energy
energy1

I can't see the problem (yet) but here are a few tips:

  1. See Wiki - how to post code on this forum. Yours is almost OK except for the first line.
  2. system.tag.readBlocking() expects a list of tag paths so even when there is only one it should be in [ ] brackets.
  3. Since system.tag.readBlocking can handle a list you do them all at once.
    tagPaths = ['xx/ATS-01-PR-A/POS',
                'xx/MPE',
                'xx/MPE_Previous',
                'xx/PE'.
                'xx/RUN'
    ]
    tagValues = system.tag.readBlocking(tagPaths)

Your code would then need to reference tagValues[0], etc.

  1. You also need [ ] lists on the writeBlocking line.
    system.tag.writeBlocking([tagPaths[2]], [mpe])

In current Ignition version the functions work when only one parameter is supplied (not in a list) but it's undocumented and could be "fixed" in the future.


A further thought - and this might be the problem: The script needs to be on the gateway, not in your project, otherwise the tag event can't find it. Where have you stored the script?

I also don't see why this calculation needs to be in a script. Why not perform the calculation directly in the expression on the expression tag? Seems that all the input tags have the same base path which could be parameterised.

3 Likes

It will accept a single string as well. It still returns a list though.

Your point #3 is valid however and is definitely the way the script should be written.

Thanks for the reply. Implemented with script configured as a scheduled gateway event.