# Add a timer before a tag get changed

**URL:** <https://forum.inductiveautomation.com/t/add-a-timer-before-a-tag-get-changed/95194>\
**Category:** Ignition\
**Created:** [August 27, 2024, 9:50am UTC](https://forum.inductiveautomation.com/t/add-a-timer-before-a-tag-get-changed/95194 "2024-08-27T09:50:10Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Odd-Simon\_Simonsen](https://avatars.discourse-cdn.com/v4/letter/o/a88e4f/32.png) [@Odd-Simon\_Simonsen](https://forum.inductiveautomation.com/u/Odd-Simon_Simonsen)\
**Post date:** [August 27, 2024, 9:50am UTC](https://forum.inductiveautomation.com/t/add-a-timer-before-a-tag-get-changed/95194/1 "2024-08-27T09:50:10Z")

</div>

Hello,

Is it possible to add an timer on the scripting menu on a tag?

I have an numeric label for the process value "PV", and an numering label for an setpoint "SP".

When the PV goes over the setpoint SP i have it automaticly pressing a One Shot Button trough scripting.  
But I dont want it to be pressed right away, I want it to wait a few seconds before pressing the One Shot Button.

Is there possible to add a timer in the scripting menu of the PV?

Thanks

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [August 27, 2024, 11:40am UTC](https://forum.inductiveautomation.com/t/add-a-timer-before-a-tag-get-changed/95194/2 "2024-08-27T11:40:38Z")

</div>

Sounds like something that should be implemented in the plc..

Or, look at this

> [@Best way to trigger a script after a time delay without pausing a script](https://forum.inductiveautomation.com/t/best-way-to-trigger-a-script-after-a-time-delay-without-pausing-a-script/89638/15):
>
> Welcome to the world of non-deterministic operating systems. slight_smile I was able to get a bit better performance by not running it through the alarm subsystem. [UDT\_Timer\_millis.json](https://forum.inductiveautomation.com/uploads/short-url/nWcXZBz8Ponvt3lJJBLQjnw6M3h.json) (2.1 KB)

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [August 27, 2024, 12:31pm UTC](https://forum.inductiveautomation.com/t/add-a-timer-before-a-tag-get-changed/95194/3 "2024-08-27T12:31:46Z")

</div>

> [@Odd-Simon\_Simonsen](#):
>
> Is there possible to add a timer in the scripting menu of the PV?

Place your script in a function and use `system.util.invokeLater()` with the desired delay.

> **[system.util.invokeLater | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-util/system-util-invokeLater)**
>
> Invokes (calls) the given Python function object after all of the currently processing and pending events are done being processed, or after a specified delay.

---

<div class="post-metadata">

**Author:** ![Odd-Simon\_Simonsen](https://avatars.discourse-cdn.com/v4/letter/o/a88e4f/32.png) [@Odd-Simon\_Simonsen](https://forum.inductiveautomation.com/u/Odd-Simon_Simonsen)\
**Post date:** [August 27, 2024, 12:44pm UTC](https://forum.inductiveautomation.com/t/add-a-timer-before-a-tag-get-changed/95194/5 "2024-08-27T12:44:32Z")

</div>

> [@lrose](#):
>
> Place your script in a function and use `system.util.invokeLater()` with the desired delay.

I tried this, but if the PV changes back down under the SP value it still pushes down the button after the delay

---

<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:** [August 27, 2024, 12:46pm UTC](https://forum.inductiveautomation.com/t/add-a-timer-before-a-tag-get-changed/95194/6 "2024-08-27T12:46:55Z")

</div>

You will not find a **robust** solution that runs in the user interface. You need repeating **gateway** logic that examines your conditions, sets timestamps, and acts upon past timestamps.

Just like a PLC would.

---

<div class="post-metadata">

**Author:** ![Odd-Simon\_Simonsen](https://avatars.discourse-cdn.com/v4/letter/o/a88e4f/32.png) [@Odd-Simon\_Simonsen](https://forum.inductiveautomation.com/u/Odd-Simon_Simonsen)\
**Post date:** [August 27, 2024, 12:49pm UTC](https://forum.inductiveautomation.com/t/add-a-timer-before-a-tag-get-changed/95194/7 "2024-08-27T12:49:42Z")

</div>

Is it possible to script the value of the button to 1 after the alarmEvent.state.isActive goes true? then I can use the alarm active delay to push the button iguess?

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [August 27, 2024, 1:00pm UTC](https://forum.inductiveautomation.com/t/add-a-timer-before-a-tag-get-changed/95194/8 "2024-08-27T13:00:01Z")

</div>

> [@Odd-Simon\_Simonsen](#):
>
> I tried this, but if the PV changes back down under the SP value it still pushes down the button after the delay

Your function needs to re-examine the conditions then.

As @pturmel said using the UI to do this isn’t going to be robust. You can get close but there will be unhandled cases.

---

<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:** [August 27, 2024, 1:18pm UTC](https://forum.inductiveautomation.com/t/add-a-timer-before-a-tag-get-changed/95194/9 "2024-08-27T13:18:59Z")

</div>

> [@lrose](#):
>
> be unhandled cases.

Like, what do you want to happen when no client is open? Or not open to the window with that button? Or with a modal notification up that stops your scripts?

Or when you have more than one client open?

Really, this is not a task for your user interface.

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [August 27, 2024, 8:23pm UTC](https://forum.inductiveautomation.com/t/add-a-timer-before-a-tag-get-changed/95194/10 "2024-08-27T20:23:00Z")

</div>

This is absoluetly something that should be done in the PLC.  
Here's my 2¢:

You'll want a UDT to organize everything.

1. OPC tag for the SP `Process_SP`
2. OPC tag for the PV `Process_PV`
3. Memory tag for the button-press delay `Auto_Adj_PRE`
4. Expression tag to detect when the PV exceeds the SP `Auto_Adj_Req` [`{Process_PV} > {Process_SP}`]
5. Memory tag used as intermediary before actually executing the update button `Auto_Adj_INT`
6. OPC tag for the button `Update_SP_PB`

2 different `valueChange` scripts should get the job done here.

1. `Auto_Adj_INT.valueChaged()`

```python
def valueChanged():
	if currentValue != previousValue and not initialChange:
		if currentValue.value == 2:
			system.tag.writeBlocking(['Update_SP_PB'], ['1']) #psuedo-path

```

1. `Auto_Adj_Req.valueChanged()`

```python
def valueChanged():
	if currentValue != previousValue and not initialChange:
		
		#PV > SP
		if currentValue.value:
			#get the delay
			delay = system.tag.readBlocking(['Auto_Adj_INT'])[0].value #psuedo-path
			system.util.invokeLater(maybeUpdate, delay)
			
		#PV < SP
		else:
			#reset INT to 0 - cancels the PB
			system.tag.writeBlocking(['Auto_Adj_INT'], [0]) #psuedo-path
			
		def maybeUpdate():
			#read current INT
			autoAdjINT = system.tag.readBlocking(['Auto_Adj_INT'])[0].value #psuedo-path
			#INT is still 1, update to 2 - triggering the INT's valueChanged script
			if autoAdjINT == 1:
				system.tag.writeBlocking(['Auto_Adj_INT'], [2]) #psuedo-path

```

Again, way easier in the PLC. Also, I am unsure if your oneshot button exists in the GUI or in the PLC, or how it gets reset

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [August 27, 2024, 8:24pm UTC](https://forum.inductiveautomation.com/t/add-a-timer-before-a-tag-get-changed/95194/11 "2024-08-27T20:24:45Z")

</div>

@Odd-Simon_Simonsen did not specify if this is a one-shot button on the GUI or in the PLC. I assumed that it was a button in the PLC.

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [August 27, 2024, 8:27pm UTC](https://forum.inductiveautomation.com/t/add-a-timer-before-a-tag-get-changed/95194/12 "2024-08-27T20:27:25Z")

</div>

What exactly are you trying to accomplish? I think if you described what the end goal was, it would help everyone understand what you're doing. What is the function of the one-shot button? Is it tied to a PLC tag? Are you running a script to do something else?

While ideally this is something done in the PLC, if it's doing something in Ignition or a DB, or something else, sharing that information would help everyone else out.

---

<div class="post-metadata">

**Author:** ![Odd-Simon\_Simonsen](https://avatars.discourse-cdn.com/v4/letter/o/a88e4f/32.png) [@Odd-Simon\_Simonsen](https://forum.inductiveautomation.com/u/Odd-Simon_Simonsen)\
**Post date:** [August 28, 2024, 5:41am UTC](https://forum.inductiveautomation.com/t/add-a-timer-before-a-tag-get-changed/95194/13 "2024-08-28T05:41:21Z")

</div>

Thanks everyone by your input. And sorry if what I tried to explain/ask for was very unclear.

I ended up setting up an alarm for the process value PV rising over the setpoint SP, this with the . And with this i added the Active delay property on the alarm.  
I then went on the scripting on the same tag, with the Alarm Events \> Alarm Active. Wrote what thag to be changed to 1/TRUE when the alarm is active:  
system.tag.writeBlocking(TagPath, 1)

This ended up working and did what I tried to accomplish without changing the PLC code (due to old PLC and dont want to edit code thats havent been touched in years).
