Error while executing the Tag Event Script

Hello Team,

I have written some script on one Tag where I am writing the data to SQL through this event, but unfortunately, it has some error now suddenly so anyone could help me to figure this issue.

I have also attached the screenshot for the details.

Thanks,
Kartik

Hi @kartik.i, do you have a screenshot or can you post the code for the valueChanged script and the CarTable script?

if currentValue.value == 1:
	# Run on every push
	def CarTable(table, Rtable, row, Treatment, Len):
		import system
		oldtable = Rtable
		CarData = system.tag.read("[.]../CarData").value
		for i in range(27):
			Rtable = system.dataset.setValue(Rtable, i,0,(table.getValueAt(row,i)))
			newCarFLG = 0
			for m in range(oldtable.getRowCount()):
				if Rtable.getValueAt(i,0) == oldtable.getValueAt(m,0):
					Rtable = system.dataset.setValue(Rtable, i,1,(oldtable.getValueAt(m,1)))
					newCarFLG = 1
			if newCarFLG ==0:
				Rtable = system.dataset.setValue(Rtable, i,1,0)

May be 1

			Flag = 0
			for j in range(table.getColumnCount()):
				if table.getValueAt(row,j) > 0 and table.getValueAt(row,j) == Rtable.getValueAt(i,0) and Rtable.getValueAt(i, 1) <= Len:
					Rtable = system.dataset.setValue(Rtable, i,1,(Rtable.getValueAt(i, 1) + 1))
					Flag = 1	
			if Flag == 0 and (table.getValueAt(row,i) > 0 or Rtable.getValueAt(i, 1) > Len):
				Rtable = system.dataset.setValue(Rtable, i,1,1)
			elif Flag == 0:
				Rtable = system.dataset.setValue(Rtable, i,1,0)
			for k in range(CarData.getRowCount()):
				if CarData.getValueAt(k,0) == Rtable.getValueAt(i,0):
					if CarData.getValueAt(k,11) == 1:
						cntr = CarData.getValueAt(k,12)
					else:
						cntr = CarData.getValueAt(k,13)
					if CarData.getValueAt(k,10) in (0,1,2):
						dbWt = CarData.getValueAt(k,1)
					elif CarData.getValueAt(k,10) in (3,4):
						dbWt = CarData.getValueAt(k,2)
					else:
						dbWt = CarData.getValueAt(k,3)
						
					Rtable = system.dataset.setValue(Rtable, i, 2, int(CarData.getValueAt(k,4)))
					Rtable = system.dataset.setValue(Rtable, i, 3, CarData.getValueAt(k,Treatment))
					Rtable = system.dataset.setValue(Rtable, i, 4, int(CarData.getValueAt(k,5)* cntr))
					Rtable = system.dataset.setValue(Rtable, i, 5, int(CarData.getValueAt(k,5)* cntr) * dbWt)
					if i < 27 and Rtable.getValueAt(0,0) != oldtable.getValueAt(0,0) :
						Rtable = system.dataset.setValue(Rtable, i+1, 6, oldtable.getValueAt(i,6))
		return Rtable
	
	
	t = system.tag.read("[.]../KilnDryerCars").value
	#For Kiln 1
	r = system.tag.read("[.]Kiln1CarData").value
	K1 =  CarTable(t, r, 1, 8, 104)
	system.tag.write("[.]Kiln1CarData", K1)
	
	#For Kiln 2

r = system.tag.read("[.]Kiln2CarData").value

K2= CarTable(t, r, 3, 9, 107)

system.tag.write("[.]Kiln2CarData", K2)

	from time import sleep
	sleep(5)
	system.tag.write("[.]KilnTrigger", 2)
	system.tag.write("[.]KilnRecord", 1)

Hi @matthew.ayre, thanks for the reply this is the below script which i have used.
if currentValue.value == 1:
# Run on every push
def CarTable(table, Rtable, row, Treatment, Len):
import system
oldtable = Rtable
CarData = system.tag.read("[.]…/CarData").value
for i in range(27):
Rtable = system.dataset.setValue(Rtable, i,0,(table.getValueAt(row,i)))
newCarFLG = 0
for m in range(oldtable.getRowCount()):
if Rtable.getValueAt(i,0) == oldtable.getValueAt(m,0):
Rtable = system.dataset.setValue(Rtable, i,1,(oldtable.getValueAt(m,1)))
newCarFLG = 1
if newCarFLG ==0:
Rtable = system.dataset.setValue(Rtable, i,1,0)

