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?
Post the script and format it with the </>
button
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')
You are tying to add to gal withoit defining it first. Where is it supposed to come from?
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.
So how can I define it without my defining it overriding the count?
You could make a tag.
Do you have a tag for backwash gallons?
Well that would get me around that. Thanks.
Let's clean this up a bit
# 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.
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.