Gateway scheduled Script issue

Hi i have return script in gateway scheduled script


i am getting error in line 3

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 @PGriffith

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.

1 Like

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?

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

1 Like

same error is coming when import system in line 3

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

import system that line itself i am getting error

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?

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

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.

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

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

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

1 Like

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()

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

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

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

Tested and working on 8.1.12

1 Like

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).

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

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. :+1:

1 Like

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

1 Like

Import JSON its working…

what is difference between Import JSON and Import system?

JSON is new (to Ignition) in V8+

It is not just used in Ignition SCADA:

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.

1 Like