May be 1

			Flag = 0
			for j in range(table.getColumnCount()):
				if table.getValueAt(row,j) > 0 and table.getValueAt(row,j) == Rtable.getValueAt(i,0) and Rtable.getValueAt(i, 1) <= Len:
					Rtable = system.dataset.setValue(Rtable, i,1,(Rtable.getValueAt(i, 1) + 1))
					Flag = 1	
			if Flag == 0 and (table.getValueAt(row,i) > 0 or Rtable.getValueAt(i, 1) > Len):
				Rtable = system.dataset.setValue(Rtable, i,1,1)
			elif Flag == 0:
				Rtable = system.dataset.setValue(Rtable, i,1,0)
			for k in range(CarData.getRowCount()):
				if CarData.getValueAt(k,0) == Rtable.getValueAt(i,0):
					if CarData.getValueAt(k,11) == 1:
						cntr = CarData.getValueAt(k,12)
					else:
						cntr = CarData.getValueAt(k,13)
					if CarData.getValueAt(k,10) in (0,1,2):
						dbWt = CarData.getValueAt(k,1)
					elif CarData.getValueAt(k,10) in (3,4):
						dbWt = CarData.getValueAt(k,2)
					else:
						dbWt = CarData.getValueAt(k,3)
						
					Rtable = system.dataset.setValue(Rtable, i, 2, int(CarData.getValueAt(k,4)))
					Rtable = system.dataset.setValue(Rtable, i, 3, CarData.getValueAt(k,Treatment))
					Rtable = system.dataset.setValue(Rtable, i, 4, int(CarData.getValueAt(k,5)* cntr))
					Rtable = system.dataset.setValue(Rtable, i, 5, int(CarData.getValueAt(k,5)* cntr) * dbWt)
					if i < 27 and Rtable.getValueAt(0,0) != oldtable.getValueAt(0,0) :
						Rtable = system.dataset.setValue(Rtable, i+1, 6, oldtable.getValueAt(i,6))
		return Rtable
	
	
	t = system.tag.read("[.]../KilnDryerCars").value
	#For Kiln 1
	r = system.tag.read("[.]Kiln1CarData").value
	K1 =  CarTable(t, r, 1, 8, 104)
	system.tag.write("[.]Kiln1CarData", K1)
	
	#For Kiln 2

r = system.tag.read("[.]Kiln2CarData").value

K2= CarTable(t, r, 3, 9, 107)

system.tag.write("[.]Kiln2CarData", K2)

	from time import sleep
	sleep(5)
	system.tag.write("[.]KilnTrigger", 2)
	system.tag.write("[.]KilnRecord", 1)

Thanks,
Kartik

Is this at line 45 in the code? The error is stating that row 27 doesn’t exist. Can you confirm that your table/dataset has a total of 28 rows?

Thanks for the quick reply,

I have checked from my side and we have 28 rows.

Can you confirm which bit of the code is line 45?

Hi @matthew.ayre i couldn’t get your point.

The error code states that an invalid row index “27” was provided on line 45 of the CarTable script.

What code is on line 45 of the car table script?

1 Like

You really need to edit your post and surround your code within preformatted blocks. It’s extremely difficult/impossible to read as it is.

