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

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?

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

I am not.

Would this work?

if not initialChange:
 
    #my script

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

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

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.)

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):

[edge]Line1/barcode_value

Script:

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?

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)

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.

1 Like

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:

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.

1 Like