I developed this script in the script console and it runs just as I want it to, BUT saving it as a Shared script function and calling it from a tag-change Gateway event script I get the screwy-est error:
line 41 in calcTankUsage TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'
def calcTankUsage(tagName, endDateTime = system.date.now(), spanInHours=24):
# supply a tagname of a tank level with fully qualified tagprovider
# such as "[/.rpmtagprovider]friona/tank_level"
# and the END date/time of the period you'd like to calculate
# and how many hours backwards from the enddate you want to start pulling data
# Note defaults of the last 24 hours
tp=[tagName]
thisLevel = 0.0
lastLevel = 0.0
startLevel = 0.0
span15Minutes = spanInHours * 4 # convert from hours to 15 minute increments
usage = 0.0
using = True
endTime = endDateTime
# print "EndTime", endTime
for h in range(span15Minutes):
startTime = system.date.addMinutes(endTime, -15)
#print endTime
x = system.tag.queryTagCalculations(tp,calculations=["LastValue"], startDate=startTime, endDate=endTime)
endTime = system.date.addMinutes(endTime, -15) #move time back 15 minutes for next loop
#print x.getValueAt(0,1)
lastLevel = thisLevel #save the value before overwriting it
thisLevel=x.getValueAt(0,1)
if h==0:
startLevel = thisLevel
else:
if thisLevel >= lastLevel: #then we're using chemical normally - or it's the 2nd pass (less than a million)
if not using: #then we JUST transitioned from going down to on our way back up
startLevel = lastLevel # so, grab a new starting point
# print "New Start: ", startLevel, endTime
using = True
lastLevel = thisLevel
else: # if it went down then they are filling the tank (remember that we are going backwards in time)
diff = lastLevel-thisLevel # Check to make sure that it wasn't just a blip in the graph > 350 gallons
if using and (diff>350): # stop and add in the difference from the starting level NOTE this won't work if it's in feet
change = lastLevel - startLevel
usage += change
using = False
change = lastLevel - startLevel
usage += change
return usage
line 41 is the third from the last: change = lastLevel - startLevel
This has stumped me as there isn't any reference to a tag or a file or anything that would be different on the Gateway as opposed to my designers script console - it's just subtraction! You'd think I could manage that much without crashing!
Can anyone spot what's wrong here?