# Gateway Restart Triggers On Change Data & Scripts

**URL:** <https://forum.inductiveautomation.com/t/gateway-restart-triggers-on-change-data-scripts/74907>\
**Category:** General Discussion\
**Tags:** database, perspective, scripting\
**Created:** [June 12, 2023, 2:49pm UTC](https://forum.inductiveautomation.com/t/gateway-restart-triggers-on-change-data-scripts/74907 "2023-06-12T14:49:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brad\_Medley](https://avatars.discourse-cdn.com/v4/letter/b/82dd89/32.png) [@Brad\_Medley](https://forum.inductiveautomation.com/u/Brad_Medley)\
**Post date:** [June 12, 2023, 2:49pm UTC](https://forum.inductiveautomation.com/t/gateway-restart-triggers-on-change-data-scripts/74907/1 "2023-06-12T14:49:02Z")

</div>

Ignition Platform 8.1.20

Currently, I am having an issue with our gateway crashing and restarting. We are addressing that issue, but as a byproduct of the issue my on change data in history is recording multiple times, instead of unique values.

History Settings:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/f/0f621e94f5a206bd1b3456db41a553340734cee1.png)

SQL History View of On Change Data  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/7/079176d7073d6ab7ff770f67872be1673494997a.png)

The big issue that I am currently having is that these restarts are activating a script in my tag event scripts. Is there a way to ensure that the script does not trigger on initial startup?

Current Script IF Statement:

```auto
if previousValue.value != currentValue.value and initialChange == False and currentValue.value <> 0 and currentValue.quality.good:

```

---

<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:** [June 12, 2023, 3:05pm UTC](https://forum.inductiveautomation.com/t/gateway-restart-triggers-on-change-data-scripts/74907/2 "2023-06-12T15:05:55Z")

</div>

All tag change events are executed once on startup with `initialChange` set `True` and `previousValue` `None`. You must not attempt to reference `previousValue.value` during initial execution as that will throw an error.

Your code must be tolerant of this as there is no way for the server to know, short of a history query, to obtain the previous value. (And a history query is too expensive to run in a tag event. Really, any database operation is too expensive for a tag event, unless it hands off to the store and forward system.)

A gateway tag change event (in a project) is more tolerant of longer execution times, and can use a project script library's "import" behavior to bulk query the most recent value for many tags at once.

Bottom line: there is no simple solution. Many applications tolerate the extra row and use lead/lag functions in the DB or scripted post-processing to deal with such duplicates.

---

<div class="post-metadata">

**Author:** ![Brad\_Medley](https://avatars.discourse-cdn.com/v4/letter/b/82dd89/32.png) [@Brad\_Medley](https://forum.inductiveautomation.com/u/Brad_Medley)\
**Post date:** [August 7, 2023, 2:57pm UTC](https://forum.inductiveautomation.com/t/gateway-restart-triggers-on-change-data-scripts/74907/3 "2023-08-07T14:57:30Z")

</div>

In your initial statement you say that on startup initialChange is set to True, but my code indicates that it must be False for it to run. How does it execute the code then?

Would referencing a memory tag be less taxing?

---

<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:** [August 7, 2023, 3:21pm UTC](https://forum.inductiveautomation.com/t/gateway-restart-triggers-on-change-data-scripts/74907/4 "2023-08-07T15:21:16Z")

</div>

> [@Brad\_Medley](#):
>
> but my code indicates that it must be False for it to run

The if statement's **condition** itself accesses `previousValue.value` **before** it checks `initialChange`.

Broken.

---

<div class="post-metadata">

**Author:** ![Brad\_Medley](https://avatars.discourse-cdn.com/v4/letter/b/82dd89/32.png) [@Brad\_Medley](https://forum.inductiveautomation.com/u/Brad_Medley)\
**Post date:** [August 10, 2023, 6:17pm UTC](https://forum.inductiveautomation.com/t/gateway-restart-triggers-on-change-data-scripts/74907/5 "2023-08-10T18:17:55Z")

</div>

Does broken mean it goes ahead and triggers whatever is after?

---

<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:** [August 10, 2023, 6:36pm UTC](https://forum.inductiveautomation.com/t/gateway-restart-triggers-on-change-data-scripts/74907/6 "2023-08-10T18:36:07Z")

</div>

No, it means it hits a null for `previousValue` whenever `initialChange` is true, never even getting to that case.

The order you put your checks in an if statement matter.
