Insert a delay in a Gateway Tag Change Scripts

I have a script that is triggered by a Gateway Tag Change Script… I would like the script to start something like 10 seconds after the tag change… InvokeLater doesn’t work on gateway script… What would be the dirty trick for that?

The dirty trick would be the gateway invokeLater() implementation in my later.py script module. Note, it uses undocumented/unsupported methods to obtain a suitable execution engine.

However, if you need a delay to allow something else to happen, it is better to work out how to make the end of that something else trigger the followup script.

Can we enable and disable a Gateway Timer from a script ?

For fun, anyone knows what’s the impact of a script like this on a Gateway script… Because on a client script, it locks the interface, but on the gateway, would it lock only that script or the whole gateway too?

def sub_delay_timer(int_seconds = 5):
	import datetime
	
	print ("Now")
	time_delay = datetime.datetime.now() + datetime.timedelta(seconds=int_seconds)
	while (datetime.datetime.now() < time_delay) :
		print("do")
		
	print("Later")

It will lock only the thread that it shares with other event scripts, not the whole gateway. Similar to what happens if you use a sleep() call, but worse, in that it will burn CPU time and flood the wrapper log. Don’t do this.

2 Likes

+1

On a personal note, I would make an expression tag that monitors the elapsed time after a tag change, then trigger the script with the expression tag.

Expression example:

if(secondsBetween({path/to/monitored/tag}, now()) >= 10, 1, 0)
3 Likes

I’m not the best with Expressions script… vs Python

Seems seems that the {path/to/monitored/tag} return that value only, and not the timestamp like a system.tag.read("{path/to/monitored/tag}").timestamp would do. So on tag change I have to write the timestamp to a memory tag. Then run the expression script against the memory tag. Is there a way I can get the tag timestamp from the tag directly in Expressions?

This is how I do that:

toDate({[~]Path/to/some/tag.Timestamp})
2 Likes

You can drill down to many of the tag’s properties.

1 Like

Try this one out…

  1. Configure the script to run on an actual tag instead of a gateway script.
  2. Configure the tag for an alarm condition, set the alarm active delay on the tag to 10 sec.
  3. Run the script on the “Alarm Active” event.
1 Like

Thanks a lot guys, this helps a lot!

I’m still @ 7.9.5… it’s not. Timestamp, but LastChange… I don’t know what will happen when I upgrade to 8? will this screw up the process?

Ok to those who are still interested. Here’s my 2 cents for my used case:

Since I use a SQL tag to get the current shift id from a database, and we have different systems that uses different methods for their shift on their own schedule, synchronising start and end of day is not an exact science. System time are not all the same, because every vendor has their own way to sync time that I have no control. I used to get the data from the LAST one that seems to changing shift, but still that is again not an exact science… But time on PLC, database and system XYZ all seem to have a minute more or less difference.

So what I’ve did, with your help, is to create a boolean expression tag that turns on for 5 secs, 2 mins after the tag change, on what I think is my last changing shift id tag. Then I run the shift change script (Collect all PLC/database data from all the different system for end of day reports)

So the monitor tag looks like this:
if(secondsBetween(toDate({[.]Current_shift.LastChange}), now()) >= 125, 0, if(secondsBetween(toDate({[.]Current_shift.LastChange}), now()) >= 120, 1, 0))

I’ve noticed that is works better when it normally false (N/O for the PLC guys out there). The initial status of an boolean expression tag seems always false after an update to the tag, I guess that is why the initialChange doesn’t work well in the script.

Anyway my script executes when it turns when it comes back false:
if not initialChange:
if currentValue.value is False:
project.production_smi.main_changement_shift_essence_smi()

I had to put it in the gateway script tag change, expression tag tag change script don’t seems to work with ‘project’ scripts, I think they only use ‘shared’ scripts. I might be wrong.

1 Like