Reset Tag Value after a Number of Seconds

Hello,

Does anybody by chance know how I could reset a tag’s value to a coded default value after a set number of seconds?

Any help is greatly appreciated…

Thanks in advance.

-Lenny

Make another tag of type Date to store the time of your existing tags last transition. In your existing tag, add an OnValueChange event script that sets your new date tag to system.date.now() whenever
previousValue.value != currentValue.value
Then make a third tag, an expression tag, that evaluates to the time between the above date tag and now(). Give it an OnValueChange event script that checks if the currentValue.value is greater than your predefined number of seconds. If it is, then reset your existing tag to the default.

See this post for something similar: Timer function that works gloabally(gateway)

2 Likes

Thanks! That worked out great…

-Lenny

Another possible option using just the one existing tag could be something like this in the tag’s Value Changed event script.

import time

defaultValue = YOUR DEFAULT VALUE HERE
delaySeconds = YOUR TIME DELAY HERE

if currentValue.value != defaultValue:
    time.sleep(delaySeconds)
    system.tag.write(tagPath, defaultValue)
else: pass

This does work and looks a lot better, though doesn’t it block the event thread?

You’re right, it does. I’ve used this on a few tags here that need to self change state and have never seen any issue from it, but I’m only sleeping 1 or 2 seconds on mine.

Now I have a fun project to see what havoc I can cause tomorrow when I sleep a test tag for longer!

1 Like

python’s time.sleep and Java’s Thread.sleep are truly dangerous in GUI environments. They should never, ever, be called from any GUI event routine. They can be called from independent threads, but it is almost always better to schedule a deferred function, using an ExecutorService if necessary. Or use a timer component or client timer event.

3 Likes

Thank you @pturmel you set me on the path to enlightenment… :slight_smile:

Working on a different issue, Ethan with IA support crafted me a superior script which still allowed me to keep everything in one tag:

import time
def resetTag():
    time.sleep(3)
    system.tag.write(tagPath, 0)

defaultValue = SOMETHING

if currentValue.value != defaultValue:
    system.util.invokeAsynchronous(resetTag)
else: pass
3 Likes

It’s unfortunate that there’s no alternative to invokeLater in gateway scope. I may just have to add something to later.py to address this.

1 Like

Try below script on reset tag > Tag Events> Value Changed event:

s = 0
delay = 0

while delay < 3:
	
	if s == 0:
		date = system.date.now()
		s = system.date.getSecond(date)
	else:
		date = system.date.now()
		t = system.date.getSecond(date)
		delay = delay + abs(t - s)
system.tag.write("Folder_Path/Scada_Reset",0)
2 Likes

Could you explain this later.py file? Is this something I can import that will let me execute asynchronously from the gateway scope?

1 Like

This will also work. system.util.invokeLater(writeLater,“Put the time delay Here”)

#Clear Values From Entry Fields
		def writeLater():
			event.source.parent.getComponent('Status').selectedValue = -1
		system.util.invokeLater(writeLater, *2000*)
1 Like

system.util.invokeLater() doesn’t exist in gateway scope. Look in later.py for a work-around.

Thanks. This helped me rig up a basic simulation of a real world panel with N.C. and N.O. buttons.