Wrap your code in 3x back ticks (```) or select all of your code and press this icon: image

E.g.

This = code
if IUsePreformattedBlock:
   ThenMyFormatting = Remains
2 Likes

Hello @lrose thanks for your reply I am updating the dataset value.

Hello @nminchin i am bit confused how to do this part.

image

This tells you exactly where the issue is. I.e. the error is within your function CarTable. The error is on line 45 of the script where this function is defined. The error is that you’re trying to reference a row in a list (presumably) that doesn’t exist.

Copy your script again into a post, then select it all and click on this button within the post formatting toolbar:
image

image

Hello @nminchin Actually I have done the same thing for two things in which one is working fine but i don’t know whats the issue with this thing.

Hello @nminchin this is the script

	if currentValue.value == 1:
		# Run on every push
		def CarTable(table, Rtable, row, Treatment, Len):
			import system
			oldtable = Rtable
			CarData = system.tag.read("[.]../CarData").value
			for i in range(27):
				Rtable = system.dataset.setValue(Rtable, i,0,(table.getValueAt(row,i)))
				newCarFLG = 0
				for m in range(oldtable.getRowCount()):
					if Rtable.getValueAt(i,0) == oldtable.getValueAt(m,0):
						Rtable = system.dataset.setValue(Rtable, i,1,(oldtable.getValueAt(m,1)))
						newCarFLG = 1
				if newCarFLG ==0:
					Rtable = system.dataset.setValue(Rtable, i,1,0)
#					May be 1
				Flag = 0
				for j in range(table.getColumnCount()):
					if table.getValueAt(row,j) > 0 and table.getValueAt(row,j) == Rtable.getValueAt(i,0) and Rtable.getValueAt(i, 1) <= Len:
						Rtable = system.dataset.setValue(Rtable, i,1,(Rtable.getValueAt(i, 1) + 1))
						Flag = 1	
				if Flag == 0 and (table.getValueAt(row,i) > 0 or Rtable.getValueAt(i, 1) > Len):
					Rtable = system.dataset.setValue(Rtable, i,1,1)
				elif Flag == 0:
					Rtable = system.dataset.setValue(Rtable, i,1,0)
				for k in range(CarData.getRowCount()):
					if CarData.getValueAt(k,0) == Rtable.getValueAt(i,0):
						if CarData.getValueAt(k,11) == 1:
							cntr = CarData.getValueAt(k,12)
						else:
							cntr = CarData.getValueAt(k,13)
						if CarData.getValueAt(k,10) in (0,1,2):
							dbWt = CarData.getValueAt(k,1)
						elif CarData.getValueAt(k,10) in (3,4):
							dbWt = CarData.getValueAt(k,2)
						else:
							dbWt = CarData.getValueAt(k,3)
							
						Rtable = system.dataset.setValue(Rtable, i, 2, int(CarData.getValueAt(k,4)))
						Rtable = system.dataset.setValue(Rtable, i, 3, CarData.getValueAt(k,Treatment))
						Rtable = system.dataset.setValue(Rtable, i, 4, int(CarData.getValueAt(k,5)* cntr))
						Rtable = system.dataset.setValue(Rtable, i, 5, int(CarData.getValueAt(k,5)* cntr) * dbWt)
						if i < 27 and Rtable.getValueAt(0,0) != oldtable.getValueAt(0,0) :
							Rtable = system.dataset.setValue(Rtable, i+1, 6, oldtable.getValueAt(i,6))
			return Rtable
		
		
		t = system.tag.read("[.]../KilnDryerCars").value
		#For Kiln 1
		r = system.tag.read("[.]Kiln1CarData").value
		K1 =  CarTable(t, r, 1, 8, 104)
		system.tag.write("[.]Kiln1CarData", K1)
		
		#For Kiln 2
#		r = system.tag.read("[.]Kiln2CarData").value
#		K2=  CarTable(t, r, 3, 9, 107)
#		system.tag.write("[.]Kiln2CarData", K2)
		
		from time import sleep
		sleep(5)
		system.tag.write("[.]KilnTrigger", 2)
		system.tag.write("[.]KilnRecord", 1)

Thanks,
Kartik

Thanks :slight_smile:
Can you also send a screenshot of the script? Include the script editor window as well

Hello @nminchin I have attached the screenshot as required.

if i < 27 and Rtable.getValueAt(0,0) != oldtable.getValueAt(0,0) :
	Rtable = system.dataset.setValue(Rtable, i+1, 6, oldtable.getValueAt(i,6))

try if i<26

2 Likes

Thanks. this is line 45 where the issue is:

Jordan’s on the money, as always. The reason is that range(n) returns a list of 0 to n-1, not 0 to n

Also, it would be better to use a variable for this value as well, not simply “26” so that you don’t have magic numbers everywhere that don’t have a single source of truth

2 Likes

Couple of notes,

Instead of multiple calls to system.tag.write(), make a single call to system.tag.writeAll()

Also, you shouldn’t use sleep() in a valueChanged script. (I would go so far as to say you shouldn’t use it anywhere, but there are some specific uses that do make since.)

2 Likes