# Run Scripts from Tag Change

**URL:** <https://forum.inductiveautomation.com/t/run-scripts-from-tag-change/34579>\
**Category:** Ignition\
**Tags:** ignition80, scripting, perspective\
**Created:** [May 8, 2020, 3:21pm UTC](https://forum.inductiveautomation.com/t/run-scripts-from-tag-change/34579 "2020-05-08T15:21:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![m.boog](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@m.boog](https://forum.inductiveautomation.com/u/m.boog)\
**Post date:** [May 8, 2020, 3:21pm UTC](https://forum.inductiveautomation.com/t/run-scripts-from-tag-change/34579/1 "2020-05-08T15:21:58Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 8, 2020, 3:33pm UTC](https://forum.inductiveautomation.com/t/run-scripts-from-tag-change/34579/2 "2020-05-08T15:33:44Z")

</div>

> [@m.boog](#):
>
> tag ‘Value Changed’ event scripts can’t call scripts in the Project Library

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.

---

<div class="post-metadata">

**Author:** ![m.boog](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@m.boog](https://forum.inductiveautomation.com/u/m.boog)\
**Post date:** [May 8, 2020, 5:51pm UTC](https://forum.inductiveautomation.com/t/run-scripts-from-tag-change/34579/3 "2020-05-08T17:51:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 8, 2020, 6:08pm UTC](https://forum.inductiveautomation.com/t/run-scripts-from-tag-change/34579/4 "2020-05-08T18:08:45Z")

</div>

> [@m.boog](#):
>
> 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](https://docs.inductiveautomation.com/display/DOC80/Tag+Event+Scripts) - what you're describing, where you specify a `valueChanged()` script on a particular tag, I'm suggesting using [tag change scripts](https://docs.inductiveautomation.com/display/DOC80/Gateway+Event+Scripts#GatewayEventScripts-TagChangeScripts) - 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.

---

<div class="post-metadata">

**Author:** ![kgamble](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kgamble/32/8019_2.png) [@kgamble](https://forum.inductiveautomation.com/u/kgamble)\
**Post date:** [May 8, 2020, 7:40pm UTC](https://forum.inductiveautomation.com/t/run-scripts-from-tag-change/34579/5 "2020-05-08T19:40:07Z")

</div>

@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?

---

<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:** [May 9, 2020, 12:45pm UTC](https://forum.inductiveautomation.com/t/run-scripts-from-tag-change/34579/6 "2020-05-09T12:45:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kgamble](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kgamble/32/8019_2.png) [@kgamble](https://forum.inductiveautomation.com/u/kgamble)\
**Post date:** [May 9, 2020, 1:20pm UTC](https://forum.inductiveautomation.com/t/run-scripts-from-tag-change/34579/7 "2020-05-09T13:20:28Z")

</div>

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?

---

<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:** [May 10, 2020, 2:20am UTC](https://forum.inductiveautomation.com/t/run-scripts-from-tag-change/34579/8 "2020-05-10T02:20:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![m.boog](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@m.boog](https://forum.inductiveautomation.com/u/m.boog)\
**Post date:** [May 10, 2020, 3:07am UTC](https://forum.inductiveautomation.com/t/run-scripts-from-tag-change/34579/9 "2020-05-10T03:07:07Z")

</div>

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