Python Timer on a tag event script

Hi there,

I am trying to execute a script on a tag on the valueChanged event. I know that you cannot use invokeLater() as this is a client scoped function. Instead, I am trying to use a Python Timer thread.

When I put this code in the script console, it executes as expected. However, when I put this code in a tag script, it does not fire at all but throws no errors either.

if currentValue.value == 1:
	from threading import Timer
	def Clear():
		system.tag.write("some/tag",0)
	Timer(4.0, Clear).start()

The odd thing is that when I put Clear() instead of just Clear, the function fires immediately without waiting for the time specified in Timer.

Any ideas as to this behavior?

Gateway scoped scripts don’t have a default tag provider, so tag writes need to be fully qualified:
system.tag.write("[default]some/tag")

There’s nothing odd about this… when you add the parenthesis you’re invoking the function rather than passing a reference to it.

1 Like

Thanks PGriffith, the fully qualified string worked correctly. Threw me off when it executed in the script console without needing that full qualification.

Hey Kevin, thanks for the response. I’m pretty new to Python. I understand that parenthesis invokes the function, but that occurs even inside of the Timer functions “Scope” (I’m not sure how to say this)… IE it ignores the parameters of Timer?

Timer(4.0, Clear).start()
Is creating an instance of the Timer class with two arguments: 4.0, and the Clear function object.
Timer(4.0, Clear()).start()
Is creating an instance of the Timer class with two arguments: 4.0, and the result of the Clear() function call.

Think of it as “unwrapping” each element - first Clear() is called, then the result is inserted into Timer(), then .start() is called.

2 Likes

Understood, thanks for the clear explanation!