ZeroDivisionError: Float division

Good Morning. my name is Felipe. I created a window to monitor spoilage fluctuation in real time on a Chart component. I wrote the script on the property change in a mes analisys controller component that provides me with a dataset with production data input and output from a machine minute by minute and from these values ​​I created a script to record in the Chart component. However, I have encountered two problems: the first is that sometimes the values ​​of the Chart component do not update to today’s date, as in the attached photo. today is 09/15/2020, but it is registering the spoilage fluctuation of 09/02/2020. but when I look at the dataset of the mes analysis controller, it continues to provide the minute-to-minute values ​​of today. and the second problem is this popup window saying: ZeroDivisionError: Float division. I’ve exhausted my attempts. Below is the script I wrote in the scripting editor >> property change of the mes analisys controller:

if event.propertyName == 'data' and event.source.data is not None:
	dataset = event.source.data
	production1 = 0  //Infeed Machine
	production2 = 0 //Outfeed Machine
	target = 0
	newDataSet = []
	headers = ["Time", "Spoilage", "ideal Spoilage"]
	for row in range(dataset.rowCount):
		date = dataset.getValueAt(row, 0)
		target = 3 + event.source.LineRate
		production1 = production1 + dataset.getValueAt(row, 1)
		production2 = production2 + dataset.getValueAt(row, 2)
		spoilage = (production1-production2)*1.00
		spoilPercent = (spoilage / (production1 * 1.00))*100.00
		newRow = [date,spoilPercent, target]
		newDataSet.append(newRow)
	event.source.parent.getComponent('Chart').Data  = system.dataset.toDataSet(headers, newDataSet)

image

Thank you!!!

I’d say some of your values for production1 are zero. You need to decide what value you want for spoilPercent when that happens:

if event.propertyName == 'data' and event.source.data is not None:
	dataset = event.source.data
	production1 = 0  //Infeed Machine
	production2 = 0 //Outfeed Machine
	target = 0
	newDataSet = []
	headers = ["Time", "Spoilage", "ideal Spoilage"]
	for row in range(dataset.rowCount):
		date = dataset.getValueAt(row, 0)
		target = 3 + event.source.LineRate
		production1 = production1 + dataset.getValueAt(row, 1)
		production2 = production2 + dataset.getValueAt(row, 2)
		spoilage = (production1-production2)*1.00
		if production1 == 0
			# Set default spoilPercent value if production1 = 0
			spoilPercent = 0
		else:
			spoilPercent = (spoilage / (production1 * 1.00))*100.00
		newRow = [date,spoilPercent, target]
		newDataSet.append(newRow)
	event.source.parent.getComponent('Chart').Data  = system.dataset.toDataSet(headers, newDataSet)```
1 Like

You are spectacular. worked perfectly. the two errors I mentioned have been resolved. thank you very much!!!

1 Like