# Tag write slower from gateway tag event than from Vision trigger

**URL:** <https://forum.inductiveautomation.com/t/tag-write-slower-from-gateway-tag-event-than-from-vision-trigger/77082>\
**Category:** Ignition\
**Tags:** vision\
**Created:** [July 28, 2023, 5:43pm UTC](https://forum.inductiveautomation.com/t/tag-write-slower-from-gateway-tag-event-than-from-vision-trigger/77082 "2023-07-28T17:43:48Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![danFromNj](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@danFromNj](https://forum.inductiveautomation.com/u/danFromNj)\
**Post date:** [July 28, 2023, 5:43pm UTC](https://forum.inductiveautomation.com/t/tag-write-slower-from-gateway-tag-event-than-from-vision-trigger/77082/1 "2023-07-28T17:43:48Z")

</div>

I have a script that updates about 1500 PLC tags and then runs some internal scripts after the update is complete. The main script uses a system.tag.writeAsync call to update all the tags at once and then call a secondary script that triggers the internal scripts. The secondary scripts use 3 system.tag.writeBlocking calls to write 3 values to the PLC. All of this is triggered from a Vision window and has been working great for 6 months.

Recently, I had to make a change that would allow this same script to be run from a gateway event tag change script. The function will work but the tag writes are much SLOWER. When triggered from the Vision window, the 1500 tag writes take less than 1s. When triggered from the gateway tag event, the same tag writes will sometimes go quickly but may also timeout at 45s. Other times the 1500 tags will write quickly and then the subsequent writeBlocking calls will be slow.

Why is this? Is there a better way to handle this? I am hesitant to post any of my code because of the sensitive nature of the project I am working on. If required, I can post a modified version.

Thanks in advance,  
Dan

---

<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:** [July 28, 2023, 5:49pm UTC](https://forum.inductiveautomation.com/t/tag-write-slower-from-gateway-tag-event-than-from-vision-trigger/77082/2 "2023-07-28T17:49:03Z")

</div>

> [@danFromNj](#):
>
> I am hesitant to post any of my code

You may want to have IA support look at this--they can work under a suitable NDA.

But first, check if you are batching your writes with lists with `system.tag.writeBlocking()`.

---

<div class="post-metadata">

**Author:** ![danFromNj](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@danFromNj](https://forum.inductiveautomation.com/u/danFromNj)\
**Post date:** [July 28, 2023, 6:14pm UTC](https://forum.inductiveautomation.com/t/tag-write-slower-from-gateway-tag-event-than-from-vision-trigger/77082/3 "2023-07-28T18:14:17Z")

</div>

Thanks for the quick reply. The large write occurs with a single call and a list of around 1500 tags. There are then three additional writes that each write to a single tag. When this is called from a Vision window, the entire thing works smoothly. When called from a gateway tag change event, it is consistently slower to the point of running into the default 45s timeout. When I watch the tags, sometimes the bottleneck is in the large write and sometimes it makes it through that write quickly and gets hung up on a writeBlocking call that is sent to a single tag.

---

<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:** [July 28, 2023, 6:15pm UTC](https://forum.inductiveautomation.com/t/tag-write-slower-from-gateway-tag-event-than-from-vision-trigger/77082/4 "2023-07-28T18:15:11Z")

</div>

What version of Ignition is this?

---

<div class="post-metadata">

**Author:** ![danFromNj](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@danFromNj](https://forum.inductiveautomation.com/u/danFromNj)\
**Post date:** [July 28, 2023, 6:15pm UTC](https://forum.inductiveautomation.com/t/tag-write-slower-from-gateway-tag-event-than-from-vision-trigger/77082/5 "2023-07-28T18:15:54Z")

</div>

8.1.25

---

<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:** [July 28, 2023, 6:17pm UTC](https://forum.inductiveautomation.com/t/tag-write-slower-from-gateway-tag-event-than-from-vision-trigger/77082/6 "2023-07-28T18:17:17Z")

</div>

Set loggers `Tags.Opc.Writes` and `tags.execution.batchoperations` to DEBUG level and next time it's slow grab the logs and upload them.

---

<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:** [July 28, 2023, 6:18pm UTC](https://forum.inductiveautomation.com/t/tag-write-slower-from-gateway-tag-event-than-from-vision-trigger/77082/7 "2023-07-28T18:18:51Z")

</div>

Also - what 45 second timeout are you talking about? What call times out from that?

edit: must be on the subsequent `writeBlocking` calls.

---

<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:** [July 28, 2023, 6:24pm UTC](https://forum.inductiveautomation.com/t/tag-write-slower-from-gateway-tag-event-than-from-vision-trigger/77082/8 "2023-07-28T18:24:58Z")

</div>

The loggers I mentioned will confirm this, but I'm pretty sure I can explain what's happening.

Writes to OPC tags batched on a per-server basis. Only one batch per server is allowed to be executing at any given time.

When you execute all of this from Vision, for whatever reasons, I'm guessing these writes all end up in the same batch.

When you execute from a tag change script, I'm guessing you end up with one batch executing the 1500 writes, and a second batch for the 3 tags waiting on that first batch. Writing to 1500 tags is likely much slower than you realize because you've been hiding it using `writeAsync`.

We're planning some changes to the batching to allow some amount of parallelism, though I can't say for sure it will help in this case or not. In the meantime you could switch to `system.opc.write*` for either the 1500 tags, or the subsequent 3 tags, or both.

---

<div class="post-metadata">

**Author:** ![danFromNj](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@danFromNj](https://forum.inductiveautomation.com/u/danFromNj)\
**Post date:** [July 28, 2023, 6:31pm UTC](https://forum.inductiveautomation.com/t/tag-write-slower-from-gateway-tag-event-than-from-vision-trigger/77082/9 "2023-07-28T18:31:43Z")

</div>

Thanks. I will see what the effect is of switching to system.opc.write.

---

<div class="post-metadata">

**Author:** ![danFromNj](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@danFromNj](https://forum.inductiveautomation.com/u/danFromNj)\
**Post date:** [July 28, 2023, 6:53pm UTC](https://forum.inductiveautomation.com/t/tag-write-slower-from-gateway-tag-event-than-from-vision-trigger/77082/10 "2023-07-28T18:53:39Z")

</div>

I moved the scripts to use system.opc.writeValues and it seems to be working.

Though, I'm having a hard time accepting your explanation.

I was using system.tag.writeAsync. I thought that this function issued the tag writes and then when that function is complete, it will issue a call to the callback function. I was originally using an invokelater call to execute the secondary scripts after the large tag write. This invokelater was waiting 5s for the writes to complete and be read back into the tag provider. However, invokelater does not work from the gateway so I put in an expression tag that sees when the update occurs and sets a expression tag to True 5s later. The secondary scripts now run from a tag change script.

EDIT: I guess what I am having a hard time understanding is this. Doesn't system.tag.writeAsync complete its write before calling the callback function? Shouldn't that block of tag writes be complete by the time it is getting to the 3 tag writes in the callback function?

---

<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:** [July 28, 2023, 6:54pm UTC](https://forum.inductiveautomation.com/t/tag-write-slower-from-gateway-tag-event-than-from-vision-trigger/77082/11 "2023-07-28T18:54:32Z")

</div>

You should have provided more information.

The loggers I mentioned will still be useful in determining what is happening, should you revert to the original setup that doesn't use `system.opc.write`.

---

<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:** [July 28, 2023, 6:56pm UTC](https://forum.inductiveautomation.com/t/tag-write-slower-from-gateway-tag-event-than-from-vision-trigger/77082/12 "2023-07-28T18:56:53Z")

</div>

> [@danFromNj](#):
>
> Doesn't system.tag.writeAsync complete its write before calling the callback function? Shouldn't that block of tag writes be complete by the time it is getting to the 3 tag writes in the callback function?

Yes, but you didn't mention using the callback at any point...

---

<div class="post-metadata">

**Author:** ![danFromNj](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@danFromNj](https://forum.inductiveautomation.com/u/danFromNj)\
**Post date:** [July 28, 2023, 6:57pm UTC](https://forum.inductiveautomation.com/t/tag-write-slower-from-gateway-tag-event-than-from-vision-trigger/77082/13 "2023-07-28T18:57:36Z")

</div>

I thought system.tag.writeAsync HAS to have a callback function.

---

<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:** [July 28, 2023, 6:58pm UTC](https://forum.inductiveautomation.com/t/tag-write-slower-from-gateway-tag-event-than-from-vision-trigger/77082/14 "2023-07-28T18:58:30Z")

</div>

Nope, optional, and often ignored by people... unfortunately.

---

<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:** [July 28, 2023, 7:01pm UTC](https://forum.inductiveautomation.com/t/tag-write-slower-from-gateway-tag-event-than-from-vision-trigger/77082/15 "2023-07-28T19:01:38Z")

</div>

So FWIW, when you write this logic in the gateway inside the callback from `writeAsync`, there's simply no need for `invokeLater`. I'm not sure there was when it was in the client, either, unless you were also manipulating the UI in some manner.

---

<div class="post-metadata">

**Author:** ![danFromNj](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@danFromNj](https://forum.inductiveautomation.com/u/danFromNj)\
**Post date:** [July 28, 2023, 7:02pm UTC](https://forum.inductiveautomation.com/t/tag-write-slower-from-gateway-tag-event-than-from-vision-trigger/77082/16 "2023-07-28T19:02:13Z")

</div>

Below is a summary of the code...

get tag values to write  
write a 1 into status tag  
call system.tag.writeAsync with callback function

callback function  
write a 2 into status tag  
write a 1 into plc update bit (this causes plc to move parameters into internal PLC tags)  
write a 3 into status tag

expression tags  
tag 1: True if status tag = 3 and has been for more than 5s  
on value change. if value==1, call secondary function

When called from Vision or Script Console, this works great. When called from the gateway, sometimes I would see the status tag get stuck at 1 for 45s, sometimes 2. Sometimes, it would work smoothly but usually not

---

<div class="post-metadata">

**Author:** ![danFromNj](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@danFromNj](https://forum.inductiveautomation.com/u/danFromNj)\
**Post date:** [July 28, 2023, 7:04pm UTC](https://forum.inductiveautomation.com/t/tag-write-slower-from-gateway-tag-event-than-from-vision-trigger/77082/17 "2023-07-28T19:04:35Z")

</div>

The invokelater was because the secondary scripts were updating internal tags based on plc values. However, the parameter update would cause the plc value changes and the OPC server needed time to both write the parameter values and read the updated values. In testing, I found 5 seconds was more than enough time for this to happen. I used invokeLater instead of pause because pause would lock up the entire UI

---

<div class="post-metadata">

**Author:** ![danFromNj](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@danFromNj](https://forum.inductiveautomation.com/u/danFromNj)\
**Post date:** [July 28, 2023, 7:07pm UTC](https://forum.inductiveautomation.com/t/tag-write-slower-from-gateway-tag-event-than-from-vision-trigger/77082/18 "2023-07-28T19:07:14Z")

</div>

I have to step out for a while to take my son to a baseball lesson. I really appreciate how quickly your responses have come and how informed they are. I will try to pick this thread back up (maybe Monday) to try to get to the bottom of the issue

---

<div class="post-metadata">

**Author:** ![danFromNj](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@danFromNj](https://forum.inductiveautomation.com/u/danFromNj)\
**Post date:** [July 31, 2023, 11:53am UTC](https://forum.inductiveautomation.com/t/tag-write-slower-from-gateway-tag-event-than-from-vision-trigger/77082/19 "2023-07-31T11:53:52Z")

</div>

I wanted to bump this thread. It seems like it was left unanswered after the realization that I was using a callback function.

The reason that the callback function was using an invokeLater is that the tag writes were writing to one set of PLC tags. After this large (1500 tags) write is successful, an update in the PLC is toggled. This triggers a series of logic that moves the parameter values into another set of PLC tags that control how the machine operates.

Based on the value of these internal PLC tags, Ignition will adjust its UI so that it displays correctly for the loaded machine type. Ignition has to wait for the OPC server to read these new tag values before it can run several scripts that are needed to adjust the UI. The invokeLater calls were timed so that these scripts would wait a few seconds for the OPC server to read the new tag values before calling these UI update scripts. Since invokeLater does not seem to work when called by a gateway tag change event, I created a internal expression tags that trigger after the parameters are loaded.

I have this whole thing working by switching away from system.tag.writeAsync and system.tag.writeBlocking. I am currently using system.opc.writeValues.

I would still like to know why the tag writes are timing out when coming triggered by a gateway tag change event. The writeBlocking that is called by the callback function should not be waiting on the initial tag write to finish.

---

<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:** [July 31, 2023, 12:36pm UTC](https://forum.inductiveautomation.com/t/tag-write-slower-from-gateway-tag-event-than-from-vision-trigger/77082/20 "2023-07-31T12:36:30Z")

</div>

Those loggers I mentioned would still be the starting point. Even if it’s not the large write you invoke right before there could still be some other writes going on causing them to queue. You may end up needing to work with support to accommodate the back and forth of logger changes and log files.

[Next page](https://forum.inductiveautomation.com/t/tag-write-slower-from-gateway-tag-event-than-from-vision-trigger/77082.md?page=2)
