Shared script runs in console but not as Gateway event script

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! :confounded:
Can anyone spot what’s wrong here?

Are you passing the tag name with the provider?
ie.
[default]Path/To/Tag/TagName

1 Like

Yes, in fact it MUST be fully qualified or the historian refuses to yield anything, to wit:


# Try it... 
date = system.date.addDays(system.date.now(),-1) 
date = system.date.midnight(date) 
used = calcTankUsage("[/.rpmtagprovider]friona/tank_level",date) #take 24 hr default 
print used

That said, I’m still at a loss as to why those variables lose their type from one line to the next!

With it saying a type for line 41, the only thing I noticed is change is one of the few variables you didn’t set an initial value for at the top. I would think it should still be able to create it where it is and be fine but I’d try adding an initial setting to it and see what it does. It doesn’t make sense that it would work in the script console but not in the shared scripts.

I just copied your code, called it using one of our history tags and it returned the value.
Are you sure that tag provider name is correct?

Yup, it’s the right tag path - it works great in the script console!
I’d agree with BP 100% -that makes no sense that it loses types when you run it as a Gateway script! The offending variables are lastLevel & startLevel and I did add those assignments the first time I got this error… didn’t help.

Could both of those be coming back as None from your query?
Put a check for lastLevel is None and thisLevel is None

I don’t quite follow your suggestion…
The error is that the variables are of “NoneType” rather than the floats they started out as, not that they contain null.
But, As it works fine in the script console, I’ll put some print statements to the wrapper log in and see if I can determine when they go south.

Update: they do indeed equal None, but after the first iteration, i.e. it works once??? Thanks for aa new direction to investigate!

Solved!
MMaynardUSG was certainly on the right track - Thank you, Thank you!
The provider name was sufficient for local use, but, at least in this case, it needed a FULLY specified provider name:
[LocalSQL/ignition-azure:RPMTagProvider]
where LocalSQL is the database connection that “houses” the history and is the recipient of the custom Tag Driving provider RPMTagProvider,
and Ignition-azure is the name of our Gateway that is itself housed on a VM in Azure cloud.

2 Likes