# Gateway scheduled Script issue

**URL:** <https://forum.inductiveautomation.com/t/gateway-scheduled-script-issue/54239>\
**Category:** Ignition\
**Created:** [December 17, 2021, 11:10am UTC](https://forum.inductiveautomation.com/t/gateway-scheduled-script-issue/54239 "2021-12-17T11:10:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![prasath.t](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@prasath.t](https://forum.inductiveautomation.com/u/prasath.t)\
**Post date:** [December 17, 2021, 11:10am UTC](https://forum.inductiveautomation.com/t/gateway-scheduled-script-issue/54239/1 "2021-12-17T11:10:34Z")

</div>

Hi i have return script in gateway scheduled script

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/5/258cc64f0ddf160fb82673b5b62bcbeb66c5d1ff.png)  
i am getting error in line 3

```auto
com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File "", line 3, in onScheduledEvent UnboundLocalError: local variable 'system' referenced before assignment

at org.python.core.Py.UnboundLocalError(Py.java:295)

at org.python.core.PyFrame.getlocal(PyFrame.java:240)

at org.python.pycode._pyx60209.onScheduledEvent$1(:16)

at org.python.pycode._pyx60209.call_function()

at org.python.core.PyTableCode.call(PyTableCode.java:171)

at org.python.core.PyBaseCode.call(PyBaseCode.java:308)

at org.python.core.PyFunction.function ___call__ (PyFunction.java:471)

at org.python.core.PyFunction. __call__ (PyFunction.java:466)

at org.python.core.PyFunction. __call__ (PyFunction.java:461)

at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:842)

at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:824)

at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runFunction(ProjectScriptLifecycle.java:681)

at com.inductiveautomation.ignition.common.script.ScriptManager$ScriptFunctionImpl.invoke(ScriptManager.java:993)

at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$AutoRecompilingScriptFunction.invoke(ProjectScriptLifecycle.java:746)

at com.inductiveautomation.ignition.common.script.ScheduledScriptManager$ScheduledScriptTask.run(ScheduledScriptManager.java:148)

at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)

at java.base/java.util.concurrent.FutureTask.run(Unknown Source)

at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)

at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.base/java.lang.Thread.run(Unknown Source)

Caused by: org.python.core.PyException: Traceback (most recent call last): File "", line 3, in onScheduledEvent UnboundLocalError: local variable 'system' referenced before assignment

... 21 common frames omitted

```

how to declare line 3 in scheduled script? Please help me out @paul-griffith

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [December 17, 2021, 11:14am UTC](https://forum.inductiveautomation.com/t/gateway-scheduled-script-issue/54239/2 "2021-12-17T11:14:24Z")

</div>

> [@prasath.t](#):
>
> `local variable 'system' referenced before assignment`

Put your imports starting at line 3, so your if statement line is line 6 instead.

Then put the rest of your code below the indented if.

---

<div class="post-metadata">

**Author:** ![prasath.t](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@prasath.t](https://forum.inductiveautomation.com/u/prasath.t)\
**Post date:** [December 17, 2021, 11:23am UTC](https://forum.inductiveautomation.com/t/gateway-scheduled-script-issue/54239/3 "2021-12-17T11:23:50Z")

</div>

but same script if i put in gateway timer its not throwing error

but its throwing error in gateway scheduled why any reason for that?

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [December 17, 2021, 11:30am UTC](https://forum.inductiveautomation.com/t/gateway-scheduled-script-issue/54239/4 "2021-12-17T11:30:10Z")

</div>

Different areas of the gateway have different scopes. Some require imports, some don’t.

---

<div class="post-metadata">

**Author:** ![prasath.t](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@prasath.t](https://forum.inductiveautomation.com/u/prasath.t)\
**Post date:** [December 17, 2021, 12:03pm UTC](https://forum.inductiveautomation.com/t/gateway-scheduled-script-issue/54239/5 "2021-12-17T12:03:06Z")

</div>

same error is coming when import system in line 3

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/0/605e1a38442457da08985a2f3af4ca51f792db43.png)

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [December 17, 2021, 12:25pm UTC](https://forum.inductiveautomation.com/t/gateway-scheduled-script-issue/54239/6 "2021-12-17T12:25:58Z")

</div>

```auto
def foo():
   import system
   x = system.tag.read (your tag path)
   xQV = x.value
   if str(xQV) == "a string to compare":
       print "hope this works"

```

Also, that looks like a V8+ GUI, if so tag.read is deprecated and you should be using .readblocking, but .read should still work

---

<div class="post-metadata">

**Author:** ![prasath.t](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@prasath.t](https://forum.inductiveautomation.com/u/prasath.t)\
**Post date:** [December 17, 2021, 12:30pm UTC](https://forum.inductiveautomation.com/t/gateway-scheduled-script-issue/54239/7 "2021-12-17T12:30:18Z")

</div>

import system that line itself i am getting error

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [December 17, 2021, 1:22pm UTC](https://forum.inductiveautomation.com/t/gateway-scheduled-script-issue/54239/8 "2021-12-17T13:22:58Z")

</div>

Do you have custom scripting modules?

What happens if you open the Script console in designer and run `import system` - do you get an error?

---

<div class="post-metadata">

**Author:** ![prasath.t](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@prasath.t](https://forum.inductiveautomation.com/u/prasath.t)\
**Post date:** [December 17, 2021, 1:37pm UTC](https://forum.inductiveautomation.com/t/gateway-scheduled-script-issue/54239/9 "2021-12-17T13:37:38Z")

</div>

Its working fine in script console and gateway timer and tag change script also… But its throwing error in only in gateway scheduled script

---

<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 17, 2021, 1:46pm UTC](https://forum.inductiveautomation.com/t/gateway-scheduled-script-issue/54239/10 "2021-12-17T13:46:04Z")

</div>

Do you actually need `import system` in that script? It is a new-style script instead of an event-style, so I wouldn’t expect it to have this legacy scope problem.

---

<div class="post-metadata">

**Author:** ![prasath.t](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@prasath.t](https://forum.inductiveautomation.com/u/prasath.t)\
**Post date:** [December 17, 2021, 1:55pm UTC](https://forum.inductiveautomation.com/t/gateway-scheduled-script-issue/54239/11 "2021-12-17T13:55:01Z")

</div>

Not need… if i use system function in script… Its throwing error…

For example system.tag.read - this itself i am getting error

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [December 17, 2021, 2:29pm UTC](https://forum.inductiveautomation.com/t/gateway-scheduled-script-issue/54239/12 "2021-12-17T14:29:18Z")

</div>

OK so try remove the import system as Phil suggests, and change to readBlocking which is the V8 version

---

<div class="post-metadata">

**Author:** ![RLmason](https://avatars.discourse-cdn.com/v4/letter/r/b19c9b/32.png) [@RLmason](https://forum.inductiveautomation.com/u/RLmason)\
**Post date:** [December 20, 2021, 3:19pm UTC](https://forum.inductiveautomation.com/t/gateway-scheduled-script-issue/54239/13 "2021-12-20T15:19:31Z")

</div>

This problem seems to be related to the Scheduled event. My script doesn’t run using the Scheduled event, but it runs without error using the Timer event. Wrapper.log shows the Scheduled event error is _UnboundLocalError: local variable ‘system’ referenced before assignment_. The first line is:

`import json`

and the error occurs at the next line:

`timeNow = system.date.now()`

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [December 20, 2021, 4:05pm UTC](https://forum.inductiveautomation.com/t/gateway-scheduled-script-issue/54239/14 "2021-12-20T16:05:28Z")

</div>

Welcome @RLmason!

I haven’t personally used a scheduled script yet, and the manual doesn’t state the scope:

[https://docs.inductiveautomation.com/display/DOC81/Gateway+Event+Scripts](https://docs.inductiveautomation.com/display/DOC81/Gateway+Event+Scripts)

I will suggest this until someone from IA confirms scope/legacy (@Paul.Scott):

```auto
import json, system
	
	
timeNow = system.date.now()
	
logger = system.util.getLogger("testScheduled")
logger.info("Hello, world: " + str(timeNow))

```

 ![scheduled](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/4/749505e9fa0540ac2a616120f68efcb68ed86c0d.png)

Tested and working on 8.1.12

---

<div class="post-metadata">

**Author:** ![kcollins](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kcollins/32/19646_2.png) [@kcollins](https://forum.inductiveautomation.com/u/kcollins)\
**Post date:** [December 20, 2021, 4:51pm UTC](https://forum.inductiveautomation.com/t/gateway-scheduled-script-issue/54239/15 "2021-12-20T16:51:01Z")

</div>

You shouldn’t need to `import system` in order to use the system functions within Scheduled Scripts. I’ve been unable to reproduce a situation where it errors, even if you do include it (both on 8.1.7 and 8.1.12).

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [December 20, 2021, 4:57pm UTC](https://forum.inductiveautomation.com/t/gateway-scheduled-script-issue/54239/16 "2021-12-20T16:57:34Z")

</div>

I’m using `system` in scheduled scripts without importing it first and it works fine.  
8.1.10.

---

<div class="post-metadata">

**Author:** ![kcollins](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kcollins/32/19646_2.png) [@kcollins](https://forum.inductiveautomation.com/u/kcollins)\
**Post date:** [December 20, 2021, 4:58pm UTC](https://forum.inductiveautomation.com/t/gateway-scheduled-script-issue/54239/17 "2021-12-20T16:58:18Z")

</div>

It does look like if you try to place an `import system` within an `if` block like the original post it does indeed produce that error… Just remove that line altogether, you shouldn’t need it. 👍

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [December 20, 2021, 5:03pm UTC](https://forum.inductiveautomation.com/t/gateway-scheduled-script-issue/54239/18 "2021-12-20T17:03:36Z")

</div>

Yep, changed “import json, system” to “import json” and the logger fired at 17:00 on 8.1.12

---

<div class="post-metadata">

**Author:** ![prasath.t](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@prasath.t](https://forum.inductiveautomation.com/u/prasath.t)\
**Post date:** [December 21, 2021, 7:03am UTC](https://forum.inductiveautomation.com/t/gateway-scheduled-script-issue/54239/19 "2021-12-21T07:03:34Z")

</div>

Import JSON its working…

what is difference between Import JSON and Import system?

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [December 21, 2021, 7:06am UTC](https://forum.inductiveautomation.com/t/gateway-scheduled-script-issue/54239/20 "2021-12-21T07:06:39Z")

</div>

JSON is new (to Ignition) in V8+

It is not just used in Ignition SCADA:

> **[JSON](https://en.wikipedia.org/wiki/JSON)**
>
> JSON (JavaScript Object Notation, pronounced /ˈdʒeɪsən/; also /ˈdʒeɪˌsɒn/) is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and arrays (or other serializable values). It is a common data format with diverse uses in electronic data interchange, including that of web applications with servers.
> JSON is a language-independent data format. It was derived from JavaScript, but many modern...

Anything that starts with system.\* e.g. system.db.runPrepQuery had legacy scope intricacies in versions prior to 8, depending on where the code was running.

[Next page](https://forum.inductiveautomation.com/t/gateway-scheduled-script-issue/54239.md?page=2)
