# Gateway script triggering twice

**URL:** <https://forum.inductiveautomation.com/t/gateway-script-triggering-twice/78768>\
**Category:** Ignition\
**Created:** [September 5, 2023, 4:22pm UTC](https://forum.inductiveautomation.com/t/gateway-script-triggering-twice/78768 "2023-09-05T16:22:18Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Faz](https://avatars.discourse-cdn.com/v4/letter/f/85e7bf/32.png) [@Faz](https://forum.inductiveautomation.com/u/Faz)\
**Post date:** [September 5, 2023, 4:22pm UTC](https://forum.inductiveautomation.com/t/gateway-script-triggering-twice/78768/1 "2023-09-05T16:22:18Z")

</div>

I have a gateway script triggers a tag that triggers an sql query to write some data to a table.  
I'm getting two timestamps (very close to each other). How can i resolve this rather than changing plc code?  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/6/c6f9adf3d52025e9e4a7fffc66823e9d8cd9003c.png)

---

<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:** [September 5, 2023, 5:26pm UTC](https://forum.inductiveautomation.com/t/gateway-script-triggering-twice/78768/2 "2023-09-05T17:26:29Z")

</div>

What does the script look like?

Are you filtering for the appropriate things? How are you determining if the query needs to be executed or not?

You'll be hard pressed to get any good help if we cant see the code is question.

---

<div class="post-metadata">

**Author:** ![Faz](https://avatars.discourse-cdn.com/v4/letter/f/85e7bf/32.png) [@Faz](https://forum.inductiveautomation.com/u/Faz)\
**Post date:** [September 5, 2023, 5:30pm UTC](https://forum.inductiveautomation.com/t/gateway-script-triggering-twice/78768/3 "2023-09-05T17:30:57Z")

</div>

This is the gateway tag:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/0/4064c159e16525fb6cea1aa27da7f63ee684fd78.png)  
Above is a PLC step number.  
Below is the gateway script:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/f/7f8ab5960798a1d2809f43a474b672833fdedb60.png)  
When fiinalData becomes 1, SQL query gets executed.

---

<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:** [September 5, 2023, 5:42pm UTC](https://forum.inductiveautomation.com/t/gateway-script-triggering-twice/78768/4 "2023-09-05T17:42:00Z")

</div>

Ok, first you'll want to filter against initial change, otherwise you'll get a data entry at gateway startup.

This code can just be:

```auto
if not initialChange:
    system.tag.writeBlocking([event.tagPath.parentPath.replace('PLCSteps','SQLGatewayTriggers/finalData'),[newValue.value == 100])

```

How is the query being triggered?

---

<div class="post-metadata">

**Author:** ![Faz](https://avatars.discourse-cdn.com/v4/letter/f/85e7bf/32.png) [@Faz](https://forum.inductiveautomation.com/u/Faz)\
**Post date:** [September 5, 2023, 5:44pm UTC](https://forum.inductiveautomation.com/t/gateway-script-triggering-twice/78768/5 "2023-09-05T17:44:19Z")

</div>

Ok. I'll try adding that.  
Query is triggered using finalData tag.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/4/54533c2498b2ddea5a5e3cbf28672021e8567183.png)

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [September 5, 2023, 5:47pm UTC](https://forum.inductiveautomation.com/t/gateway-script-triggering-twice/78768/6 "2023-09-05T17:47:22Z")

</div>

Never seen a parameter used in a NQ like that before - this works? Personally I would put all control flow into the jython directly, I feel that is more clear. I would not like to see a NQ called in code that has the potential to be a complete no-op.

```auto
if serverTrigger == 1:
    system.db.runNamedQuery("insertQuery",{'someParam':'somevalue'}
else:
    # do fall back

```

And removing the `IF :serverTrigger = 1` from the NQ - just have it be the INSERT statement.

---

<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:** [September 5, 2023, 5:56pm UTC](https://forum.inductiveautomation.com/t/gateway-script-triggering-twice/78768/7 "2023-09-05T17:56:09Z")

</div>

> [@Faz](#):
>
> Query is triggered using finalData tag.

I understand that, but how? Tag Change Script, Transaction Group ? There are a lot of ways to execute a query.

---

<div class="post-metadata">

**Author:** ![Faz](https://avatars.discourse-cdn.com/v4/letter/f/85e7bf/32.png) [@Faz](https://forum.inductiveautomation.com/u/Faz)\
**Post date:** [September 5, 2023, 10:24pm UTC](https://forum.inductiveautomation.com/t/gateway-script-triggering-twice/78768/8 "2023-09-05T22:24:50Z")

</div>

I call a NamedQuery in a table and give the path. It works well with IF :serverTrigger =1 except the multiple lines at very small time intervals (as i posted at the beginning).  
I tried to call the query as @bkarabinchak.psi posted but it's missing the step number in the gateway script every time. Issue is still not resolved and any help is appreciated.

---

<div class="post-metadata">

**Author:** ![amy.sudibyo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amy.sudibyo/32/38659_2.png) [@amy.sudibyo](https://forum.inductiveautomation.com/u/amy.sudibyo)\
**Post date:** [September 5, 2023, 11:29pm UTC](https://forum.inductiveautomation.com/t/gateway-script-triggering-twice/78768/9 "2023-09-05T23:29:31Z")

</div>

Does the tag serve any purpose other than triggering the query?  
If not, I don't see why you wouldn't just call the named query in your gateway script.

---

<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:** [September 6, 2023, 1:17am UTC](https://forum.inductiveautomation.com/t/gateway-script-triggering-twice/78768/10 "2023-09-06T01:17:22Z")

</div>

> [@Faz](#):
>
> I tried to call the query as @bkarabinchak.psi posted but it's missing the step number in the gateway script every time

It’s missing the number or you’re not sending it to the query??

Would need to see the complete query structure to help much more, but I would expect to see something similar to this.

Named Query:

```auto
INSERT INTO tableName
Values (:stepNumber)

```

Then in the script:

```python
if not initialChange:
   system.db.runNamedQuery('yourNamedQuery',{'stepNumber':newValue.value})

```

If your intention is to use the step number then I would just put the tag event on that tag.

As of right now I don’t see a need for the trigger tag, but I’m not sure what the query is truly trying to accomplish as you haven’t provided enough detail. Make the names generic if you need to obfuscate information for NDA reasons, we can work with that, but sprinkles of info and it’s still not working make it hard for us to help you.

---

<div class="post-metadata">

**Author:** ![Alistair](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Alistair](https://forum.inductiveautomation.com/u/Alistair)\
**Post date:** [September 6, 2023, 9:06am UTC](https://forum.inductiveautomation.com/t/gateway-script-triggering-twice/78768/11 "2023-09-06T09:06:49Z")

</div>

Do don't have a parent project by any chance?  
If the gateway script is in the parent project, then it will be inherited and the script will trigger once per inherited project and the parent. Same with report schedules.

---

<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:** [September 6, 2023, 12:30pm UTC](https://forum.inductiveautomation.com/t/gateway-script-triggering-twice/78768/12 "2023-09-06T12:30:21Z")

</div>

> [@lrose](#):
>
> It’s missing the number or you’re not sending it to the query??

I think @Faz is meaning that it's not triggering on the required step.

If we go back to the original script, and use the suggestions from others we would end up with:

```python
step = newValue.value

if not initialChange and step == 100:
	system.db.runNamedQuery("insertQuery",{'someParam':'somevalue'})

```

Then, the IF statement in the Named Query can be removed, as well as the redundant trigger tag.

---

<div class="post-metadata">

**Author:** ![Faz](https://avatars.discourse-cdn.com/v4/letter/f/85e7bf/32.png) [@Faz](https://forum.inductiveautomation.com/u/Faz)\
**Post date:** [September 6, 2023, 3:50pm UTC](https://forum.inductiveautomation.com/t/gateway-script-triggering-twice/78768/13 "2023-09-06T15:50:11Z")

</div>

@JordanCClark is correct. It's not triggering at the required step.

This is the query:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/4/14a57a9f3ed2ecf0c4eff730b1a9821eac0924c5.png)

i want the above query to trigger when step is 100 and insert several tag values.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/5/55683f9f9f308febc94a620354b017a90ce65ed8.png)

This is how data was written to my DB.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/b/9b09b7230f570c2bb160b3804f20198b67f86415.png)

Could it possibly be tag scanclass issue?

---

<div class="post-metadata">

**Author:** ![Faz](https://avatars.discourse-cdn.com/v4/letter/f/85e7bf/32.png) [@Faz](https://forum.inductiveautomation.com/u/Faz)\
**Post date:** [September 6, 2023, 6:12pm UTC](https://forum.inductiveautomation.com/t/gateway-script-triggering-twice/78768/14 "2023-09-06T18:12:25Z")

</div>

Multiple trigger as i posted first is happening no matter how i trigger the query from ignition

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/c/8cf03de232b78b995254357d9a924aa1bcee73b9.jpeg)

Any idea why this is happening? I manually triggered the tag to run the query and it inserted two lines within milliseconds.

---

<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:** [September 6, 2023, 6:31pm UTC](https://forum.inductiveautomation.com/t/gateway-script-triggering-twice/78768/15 "2023-09-06T18:31:40Z")

</div>

Did you define this event in an inheritable project? And have two leaf projects inheriting from it?

(Don't define events of any kind in an inheritable project.)

---

<div class="post-metadata">

**Author:** ![Faz](https://avatars.discourse-cdn.com/v4/letter/f/85e7bf/32.png) [@Faz](https://forum.inductiveautomation.com/u/Faz)\
**Post date:** [September 6, 2023, 6:35pm UTC](https://forum.inductiveautomation.com/t/gateway-script-triggering-twice/78768/16 "2023-09-06T18:35:06Z")

</div>

I have only one project so far and inheritable is set to false.

---

<div class="post-metadata">

**Author:** ![Felipe\_CRM](https://avatars.discourse-cdn.com/v4/letter/f/dfb087/32.png) [@Felipe\_CRM](https://forum.inductiveautomation.com/u/Felipe_CRM)\
**Post date:** [September 6, 2023, 7:33pm UTC](https://forum.inductiveautomation.com/t/gateway-script-triggering-twice/78768/17 "2023-09-06T19:33:40Z")

</div>

> [@Faz](#):
>
> I call a NamedQuery in a table

Is this a polling query binding? If it polls the query twice while the step is 100, it'll write twice (at least under the old logic where the query was happening unconditionally and just looking at the `serverTrigger` parameter to see whether to update or not).

I notice that when you're running it from the change script:

> [@Faz](#):
>
> ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/b/9b09b7230f570c2bb160b3804f20198b67f86415.png)
> 
> image1747×442 94.8 KB

it's writing every few seconds instead of milliseconds. In particular, I don't see the double writes happening in this screenshot.

As for the issue where some values aren't being written, that could be because the ignition tags aren't updating fast enough to keep up with the PLC. If those tags are reset to 0 and empty strings every machine cycle, Ignition might still be getting those old values. Usually if something depends on tight timing like this, I use `system.opc.readValues()` instead of `system.tag.writeBlocking` as it forces an OPC read and is going to be a fresh value
