# Insert a delay in a Gateway Tag Change Scripts

**URL:** <https://forum.inductiveautomation.com/t/insert-a-delay-in-a-gateway-tag-change-scripts/32168>\
**Category:** Ignition\
**Created:** [February 12, 2020, 4:02pm UTC](https://forum.inductiveautomation.com/t/insert-a-delay-in-a-gateway-tag-change-scripts/32168 "2020-02-12T16:02:02Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![EricFrancoeur](https://avatars.discourse-cdn.com/v4/letter/e/dfb087/32.png) [@EricFrancoeur](https://forum.inductiveautomation.com/u/EricFrancoeur)\
**Post date:** [February 12, 2020, 4:02pm UTC](https://forum.inductiveautomation.com/t/insert-a-delay-in-a-gateway-tag-change-scripts/32168/1 "2020-02-12T16:02:03Z")

</div>

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?

---

<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:** [February 12, 2020, 4:12pm UTC](https://forum.inductiveautomation.com/t/insert-a-delay-in-a-gateway-tag-change-scripts/32168/2 "2020-02-12T16:12:33Z")

</div>

The dirty trick would be the gateway `invokeLater()` implementation in my [later.py](https://www.automation-pros.com/ignition/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.

---

<div class="post-metadata">

**Author:** ![EricFrancoeur](https://avatars.discourse-cdn.com/v4/letter/e/dfb087/32.png) [@EricFrancoeur](https://forum.inductiveautomation.com/u/EricFrancoeur)\
**Post date:** [February 12, 2020, 5:22pm UTC](https://forum.inductiveautomation.com/t/insert-a-delay-in-a-gateway-tag-change-scripts/32168/3 "2020-02-12T17:22:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![EricFrancoeur](https://avatars.discourse-cdn.com/v4/letter/e/dfb087/32.png) [@EricFrancoeur](https://forum.inductiveautomation.com/u/EricFrancoeur)\
**Post date:** [February 12, 2020, 5:45pm UTC](https://forum.inductiveautomation.com/t/insert-a-delay-in-a-gateway-tag-change-scripts/32168/4 "2020-02-12T17:45:04Z")

</div>

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?

```auto
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")

```

---

<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:** [February 12, 2020, 6:23pm UTC](https://forum.inductiveautomation.com/t/insert-a-delay-in-a-gateway-tag-change-scripts/32168/5 "2020-02-12T18:23:51Z")

</div>

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.

---

<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:** [February 12, 2020, 6:34pm UTC](https://forum.inductiveautomation.com/t/insert-a-delay-in-a-gateway-tag-change-scripts/32168/6 "2020-02-12T18:34:29Z")

</div>

> [@pturmel](#):
>
> Don’t do this.

+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:

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

```

---

<div class="post-metadata">

**Author:** ![EricFrancoeur](https://avatars.discourse-cdn.com/v4/letter/e/dfb087/32.png) [@EricFrancoeur](https://forum.inductiveautomation.com/u/EricFrancoeur)\
**Post date:** [February 12, 2020, 7:30pm UTC](https://forum.inductiveautomation.com/t/insert-a-delay-in-a-gateway-tag-change-scripts/32168/7 "2020-02-12T19:30:47Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![wking](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wking/32/21823_2.png) [@wking](https://forum.inductiveautomation.com/u/wking)\
**Post date:** [February 12, 2020, 8:09pm UTC](https://forum.inductiveautomation.com/t/insert-a-delay-in-a-gateway-tag-change-scripts/32168/8 "2020-02-12T20:09:13Z")

</div>

> [@EricFrancoeur](#):
>
> Is there a way I can get the tag timestamp from the tag directly in Expressions?

This is how I do that:

```auto
toDate({[~]Path/to/some/tag.Timestamp})

```

---

<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:** [February 12, 2020, 9:48pm UTC](https://forum.inductiveautomation.com/t/insert-a-delay-in-a-gateway-tag-change-scripts/32168/9 "2020-02-12T21:48:49Z")

</div>

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

 ![2020-02-12_16-48-12](https://us1.discourse-cdn.com/inductiveautomation/original/2X/9/9b0ad083a02699e29e60fd70eb45fb4ba59e500b.png)

---

<div class="post-metadata">

**Author:** ![Paullys50](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paullys50/32/3324_2.png) [@Paullys50](https://forum.inductiveautomation.com/u/Paullys50)\
**Post date:** [February 13, 2020, 5:32am UTC](https://forum.inductiveautomation.com/t/insert-a-delay-in-a-gateway-tag-change-scripts/32168/10 "2020-02-13T05:32:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![EricFrancoeur](https://avatars.discourse-cdn.com/v4/letter/e/dfb087/32.png) [@EricFrancoeur](https://forum.inductiveautomation.com/u/EricFrancoeur)\
**Post date:** [February 13, 2020, 11:12am UTC](https://forum.inductiveautomation.com/t/insert-a-delay-in-a-gateway-tag-change-scripts/32168/11 "2020-02-13T11:12:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![EricFrancoeur](https://avatars.discourse-cdn.com/v4/letter/e/dfb087/32.png) [@EricFrancoeur](https://forum.inductiveautomation.com/u/EricFrancoeur)\
**Post date:** [February 13, 2020, 5:33pm UTC](https://forum.inductiveautomation.com/t/insert-a-delay-in-a-gateway-tag-change-scripts/32168/12 "2020-02-13T17:33:12Z")

</div>

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.
