# How to save Gateway Event scrips (mainly, Tag Change) without resetting tags?

**URL:** <https://forum.inductiveautomation.com/t/how-to-save-gateway-event-scrips-mainly-tag-change-without-resetting-tags/98139>\
**Category:** Ignition\
**Tags:** perspective, scripting\
**Created:** [November 4, 2024, 4:57pm UTC](https://forum.inductiveautomation.com/t/how-to-save-gateway-event-scrips-mainly-tag-change-without-resetting-tags/98139 "2024-11-04T16:57:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_Karee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matthew_karee/32/63956_2.png) [@Matthew\_Karee](https://forum.inductiveautomation.com/u/Matthew_Karee)\
**Post date:** [November 4, 2024, 4:57pm UTC](https://forum.inductiveautomation.com/t/how-to-save-gateway-event-scrips-mainly-tag-change-without-resetting-tags/98139/1 "2024-11-04T16:57:11Z")

</div>

Whenever I make a change to a Tag Change script and click Save, all of my integer and float memory tags are reset to 0, which makes it difficult to debug one of my active dashboards.

Is there a way to prevent this from happening?

---

<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 4, 2024, 5:08pm UTC](https://forum.inductiveautomation.com/t/how-to-save-gateway-event-scrips-mainly-tag-change-without-resetting-tags/98139/2 "2024-11-04T17:08:40Z")

</div>

> [@Matthew\_Karee](#):
>
> all of my integer and float memory tags are reset to 0

That means you are writing zeros from your tag event scripts. Are you checking the `initialChange` flag?

---

<div class="post-metadata">

**Author:** ![Matthew\_Karee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matthew_karee/32/63956_2.png) [@Matthew\_Karee](https://forum.inductiveautomation.com/u/Matthew_Karee)\
**Post date:** [November 4, 2024, 5:18pm UTC](https://forum.inductiveautomation.com/t/how-to-save-gateway-event-scrips-mainly-tag-change-without-resetting-tags/98139/3 "2024-11-04T17:18:10Z")

</div>

I am not.

Would this work?

```python
if not initialChange:
 
    #my script

```

I am confused why the script is writing zeros at all, though (here is an example of one):

```python
#my script=
driverLoadedQuantity = system.tag.readBlocking('[edge]Line1/dl_quantity')[0].value
driverLoadedQuantity += 1
system.tag.writeBlocking('[edge]Line1/dl_quantity', driverLoadedQuantity)

```

---

<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 4, 2024, 5:20pm UTC](https://forum.inductiveautomation.com/t/how-to-save-gateway-event-scrips-mainly-tag-change-without-resetting-tags/98139/4 "2024-11-04T17:20:52Z")

</div>

Doesn't look like that script would do it. Are there other places in your scripts where you write to these tags?

(A project save causes the script environment to reload, which will re-"import" project library scripts as they are accessed.)

---

<div class="post-metadata">

**Author:** ![Matthew\_Karee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matthew_karee/32/63956_2.png) [@Matthew\_Karee](https://forum.inductiveautomation.com/u/Matthew_Karee)\
**Post date:** [November 4, 2024, 5:29pm UTC](https://forum.inductiveautomation.com/t/how-to-save-gateway-event-scrips-mainly-tag-change-without-resetting-tags/98139/5 "2024-11-04T17:29:59Z")

</div>

Oops, yes there are definitely scripts that write 0 to the tags that are getting "reset".

Here's one (Tag Paths and Script):

Tag Path(s):

```python
[edge]Line1/barcode_value

```

Script:

```python
barcodeValue = system.tag.readBlocking('[edge]Line1/barcode_value')[0].value
lotNumber = barcodeValue[0:12]
subassemblyNumber = barcodeValue[1:7]
lotQuantityString = barcodeValue[12:]
lotQuantity = float(lotQuantityString)

system.tag.writeBlocking('[edge]Line1/lot_number', lotNumber)
system.tag.writeBlocking('[edge]Line1/lot_quantity', lotQuantity)
system.tag.writeBlocking('[edge]Line1/completed_quantity', 0)
system.tag.writeBlocking('[edge]Line1/scrap_quantity', 0)
system.tag.writeBlocking('[edge]Line1/dl_quantity', 0)
system.tag.writeBlocking('[edge]Line1/subassembly', subassemblyNumber)

```

The idea is that I _do_ want those tags to be reset every time a new barcode is scanned (and I do only have the "Value" box checked for Change Triggers). But it appears that the above script is also running when I click Save.

Would this fix my issue?

```python
if not initialChange:
     barcodeValue = system.tag.readBlocking('[edge]Line1/barcode_value')[0].value
     lotNumber = barcodeValue[0:12]
     subassemblyNumber = barcodeValue[1:7]
     lotQuantityString = barcodeValue[12:]
     lotQuantity = float(lotQuantityString)

     system.tag.writeBlocking('[edge]Line1/lot_number', lotNumber)
     system.tag.writeBlocking('[edge]Line1/lot_quantity', lotQuantity)
     system.tag.writeBlocking('[edge]Line1/completed_quantity', 0)
     system.tag.writeBlocking('[edge]Line1/scrap_quantity', 0)
     system.tag.writeBlocking('[edge]Line1/dl_quantity', 0)
     system.tag.writeBlocking('[edge]Line1/subassembly', subassemblyNumber)

```

---

<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 4, 2024, 5:33pm UTC](https://forum.inductiveautomation.com/t/how-to-save-gateway-event-scrips-mainly-tag-change-without-resetting-tags/98139/6 "2024-11-04T17:33:24Z")

</div>

That would explain it, and the solution is a likely fix.

Strictly speaking, you should **handle** the `initialChange` situation. It means, from Ignition's point of view: "I'm restarting, and I don't know if this is a change, and I do not know the previous value if it is."

Consider doing your own change detection with a memory tag.

---

<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:** [November 4, 2024, 5:49pm UTC](https://forum.inductiveautomation.com/t/how-to-save-gateway-event-scrips-mainly-tag-change-without-resetting-tags/98139/7 "2024-11-04T17:49:12Z")

</div>

A couple of things, assuming this is running in a Tag Value Change script:

1. Since this script is on the `Line1/barcode_value` tag, there is no need to read the tag to get the value, it is already sent to the event in the `currentValue`

2. You should not be calling the `writeBlocking()` function multiple times. This is doubly important in Tag Value Change scripts where you want the script to execute as quickly as possible. I would even recommend that you not call the blocking version of the function at all in this case as, you aren't doing anything after the calls that you would need to wait for (unless you haven't shown the entire script.

I would expect that script to look something like this:

```python
if not initialChange:
    barcodeValue = currentValue.value
    lotNumber = barcodeValue[:12]
    subassemblyNumber = barcodeValue[1:7]
    lotQuantity = float(barcodeValue[12:])

    tags = ('lot_number','lot_quantity','completed_quantity','scrap_quantity','dl_quantity','subassembly')
    tagPaths = ['[edge]Line1/{}'.format(tag) for tag in tags]
    tagValues = [lotNumber,lotQuantity,0,0,0,subassemblyNumber]
    system.tag.writeAsync(tagPaths,tagValues)

```

Normally, I would recommend not using a Tag Value Change event for this but rather a Gateway Tag Change Event. If you are using that then, `currentValue` becomes `newValue`, but the script otherwise would remain the same.
