Timer Script - Gateway Events

I have an issue when trying to make an script for updating a tag value every second, it just don´t obey the time configuration.
Here is the code:
s=system.tag.read(“REGISTRO”).value
if s:
A=system.tag.read(“SEG”).value
B=system.tag.read(“MIN”).value
C=system.tag.read(“HOR”).value
if A==60:
A=0
B=B+1
else:
A=A+1
if B==60:
B=0
C=C+1
system.tag.write(“SEG”,A)
system.tag.write(“MIN”,B)
system.tag.write(“HOR”,C)
else:
system.tag.write(“SEG”,0)
system.tag.write(“MIN”,0)
system.tag.write(“HOR”,0)
And the script configuration is:
Delay(ms): 1,000
Enabled: True
Delay Type: Fixed Delay
Threading: Shared

Please edit your post and wrap your code with 3 back ticks ``` at the start and end so that formatting is maintained.

Also, timing is not guaranteed especially not when run in the shared thread . But try using fixed period and run it in a dedicated thread

“A Fixed Delay timer script (the default) waits for the given Delay between each script invocation. This means that the script’s rate will actually be the delay plus the amount of time it takes to execute the script”

Also you should optimise your read and writes by bulk reading and writing all tags at once

What is the value of “REGISTRO”? It may make more sense to do this as a tag event instead.

1 Like

Also would be better to not make your own timer. Save a timestamp on changes of REGISTRO and do math on the time deltas. Ignition’s timer events have no precision guarantees.