Reporting Tag Historian Query Error

I have given an error like this in my report:
WARN: Error invoking script.Traceback (most recent call last):
File "function:updateData", line 62, in updateData
TypeError: list indices must be integers

NOTE: Line 62 is the last line in the code.
Here is my code:

def updateData(data, sample):
	currentDate = system.tag.readBlocking(['[System]Gateway/CurrentDateTime'])[0].value
	currentYear = data['year']
	dateStart = system.date.addYears(system.date.parse(system.date.format(currentDate,currentYear + "-12-22 08:00:00"),'yyyy-MM-dd HH:mm:ss'),-1)
	endDate = system.date.addYears(system.date.parse(system.date.format(dateStart,currentYear + "-12-22 08:00:00"),'yyyy-MM-dd HH:mm:ss' ),1)
	data = []
	data1 = []
	dataAlarms = []
	header1 = ['Label','Jan','Feb','Mar','Apr','May','June','July','Aug','Sept','Oct','Nov','Dec']
	facilities = system.tag.readBlocking('[default]MQTT Facilities/Facilities')[0].value
	alarmProperties = ['eventTime',
	'label',
	'displayPath',
	'eventValue',
	'name',
	'Source']

	header = ['EventTime',
	'label',
	'DisplayPath',
	'eventValue',
	'State',
	'Source']
	count = 0
	count1 = 0
	for month in range(12):
		dataBuffer = []
		startDateAggre = system.date.addMonths(dateStart, month)
		endDateAggre = system.date.addMonths(startDateAggre, 1)
		alarmJournal = system.alarm.queryJournal(startDate = startDateAggre, endDate = endDateAggre)
		
		if len(alarmJournal) != 0:
			for alarm in alarmJournal:
				bufferData = []
				for prop in alarmProperties:
					propertyAlarm = alarm.get(prop)
					bufferData.append(propertyAlarm)
				data.append(bufferData)
			datasetFinal = system.dataset.toDataSet(header, data)
			dataAlarm = system.dataset.filterColumns(datasetFinal, [5])
			for row in range(dataAlarm.getRowCount()):
				dataLocation = str(dataAlarm.getValueAt(row,0))
				splitName = dataLocation.split('/')[1]
				splitName1 = splitName.split(':')[1]
				data1.append(splitName1)
			for location in range(facilities.getRowCount()):
				number = count
				label1 = facilities.getValueAt(location,0)
				x = label1.split('/')
				y = str(x[0])
				splitName = y.replace('Ground Water Reservoir - 1','GWR1')
				dataBuffer.append(y)
				count = count + 1
				for loc in dataBuffer:
					countList = [0,0,0,0,0,0,0,0,0,0,0,0]
					count1 += 1
					for i in range(len(data1)):
						if data1[i] == loc:
							countList[month] += 1
				dataAlarms.append([y] + countList)
	data['alarmFinal'] = system.dataset.toDataSet(header1, dataAlarms)

@justinedwards.jle can you help me with this?

@nminchin i saw you in one of the post here in forum with the same problem as me. Can you help me with this?

The variable data is a list, but you are using a string instead of an integer for an index:

data['alarmFinal']

'alarmFinal' is not correct

1 Like

What Justin said. You're treating the list as if it were a dictionary. You're also completely overwriting the data parameter that you're passing into the function without ever using it.
image


Also, unrelated, this is overly convoluted and doesn't look like it will work (why are you trying to fix the date? is this for testing?)

dateStart = system.date.addYears(system.date.parse(system.date.format(currentDate,currentYear + "-12-22 08:00:00"),'yyyy-MM-dd HH:mm:ss'),-1)
	endDate = system.date.addYears(system.date.parse(system.date.format(dateStart,currentYear + "-12-22 08:00:00"),'yyyy-MM-dd HH:mm:ss' ),1)

Simply use:

dateStart = system.date.addYears(currentDate, -1) # subtracts 1 year from the current date
dateEnd = system.date.addYears(currentDate, 1) # adds 1 year to the current date

If you want to set the month and day to specific values for some reason, you can use:

dateStart.setDate(22)
dateStart.setMonth(12-1) # sets to December, days start at 1, months start at 0... stupid Java... :face_with_symbols_over_mouth:
2 Likes

It must be a dictionary when it's passed in because this line doesn't bomb out:

currentYear = data['year']

It looks like this should be rewritten:

data = []
if len(alarmJournal) != 0:
	for alarm in alarmJournal:
		bufferData = []
		for prop in alarmProperties:
			propertyAlarm = alarm.get(prop)
			bufferData.append(propertyAlarm)
		data.append(bufferData)
	datasetFinal = system.dataset.toDataSet(header, data)

Change it to this, and see if that fixes the problem:

if alarmJournal:
	bufferedData = [[alarm.get(prop) for prop in alarmProperties] for alarm in alarmJournal]
	datasetFinal = system.dataset.toDataSet(header, bufferedData)

This simplifies the code with list comprehension and eliminates the need for two variables in your list of buffered lists. Consequently, you can use the variable bufferedData for your dataset data and not overwrite your dictionary.

Thanks! It works!