Best Practices for Querying Tag History Data [SOLVED]

The code will run much faster if you don't split these up. Read all of them at once. Consider using a project script module top level variable to hold the constructed list of tag paths, so that part won't have to execute every time. (I strongly recommend placing all gateway event code in a project script module, leaving just a one-line function call in the event itself.)

Something like this:

# In a script, "bots", perhaps

# Base path and channel names
basepath = "[default]Alphabot/05 Bot Status/Bot"
basetags = ["/ConnectionSTS", "/InductionSTS", "/SafetySTS"]

tagpaths = [basepath+str(i)+t for i in range(1, 201) for t in basetags]
headers = ["Alphabot", "Connected", "Inducted", "Safety_OK"]
write_path = "[default]Alphabot/05 Bot Status/bot_status_summary_table"

def updateBots():
	values = [x.value for x in system.tag.readBlocking(tagpaths)]
	enumerated_triplets = zip(range(1, 201), *([iter(values)]*3))
	data = []
	for i, conx, induct, safe in enumerated_triplets:
		if induct:
			data.append([i, conx, induct, safe])
	system.tag.writeBlocking([write_path], [system.dataset.toDataSet(headers, data)])

Then the event just has:

bots.updateBots()
2 Likes