# Tag Value Change Script gets executed multiple times

**URL:** <https://forum.inductiveautomation.com/t/tag-value-change-script-gets-executed-multiple-times/65089>\
**Category:** Ignition\
**Tags:** scripting, ignition79\
**Created:** [October 11, 2022, 9:24pm UTC](https://forum.inductiveautomation.com/t/tag-value-change-script-gets-executed-multiple-times/65089 "2022-10-11T21:24:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![sathianandan.dharmas](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@sathianandan.dharmas](https://forum.inductiveautomation.com/u/sathianandan.dharmas)\
**Post date:** [October 11, 2022, 9:24pm UTC](https://forum.inductiveautomation.com/t/tag-value-change-script-gets-executed-multiple-times/65089/1 "2022-10-11T21:24:30Z")

</div>

Hi All,

I have a named query that inserts one row of data, I'm calling this named query from a tag value change script (which also passes the tag value to that named query). Ideally I shouldn't see any duplicate values as it's a tag value change but I'm seeing a lot of duplicates.

I have seen instances when the gateway is restarted then it triggers the tag value change script somehow but even if the gateway stays on it's happening. This is an OPC tag and I don't think the PLC is writing different values or same values to it that often. I even tried testing by writing the same value to the tag from a script console and it didn't trigger the script.

Is this issue seem familiar to anyone or any recommendations to avoid duplicate entries?

Thanks in advance, Sam

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [October 11, 2022, 9:28pm UTC](https://forum.inductiveautomation.com/t/tag-value-change-script-gets-executed-multiple-times/65089/2 "2022-10-11T21:28:48Z")

</div>

Is this a tag event script or gateway tag change script (part of a project)? Are you checking the initial change flag? If it's a gateway tag change script then it's going to fire every time somebody saves the project, and if you aren't checking the flag to guard your logic it will run every time.

---

<div class="post-metadata">

**Author:** ![sathianandan.dharmas](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@sathianandan.dharmas](https://forum.inductiveautomation.com/u/sathianandan.dharmas)\
**Post date:** [October 11, 2022, 9:57pm UTC](https://forum.inductiveautomation.com/t/tag-value-change-script-gets-executed-multiple-times/65089/3 "2022-10-11T21:57:10Z")

</div>

This is a tag event script, this is how the calling works:

Tag event change -\> Shared script -\> Named Query

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [October 11, 2022, 10:03pm UTC](https://forum.inductiveautomation.com/t/tag-value-change-script-gets-executed-multiple-times/65089/4 "2022-10-11T22:03:56Z")

</div>

Maybe it's the quality that is changing sometimes.

You could add some debug logging that prints the initial change flag, previous value, and current value and try to figure out what's happening.

You can also check that previous value is not null and that it's not equal to the current value to prevent your logic from firing when the value hasn't changed.

---

<div class="post-metadata">

**Author:** ![sathianandan.dharmas](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@sathianandan.dharmas](https://forum.inductiveautomation.com/u/sathianandan.dharmas)\
**Post date:** [October 11, 2022, 10:22pm UTC](https://forum.inductiveautomation.com/t/tag-value-change-script-gets-executed-multiple-times/65089/5 "2022-10-11T22:22:22Z")

</div>

Can a quality change trigger a value change script?

I'll try your approach of comparing with the previous value before insertion and checking for the initial change flag. Thanks a lot, @Kevin.Herron for the quick help.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [October 12, 2022, 3:25am UTC](https://forum.inductiveautomation.com/t/tag-value-change-script-gets-executed-multiple-times/65089/6 "2022-10-12T03:25:54Z")

</div>

> [@sathianandan.dharmas](#):
>
> Can a quality change trigger a value change script?

Yes.

It is common to do a check that the actual value has changed.

```python
if not initialChange and currentValue.value != previousValue.value:
    #change script here

```

If you think about it, it makes more since. If the quality is something other than 'Good', then the OPC server is saying that it can not guarantee that the value is up to date.

Since a tag's "value" is only part of its true value, which is a `QualifiedValue`, then any part of that value that becomes different, is a change.

Edit: Changed the order of the checks in the script. Per @pascal.fragnoud note.

---

<div class="post-metadata">

**Author:** ![PRAMANJ](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pramanj/32/66935_2.png) [@PRAMANJ](https://forum.inductiveautomation.com/u/PRAMANJ)\
**Post date:** [October 12, 2022, 5:24am UTC](https://forum.inductiveautomation.com/t/tag-value-change-script-gets-executed-multiple-times/65089/7 "2022-10-12T05:24:10Z")

</div>

If you have the same script defined in another project other than the currently open project in the gateway, the script will run there also whenever there is a tag change seen by both scripts! You have to disable all other scripts in other projects defined in the gateway. I had experienced similar situation in my case.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [October 12, 2022, 7:02am UTC](https://forum.inductiveautomation.com/t/tag-value-change-script-gets-executed-multiple-times/65089/8 "2022-10-12T07:02:31Z")

</div>

> [@lrose](#):
>
> ```auto
> if currentValue.value != previousValue.value and not initialChange:
> #change script here
> 
> ```

I think this should be the other way around, check for `initialChange first` to make sure `previousValue` does exist and has a value.  
If `initialChange` is true, the second condition would then be skipped, and dereferencing `previousValue` would not raise an error as it would if done first.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [October 12, 2022, 11:11am UTC](https://forum.inductiveautomation.com/t/tag-value-change-script-gets-executed-multiple-times/65089/9 "2022-10-12T11:11:42Z")

</div>

> [@pascal.fragnoud](#):
>
> I think this should be the other way around, check for `initialChange first` to make sure `previousValue` does exist and has a value.

Yeah. Fixed in the original post. Thanks.

> [@prafull\_j](#):
>
> If you have the same script defined in another project other than the currently open project in the gateway, the script will run there also whenever there is a tag change seen by both scripts! You have to disable all other scripts in other projects defined in the gateway

This is because, _Gateway_ Tag Change Scripts are Project Resources. However, this thread is in reference to Tag Value Change scripts, which are Gateway Global. You can not define more than one script.

---

<div class="post-metadata">

**Author:** ![sathianandan.dharmas](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@sathianandan.dharmas](https://forum.inductiveautomation.com/u/sathianandan.dharmas)\
**Post date:** [October 12, 2022, 1:41pm UTC](https://forum.inductiveautomation.com/t/tag-value-change-script-gets-executed-multiple-times/65089/10 "2022-10-12T13:41:42Z")

</div>

@lrose thanks a lot!  
It makes sense now, maybe change in the 'QualifiedValue' is what is happening in my case. Nonetheless. I'll put both of those conditions check in place.

However can this if statement prevent a duplicate entry when I restart the gateway? Initially I used to see some.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [October 12, 2022, 3:14pm UTC](https://forum.inductiveautomation.com/t/tag-value-change-script-gets-executed-multiple-times/65089/11 "2022-10-12T15:14:37Z")

</div>

> [@sathianandan.dharmas](#):
>
> However can this if statement prevent a duplicate entry when I restart the gateway?

Yeah, that is one of, if not "the", main reason for the `initialChange` flag.

---

<div class="post-metadata">

**Author:** ![sathianandan.dharmas](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@sathianandan.dharmas](https://forum.inductiveautomation.com/u/sathianandan.dharmas)\
**Post date:** [October 12, 2022, 3:24pm UTC](https://forum.inductiveautomation.com/t/tag-value-change-script-gets-executed-multiple-times/65089/12 "2022-10-12T15:24:35Z")

</div>

Awesome. Appreciate the help!

---

<div class="post-metadata">

**Author:** ![sathianandan.dharmas](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@sathianandan.dharmas](https://forum.inductiveautomation.com/u/sathianandan.dharmas)\
**Post date:** [October 12, 2022, 8:57pm UTC](https://forum.inductiveautomation.com/t/tag-value-change-script-gets-executed-multiple-times/65089/13 "2022-10-12T20:57:12Z")

</div>

> [@lrose](#):
>
> `if not initialChange and currentValue.value != previousValue.value:`

I tried the solution you mentioned and it's not producing duplicates except for a gateway restart. I tried print on the logs like @Kevin.Herron suggested and the 'initialChange' bool was false as well as previousValue.value was None. So I had to add another condition to your if statement not to allow None.

> [@Kevin.Herron](#):
>
> You can also check that previous value is not null and that it's not equal to the current value to prevent your logic from firing when the value hasn't changed.

---

<div class="post-metadata">

**Author:** ![hhbauss](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@hhbauss](https://forum.inductiveautomation.com/u/hhbauss)\
**Post date:** [April 28, 2023, 3:36pm UTC](https://forum.inductiveautomation.com/t/tag-value-change-script-gets-executed-multiple-times/65089/14 "2023-04-28T15:36:20Z")

</div>

How did you fix this?? I am currently running into this.

I have a gateway with 16 identical projects on it for 16 lines. In the event of a tag getting triggered, all 16 projects run the same tag change script and I get 16 identical entries in my database. All the lines are running in separate sessions.

How do I only run the script for the session that calls it?

Please and thank you!

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [April 28, 2023, 3:42pm UTC](https://forum.inductiveautomation.com/t/tag-value-change-script-gets-executed-multiple-times/65089/15 "2023-04-28T15:42:52Z")

</div>

There's a design issue somewhere.

Why are all projects monitoring the same tags ?  
If a tag only concern one project, monitor it from this project.  
If they are 'global' tags, monitor them from a 'global' project, or use tags 'value changed' scripts.

---

<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:** [April 28, 2023, 3:47pm UTC](https://forum.inductiveautomation.com/t/tag-value-change-script-gets-executed-multiple-times/65089/16 "2023-04-28T15:47:50Z")

</div>

> [@hhbauss](#):
>
> all 16 projects run the same tag change script

That means you've defined your events in an inheritable project. While inheritance is wonderful for many things, it is fundamentally unusable for project event scripts.

Edit each project's tag change events to list only those tags that the specific project should respond to.

Then eliminate all event definitions from **inheritable** projects.

Remember that tags themselves, other that Vision client tags, do not "belong" to any particular project.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [April 28, 2023, 4:02pm UTC](https://forum.inductiveautomation.com/t/tag-value-change-script-gets-executed-multiple-times/65089/17 "2023-04-28T16:02:26Z")

</div>

> [@pturmel](#):
>
> That means you've defined your events in an inheritable project.

You'd think (and hope).  
But he might have 16 copies of the same project, with each update being copied to the 15 other projects.  
I've suffered through this. Took me a year and a half to convince them to let me refactor the whole thing into a single inheritable project.

---

<div class="post-metadata">

**Author:** ![PRAMANJ](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pramanj/32/66935_2.png) [@PRAMANJ](https://forum.inductiveautomation.com/u/PRAMANJ)\
**Post date:** [April 28, 2023, 5:54pm UTC](https://forum.inductiveautomation.com/t/tag-value-change-script-gets-executed-multiple-times/65089/18 "2023-04-28T17:54:22Z")

</div>

Tag data base being common for all projects and global scripts remaining active in all projects, whether open or close, is the main cause of this problem! The scripts that are triggered by a tag in all projects will fire irrespective of whether a project is open or not!

---

<div class="post-metadata">

**Author:** ![hhbauss](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@hhbauss](https://forum.inductiveautomation.com/u/hhbauss)\
**Post date:** [May 1, 2023, 8:04pm UTC](https://forum.inductiveautomation.com/t/tag-value-change-script-gets-executed-multiple-times/65089/19 "2023-05-01T20:04:55Z")

</div>

Sorry...still fighting through Perspective vs Vision in respects to client events. I am using Perspective.

Back story: There are 12 lines at a plant. Each line needs to display the same screens but with their own tag information. Each line has its own PLC and set of tags associated with it. Since they are using thin clients through terminal services, I needed to use Workstation launcher in order to have separate sessions for the same project (they all use the same browser, so they all would be the same session if launched from browser). But launching the same project from a workstation still had projects overriding the other's screens and actions. So, I copied the main project to create separate projects. All 12 (current) projects query the same DB for the work orders, with a custom session property dictating what info is displayed on the screens.

As for solutions, how would one design around 16 identical projects, each with their own tags, running on one gateway, using a thin clients attached to one server?

---

<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 1, 2023, 8:18pm UTC](https://forum.inductiveautomation.com/t/tag-value-change-script-gets-executed-multiple-times/65089/20 "2023-05-01T20:18:30Z")

</div>

> [@hhbauss](#):
>
> using a thin clients attached to one server

Change this. Don't use thin clients. Or rather, thin clients are fine if not used with a terminal server. Ignition clients tend to be thin enough to run directly on such hardware, and should be.

[Next page](https://forum.inductiveautomation.com/t/tag-value-change-script-gets-executed-multiple-times/65089.md?page=2)
