# tag.writeSynchronous not reflected immediately?

**URL:** <https://forum.inductiveautomation.com/t/tag-writesynchronous-not-reflected-immediately/16392>\
**Category:** Ignition\
**Created:** [December 21, 2017, 4:02pm UTC](https://forum.inductiveautomation.com/t/tag-writesynchronous-not-reflected-immediately/16392 "2017-12-21T16:02:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lyndon.schroeder](https://avatars.discourse-cdn.com/v4/letter/l/7c8e57/32.png) [@lyndon.schroeder](https://forum.inductiveautomation.com/u/lyndon.schroeder)\
**Post date:** [December 21, 2017, 4:02pm UTC](https://forum.inductiveautomation.com/t/tag-writesynchronous-not-reflected-immediately/16392/1 "2017-12-21T16:02:17Z")

</div>

I have a gateway tag change script, I wanted to insure only executed once, it sends an email and I don’t want to spam the recipients if we have a network error. So I wrote it like this…

trigger = system.tag.read(“DoTask”)  
if trigger.value == 1:

```
#tell the plc we are good
system.tag.writeSynchronous("DoTask", 0)

#check that everything worked
trigger = system.tag.read("DoTask")
if trigger.value == 0:
	#do the task

```

I figured this way I was guaranteeing to only send the message once and I could use the writeSynchonous since I was executing in a gateway script. The code executes, however it seems the writeSynchonous is not really synchronous. In the next read, my trigger.value still returns true.

Am I misunderstanding how this should work?

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [December 21, 2017, 4:08pm UTC](https://forum.inductiveautomation.com/t/tag-writesynchronous-not-reflected-immediately/16392/2 "2017-12-21T16:08:26Z")

</div>

writeSynchronous can't be used in the foreground. take a look at this thread:

> [@Tag.read after tag.write](http://forum.inductiveautomation.com/t/tag-read-after-tag-write/11218):
>
> Hi, I have a question about system.tag.read and system.tag.write instructions: if I execute a read just after a write on the same tag path, e.g. system.tag.write(tagPath,someValue) newValue=system.tag.read(tagPath).value shall the newValue be the value just written to tagPath by the write instruction? Or is the result not deterministic: it could be the old value, i.e. the value stored in tagPath before the write instruction, as well as the new one, i.e. the one written to the tagPath with the…

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [December 22, 2017, 8:38am UTC](https://forum.inductiveautomation.com/t/tag-writesynchronous-not-reflected-immediately/16392/3 "2017-12-22T08:38:19Z")

</div>

I believe the writeSynchronous should work like you want, but it doesn’t block other writes to that tag while your round trip is busy.

How often, or on what triggers is other code writing to that tag?

Does it always fail, or only intermittent? And what happens if you sleep for a second before reading the tag again?

---

<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:** [December 22, 2017, 1:24pm UTC](https://forum.inductiveautomation.com/t/tag-writesynchronous-not-reflected-immediately/16392/4 "2017-12-22T13:24:03Z")

</div>

It is almost always unwise to depend on any synchronization of a piece of data that exists in two places (PLC and Ignition) and can be written from either direction at any time. Consider using two separate integer tags, one that the PLC increments to trigger your code, and another where you write the last triggered value. The latter tag would be a memory tag if the PLC’s trigger is “fire and forget”, or another PLC address if the PLC needs an acknowledgement. Only the PLC writes to the first tag, only Ignition writes to the second tag.

---

<div class="post-metadata">

**Author:** ![lyndon.schroeder](https://avatars.discourse-cdn.com/v4/letter/l/7c8e57/32.png) [@lyndon.schroeder](https://forum.inductiveautomation.com/u/lyndon.schroeder)\
**Post date:** [December 22, 2017, 9:32pm UTC](https://forum.inductiveautomation.com/t/tag-writesynchronous-not-reflected-immediately/16392/5 "2017-12-22T21:32:39Z")

</div>

Thanks Jordan,

You were right. I assumed that since it was on the gateway it was already a background task. Clearly that was a wrong assumption. It works as I expected now.
