system.tag.queryTagHistory showing blanks when running through gateway script

I have this script:

def getCSVItem1():
	#if newValue.getValue() == True:
	import csv
	from java.util import Date
	
	runstartTime = system.tag.readBlocking("[default]SystemA/Item1/IgnitionValues/RunTimes/RunStartTime")
	runstartTime = runstartTime[0].value
	
	# Define the start and end times for the query
	previousRunStartTime = system.tag.read("SystemA/Item1/IgnitionValues/RunTimes/PreviousRunStartTime").value
	RunEndTime = system.tag.read("SystemA/Item1/IgnitionValues/RunTimes/RunEndTime").value
	
	# Define the tag paths
	tagPaths = [
	    "SystemA/Item1/Actual_Weight",
	    "SystemA/Item1/Machine_Status",
	    "SystemA/Item1/Maximum_Weight",
	    "SystemA/Item1/Minimum_Weight",
	    "SystemA/Item1/Open_Lid_Kickoff",
	    "SystemA/Item1/Preset_Number",
	    "SystemA/Item1/Standard_Deviation_LH",
	    "SystemA/Item1/Standard_Deviation_LL",
	    "SystemA/Item1/Tare",
	    "SystemA/Item1/Target_Weight",
	    "SystemA/Item1/Total_Scanned",
	    "SystemA/Item1/UnderWeight_Count",
	]
	
	# Extract the short names from the tag paths
	headers = ['Timestamp'] + [path.split('/')[-1] for path in tagPaths]
	
	# Query the tag history
	history = system.tag.queryTagHistory(paths=tagPaths, startDate=previousRunStartTime, endDate=RunEndTime)
	
	# Get the current date and time and format it to be safe for file names
	currentDateTime = system.date.format(system.date.now(), "yyyy-MM-dd_HH-mm-ss")
	
	# Define the output CSV file path with the formatted date and time
	outputFilePath = "C:\\Users\\Account1\\Documents\\Item1-" + currentDateTime + ".csv"
	
	# Write the history data to a CSV file
	with open(outputFilePath, 'wb') as csvfile:
	    writer = csv.writer(csvfile)
	    
	    # Write header row
	    writer.writerow(headers)
	
	    # Iterate over the dataset rows
	    for row in range(history.getRowCount()):
	        # Write timestamp and values for each tag
	        timestamp = history.getValueAt(row, 0)  # Assuming timestamp is in the first column
	        values = [history.getValueAt(row, col) for col in range(1, history.getColumnCount())]
	        writer.writerow([timestamp] + values)

If I run it using script console it works fine.

However when I run it using Gateway Script Console on a tag change it just returns a csv file with the header but after that its blank. Anyone have any ideas?

You're not using fully qualified paths consistently - e.g. [default]SystemA/Item1/X at the beginning of your code, but not using [default] elsewhere. Depending on where in the gateway you're running this from, there may not be a default tag provider to reference, so the paths are underspecified and point to nothing.

1 Like

I would also recommend some other refactors to your script - consolidating read operations, using builtin CSV formatting, reducing error prone string manipulations, etc:

def getCSV(provider="default", system="SystemA", item="Item1"):
	def qualify(path):
		return "[{}]{}/{}/{}".format(provider, system, item, path)
	
	timing = [
		"IgnitionValues/RunTimes/RunStartTime",
		"IgnitionValues/RunTimes/PreviousRunStartTime",
		"IgnitionValues/RunTimes/RunEndTime",
	]

	runstartTime, previousRunStartTime, runEndTime = [
		qv.value
		for qv in system.tag.readBlocking([qualify(path) for path in timing])
	]
	
	# Define the tag paths
	tagPaths = [
		"Actual_Weight",
		"Machine_Status",
		"Maximum_Weight",
		"Minimum_Weight",
		"Open_Lid_Kickoff",
		"Preset_Number",
		"Standard_Deviation_LH",
		"Standard_Deviation_LL",
		"Tare",
		"Target_Weight",
		"Total_Scanned",
		"UnderWeight_Count",
	]

	# Query the tag history
	history = system.tag.queryTagHistory(
		paths=[qualify(path) for path in tagPaths],
		startDate=previousRunStartTime,
		endDate=runEndTime,
		columnNames=tagPaths,
	)
	
	# Get the current date and time and format it to be safe for file names
	currentDateTime = system.date.format(system.date.now(), "yyyy-MM-dd_HH-mm-ss")
	
	# Define the output CSV file path with the formatted date and time
	outputFilePath = "C:/Users/Account1/Documents/{}-{}.csv".format(item, currentDateTime)
	
	csv = system.dataset.toCSV(history, True, False, False)
	system.file.writeFile(outputFilePath, csv)

Wow can't believe it was that simple. Thank you for that.