# Script occasionally runs often than configured and returns bogus data

**URL:** <https://forum.inductiveautomation.com/t/script-occasionally-runs-often-than-configured-and-returns-bogus-data/59048>\
**Category:** Ignition\
**Created:** [April 29, 2022, 12:23pm UTC](https://forum.inductiveautomation.com/t/script-occasionally-runs-often-than-configured-and-returns-bogus-data/59048 "2022-04-29T12:23:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![szcz](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@szcz](https://forum.inductiveautomation.com/u/szcz)\
**Post date:** [April 29, 2022, 12:23pm UTC](https://forum.inductiveautomation.com/t/script-occasionally-runs-often-than-configured-and-returns-bogus-data/59048/1 "2022-04-29T12:23:52Z")

</div>

Hi dear Ignited,

I am troubleshooting problem with script. We have set of machines that have ever-growing counter (it resets only on very rare occasions). On several occassions, the script has apparently ran twice at the same time. One run was legit but the other (given the bogus data in database) seems to return values tens of hours old. Let me demonstrate:

```auto
dbconn = system.tag.read('Globals/IgnitionRuntimeConnection').value
import datetime
t_sql = """
INSERT INTO [dbo].[x56_PROCESS_MAC_MONITORING](machineid, timestamp, alive, totalcounter)
VALueS (?,?,?,?)
"""
for i in range(13,29):
	tc = system.tag.read('x56_Process/Machine'+str(i)+'/Statistics/TotalCounter').value
	alive = True if tc is not None else False
	system.db.runPrepUpdate(t_sql, [i, datetime.datetime.now(), alive, tc], dbconn)

```

Script above runs every second and writes current value of one tag to database table for troubleshooting purposes **for each machine**. How come it is able to write 2 different values for one and the same second for **one individual machine**? The first value is correct (number is OK in accordance to ever-growing TotalCounter), but the other value written at the very same second must be at least 12 hours old. I can’t get my head around why this can happen. In general, there shouldn’t be more than 1 row per one machine per 1 second, yet there is.

Thanks for your feedback, SZ

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [April 29, 2022, 12:45pm UTC](https://forum.inductiveautomation.com/t/script-occasionally-runs-often-than-configured-and-returns-bogus-data/59048/2 "2022-04-29T12:45:22Z")

</div>

Assuming v7.9?

First off, you are doing too many tag reads which is probably slowing the script down quite a bit.  
generate a list of tag paths to read, read them all, then loop over the results.

```auto
dbconn = system.tag.read('Globals/IgnitionRuntimeConnection').value
t_sql = """
INSERT INTO [dbo].[x56_PROCESS_MAC_MONITORING](machineid, timestamp, alive, totalcounter)
VALueS (?,?,?,?)
"""
machNumbers = range (13,29)
tagPaths = ['x56_Process/Machine%s/Statistics/TotalCounter' % i for i in machNumbers]
tags = system.tag.readAll(tagPaths)

for i, tag in zip(machNumbers,tags):
	tc = tag.value
	alive = True if tc is not None else False
	system.db.runPrepUpdate(t_sql, [i, system.date.now(), alive, tc], dbconn)

```

---

<div class="post-metadata">

**Author:** ![szcz](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@szcz](https://forum.inductiveautomation.com/u/szcz)\
**Post date:** [April 29, 2022, 4:07pm UTC](https://forum.inductiveautomation.com/t/script-occasionally-runs-often-than-configured-and-returns-bogus-data/59048/3 "2022-04-29T16:07:35Z")

</div>

Hi @dkhayes117 ,

thank you for useful hint. Yes, the gateway version is 7.9.18.
