# Gateway Tag Change Script not finishing

**URL:** <https://forum.inductiveautomation.com/t/gateway-tag-change-script-not-finishing/37467>\
**Category:** Ignition\
**Tags:** ignition79, scripting\
**Created:** [August 6, 2020, 4:51pm UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-not-finishing/37467 "2020-08-06T16:51:35Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpmccormick](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wpmccormick/32/13940_2.png) [@wpmccormick](https://forum.inductiveautomation.com/u/wpmccormick)\
**Post date:** [August 6, 2020, 4:51pm UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-not-finishing/37467/1 "2020-08-06T16:51:35Z")

</div>

I have a Gateway Tag Change script that seems not to finish. I know it starts, but it seems to fail when calling a project script in a the Script Library that does some recursive browseTags. My intent is have this script called on gateway startup, and I know it works because I’ve run the code in the Script Console and it works.

I’m really stumped … here’s some cut-n-paste:

–Tag Change–

```auto
def AddTags(path, udttype, data):
	#get all the tags that have the given UDT type under the given path
	tagList = project.alarmlist.manualSearch(path, udttype)

	# strip of the last part of the tag path and make that the alarm name
	# the first part then becomes the path
	for tag in tagList:
		lastDel = tag.path.rfind("/")
		strLen = len(tag.path)
		alarmName = tag.path[lastDel+1:strLen]
		path = tag.path[0:lastDel]
		row = [path,alarmName,'default','color(255,255,255,255)','color(0,0,0,255)','','','','default','color(250,214,138,255)','color(0,0,0,255)','','',udttype]
		
		data.append(row)

logger = system.util.getLogger("alarmlist")

if newValue.value:
	data = [] # create an empty list to fill with tag paths
	
	logger.info("Start Alarm Tag Refresh")
	system.tag.writeSynchronous('GUI/Test','Starting')
	
	AddTags("[default]/.","DigitalAlarm",data)
	logger.info("DigitalAlarm Tag Refresh Complete")

	AddTags("[default]/.","AnalogAlarm",data)
	logger.info("AnalogAlarm Tag Refresh Complete")

	AddTags("[default]/.","AnalogLockoutAlarm",data)
	logger.info("AnalogLockoutAlarm Tag Refresh Complete")
	
	almDataSet = system.tag.read('GUI/Alarm Tags')
	headers = system.dataset.getColumnHeaders(almDataSet.value)
	dataset = system.dataset.toDataSet(headers, data)	
	system.tag.writeSynchronous('GUI/Alarm Tags',dataset)
	logger.info("Tag Refresh Complete")

	system.tag.writeSynchronous('GUI/Alarm Tag Refresh',0)
	system.tag.writeSynchronous('GUI/Test','Complete')
else:
	system.tag.writeSynchronous('GUI/Test','Nothing Done')

```

– Script Library –

```auto
# Define the search as a function. This makes recursion easier.
def manualSearch(initPath, alarmType):
    # Create a result set of just tags.
    tagSet = system.tag.browseTags(parentPath = initPath, tagPath = '*', udtParentType=alarmType, recursive=False)
     
    # Create a result set of UDT instances.
    udtSet = system.tag.browseTags(parentPath = initPath, tagPath = '*', tagType='UDT_INST', recursive=False)
	 
    # Create a result set of just folders. We'll iterate over this set and call browseTags() again for the results.                                             
    folderSet = system.tag.browseTags(parentPath = initPath, tagPath = '*', tagType = 'Folder', recursive=False)

    # Iterate through folders...                                  
    for folder in folderSet:    
        # Recursively start the process over again until we run out of folders.
        tagSet+=manualSearch(folder.fullPath,alarmType)
    
    # Iterate through UDT's ...                                  
    for udt in udtSet: 
		# Recursively start the process over again until we run out of folders.
		tagSet+=manualSearch(udt.fullPath,alarmType)
    
    # Return the list of tags.
    return tagSet

```

---

<div class="post-metadata">

**Author:** ![code\_skin](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@code\_skin](https://forum.inductiveautomation.com/u/code_skin)\
**Post date:** [August 6, 2020, 5:00pm UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-not-finishing/37467/2 "2020-08-06T17:00:31Z")

</div>

> [@wpmccormick](#):
>
> tagSet = system.tag.browseTags(parentPath = **initPath** , tagPath = '\*', udtParentType=alarmType, recursive=False)

Does **initPath** contain the tag provider as well?

Because this project script is being called from a gateway scope event the system.tag.browseTags() needs a tag provider. To me this explains why it was working from the script console but not at the gateway level.

---

<div class="post-metadata">

**Author:** ![wpmccormick](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wpmccormick/32/13940_2.png) [@wpmccormick](https://forum.inductiveautomation.com/u/wpmccormick)\
**Post date:** [August 6, 2020, 5:12pm UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-not-finishing/37467/3 "2020-08-06T17:12:16Z")

</div>

Yes, as you can see in the calls to AddTags, they all include a tag path that includes the provider ([default]/.):

> [@wpmccormick](#):
>
> AddTags("[default]/.","DigitalAlarm",data)

---

<div class="post-metadata">

**Author:** ![WillMT10](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@WillMT10](https://forum.inductiveautomation.com/u/WillMT10)\
**Post date:** [August 6, 2020, 5:32pm UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-not-finishing/37467/4 "2020-08-06T17:32:58Z")

</div>

If I remember correctly, you cannot use “default” for the tag provider in any Gateway scripting. “default” is defined for each project, not for the gateway. You need to replace this with the name of the tag provider you want this script to check.

---

<div class="post-metadata">

**Author:** ![wpmccormick](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wpmccormick/32/13940_2.png) [@wpmccormick](https://forum.inductiveautomation.com/u/wpmccormick)\
**Post date:** [August 6, 2020, 6:00pm UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-not-finishing/37467/6 "2020-08-06T18:00:53Z")

</div>

I don’t know … the Tag Browser under _All Providers_ doesn’t have a provider for the my gateway other than _default_.

---

<div class="post-metadata">

**Author:** ![code\_skin](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@code\_skin](https://forum.inductiveautomation.com/u/code_skin)\
**Post date:** [August 6, 2020, 6:36pm UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-not-finishing/37467/7 "2020-08-06T18:36:27Z")

</div>

Any errors in the gateway logs when you run the scripts? Another thing to consider is the number of tags you have in your tag provider. Could it be the scripts hasn’t finished?

---

<div class="post-metadata">

**Author:** ![wpmccormick](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wpmccormick/32/13940_2.png) [@wpmccormick](https://forum.inductiveautomation.com/u/wpmccormick)\
**Post date:** [August 6, 2020, 6:52pm UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-not-finishing/37467/8 "2020-08-06T18:52:07Z")

</div>

I missed this before …

com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File “”, line 24, in File “”, line 3, in AddTags NameError: global name ‘project’ is not defined

Do I need an import project statement I wonder?

---

<div class="post-metadata">

**Author:** ![code\_skin](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@code\_skin](https://forum.inductiveautomation.com/u/code_skin)\
**Post date:** [August 6, 2020, 6:53pm UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-not-finishing/37467/9 "2020-08-06T18:53:17Z")

</div>

Yes, when calling project scripts from gateway scope events I always import project to avoid these types of errors.

---

<div class="post-metadata">

**Author:** ![WillMT10](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@WillMT10](https://forum.inductiveautomation.com/u/WillMT10)\
**Post date:** [August 6, 2020, 7:06pm UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-not-finishing/37467/10 "2020-08-06T19:06:49Z")

</div>

Whoops, sorry about that. It looks like I didn’t remember correctly! I was mixing up “[~]” (which means the default provider for the project) with “[default]”…

---

<div class="post-metadata">

**Author:** ![wpmccormick](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wpmccormick/32/13940_2.png) [@wpmccormick](https://forum.inductiveautomation.com/u/wpmccormick)\
**Post date:** [August 6, 2020, 7:14pm UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-not-finishing/37467/11 "2020-08-06T19:14:59Z")

</div>

And more specifically, the import only works if placed inside my AddTags function.

---

<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:** [August 6, 2020, 7:19pm UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-not-finishing/37467/12 "2020-08-06T19:19:04Z")

</div>

That is a normal side-effect of the legacy python scoping rules that apply to gateway event scripts. Consider putting all of your gateway event code in functions in project script modules and have all of the events be just a one-line call to the corresponding function. You’ll have modern scope rules in the script module.

---

<div class="post-metadata">

**Author:** ![wpmccormick](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wpmccormick/32/13940_2.png) [@wpmccormick](https://forum.inductiveautomation.com/u/wpmccormick)\
**Post date:** [August 6, 2020, 9:26pm UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-not-finishing/37467/13 "2020-08-06T21:26:01Z")

</div>

Useful.

Now I can’t seem to make the Gateway Startup script system.tag.write the tag to trigger the Gateway Tag Change script.

It’s just one thing after another.

---

<div class="post-metadata">

**Author:** ![wpmccormick](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wpmccormick/32/13940_2.png) [@wpmccormick](https://forum.inductiveautomation.com/u/wpmccormick)\
**Post date:** [August 6, 2020, 9:41pm UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-not-finishing/37467/14 "2020-08-06T21:41:23Z")

</div>

okay, so like you said @pturmel … bury everything in the project script and then just call it with a one-liner.

That’s gives me what I want both on setting the refresh tag and on gw startup.

Thanks!
