# Tag Change Script - initialChange behaviour

**URL:** <https://forum.inductiveautomation.com/t/tag-change-script-initialchange-behaviour/33976>\
**Category:** Ignition\
**Tags:** ignition80\
**Created:** [April 16, 2020, 11:44am UTC](https://forum.inductiveautomation.com/t/tag-change-script-initialchange-behaviour/33976 "2020-04-16T11:44:42Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavidWisely](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/davidwisely/32/10482_2.png) [@DavidWisely](https://forum.inductiveautomation.com/u/DavidWisely)\
**Post date:** [April 16, 2020, 11:44am UTC](https://forum.inductiveautomation.com/t/tag-change-script-initialchange-behaviour/33976/1 "2020-04-16T11:44:42Z")

</div>

In my Gateway Tag Change Event Scripts I use

```auto
if not initialChange:

```

to prevent the script running after a script change or tag change. And it works well for this. If I don’t use this, the script will run when it shouldn’t.

However, if the gateway is restarted, it also prevents the script running the first time its triggered. In this case I do want it to fire, and this is causing problems. Subsequent triggers run fine after this.  
For example, if the gateway is restarted and 4 hours later the event is triggered for the first time since the restart, the initialChange line will prevent it from running.

How do I prevent it running on a script change or tag change, and allow it to run the first time it’s triggered after a gateway restart?

Note: This is on version 8.0.6

---

<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 16, 2020, 12:11pm UTC](https://forum.inductiveautomation.com/t/tag-change-script-initialchange-behaviour/33976/2 "2020-04-16T12:11:14Z")

</div>

Sounds like a bug.

---

<div class="post-metadata">

**Author:** ![DavidWisely](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/davidwisely/32/10482_2.png) [@DavidWisely](https://forum.inductiveautomation.com/u/DavidWisely)\
**Post date:** [April 16, 2020, 12:24pm UTC](https://forum.inductiveautomation.com/t/tag-change-script-initialchange-behaviour/33976/3 "2020-04-16T12:24:52Z")

</div>

I just tested in on version 8.0.11 and it behaves the same. I’ll contact support and see what they think.

---

<div class="post-metadata">

**Author:** ![diat150](https://avatars.discourse-cdn.com/v4/letter/d/a587f6/32.png) [@diat150](https://forum.inductiveautomation.com/u/diat150)\
**Post date:** [April 16, 2020, 5:32pm UTC](https://forum.inductiveautomation.com/t/tag-change-script-initialchange-behaviour/33976/4 "2020-04-16T17:32:24Z")

</div>

pretty sure this has always worked in this manner.

---

<div class="post-metadata">

**Author:** ![DavidWisely](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/davidwisely/32/10482_2.png) [@DavidWisely](https://forum.inductiveautomation.com/u/DavidWisely)\
**Post date:** [April 17, 2020, 1:07am UTC](https://forum.inductiveautomation.com/t/tag-change-script-initialchange-behaviour/33976/5 "2020-04-17T01:07:35Z")

</div>

Not sure if this is a bug or not. Still talking to support about it.  
But in the meantime, to get around this issues the following works:

1. Create an additional memory tag to store the last value of the trigger.
2. Use this to test if it is the same as the trigger value, instead of using the initialChange flag.

E.g. instead of this:

```auto
if newValue.value == 1: 
	if not initialChange:
		#[run code here]

```

use this:

```auto
# read memory flag
tagPath = '[default]MemoryFlag'
flag = system.tag.readBlocking([tagPath])[0].value
 
if newValue.value == 1: 
	if newValue.value != flag:
		#[run code here]

# update memory flag		
if newValue.value == 1 and flag == 0:
	system.tag.writeAsync([tagPath],[1])
elif newValue.value == 0 and flag == 1:
	system.tag.writeAsync([tagPath],[0])

```

This works in all cases I’ve tested and is how I expected the initialChange flag to work. But it requires a bit more effort.

---

<div class="post-metadata">

**Author:** ![filipg](https://avatars.discourse-cdn.com/v4/letter/f/a88e4f/32.png) [@filipg](https://forum.inductiveautomation.com/u/filipg)\
**Post date:** [April 21, 2020, 2:59pm UTC](https://forum.inductiveautomation.com/t/tag-change-script-initialchange-behaviour/33976/6 "2020-04-21T14:59:54Z")

</div>

Does the same bug/nuance exist in 7.9.xx?

I make it a practice to read through this group and use advice like David’s in my scripts, as a “good practice” (regardless of whether a bug bit me or not).

---

<div class="post-metadata">

**Author:** ![DavidWisely](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/davidwisely/32/10482_2.png) [@DavidWisely](https://forum.inductiveautomation.com/u/DavidWisely)\
**Post date:** [April 21, 2020, 11:16pm UTC](https://forum.inductiveautomation.com/t/tag-change-script-initialchange-behaviour/33976/7 "2020-04-21T23:16:16Z")

</div>

Hi filipg,

Not sure if this happens in 7.9 but I suspect it does.  
You can test it by creating this test event:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/2/2e4e302e5eadb02688d84369fe5ff72acfb69907.jpeg)

Each time you toggle the TestTag from 0 to 1, all three prints should appear in the wrapper.log.  
If you clear the TestTag, restart the gateway, then set the TestTag to 1: if it has this problem then you will only see print 1 and 2. If there is no problem you should see print 1,2 and 3.

---

<div class="post-metadata">

**Author:** ![Tom\_Beard](https://avatars.discourse-cdn.com/v4/letter/t/ecb155/32.png) [@Tom\_Beard](https://forum.inductiveautomation.com/u/Tom_Beard)\
**Post date:** [June 26, 2023, 6:40am UTC](https://forum.inductiveautomation.com/t/tag-change-script-initialchange-behaviour/33976/8 "2023-06-26T06:40:43Z")

</div>

Hi @DavidWisely,

Did you ever get this resolved with support?  
I'm experiencing the same issue in 8.1.18. Tested it in a clean installation and have found it only occurs with the Default tag group. Creating a new tag group identical to the default and the issue goes away.

---

<div class="post-metadata">

**Author:** ![DavidWisely](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/davidwisely/32/10482_2.png) [@DavidWisely](https://forum.inductiveautomation.com/u/DavidWisely)\
**Post date:** [June 29, 2023, 6:05am UTC](https://forum.inductiveautomation.com/t/tag-change-script-initialchange-behaviour/33976/9 "2023-06-29T06:05:34Z")

</div>

Hi Tom,  
Didn't get it fixed by support and still using the work around code. Not sure if newer versions have the same problem. The work around code works so well I just left it. Good to know about creating a new tag group.

---

<div class="post-metadata">

**Author:** ![diamond](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@diamond](https://forum.inductiveautomation.com/u/diamond)\
**Post date:** [November 16, 2023, 9:18pm UTC](https://forum.inductiveautomation.com/t/tag-change-script-initialchange-behaviour/33976/10 "2023-11-16T21:18:34Z")

</div>

Hi, wouldn't it be cleaner just to create a boolean memory tag ("bootFlag"), and just add it to your trigger list?

In the startup script, just flip the boolean value to trigger your scripts for the first time.

> bootFlag = not bootFlag

Thinking about scalability, I believe this could be a better approach for big projects.

Let me know if you see any objection to this.

---

<div class="post-metadata">

**Author:** ![DavidWisely](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/davidwisely/32/10482_2.png) [@DavidWisely](https://forum.inductiveautomation.com/u/DavidWisely)\
**Post date:** [November 16, 2023, 10:31pm UTC](https://forum.inductiveautomation.com/t/tag-change-script-initialchange-behaviour/33976/11 "2023-11-16T22:31:55Z")

</div>

Hi Fernando,  
That will probably work too. I'll stick to my solution for now as it's more explicit and I find it easier to understand. One issue with the bootFlag is that if this is a bug, as suggested, and fixed at some point, then scripts will start triggering on startup when they are not supposed to.

---

<div class="post-metadata">

**Author:** ![diamond](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@diamond](https://forum.inductiveautomation.com/u/diamond)\
**Post date:** [November 17, 2023, 7:11am UTC](https://forum.inductiveautomation.com/t/tag-change-script-initialchange-behaviour/33976/12 "2023-11-17T07:11:58Z")

</div>

Good point. It is surprising that the ignition team does not take a position on this issue. If this is the expected performance, they should at least specify it in the documentation. In our plant this has caused problems; it is not possible to design systems reliably in this way.

---

<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:** [November 17, 2023, 9:35am UTC](https://forum.inductiveautomation.com/t/tag-change-script-initialchange-behaviour/33976/13 "2023-11-17T09:35:35Z")

</div>

I think you are reading too much into the supposed meaning of the `initialChange` flag. Fundamentally, it is a signal from Ignition saying something like "I just woke up, don't know what was going on while I slept/dozed/crashed, and you should double check the following values." Simply put, this isn't a bug.

Ignition is pretty good about identifying and passing on valid change while running, but simply cannot do so upon startup or project edit. Using another form of state storage is entirely appropriate.

---

<div class="post-metadata">

**Author:** ![rbachman](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/rbachman/32/43718_2.png) [@rbachman](https://forum.inductiveautomation.com/u/rbachman)\
**Post date:** [October 22, 2024, 11:13am UTC](https://forum.inductiveautomation.com/t/tag-change-script-initialchange-behaviour/33976/14 "2024-10-22T11:13:58Z")

</div>

We are having a similar problem, and it is specific to Memory tags. I have a theory on why this happens, although I would need some help from someone with deeper knowledge of how Ignition works to know if this is true.

For an OPC tag, I suspect that the default value of the tag is essentially _ **null** _ until the tag is read by Ignition for the first time. When that first read tags place, the value of the OPC tag within the Tag Browser essentially changes from _ **null** _ to whatever the actual value of the tag is, and the event executes.

However, for a Memory tag, I suspect that the value does **not** change following a reboot since the tag is basically a value in a SQLite database. As a result, when the event script executes for the first time after the gateway has been restarted, it is the first execution of the script and therefore exits intentionally because of the

> if not initialChange:

line. It would be really nice if someone from IA could confirm this so we can know for future reference.

@DavidWisely Can you confirm if the original tag for your Gateway Tag Change Event Script is a Memory tag?

---

<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:** [October 22, 2024, 11:19am UTC](https://forum.inductiveautomation.com/t/tag-change-script-initialchange-behaviour/33976/15 "2024-10-22T11:19:49Z")

</div>

I'm not sure I understand your supposition. This is what happens:

- Tag starts up. Fires event with `initialChange` true. OPC tag doesn't have a current value yet. Memory tag does.

- Value arrives (OPC by subscription, memory by deliberate write), if different from prior value, event fires.

🤷‍♂️

---

<div class="post-metadata">

**Author:** ![rbachman](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/rbachman/32/43718_2.png) [@rbachman](https://forum.inductiveautomation.com/u/rbachman)\
**Post date:** [October 22, 2024, 11:28am UTC](https://forum.inductiveautomation.com/t/tag-change-script-initialchange-behaviour/33976/16 "2024-10-22T11:28:30Z")

</div>

> [@pturmel](#):
>
> Tag starts up. Fires event with `initialChange` true. OPC tag doesn't have a current value yet. Memory tag does.

Yes, this is related to what I was trying to say. I don't think the Gateway Tag Change Event executes when the tag starts up for Memory tags. As a result, the first execution of the Event is when the actual value of the Memory tag changes. However, for an OPC tag, the first execution of the Event is when the tag is read for the first time because, at the time of the Tag starting up, the OPC tag did not have a current value.

At least that's my theory. 🙂

---

<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:** [October 22, 2024, 11:48am UTC](https://forum.inductiveautomation.com/t/tag-change-script-initialchange-behaviour/33976/17 "2024-10-22T11:48:56Z")

</div>

Ah, I see. The project's Gateway Tag Change event has different timing from a tag's valueChange event.

---

<div class="post-metadata">

**Author:** ![rbachman](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/rbachman/32/43718_2.png) [@rbachman](https://forum.inductiveautomation.com/u/rbachman)\
**Post date:** [October 22, 2024, 1:47pm UTC](https://forum.inductiveautomation.com/t/tag-change-script-initialchange-behaviour/33976/18 "2024-10-22T13:47:26Z")

</div>

I think I've been able to confirm my hypothesis, although I don't know exactly why it works this way. On a fresh gateway, I set up two different Gateway Tag Change Event Scripts. For one event, I used a memory tag for the trigger, and the other event used a PLC/OPC tag. After a restart of the gateway, the event for the PLC/OPC tag executes as soon as the initial read of the tag takes place, but the event with the memory tag for the trigger does **not** execute until the tag actually changes.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/8/a85737c61aa1dc8444a60c711850fdb044a8050f.png)

---

<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 22, 2024, 2:15pm UTC](https://forum.inductiveautomation.com/t/tag-change-script-initialchange-behaviour/33976/19 "2024-10-22T14:15:19Z")

</div>

> [@rbachman](#):
>
> I don't know exactly why it works this way

I'm only guessing, but I could see how an initial change for a memory tag would only occur at creation. As by design the value is persistent in the Internal DB, so since they know that the last value is "correct" then there is no need to set initial change flag, however, with OPC, that value would be "uncertain".

Again, just a guess.
