# Delay in Tag Event Script (Value Changed)

**URL:** <https://forum.inductiveautomation.com/t/delay-in-tag-event-script-value-changed/70666>\
**Category:** Ignition\
**Tags:** ignition81, scripting\
**Created:** [March 6, 2023, 1:19pm UTC](https://forum.inductiveautomation.com/t/delay-in-tag-event-script-value-changed/70666 "2023-03-06T13:19:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ali\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@Ali\_Ali](https://forum.inductiveautomation.com/u/Ali_Ali)\
**Post date:** [March 6, 2023, 1:19pm UTC](https://forum.inductiveautomation.com/t/delay-in-tag-event-script-value-changed/70666/1 "2023-03-06T13:19:47Z")

</div>

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:

```auto
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:

```auto
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	import threading
	from threading import Timer
	speedValue = system.tag.read("[.]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()
	else:
		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.

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [March 6, 2023, 1:43pm UTC](https://forum.inductiveautomation.com/t/delay-in-tag-event-script-value-changed/70666/2 "2023-03-06T13:43:28Z")

</div>

Instead of using timer, put your delayed line in a function and use [invokelater](https://docs.inductiveautomation.com/display/DOC81/system.util.invokeLater)

---

<div class="post-metadata">

**Author:** ![Ali\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@Ali\_Ali](https://forum.inductiveautomation.com/u/Ali_Ali)\
**Post date:** [March 6, 2023, 2:32pm UTC](https://forum.inductiveautomation.com/t/delay-in-tag-event-script-value-changed/70666/3 "2023-03-06T14:32:23Z")

</div>

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")`

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [March 6, 2023, 2:48pm UTC](https://forum.inductiveautomation.com/t/delay-in-tag-event-script-value-changed/70666/4 "2023-03-06T14:48:36Z")

</div>

> [@Ali\_Ali](#):
>
> InvokeLater scope is only Vision Client,

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

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 6, 2023, 3:31pm UTC](https://forum.inductiveautomation.com/t/delay-in-tag-event-script-value-changed/70666/5 "2023-03-06T15:31:00Z")

</div>

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

> [@Ignition 8 invokeLater function doesn't seem to exist](https://forum.inductiveautomation.com/t/ignition-8-invokelater-function-doesnt-seem-to-exist/25174/4):
>
> I’ve updated [later.py](https://www.automation-pros.com/ignition/later.py) for Ignition v8. It includes an implementation of invokeLater usable in gateway scope.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 6, 2023, 4:33pm UTC](https://forum.inductiveautomation.com/t/delay-in-tag-event-script-value-changed/70666/6 "2023-03-06T16:33:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ali\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@Ali\_Ali](https://forum.inductiveautomation.com/u/Ali_Ali)\
**Post date:** [March 6, 2023, 8:15pm UTC](https://forum.inductiveautomation.com/t/delay-in-tag-event-script-value-changed/70666/7 "2023-03-06T20:15:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ali\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@Ali\_Ali](https://forum.inductiveautomation.com/u/Ali_Ali)\
**Post date:** [March 6, 2023, 8:22pm UTC](https://forum.inductiveautomation.com/t/delay-in-tag-event-script-value-changed/70666/8 "2023-03-06T20:22:25Z")

</div>

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.
