Numeric Label Background Color

from java.awt import Color
if event.propertyName == "componentRunning" and event.newValue:
  
		
		if pastDueRL_res == None or pastDueSO_res == None or CPCP_res == None:
			system.db.rollbackTransaction(transactionID)
			print("Transaction Failed! Rolling back...")
		
		system.db.commitTransaction(transactionID)
		print("Transaction Succeeded!")
		system.db.closeTransaction(transactionID)
		print("Edning Transaction...")
		
		print("========= rl ==========")
		for row in pastDueRL_res:
			print(' '.join(map(str, row)))
		
		machines = event.source.Machines
		pyMachines = system.dataset.toPyDataSet(machines)
		# first accessor will be different per button, param4 = assetno
		machinesLen = len(pyMachines)
		tableData = []

		# Preprocess results
		pastDueRL_dict = {}
		pastDueSO_dict = {}
		CPCP_dict = {}
		
		# Preprocess pastDueRL_res
		for row in pastDueRL_res:
			assetNo = row[4]
			hours = row[3]
			if assetNo not in pastDueRL_dict:
				pastDueRL_dict[assetNo] = 0.0
			pastDueRL_dict[assetNo] += hours/row[2]
		
		# Preprocess pastDueSO_res
		for row in pastDueSO_res:
			assetNo = row[4]
			hours = row[3]
			if assetNo not in pastDueSO_dict:
				pastDueSO_dict[assetNo] = 0.0
			pastDueSO_dict[assetNo] += hours/row[2]
		
		# Preprocess CPCP_res
		for row in CPCP_res:
			assetNo = row[4]
			if assetNo not in CPCP_dict:
				CPCP_dict[assetNo] = 0.0
			CPCP_dict[assetNo] += row[3] / row[2]
		
		# Main loop: Iterate through each machine
		for i in range(machinesLen):
#			currRow = ['', '', 0.0, 0.0, 0.0, 0.0] <- RCCIG-106
			currRow = ['', '', 0.0, 0.0, 0.0]
			name = pyMachines[i][1]
			currRow[0] = name
			assetNo = pyMachines[i][0]
			currRow[1] = str(assetNo)
		
			# Use precomputed dictionaries for constant-time lookups
			currRow[2] = pastDueRL_dict.get(assetNo, 0.0)  # Sum from pastDueRL_res
			currRow[3] = pastDueSO_dict.get(assetNo, 0.0)  # Sum from pastDueSO_res
			currRow[4] = CPCP_dict.get(assetNo, 0.0)       # Sum of ratios from CPCP_res <- RCCIG-106 used to be 5 instead of 4

			# row now complete
			tableData.append(currRow)
			
	
		totalCPCP = sum(row[4] for row in tableData)
		#print(totalCPCP)
		yellowThreshold = 5100 * machinesLen
		redThreshold = 7100 * machinesLen
		CPCPLabel = event.source.getComponent('Row8').getComponent('Numeric Label 1')
		print(event.source.getComponent('Row8').getComponent('Numeric Label 1').background)
	
		if totalCPCP > redThreshold:
			CPCPLabel.background= Color(255,0,0) 
        elif totalCPCP > yellowThreshold:
        	CPCPLabel.background= Color(255,255,0) 
        else:
        	CPCPLabel.background = Color(250,250,251) 

#		headers = ["Name", "Asset Number", "Past Due (RL)", "Past Due (SO)", "1 Year Forecast w/ Batching", "1 Year Forecast (CPCP)"] <- RCCIG-106
		headers = ["Name", "Asset Number", "Past Due (RL)", "Past Due (SO)", "1 Year Forecast (CPCP)"]
		dataset = system.dataset.toDataSet(headers, tableData)
		
		event.source.getComponent('Power Table').data = dataset
	event.source.getComponent('LoadingGraphic').visible = False

So I have a script for a template property change event and I am trying to set the background color for the numeric label based on the value of the "totalCPCP". I learned that the background color for Numeric Label returns java.awt.Color[r=222,g=223,b=222] from the console.

Currently, the power table in the template does not display data but the numeric label does display data but not the background color

Any ideas how to initialize the background colors?

You can just bind the background color of the numeric label to it's own value and then set the color based on the range.

Yes, that is what I initially had but this is a template and I am trying to set the color to be based on the number of machines in the window itself. For example RED- if it is 7100* machine count, YELLOW- 5100 * machine count.

I need the background to be dynamic based on the machine count.

Create a custom property on the template which holds the machine count. Then you can bind the background color to that value, as @MMaynard has shown.

I would have the script write that value to a custom property on the template, and bind the background color to that instead of the value then.

Sounds good, that makes sense. But is there a reason the custom property method is preferred over the script? I am new to this and trying to learn as well

Scripts are much more intensive vs native expression bindings.

And the expression will trigger on any change of the custom properties value, so if you designed your interface so that the values could be modified, edited, etc. by the operator, it would automatically update when the value changes vs having to rerun the script.

Scripts are a powerful tool, but they come with a cost, not the least of which is a massive footgun. General rule of thumb with Ignition is that a script should be your last resort.

For instance, I would move your queries to named quries, and configure custom properties to hold the result of those queries. Then I would use property and expression bindings to do all of the other work.

I obviously don't have an understanding of exactly what you're ultimately trying to accomplish, but I don't see anything in that script which requires a script to accomplish.

The fact that you have a "LoadingGraphic" suggest that you've already run into some of the downfalls of scripting.

In this instance, you're doing a whole lot of proccessing on the EDT (Event Dispatch Thread) and that will lock up the client until it is done.