# Timer ON and timer OFF

**URL:** <https://forum.inductiveautomation.com/t/timer-on-and-timer-off/55156>\
**Category:** Ignition\
**Tags:** ignition80, scripting\
**Created:** [January 19, 2022, 8:44am UTC](https://forum.inductiveautomation.com/t/timer-on-and-timer-off/55156 "2022-01-19T08:44:29Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![grzegorz.kaczmarczyk](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/grzegorz.kaczmarczyk/32/24167_2.png) [@grzegorz.kaczmarczyk](https://forum.inductiveautomation.com/u/grzegorz.kaczmarczyk)\
**Post date:** [January 19, 2022, 8:44am UTC](https://forum.inductiveautomation.com/t/timer-on-and-timer-off/55156/1 "2022-01-19T08:44:29Z")

</div>

Hi,

Is there a way to recreate timer ON and timer OFF like the ones in the PLC via scripting?

Let’s say we have a program:

timer1(tag1, 3000ms) ← Timer ON turns on, when tag1 is true for 3000ms  
IF timer1.Q THEN: ← timer 1 is true  
#code  
END;

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [January 19, 2022, 9:30am UTC](https://forum.inductiveautomation.com/t/timer-on-and-timer-off/55156/2 "2022-01-19T09:30:09Z")

</div>

See if [Reset Tag Value after a Number of Seconds - #6 by hamlinjr](https://forum.inductiveautomation.com/t/reset-tag-value-after-a-number-of-seconds/17098/6) helps. Pay attention to the warnings and cautions.

---

<div class="post-metadata">

**Author:** ![grzegorz.kaczmarczyk](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/grzegorz.kaczmarczyk/32/24167_2.png) [@grzegorz.kaczmarczyk](https://forum.inductiveautomation.com/u/grzegorz.kaczmarczyk)\
**Post date:** [January 19, 2022, 11:46am UTC](https://forum.inductiveautomation.com/t/timer-on-and-timer-off/55156/3 "2022-01-19T11:46:42Z")

</div>

Ok. So, now I want to create a function, that will work like a Timer ON from PLC, but I encounter a problem:

```auto
def TimerON(tagPath, time):
	
	if system.tag.read(tagPath).value == 0:
		date_ms = system.date.toMillis(system.date.now())
		return 0
	else:
		time_diff = system.date.toMillis(system.date.now()) - date_ms
		if time_diff > time:
			return 1
		
TimerON("[default]Testowe/test1",5000)

```

The scripting console shows an error, when tag value is 1:  
UnboundLocalError: local variable ‘date\_ms’ referenced before assignment

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [January 19, 2022, 11:54am UTC](https://forum.inductiveautomation.com/t/timer-on-and-timer-off/55156/4 "2022-01-19T11:54:30Z")

</div>

You’ve assigned `date_ms` in the `if` block (for no apparent reason, not using it) but are trying to use it in the `else` block.

---

<div class="post-metadata">

**Author:** ![grzegorz.kaczmarczyk](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/grzegorz.kaczmarczyk/32/24167_2.png) [@grzegorz.kaczmarczyk](https://forum.inductiveautomation.com/u/grzegorz.kaczmarczyk)\
**Post date:** [January 19, 2022, 11:58am UTC](https://forum.inductiveautomation.com/t/timer-on-and-timer-off/55156/5 "2022-01-19T11:58:02Z")

</div>

`date_ms` in `if` block is to remember the timestamp, when the variable was ‘0’ last time.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [January 19, 2022, 12:07pm UTC](https://forum.inductiveautomation.com/t/timer-on-and-timer-off/55156/6 "2022-01-19T12:07:12Z")

</div>

Doesn’t work like that. Your script has no “memory”.

---

<div class="post-metadata">

**Author:** ![grzegorz.kaczmarczyk](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/grzegorz.kaczmarczyk/32/24167_2.png) [@grzegorz.kaczmarczyk](https://forum.inductiveautomation.com/u/grzegorz.kaczmarczyk)\
**Post date:** [January 19, 2022, 12:21pm UTC](https://forum.inductiveautomation.com/t/timer-on-and-timer-off/55156/7 "2022-01-19T12:21:36Z")

</div>

So is it possible to create this kind of function via scripting?

---

<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:** [January 19, 2022, 12:33pm UTC](https://forum.inductiveautomation.com/t/timer-on-and-timer-off/55156/8 "2022-01-19T12:33:33Z")

</div>

It’s possible, you just need to find a place to store the start time that is scoped outside of your function.

For instance, perhaps create a UDT with the needed tags. Note that creating a “Timer” in this manner will be pretty inaccurate as there will be built in latencies with tag scan times, and read and right times. Can it be done? Yes. Should it? I’m not sure.

What exactly are you trying to accomplish, other than to replicate a construct from a PLC? There may be a better approach then this.

---

<div class="post-metadata">

**Author:** ![grzegorz.kaczmarczyk](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/grzegorz.kaczmarczyk/32/24167_2.png) [@grzegorz.kaczmarczyk](https://forum.inductiveautomation.com/u/grzegorz.kaczmarczyk)\
**Post date:** [January 19, 2022, 1:21pm UTC](https://forum.inductiveautomation.com/t/timer-on-and-timer-off/55156/9 "2022-01-19T13:21:53Z")

</div>

I want to trigger a IF condition in my script by a boolean value, that becomes TRUE, when tag is TRUE for 5 seconds.

---

<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:** [January 19, 2022, 1:54pm UTC](https://forum.inductiveautomation.com/t/timer-on-and-timer-off/55156/10 "2022-01-19T13:54:45Z")

</div>

Use a tag change event of some kind on your boolean to record the current timestamp into another tag (memory tag) whenever the boolean becomes true. In the script where you need this, read both the boolean tag and the timestamp tag. Then you have enough information to evaluate your condition.

---

<div class="post-metadata">

**Author:** ![abiesbiogas](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@abiesbiogas](https://forum.inductiveautomation.com/u/abiesbiogas)\
**Post date:** [January 25, 2022, 9:14am UTC](https://forum.inductiveautomation.com/t/timer-on-and-timer-off/55156/11 "2022-01-25T09:14:22Z")

</div>

Hello!  
sorry for the delay 😅, I had a similar need, check if my solution could fit yours:  
[udt\_TON.xml](https://forum.inductiveautomation.com/uploads/short-url/jEx20VNdY4ls0C48skL8aSfuRxy.xml) (2.1 KB)

use: create an instance of TON, say myTON  
set myTON.PT = 3.5 → it’s a float, PresetTime in seconds  
set myTON.EN = True to enable the timer  
Q is set to False at this moment and will be set to True after 3500ms

caveat: PT can’t be 0

see [Adding a Delay to a Script - Ignition User Manual 7.9 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC79/Adding+a+Delay+to+a+Script) for details on the use of Timer

🙂

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [January 25, 2022, 10:13am UTC](https://forum.inductiveautomation.com/t/timer-on-and-timer-off/55156/12 "2022-01-25T10:13:37Z")

</div>

That’s not what you want to do, that’s how you’re trying to do it.

Try to be as closed to the problem you want to solve, without including anything you think might be part of the solution. You might not even need a script to achieve your ‘true’ goal.

Unless of course your ultimate goal is to trigger an `if` in a script, in which case… why ?

---

<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:** [January 25, 2022, 2:23pm UTC](https://forum.inductiveautomation.com/t/timer-on-and-timer-off/55156/13 "2022-01-25T14:23:55Z")

</div>

UDT attached.

- Monitor has two Diagnostic level alarms for TON and TOF operation.
- Expression tags bound to each alarm isActive flag.
- Integer tags for TON and TOF time values.
- limited to even seconds

[timer\_udt.json](https://forum.inductiveautomation.com/uploads/short-url/iZa3PJfppuHE6IfYY2FYBxMIlKz.json) (1.2 KB)

[https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/0/e02c5f0026b5e497c1b12923746a055a91f82a77.mp4](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/0/e02c5f0026b5e497c1b12923746a055a91f82a77.mp4)
