# Timer script in tag

**URL:** <https://forum.inductiveautomation.com/t/timer-script-in-tag/96636>\
**Category:** General Discussion\
**Created:** [September 30, 2024, 9:05pm UTC](https://forum.inductiveautomation.com/t/timer-script-in-tag/96636 "2024-09-30T21:05:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rahman.tarek](https://avatars.discourse-cdn.com/v4/letter/r/b77776/32.png) [@rahman.tarek](https://forum.inductiveautomation.com/u/rahman.tarek)\
**Post date:** [September 30, 2024, 9:05pm UTC](https://forum.inductiveautomation.com/t/timer-script-in-tag/96636/1 "2024-09-30T21:05:47Z")

</div>

Hello i need to write an expression tag where if a specific tag is above a threshold value for \> than 5 seconds it will turn to 1. What is the best way to incorporate the time element? is it to have a runscript in the tag itself?

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [September 30, 2024, 9:12pm UTC](https://forum.inductiveautomation.com/t/timer-script-in-tag/96636/2 "2024-09-30T21:12:35Z")

</div>

Is this for alarming purposes or for triggering something else?  
You could configure an alarm on the tag with a 5 second `ActiveDelay`

---

<div class="post-metadata">

**Author:** ![rahman.tarek](https://avatars.discourse-cdn.com/v4/letter/r/b77776/32.png) [@rahman.tarek](https://forum.inductiveautomation.com/u/rahman.tarek)\
**Post date:** [September 30, 2024, 9:14pm UTC](https://forum.inductiveautomation.com/t/timer-script-in-tag/96636/3 "2024-09-30T21:14:06Z")

</div>

No not for alarm purpose. Basically i have a plc tag which if about a certain value for a period of time we consider it a quality issue.

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [September 30, 2024, 9:21pm UTC](https://forum.inductiveautomation.com/t/timer-script-in-tag/96636/4 "2024-09-30T21:21:45Z")

</div>

Other way to handle this without using the alarm properties of a tag is to have a separate memory tag of type datetime, and a boolean expression tag.

Using a Gateway Tag Change Event pointed at the tag you want to monitor, push the current timestamp into the memory tag only if the monitored tag's previous value was not above the limit, and the current value is above the limit. If the value is not above the limit, put a null into the tag.

In the expression tag, have it determine if the memory tag is a valid date (not null) and then compare the elapsed time between that datetime and the current timestamp and return True if the time difference is more than your time limit (5 seconds). Something along the lines of:

```python
if(
	!isNull{path.to.timestamp.tag}),
	dateDiff(now(1000), {path.to.timestamp.tag}, 'second') > 5,
	false
)

```

---

<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:** [September 30, 2024, 9:24pm UTC](https://forum.inductiveautomation.com/t/timer-script-in-tag/96636/5 "2024-09-30T21:24:28Z")

</div>

> [@rahman.tarek](#):
>
> No not for alarm purpose. Basically i have a plc tag which if about a certain value for a period of time we consider it a quality issue.

This really is alarm behavior. Consider using an alarm and just not call it an alarm. Perhaps send it to a special alarm pipeline for quality notifications.
