Run Scripts from Tag Change

I am working on a Perspective project on Gateway 8.0.10.

I have a script in the Project Library that is a very simple state machine: If A is true, go to B. If A and F are true, go to C (and so forth). I want to trigger running the script (which checks tag values, then updates the state) on a tag change, since the script only needs to be checked when a tag changes.

Since tag ‘Value Changed’ event scripts can’t call scripts in the Project Library, is there an easy way to use this event to run my script in the Project Library?

Thanks in advance!

Sure they can - set the 'gateway scripting project' in Configure -> Gateway Settings. Or, as @pturmel will tell you, use project-level gateway tag change scripts over individual tag events.

Thanks for the quick reply. Setting the Gateway Scripting Project is not an option, as another project on the gateway has already been assigned that purpose.

To your second point - I added the code state_machine.state_machine_evaluate()
in the tag’s valueChanged() event script.

I have a script in the Project Library root folder called state_machine, with the function:

def state_machine_evaluate():
system.tag.write("[default]dln_wcs/Station_1/Container_Present",True)
return 0

This function is not called (or errors quietly) when the tag changes. Am I missing something?

In a word, yes, but it's because the terminology here is a little overloaded. Since changing the gateway scripting project isn't an option, you need to use a different paradigm. Rather than tag event scripts - what you're describing, where you specify a valueChanged() script on a particular tag, I'm suggesting using tag change scripts - a project scoped resource (that still executes on the gateway) that has full access to your project script library.

"Tag change" vs "tag event" scripts are obviously pretty unfortunate terminology, but it's what we've got.

2 Likes

@pturmel, I am just curious, why do you advise to use project-level gateway tag scripts over individual tag events? Just for keeping everything in the same place? Or is there another reason?

Maintainability–shared script editing in particular–without disrupting the global scripting project (v8) or all projects (v7.x). Tag events defined in UDTs mitigate the problem somewhat, but then all of the affected tags restart when those are edited. Editing gateway events in a project, along with the associated project scripts, disrupts only that project. Tags don’t restart at all.

2 Likes

I’m curious so let’s say we have a UDT with 10 tags each with event scripts. Then you have 10 instances of that UDT.

Do you create your script in a library, and then have to set a tag event for all 100 of those tags? Or is there a way to have it apply to everything like when you put it on the UDT. Like having the UDT Tag script just call the library function, so that when you adjust the library function it doesn’t restart the tags?

Can you do a project level tag even script for a UDT definition?

Gateway tag change events allow you to list the tags that should trigger them. Can be hundreds or thousands. Which tag is involved in each event is passed in. Changing the list of subscribed tags when adding/removing/changing behavior is a project save, not a tag restart. The project does not have to the global scripting project. The event script can (and should) call project script functions in that same project.

2 Likes

Thanks for the clarification, that’s the piece I was missing. Works like a charm now.

1 Like