# Gateway timer script question

**URL:** <https://forum.inductiveautomation.com/t/gateway-timer-script-question/26749>\
**Category:** Ignition\
**Created:** [July 22, 2019, 12:36pm UTC](https://forum.inductiveautomation.com/t/gateway-timer-script-question/26749 "2019-07-22T12:36:01Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![christopher.berg](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@christopher.berg](https://forum.inductiveautomation.com/u/christopher.berg)\
**Post date:** [July 22, 2019, 12:36pm UTC](https://forum.inductiveautomation.com/t/gateway-timer-script-question/26749/1 "2019-07-22T12:36:01Z")

</div>

I’ve inherited a gateway timer script that is supposed to write to a tag once every second starting at 0, getting reset every so often in response to some condition being met. The issue I’m having is that the timer isn’t keeping accurate time. You can see it count up with each execution but it stumbles every so often and gains time. It ends up counting 60 every 50 seconds or so. I’ve compensated for it by having the script execute a little less frequently which gets us closer to 60 counts in 60 seconds but I’d like it to be as accurate as possible. Is there a way to set the timer script to execute reliably every 1000ms? I realize that taking a time stamp and a current time then doing a timediff would be the best way to do this but, as I said, I inherited this and the timediff option would require a lot of rewriting of the rest of the project. That’s an option but I’d like to exhaust other possibilities first.

Thanks

---

<div class="post-metadata">

**Author:** ![PRAMANJ](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pramanj/32/66935_2.png) [@PRAMANJ](https://forum.inductiveautomation.com/u/PRAMANJ)\
**Post date:** [July 22, 2019, 12:44pm UTC](https://forum.inductiveautomation.com/t/gateway-timer-script-question/26749/2 "2019-07-22T12:44:32Z")

</div>

Have you set the timer to fixed delay or fixed interval ?

---

<div class="post-metadata">

**Author:** ![christopher.berg](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@christopher.berg](https://forum.inductiveautomation.com/u/christopher.berg)\
**Post date:** [July 22, 2019, 12:49pm UTC](https://forum.inductiveautomation.com/t/gateway-timer-script-question/26749/3 "2019-07-22T12:49:55Z")

</div>

It’s set to fixed rate.

---

<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:** [July 22, 2019, 1:03pm UTC](https://forum.inductiveautomation.com/t/gateway-timer-script-question/26749/4 "2019-07-22T13:03:05Z")

</div>

Is it on a dedicated thread?

---

<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:** [July 22, 2019, 1:10pm UTC](https://forum.inductiveautomation.com/t/gateway-timer-script-question/26749/5 "2019-07-22T13:10:51Z")

</div>

Some other things to try:

- Gateway Tag Change Script with an expression tag using now() as the trigger.

- Use a transaction group.

---

<div class="post-metadata">

**Author:** ![christopher.berg](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@christopher.berg](https://forum.inductiveautomation.com/u/christopher.berg)\
**Post date:** [July 22, 2019, 3:32pm UTC](https://forum.inductiveautomation.com/t/gateway-timer-script-question/26749/6 "2019-07-22T15:32:33Z")

</div>

It’s a dedicated thread.

---

<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:** [July 22, 2019, 3:34pm UTC](https://forum.inductiveautomation.com/t/gateway-timer-script-question/26749/7 "2019-07-22T15:34:24Z")

</div>

Is this running on a virtual machine instead of real hardware? Is there any kind of time sync set up?

---

<div class="post-metadata">

**Author:** ![christopher.berg](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@christopher.berg](https://forum.inductiveautomation.com/u/christopher.berg)\
**Post date:** [July 22, 2019, 3:36pm UTC](https://forum.inductiveautomation.com/t/gateway-timer-script-question/26749/8 "2019-07-22T15:36:33Z")

</div>

The gateway server is virtual but I can’t say if there’s a time sync on it. I would assume that it’s getting its time directly from the underlying hardware.

---

<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:** [July 22, 2019, 3:38pm UTC](https://forum.inductiveautomation.com/t/gateway-timer-script-question/26749/9 "2019-07-22T15:38:08Z")

</div>

You said you saw 60 executions in 50 seconds, which implies time is running _too fast_ rather than too slow, which is the more common problem.

I can’t imagine why this would happen other than the system time running too fast.

---

<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:** [July 22, 2019, 3:41pm UTC](https://forum.inductiveautomation.com/t/gateway-timer-script-question/26749/10 "2019-07-22T15:41:22Z")

</div>

Actually… I guess since you’re using fixed rate, another possibility is that some previous execution of the timer stalled for a while and then a bunch of executions “stacked” up, which could explain seeing 60 within a 50 second window.

What’s this script doing?

---

<div class="post-metadata">

**Author:** ![christopher.berg](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@christopher.berg](https://forum.inductiveautomation.com/u/christopher.berg)\
**Post date:** [July 22, 2019, 3:42pm UTC](https://forum.inductiveautomation.com/t/gateway-timer-script-question/26749/11 "2019-07-22T15:42:39Z")

</div>

Yeah, based on what I know about the timer settings, too slow makes more sense. I would think that a system clock running too quickly would result in a uniform speed up though. If you watch the tag, this counts of more more like 1… 2… 3… 4… 5 6 7… 8… 9… 10 11 12… the seconds mostly count out normally then every so often the seconds race and it will pick up about 3 seconds in the space of 1 and then continue to count normally again for a while. Very odd.

---

<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:** [July 22, 2019, 3:47pm UTC](https://forum.inductiveautomation.com/t/gateway-timer-script-question/26749/12 "2019-07-22T15:47:34Z")

</div>

That pattern would mean execution 4 took ~3 seconds to execute for some reason.

Fixed rate timers stack up - you have to be very careful that executions never take longer than the rate.

It could be GC activity if this is a busy system and/or starved for memory, or it could be related to whatever the script is doing.

---

<div class="post-metadata">

**Author:** ![christopher.berg](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@christopher.berg](https://forum.inductiveautomation.com/u/christopher.berg)\
**Post date:** [July 22, 2019, 3:55pm UTC](https://forum.inductiveautomation.com/t/gateway-timer-script-question/26749/13 "2019-07-22T15:55:13Z")

</div>

I gotcha. I think I have a better understanding of what he was doing now. Here’s a little sample:

if system.tag.read(‘Idle Active S2’).value \> 1 :  
system.tag.write(‘Timer Idle Time S2’, actualIdleTimeS2 + 1)  
else :  
system.tag.write(‘Timer Idle Time S2’, 0)

this is repeated 10 times, one for each station he’s monitoring. He’s relying on the script to execute every one second exactly to increment his counters but that kind of precision is impossible to guarantee with a timer script because you can’t always account for performance burps elsewhere on the server.

is that basically correct?

---

<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:** [July 22, 2019, 4:05pm UTC](https://forum.inductiveautomation.com/t/gateway-timer-script-question/26749/14 "2019-07-22T16:05:20Z")

</div>

> [@christopher.berg](#):
>
> He’s relying on the script to execute every one second exactly to increment his counters but that kind of precision is impossible to guarantee with a timer script because you can’t always account for performance burps elsewhere on the server.
> 
> is that basically correct?

Yeah, pretty much. This is not a realtime system, and you've got another entire layer of timing uncertainty in the picture because you're running on a virtual machine too.

If your Ignition is not already set up to use G1GC that may help things. You can see in the ignition.conf file if it's still configured for the old CMS GC.

---

<div class="post-metadata">

**Author:** ![christopher.berg](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@christopher.berg](https://forum.inductiveautomation.com/u/christopher.berg)\
**Post date:** [July 22, 2019, 4:28pm UTC](https://forum.inductiveautomation.com/t/gateway-timer-script-question/26749/15 "2019-07-22T16:28:44Z")

</div>

I think I’d be better off just creating a couple more memory tags, one with the current time, and another with the stop time of the station, then just displaying the time difference every execution cycle. If the difference doesn’t display every second exactly, that’s fine. At least when it does display, the number will be right.
