# Reading and Writing tags under Gateway Events script

**URL:** <https://forum.inductiveautomation.com/t/reading-and-writing-tags-under-gateway-events-script/74863>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [June 9, 2023, 9:17pm UTC](https://forum.inductiveautomation.com/t/reading-and-writing-tags-under-gateway-events-script/74863 "2023-06-09T21:17:30Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_Bench](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@Jason\_Bench](https://forum.inductiveautomation.com/u/Jason_Bench)\
**Post date:** [June 9, 2023, 9:17pm UTC](https://forum.inductiveautomation.com/t/reading-and-writing-tags-under-gateway-events-script/74863/1 "2023-06-09T21:17:30Z")

</div>

I am trying to setup a scheduled script under Gateway Events. Using v8.1.28

I am setting it to trigger every day at 7am. The preview under settings is giving me "At 7:00 am", which looks correct. The Script is:

def onScheduledEvent():  
holding = system.tag.readBlocking("[default]Plant PLC/Distribution/Flow/Flow Total")  
system.tag.writeBlocking("[default]Plant PLC/Distribution/Flow/Flow Total Holding", holding)

_last two lines are indented_

The Flow Total tag has an integer in it (35) and the Flow Total Holding tag is a memory integer tag.

the time comes and goes but I still have a null value in the Flow Total Holding tag. It should have written the value from Flow Total to Flow Total Holding. Am I missing something?

---

<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:** [June 9, 2023, 11:57pm UTC](https://forum.inductiveautomation.com/t/reading-and-writing-tags-under-gateway-events-script/74863/2 "2023-06-09T23:57:28Z")

</div>

Run those 2 lines in the Designer script console and see what happens.

---

<div class="post-metadata">

**Author:** ![carlosbqz](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carlosbqz/32/52324_2.png) [@carlosbqz](https://forum.inductiveautomation.com/u/carlosbqz)\
**Post date:** [June 10, 2023, 12:31am UTC](https://forum.inductiveautomation.com/t/reading-and-writing-tags-under-gateway-events-script/74863/3 "2023-06-10T00:31:40Z")

</div>

As Kevin already said, try to run the script on script console and it will throw you the error ([readBlocking and writeBlocking needs a list of tags/values](https://docs.inductiveautomation.com/display/DOC81/system.tag.readBlocking)).

Also you can set a logger in your gateway event and check those errors in the gateway logs

```auto
logger = system.util.getLogger("myevent-debug")
logger.info("Initializing")
try:
     myscript()
except Exception as e:
     logger.error(e)

```

Edit: Sorry, I didn't add the quotation marks on the name of the logger 😅

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [June 10, 2023, 12:50am UTC](https://forum.inductiveautomation.com/t/reading-and-writing-tags-under-gateway-events-script/74863/4 "2023-06-10T00:50:47Z")

</div>

```auto
def onScheduledEvent():
` holding = system.tag.readBlocking("[default]Plant PLC/Distribution/Flow/Flow Total")
    system.tag.writeBlocking("[default]Plant PLC/Distribution/Flow/Flow Total Holding", holding)

```

should probably be,

```auto
def onScheduledEvent():
    holding = system.tag.readBlocking(["[default]Plant PLC/Distribution/Flow/Flow Total"])
    system.tag.writeBlocking(["[default]Plant PLC/Distribution/Flow/Flow Total Holding"], [holding])

```

Note the sets of `[]` enclosing the tag(s) and arguement(s).

* * *

> [@Jason\_Bench](#):
>
> last two lines are indented

Tip: select your text and press the \</\> button to format your code as code. It will preserve indentation and apply syntax highlighting. Use the pencil icon on your post to edit and fix it. Use the preview pane to check your post before submitting it.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [June 10, 2023, 1:21am UTC](https://forum.inductiveautomation.com/t/reading-and-writing-tags-under-gateway-events-script/74863/5 "2023-06-10T01:21:51Z")

</div>

Well, should probably be:

```python
def onScheduledEvent():
    holding = system.tag.readBlocking(["[default]Plant PLC/Distribution/Flow/Flow Total"])[0].value
    system.tag.writeBlocking(["[default]Plant PLC/Distribution/Flow/Flow Total Holding"], [holding])

```

But yes.

---

<div class="post-metadata">

**Author:** ![Jason\_Bench](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@Jason\_Bench](https://forum.inductiveautomation.com/u/Jason_Bench)\
**Post date:** [June 10, 2023, 1:03pm UTC](https://forum.inductiveautomation.com/t/reading-and-writing-tags-under-gateway-events-script/74863/6 "2023-06-10T13:03:03Z")

</div>

This code did not work, but what was supplied by Transistor worked perfectly, thanks guys! Didn't know about the script console, that will make testing much faster. Thanks for the quick responses!

---

<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:** [June 10, 2023, 1:27pm UTC](https://forum.inductiveautomation.com/t/reading-and-writing-tags-under-gateway-events-script/74863/7 "2023-06-10T13:27:19Z")

</div>

> [@Jason\_Bench](#):
>
> Didn't know about the script console

Well, be aware that the script console is not in gateway scope, and many `system.*` functions behave differently than in gateway scope or in Perspective. Each function's documentation explains the differences. (Designer scope is a variation of Vision Client scope.)

---

<div class="post-metadata">

**Author:** ![Jason\_Bench](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@Jason\_Bench](https://forum.inductiveautomation.com/u/Jason_Bench)\
**Post date:** [June 12, 2023, 2:06pm UTC](https://forum.inductiveautomation.com/t/reading-and-writing-tags-under-gateway-events-script/74863/8 "2023-06-12T14:06:12Z")

</div>

> [@Transistor](#):
>
> ```auto
> def onScheduledEvent():
> holding = system.tag.readBlocking(["[default]Plant PLC/Distribution/Flow/Flow Total"])
> system.tag.writeBlocking(["[default]Plant PLC/Distribution/Flow/Flow Total Holding"], [holding])
> 
> ```

Checking again this morning and my script does not work. I am trying the logger as Carlos suggested, but it gives me an error of: name 'myevent-debug' is not defined. Here is my full code I'm putting in console:

```auto
logger = system.util.getLogger(myevent-debug)
logger.info("Initializing")
try:
	def onScheduledEvent():
		holding = system.tag.readBlocking(["[default]Plant PLC/Distribution/Flow/Flow Total"])
		system.tag.writeBlocking(["[default]Plant PLC/Distribution/Flow/Flow Total Holding"], [holding])
except Exception as e:
	logger.error(e)	

```

error

```auto
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'myevent' is not defined

```

When a change is made to the Gateway Events scripting, does the application need to be saved everytime? I have tried doing it and not. Wondering what else could have changed..?

---

<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:** [June 12, 2023, 2:09pm UTC](https://forum.inductiveautomation.com/t/reading-and-writing-tags-under-gateway-events-script/74863/9 "2023-06-12T14:09:01Z")

</div>

> [@Jason\_Bench](#):
>
> `logger = system.util.getLogger(myevent-debug)`

the `getLogger()` function takes a string parameter.

```auto
logger = system.util.getLogger("myevent-debug")

```

---

<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:** [June 12, 2023, 2:32pm UTC](https://forum.inductiveautomation.com/t/reading-and-writing-tags-under-gateway-events-script/74863/10 "2023-06-12T14:32:22Z")

</div>

Also, you are _defining_ `onScheduledEvent()` but not calling it. Just `def` does not cause execution.

Finally, you cannot expect scripts run in the designer's script console to behave the way the script will in gateway scope (the actual event). The designer runs a variation of Vision Client scope and cannot be made to run as if gateway scope.

---

<div class="post-metadata">

**Author:** ![Jason\_Bench](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@Jason\_Bench](https://forum.inductiveautomation.com/u/Jason_Bench)\
**Post date:** [June 12, 2023, 4:52pm UTC](https://forum.inductiveautomation.com/t/reading-and-writing-tags-under-gateway-events-script/74863/12 "2023-06-12T16:52:39Z")

</div>

Thanks for the help guys. Wanted to share what I ended up doing. This helps me to keep up with the long tag arrays much easier. Another thing, once you make changes to the Gateway Event Scripts, you have to save the project. That was a big part of my problem. You don't have to specify ".value" for OPC tags but you do for expressions, so I do it on all.

```auto
def onScheduledEvent():
	r1 = "[default]Plant PLC/Distribution/Flow/Flow Total Today"
	r2 = "[default]Plant PLC/Influent/Flow/Flow Total Today"
	r3 = "[default]Plant PLC/Chemical/Caustic/Flow/Flow Total Today"
	r4 = "[default]Plant PLC/chemical/HMO/Flow/Flow Total Today"
	pathsRead = [r1, r2, r3, r4]
	w1 = "[default]Plant PLC/Distribution/Flow/Flow Total Yesterday"
	w2 = "[default]Plant PLC/Influent/Flow/Flow Total Yesterday"
	w3 = "[default]Plant PLC/Chemical/Caustic/Flow/Flow Total Yesterday"
	w4 = "[default]Plant PLC/chemical/HMO/Flow/Flow Total Yesterday"
	pathsWrite = [w1, w2, w3, w4]
	valuesRead = system.tag.readBlocking(pathsRead)
	valuesWrite = [valuesRead[0].value, valuesRead[1].value, valuesRead[2].value, valuesRead[3].value]
	system.tag.writeBlocking(pathsWrite, valuesWrite)

```

---

<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:** [June 12, 2023, 5:07pm UTC](https://forum.inductiveautomation.com/t/reading-and-writing-tags-under-gateway-events-script/74863/13 "2023-06-12T17:07:19Z")

</div>

> [@Jason\_Bench](#):
>
> You don't have to specify ".value" for OPC tags but you do for expressions

I'm not sure what you mean by this. It doesn't matter if you read the value from an OPC tag or an Expression tag, `readBlocking()` returns a list of qualified values and to get just the value of an item you always need to use `.value`

I would definitely write this write this differently though.

```auto
def onScheduledEvent():
	tagPaths = [ 
				"[default]Plant PLC/Distribution/Flow/Flow Total Today",
				"[default]Plant PLC/Influent/Flow/Flow Total Today",
				"[default]Plant PLC/Chemical/Caustic/Flow/Flow Total Today",
				"[default]Plant PLC/chemical/HMO/Flow/Flow Total Today"
			]
	system.tag.writeBlocking([path.replace('Today','Yesterday') for path in tagPaths], [total.value for total in system.tag.readBlocking(tagPaths)])

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [June 12, 2023, 5:14pm UTC](https://forum.inductiveautomation.com/t/reading-and-writing-tags-under-gateway-events-script/74863/14 "2023-06-12T17:14:56Z")

</div>

To be abundantly clear:

In a _tagpath_, whether read from an expression (`{path/to/tag}`) or a script (`system.tag.read*("path/to/tag")`), you do not need to append `.value` _to the tag path_. It is _assumed_ that if you are targeting any discrete (not a folder or UDT instance) tag, you want to read the `.value` property unless you specify some other property (`path/to/tag.engUnits`, for instance).

In _all cases_, reading a tag returns a [qualified value object](https://docs.inductiveautomation.com/display/DOC81/Scripting+Object+Reference#ScriptingObjectReference-QualifiedValue).  
In expressions, qualified values are automatically "unwrapped" - the quality is used in the internal evaluation of expression functions, but the actual user code you write will always deal with 'bare' values.

In scripting, we can't make assumptions for you, so reading a tag will always return a (list of) qualified value(s). The modern, idiomatic `system.tag.readBlocking` function in particular will _unconditionally_ return a list of qualified values that _must_ be unpacked to be used.
