How to trigger script written in project library

I have created a simple gateway event script for testing.

def test:
      a =1
      b=2
      c = a+b
      print c

test()

And i have named this script as Test
After saving the script, i have waited for this to execute automatically but didnt work.

if i run the same script in script console, it is working… I believe there is nothing wrong with this test script.

I just wanna run this Test script automatically, Anyone pls tell me how to do that?

When you create a gateway event script you have to choose what event is to trigger it. Your options are:

  • Startup
  • Update
  • Shutdown
  • Timer
  • Tag Change
  • Message
  • Scheduled

Which did you choose and what was the trigger?

okay,
I have created the Test script under the Scripting section,

Could you pls tell me to run that script?

That’s a library script, not an event script. Library scripts don’t run unless an event calls a function within them. One of the events @Transistor listed.

Edit: Also note that when you have an event that calls Test.test(), the first time will yield two calls to test(), since you include a call at the top level of the library script. The top level runs once, to completion, the first time any member of it is accessed. (Like an auto-import.)

Another thing to note here is that print 'hi' in the gateway context - you will never see this.

You need to use a logger to log your messages to the gateway.

def test():
    logger = system.util.getLogger("My Logger")
    a=1
    b=2
    c =  a + b
    logger.info("Value of c is %i"%(c))

is how you would be able to see results in your gateway logs.

You need to be more precise in your writing. Your question title says “event script”. I now suspect that you mean you have written it in the Project Library.

You can run a Project Library script from a button.

  • Right-click the button.
  • Select Configure Events ….
  • Select onActionPerformed, hit + and select Script.
  • Add the path and name of script.
1 Like

Thanks,
I was doing it in multiple ways, forgot to rename it.