Gateway script not inserting records in my table

I am tring to insert records for previous_efficiency into my table but only one row which is Twister 1 is inserting into the table. Is there something wrong Iam doing?

if system.tag.readBlocking("[default]zzzTimezzz/Twisting Data Write 810am 810pm")[0].value == True:

	line = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62]
	#quad = [A,B,C,D]
	
	for i in line:
		tagPathLeft_Eff = "[default]Twisters/TW" + str(i) +"/Left/Shift_2/Previous_Efficiency"
		tagPathRight_Eff = "[default]Twisters/TW" + str(i) +"/Right/Shift_2/Previous_Efficiency"
		Left_Eff = system.tag.readBlocking(tagPathLeft_Eff)[0].value
		Right_Eff = system.tag.readBlocking(tagPathRight_Eff)[0].value
		Twisters = i
		DateTime = system.date.now()
		Shift = system.tag.readBlocking("[default]Twisters/Previous Shift")[0].value[0]
		 
		
	    # Use a dictionary to map supervisor names based on twisters and Shift
	  	Supervisor_mapping = {
	  		        ('J', True): 'Carol Brookeshire' if Twisters <= 32 else 'Lind Smith',
	  		        ('K', True): 'Andrew Frazier' if Twisters <= 32 else 'Tracy Presley',
	  		        ('L', True): 'Sandra McIntyre' if Twisters <= 33 else 'Tim Foley',
	  		        ('M', True): 'Juanita Gordon' if Twisters <= 33 else 'Jan Kelly',
	  		    }
	  		    
	  	# Get supervisor name from the dictionary based on Shift and Twisters	
	  	Supervisor = Supervisor_mapping.get((Shift, True), 'Unknown Supervisor')
	  	
	  	system.db.runPrepUpdate("INSERT INTO Plt65_NightShift_Twisting_Efficiencies (Twisters, DateTime, Shift, Supervisor, Left_Eff, Right_Eff) VALUES(?,?,?,?,?,?)", [Twisters, DateTime, Shift, Supervisor, Left_Eff, Right_Eff], 'IgnitionHUD')
	  	logger.info("NIGHT SHIFT EXECUTED")

I can't be certain due to forum formatting, but when I post your code into Notepad++ and turn on show tabs and spaces I can see inconsistent indentation. From the # Use a dictionary ... onwards each line starts with two spaces followed by a tab.
Try retabbing those lines. (Select, Shift-tab to outdent to the left margin and then tab again.)

1 Like

This is on a gateway script? Do you see NIGHT SHIFT EXECUTED for your logger in your gateway logs? If not you're getting an error somewhere, either in the query or somewhere earlier. If Twister 1 works, then I would imagine you see NIGHT SHIFT EXECUTED once in your gateway logs, and then another log after it with an error (if Twister 2 is erroring out). Check your gateway logs and look for your logger name ( you may want to put a few more logger.info's elsewhere in the event its never getting to that last line and can make it harder to find in your gw logs). I don't see anything obviously wrong though.

However it can be tricky please format your code with either ``` ``` before and after the entire code block or use
image
Indenting is how python controls the flow of code so it's vital we see how it really is.

2 Likes

Hmph! I didn't even notice the first line due to the formatting error.

2 Likes

I fixed the formatting in the post. Definitely looks like some weird mixed indentation, which could be the cause of the issue, however, I'd expect it to throw an error about unbound locals if it's being interpreted like this?

if system.tag.readBlocking("[default]zzzTimezzz/Twisting Data Write 810am 810pm")[0].value == True:

	line = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62]
	#quad = [A,B,C,D]
	
	for i in line:
		tagPathLeft_Eff = "[default]Twisters/TW" + str(i) +"/Left/Shift_2/Previous_Efficiency"
		tagPathRight_Eff = "[default]Twisters/TW" + str(i) +"/Right/Shift_2/Previous_Efficiency"
		Left_Eff = system.tag.readBlocking(tagPathLeft_Eff)[0].value
		Right_Eff = system.tag.readBlocking(tagPathRight_Eff)[0].value
		Twisters = i
		DateTime = system.date.now()
		Shift = system.tag.readBlocking("[default]Twisters/Previous Shift")[0].value[0]
		 
		
	# Use a dictionary to map supervisor names based on twisters and Shift
	Supervisor_mapping = {
				('J', True): 'Carol Brookeshire' if Twisters <= 32 else 'Lind Smith',
				('K', True): 'Andrew Frazier' if Twisters <= 32 else 'Tracy Presley',
				('L', True): 'Sandra McIntyre' if Twisters <= 33 else 'Tim Foley',
				('M', True): 'Juanita Gordon' if Twisters <= 33 else 'Jan Kelly',
			}
			
	# Get supervisor name from the dictionary based on Shift and Twisters	
	Supervisor = Supervisor_mapping.get((Shift, True), 'Unknown Supervisor')
	
	system.db.runPrepUpdate("INSERT INTO Plt65_NightShift_Twisting_Efficiencies (Twisters, DateTime, Shift, Supervisor, Left_Eff, Right_Eff) VALUES(?,?,?,?,?,?)", [Twisters, DateTime, Shift, Supervisor, Left_Eff, Right_Eff], 'IgnitionHUD')
	logger.info("NIGHT SHIFT EXECUTED")
2 Likes

Thank you so much PG.

I realized that the code logger was not defined and that's why it was acting up. I am not entirely sure if that was the main issue but after I removed it, it worked pretty fine.