Time tracking using Gateway Timer Script

The reason for that is complicated and hard to explain - basically, legacy code baggage. The easiest fix (and a good practice overall) is to move the function you're defining into the project library. That is, literally take the whole block of code you have starting from def and put it into a new project library script. Name it whatever you want, and then call it from your timer script using yourScriptLibraryName.updateVFD1().

That aside, you've got some syntax issues/misunderstandings that I'll correct here, but I'll again recommend a basic Python course.

run_state1 = float
memory_state1 = float

Python doesn't require you to initialize variables before you use them, with or without type information. You can just declare a variable with a value, and the type is implicit. Drop these lines completely.

def updateVFD1(run_state1, memory_state1):

You only need to write run_state1, memory_state1 (or anything else) at the top of your function if it's a parameter your function expects.
That is, look at these different function definitions:

def addTwo(a):
	return a + 2

def multiplyAndAddSomething(a, b):
	something = 13
	return (a * b) + something

def doSomething():
	print "I did something!"

You don't need to (and shouldn't) define a method as accepting parameters if it doesn't actually need them. In your case, you're overwriting any possible input parameters anyways, so it's completely pointless.

print statements, when code is executed on the gateway, go straight to the wrapper.log file, which can be annoying to get to. Prefer using system.util.getLogger when writing gateway scripts, so you can see the output directly in the gateway webpage.
More info here: Event Configuration Print Out to debug - #5 by PGriffith

There's no need to reassign the value of a local variable. This won't do anything to the tags - it's just having your CPU spin for a few cycles rewriting memory with no effect.

As mentioned above - there's no need to pass parameters in here - just invoke the function directly (updateVFD1()). Also, your function isn't returning anything, so there's no need to print the output - that's what the None value you're seeing in the script console is.

I would put the following into the project library, for now:

def updateVFD1():
	log = system.util.getLogger("VFD Debug")
	run_state1 = system.tag.readBlocking("[default]CHW/VFD/New Instance/VFD1")[0].value
	memory_state1 = system.tag.readBlocking("[default]CHW/VFD/Runtime/CHP-1")[0].value

	log.info("Original value: " + run_state1)
	if run_state1 > memory_state1:
		system.tag.writeBlocking("[default]CHW/VFD/Runtime/CHP-1", memory_state1)
		log.info("Updated memory state value: " + memory_state1)
	else: 
		log.info("Not updated")

And then in your timer script, just write:

whateverNameYouGaveYourProjectScriptLibrary.updateVFD1()

If you weren't aware of it, Ctrl + Space brings up the scripting autocomplete, and if you're on a recent enough version this will also bring up project library scripts.

2 Likes