# Gateway Timer Event Script Help

**URL:** <https://forum.inductiveautomation.com/t/gateway-timer-event-script-help/43565>\
**Category:** Ignition\
**Tags:** ignition80\
**Created:** [February 18, 2021, 1:45pm UTC](https://forum.inductiveautomation.com/t/gateway-timer-event-script-help/43565 "2021-02-18T13:45:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![iceberg](https://avatars.discourse-cdn.com/v4/letter/i/e99b99/32.png) [@iceberg](https://forum.inductiveautomation.com/u/iceberg)\
**Post date:** [February 18, 2021, 1:45pm UTC](https://forum.inductiveautomation.com/t/gateway-timer-event-script-help/43565/1 "2021-02-18T13:45:51Z")

</div>

I have a script that I would like to run at a predefined time (2am every morning). Looking through documentation, this sounds like I would want to use a Gateway Timer Event Script with a fixed rate. I can’t seem to find a good example for how to set this up. Would someone be able to help provide me an example of how to set this up? Where do I define the fixed rate? How do I define initial start? Does this need to be defined in the script somehow?

![Timer Settings](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/1/2146838378599c1986328453f157b0be3527e075.jpeg)

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [February 18, 2021, 2:12pm UTC](https://forum.inductiveautomation.com/t/gateway-timer-event-script-help/43565/2 "2021-02-18T14:12:43Z")

</div>

You are better off using a Gateway Tag Event Script.

Set up an expression tag to trigger at your desired time

```auto
getHour24(now()) = 2

```

Then subscribe the tag to the Tag Change Event.

Be sure to add a filter to the script so it only runs once.

```auto
if not initialChange and newValue:
    #your script here

```

---

<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 18, 2021, 2:52pm UTC](https://forum.inductiveautomation.com/t/gateway-timer-event-script-help/43565/3 "2021-02-18T14:52:13Z")

</div>

> [@lrose](#):
>
> better off using a Gateway Tag Event Script

Blegh. No. Gateway timers have numerous advantages. Slightly different context in this post, but most of these are relevant:

> [@Ignition 8.1.2 SQL Tags Not Updating After Update From 7.9](http://forum.inductiveautomation.com/t/ignition-8-1-2-sql-tags-not-updating-after-update-from-7-9/43418/7):
>
> Gateway project scope instead of gateway global scope. Option for a dedicated thread. Single event script can run multiple queries in any desired order, writing results to memory tags with any desired timing. Event scripts can post-process results before writing to tags, relieving the DB of static joins and other lookups. Including using cached lookups. Event scripts can drive multiple target tags from single, larger queries. Event scripts can simulate joins across datasources. Easy parallelis…

---

<div class="post-metadata">

**Author:** ![iceberg](https://avatars.discourse-cdn.com/v4/letter/i/e99b99/32.png) [@iceberg](https://forum.inductiveautomation.com/u/iceberg)\
**Post date:** [February 18, 2021, 2:58pm UTC](https://forum.inductiveautomation.com/t/gateway-timer-event-script-help/43565/4 "2021-02-18T14:58:14Z")

</div>

@pturmel I appreciate the feedback. Based on your feedback, are you able to help with the my original questions? I wasn’t able to find the answers to my questions in the post you added. Thanks!

---

<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 18, 2021, 3:09pm UTC](https://forum.inductiveautomation.com/t/gateway-timer-event-script-help/43565/5 "2021-02-18T15:09:19Z")

</div>

How soon/precise does it need to be after 2am? Pick a delay value that will check often enough. Use a datetime memory tag to hold the time of last full execution. The event script would then be something like this:

```auto
lastTS = system.tag.read("[default]path/to/timestamp/tag").value
currentTS = system.date.now()
today2am = system.date.setTime(currentTS, 2, 0, 0)

if currentTS.after(today2am) and lastTS.before(today2am):
	system.tag.write("[default]path/to/timestamp/tag", currentTS)
	#
	# Do the rest of your 2am script here
	#

```

Note that you don’t have to check initialChange with this algorithm, and it will automatically “catch up” if the server is disrupted/down when it would otherwise run.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [February 18, 2021, 3:22pm UTC](https://forum.inductiveautomation.com/t/gateway-timer-event-script-help/43565/6 "2021-02-18T15:22:41Z")

</div>

hmmm…hadn’t thought of that.

Today can be checked as successful as I have been schooled by @pturmel 🤣 🤣
