# Gateway Script variable not defined

**URL:** <https://forum.inductiveautomation.com/t/gateway-script-variable-not-defined/70978>\
**Category:** General Discussion\
**Tags:** ignition81\
**Created:** [March 13, 2023, 5:59pm UTC](https://forum.inductiveautomation.com/t/gateway-script-variable-not-defined/70978 "2023-03-13T17:59:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Smith](https://avatars.discourse-cdn.com/v4/letter/c/ce7236/32.png) [@Christopher\_Smith](https://forum.inductiveautomation.com/u/Christopher_Smith)\
**Post date:** [March 13, 2023, 5:59pm UTC](https://forum.inductiveautomation.com/t/gateway-script-variable-not-defined/70978/1 "2023-03-13T17:59:59Z")

</div>

I am making a timer script to record to a database at specific times when certain tag values are high. I am calculating a value and can get the value to print in the script console, but when I try to run the script in the gateway I get "line 12, in NameError: name 'gal' is not defined". I have an if statement that sets the value 'gal' to 0 when none of my tags are high, and if any are high it adds to itself (gal += calculation). Everything I have tried to troubleshoot the issue has given me back a value, but actually running the script does not.  
Any ideas as to the cause?

---

<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:** [March 13, 2023, 6:00pm UTC](https://forum.inductiveautomation.com/t/gateway-script-variable-not-defined/70978/2 "2023-03-13T18:00:59Z")

</div>

Post the script and format it with the `</>` button

---

<div class="post-metadata">

**Author:** ![Christopher\_Smith](https://avatars.discourse-cdn.com/v4/letter/c/ce7236/32.png) [@Christopher\_Smith](https://forum.inductiveautomation.com/u/Christopher_Smith)\
**Post date:** [March 13, 2023, 6:04pm UTC](https://forum.inductiveautomation.com/t/gateway-script-variable-not-defined/70978/3 "2023-03-13T18:04:21Z")

</div>

```auto
from datetime import datetime
Filter1Backwashing = system.tag.readBlocking(["[default]MainBuilding/Bits/Unit1BackwashInitiateRun"])[0].value
Filter2Backwashing = system.tag.readBlocking(["[default]MainBuilding/Bits/Unit2BackwashInitiateRun"])[0].value
dt = datetime.now()
date = dt.strftime("%m/%d/%Y")
time = dt.strftime("%H:%M:%S")
if Filter1Backwashing == 0 and Filter2Backwashing == 0:
	gal = 0
if Filter1Backwashing > 0:
	dailyInstance = system.tag.readBlocking(["[default]MainBuilding/Control/Backwash1DailyInstance"])[0].value
	loh_gpm = system.tag.readBlocking(["[default]Flow/MainFilter1_LOH_GPM/ActualValeu"])[0].value
	gal += ((loh_gpm * 5)/60)
	ntu_before = system.tag.readBlocking(["[default]MainBuilding/Control/Finished1NTU_AtBackwashStart"])[0].value
	loh_before = system.tag.readBlocking(["[default]MainBuilding/Control/LOH1Before"])[0].value
	rewash_time = system.tag.readBlocking(["[default]MainBuilding/Control/RewashTime"])[0].value
	ntu = system.tag.readBlocking(["[default]Turbidity/MainFilteredWater1Turbidity/ActualValue"])[0].value
	
	query = "INSERT INTO Backwash1 (BackwashGallons, NTU_Before, LOH_Before, RewashTime, NTU) VALUES (?,?,?,?,?)
	args= [loh_gpm, ntu_before, loh_before, rewash_time, ntu]
	system.db.runPrepUpdate("INSERT INTO Backwash1 (Date, Time, DailyInstance, Backwash_GPM, BackwashGallons, NTU_Before, LOH_Before, RewashTime, NTU) Values(?,?,?,?,?)",[date, time, dailyInstance, loh_gpm, gal, ntu_before, loh_before, rewash_time, ntu],'Wilderness_WTP_SCADA')

```

---

<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:** [March 13, 2023, 6:19pm UTC](https://forum.inductiveautomation.com/t/gateway-script-variable-not-defined/70978/4 "2023-03-13T18:19:37Z")

</div>

> [@Christopher\_Smith](#):
>
> `gal += ((loh_gpm * 5)/60)`

You are tying to add to gal withoit defining it first. Where is it supposed to come from?

---

<div class="post-metadata">

**Author:** ![leonardo.godoi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/leonardo.godoi/32/27178_2.png) [@leonardo.godoi](https://forum.inductiveautomation.com/u/leonardo.godoi)\
**Post date:** [March 13, 2023, 6:22pm UTC](https://forum.inductiveautomation.com/t/gateway-script-variable-not-defined/70978/5 "2023-03-13T18:22:23Z")

</div>

```auto
if Filter1Backwashing == 0 and Filter2Backwashing == 0:
	gal = 0

```

If this condition isn't met, your variable named gal never gets defined so later when you try to add to it, it throws you the error saying it's not defined.  
**`gal += ((loh_gpm * 5)/60)`**  
You need to have a way de define it when it doesn't meet your first condition.

---

<div class="post-metadata">

**Author:** ![Christopher\_Smith](https://avatars.discourse-cdn.com/v4/letter/c/ce7236/32.png) [@Christopher\_Smith](https://forum.inductiveautomation.com/u/Christopher_Smith)\
**Post date:** [March 13, 2023, 6:24pm UTC](https://forum.inductiveautomation.com/t/gateway-script-variable-not-defined/70978/6 "2023-03-13T18:24:52Z")

</div>

So how can I define it without my defining it overriding the count?

---

<div class="post-metadata">

**Author:** ![Dalton\_Adams](https://avatars.discourse-cdn.com/v4/letter/d/7c8e57/32.png) [@Dalton\_Adams](https://forum.inductiveautomation.com/u/Dalton_Adams)\
**Post date:** [March 13, 2023, 6:29pm UTC](https://forum.inductiveautomation.com/t/gateway-script-variable-not-defined/70978/7 "2023-03-13T18:29:56Z")

</div>

You could make a tag.

---

<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:** [March 13, 2023, 6:31pm UTC](https://forum.inductiveautomation.com/t/gateway-script-variable-not-defined/70978/8 "2023-03-13T18:31:34Z")

</div>

Do you have a tag for backwash gallons?

---

<div class="post-metadata">

**Author:** ![Christopher\_Smith](https://avatars.discourse-cdn.com/v4/letter/c/ce7236/32.png) [@Christopher\_Smith](https://forum.inductiveautomation.com/u/Christopher_Smith)\
**Post date:** [March 13, 2023, 6:31pm UTC](https://forum.inductiveautomation.com/t/gateway-script-variable-not-defined/70978/9 "2023-03-13T18:31:47Z")

</div>

Well that would get me around that. Thanks.

---

<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:** [March 13, 2023, 6:35pm UTC](https://forum.inductiveautomation.com/t/gateway-script-variable-not-defined/70978/10 "2023-03-13T18:35:45Z")

</div>

Let's clean this up a bit

```python
# read mulitple tags at once for much better performance
filterPaths = ["[default]MainBuilding/Bits/Unit1BackwashInitiateRun","[default]MainBuilding/Bits/Unit2BackwashInitiateRun"]

backwashingFilters = system.tag.readBlocking(filterPaths)
Filter1Backwashing = backwashingFilters[0].value
Filter2Backwashing = backwashingFilters[1].value

dt = system.date.now()
date = system.date.format(dt, 'MM/dd/YYYY')
time = system.date.format(dt, 'HH:mm:ss')

# setup a default value for 'gal' variable
gal = 0

# Override the 'gal' variable when appropriate and run query
if Filter1Backwashing > 0:
	readPaths = [
		"[default]MainBuilding/Control/Backwash1DailyInstance", "[default]Flow/MainFilter1_LOH_GPM/ActualValue", "[default]MainBuilding/Control/Finished1NTU_AtBackwashStart",
		"[default]MainBuilding/Control/LOH1Before", "[default]MainBuilding/Control/RewashTime", "[default]Turbidity/MainFilteredWater1Turbidity/ActualValue"
	]
	readValues = system.tag.readBlocking(readPaths)
	dailyInstance = readValues[0].value
	loh_gpm = readValues[1].value
	ntu_before = readValues[2].value
	loh_before = readValues[3].value
	rewash_time = readValues[4].value
	ntu = readValues[5].value
	
	gal += ((loh_gpm * 5)/60)
	
	query = "INSERT INTO Backwash1 (BackwashGallons, NTU_Before, LOH_Before, RewashTime, NTU) VALUES (?,?,?,?,?)"
	args= [loh_gpm, ntu_before, loh_before, rewash_time, ntu]
	system.db.runPrepUpdate(
		query,
		[date, time, dailyInstance, loh_gpm, gal, ntu_before, loh_before, rewash_time, ntu],
		'Wilderness_WTP_SCADA'
	)

```

YMMV since I don't have the full picture of the situation.

---

<div class="post-metadata">

**Author:** ![Christopher\_Smith](https://avatars.discourse-cdn.com/v4/letter/c/ce7236/32.png) [@Christopher\_Smith](https://forum.inductiveautomation.com/u/Christopher_Smith)\
**Post date:** [March 13, 2023, 7:04pm UTC](https://forum.inductiveautomation.com/t/gateway-script-variable-not-defined/70978/11 "2023-03-13T19:04:55Z")

</div>

Thanks, I was looking to have gal increment to record the number of gallons for end of cycle and have a running total for the number of gallons used during the cycle. Looks like it might be easier to use a tag to keep up with it as I as trying to not have gal = 0 anywhere that would override the count while in process.
