Delay in Tag Event Script (Value Changed)

Hello, I have problem my scripts do not run as expected or don't run at all. I need to delay script execution in Tag Event Scripts > Value Changed. The purpose is just to simulate the PLC feedbacks for while connected to the built-in Programmable Device Simulator.
This code is working but no matter how much the delay in timer it is always run immediately:

def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	import threading
	from threading import Timer			
	Timer(10.0, system.tag.write("[.]Run_FB", currentValue.value)).start()

And this one is not running at all:

def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	import threading
	from threading import Timer
	speedValue ="[.]Speed_FB").value
	def speedRampUp():
		speedValue += 30
		system.tag.write("[.]Speed_FB", speedValue)
	def speedRampDown():
		speedValue -= 30
		system.tag.write("[.]Speed_FB", speedValue)
	if (currentValue.value == 1):
		while (speedValue <= 1500):
			Timer(0.1, speedRampUp).start()
		while (speedValue > 0):
			Timer(0.1, speedRampDown).start()

I know there may be better approaches to what I need to do, but I really need to know how to make this approach work. Also, I am restricted to use the built-in Programmable Device Simulator, no PLC simulator or external scripting.
Hope script experts can help. Thanks in advance.

Instead of using timer, put your delayed line in a function and use invokelater

I already tried it but also failed for two reasons, first, InvokeLater scope is only Vision Client, and second, the "[.]Run_FB" causes error if used inside a function I don't know why.
Error: LegacyScripting[system_util_tag] 06Mar2023 14:17:30 Error writing tags Error writing to tag '[.]Run_FB': Error_Configuration("Tag provider '.' not found")

1 Like

That is correct; my mistake. If this is just a temporary simulation, you can get away with using sleep in Perspective.

sleep() should always be avoided. Luckily, we have a helper script.

In general, any delays of any kind are not acceptable in tag value change scripts, or any other tag event, due to the shared execution thread pool. Don't do it. You will bog down your tag infrastructure.

Instead, design a state machine, perhaps with a timestamp in a memory tag, that is updated by a regular timer event. Do not use jython time, thread, or sleeping functionality in such events (or anywhere in Ignition, really).

Thank you for the advice and link. I already did my search and found the link, but unfortunately I'm a beginner in scripting and couldn't understand how to use the script in the link. Also, I think those methods are not meant to be used in tag value changed scripts. If you have any ideas, please share with use a sample script, I would be grateful.

Thank you for the advice. Sorry for being so ignorant, but I didn't get your solution about building a state machine and how to ramp up and down (simulate) values like pump speed, valve position and flow conditionally according to other tags in the UDT. Let's say I want the Pump speed to ramp up from 0 to 1500 once the run feedback back tag change from False to True. And vice versa when the feedback change to false. If you may share a small sample with us I would be grateful.