Converting to String in UDT Event Scripting

I am trying to create an event log message for active, ack'd and cleared alarms within a UDT. I had a very hard time finding how to properly access parameters of the UDT within this scripting and nothing in the manual seemed to be working, and now I am having the same problem with converting an integer to a string.

Here is a snippet :

message = "Alarm Active: " + alarmName + " " + diag + " " + tag[ 'parameters' ][ 'alarmMessage' ]

Above script works perfectly when I remove the diag variable and when I run as above I see an error in the log for " Type error: cannot concatenate 'str' and 'int' objects". I have tried to toStr and toString methods in the manual but they are not working in the UDT script or script console.

try str(diag)

Where are you putting this "script"?

Scripting (like you would use in the script console) is not the same as the expression language.

toStr() is not a valid built in of the Jython Language that Ignition uses for scripting.

Can you provide the entire "script" as Preformatted text?

I am using this script within the UDT scripting events. I have noticed that the expression language and script console / UDT event scripting are different, but I don't fully understand the differences. Can you explain further? Or is there any documentation about the differences? Thank you for your help!

This worked! Thank you!

Expressions:

Scripting (note the many subpages):

1 Like

The expression language is IA's own language. It can not assign values and has no concept of local variables. In other words it can't do something like:

diag = 10
message = "Some message" + toStr(diag)
return message

Instead you would do something like:

concat("Some message", toStr({[.]diag}))

Where the result of the concat expression function is returned to the caller.

Where as scripting uses the Jython language. Jython is an implementation of Python (2.7 for Ignition 8.0+) written in Java and integrated with Java. This works well with Ignition as it is also written in Java, so Java classes including many of Ignitions own classes can be imported and used directly. That would look something like the following:

diag = 10
message = "Some message" + str(diag)
return message

All of this and more can be found in the User Manual.

The reason I was asking is because I don't want to guess at exactly what it is you're trying to accomplish. If I knew what the entire script was meant to accomplish I could possibly suggest an alternate way to accomplish whatever your goal is.

In general, I try to avoid any scripting in tags UDT or otherwise.

2 Likes

Thank you so much for this answer! I am very new to ignition and have inherited a fully formed project that I am basically trying to hack my way through. I'm sure there is a better way to do what I am doing, but scripting within the UDT seems like the least amount of effort and changes to the current project. Why is it that you try to avoid scripting in tags?

Tag Value Change scripts for all tags have a limited thread pool (size of 3) that they can use. If all three threads in the pool are "in use", then any events that occurs during that time will be missed. Some things can tolerate a missed event or two, but many can not.

Best practice is that any Tag Value Change event executes in single digit milliseconds. It doesn't take much of a script to exceed that limit.

Sometimes scripts just need moved to Gateway Events, sometimes there is another mechanism that is intended for what is being done.

It doesn't take long to overwhelm the tag system, and the side effects can be hard to detect to say the least.

1 Like

There's a whole bunch of posts on this forum from people experiencing this. ):

1 Like

:grimacing:
My UDT has 4 sub-folders. In 3 of those sub folders there is scripting for Value Changed events. I am not sure what the scripting does, I haven't dug too deep since I am using the Alarm Events scripts. I have ~ 3000 tags with this UDT as the parent type. Does this mean that if more than 3 of the tags alarm active that I could miss other alarm events? I definitely cannot have missed alarm events.
I would happily change my scripting to a gateway event, but I am not sure how to trigger the script if I am trying to use the alarm active status from my tags. I see that tag change is already a gateway event script, but there is nothing that jumps out to me as clearly working for alarm events there.

You'll have to look at the scripts to see what they are doing. There's no shortcut.

If you find that the alarm events are doing potentially dangerous things (running too long), you might find the advanced techniques described here helpful:

1 Like

I have looked at the scripts that were in place before I added anything to the alarm events. The UDT has 4 sub-divisions and from what I understand it is looking at the value (0/1) from one and writing that value to the others if they don't match. They are only a few lines each, but I am not sure how to view how long the script takes to run. Regardless, there are 3 scripts in every instance of that UDT, so even if I remove the alarm event scripting I could still have issues because of the 3 value changed scripts for every alarm? If that is correct I fear I might have to do a pretty big overhaul and the advanced techniques you linked seem a bit out of my league. If I described what I have written here to someone with more ignition experience, is what I have said correct?

That sounds sketchy. You should share actual scripts for us to critique. Or ask for support to help. (Or ask your favorite integrator for help.)