Calculate Kwh consumption and store it together with Kwh meter

  • Create the “HistorianTable” and “DiffTable” table objects.
  • Bind HistorianTable to tag history for the meters in question. The example below uses my meters A to E. (Modify to suit.)
  • Leave DiffTable blank.

Here’s the code for the “Historian” table, Event Handlers, propertyChange, propertyChange.

# Difference between dataset rows.
# This script takes the dataset from a table component and generates an array
# giving the timestamp and difference between rows.
# The original application has five main meters, A - E.

if event.propertyName == "data":
	# Pull the dataset property from a table component
	data = event.source.parent.getComponent("HistorianTable").data
	# Create an array for the output data.
	rowsOut = []
#	system.gui.messageBox(str(type(data.getValueAt(0, 1))))
	# Loop through the dataset, perform the diff calculation and add to the array.
	for row in range(data.rowCount - 1):
		# For 15 minute recording multiply the result by 4 to give meaningful kW.
		if data.getValueAt(row, 1) > 0:
			kWhA = (data.getValueAt(row + 1, 1) - data.getValueAt(row, 1)) * 4
		else:
			kWhA = 0

		if data.getValueAt(row, 2) > 0:
			kWhB = (data.getValueAt(row + 1, 2) - data.getValueAt(row, 2)) * 4
		else:
			kWhB = 0

		if data.getValueAt(row, 3) > 0:
			kWhC = (data.getValueAt(row + 1, 3) - data.getValueAt(row, 3)) * 4
		else:
			kWhC = 0

		if data.getValueAt(row, 4) > 0:
			kWhD = (data.getValueAt(row + 1, 4) - data.getValueAt(row, 4)) * 4
		else:
			kWhD = 0

		if data.getValueAt(row, 5) > 0:
			kWhE = (data.getValueAt(row + 1, 5) - data.getValueAt(row, 5)) * 4
		else:
			kWhE = 0

		kWhSum = kWhA + kWhB + kWhC + kWhD + kWhE 
		oneRow = [data.getValueAt(row, 0), kWhSum, kWhA, kWhB, kWhC, kWhD, kWhE]
		rowsOut.append(oneRow)
	# Create headers for the new table.
	headers = ["Timestamp", "Total", "Board A", "Board B", "Board C", "Board D", "Board E"]
	# Create the new dataset from the array.
	dataOut = system.dataset.toDataSet(headers, rowsOut)
	# Use our new dataset to fill in a Table
	chart = event.source.parent.getComponent("Chart")
	chart.Data = dataOut
	table = event.source.parent.getComponent("DiffTable")
	table.data = dataOut

Scan Class for on-the-hour and 15-minute logging
You seem to have it sorted out already but this may be of interest to others.

  • Create an expression tag “15 minute scan class trigger”, Boolean, Read/Write and have it run at your Default scan class.
  • In the Expression properties of the tag enter the following code:
//Turn on for one minute at 00, 15, 30 and 45 past the hour.
dateExtract(now(0), "minute") = 0
|| dateExtract(now(0), "minute") = 15
|| dateExtract(now(0), "minute") = 30
|| dateExtract(now(0), "minute") = 45
  • Now create a new scan class “15 minute energy logging” with the above tag as the driving tag, operator “=”, value “1” and one-shot execution enabled.
  • Use this scan class for your history logging. You’ll get one reading on the hour and every 15 minutes after that.

See Project screenshots - share yours to see my early attemps at the meter pages.

1 Like