Filtering a Tag History Binding Dataset

Hello,

Still relatively new to scripting in Ignition and I can’t seem to find solid documentation on the best way to approach numerically filtering a dataset.

What I’m trying to do is setup the value of an LedDisplay component in a perspective view using an average of a selected tag history. Right now I have a tag that is recording data every few seconds or so to our historian. So I’m configuring a tag history binding for the value of the LedDisplay. I’m using a 1min periodic query in the tag history binding for a 60min period, basically a dataset of 60 rows and 2 columns. The original dataset itself looks something like this:


  • 2.34
  • 2.38
  • 2.54
  • 18.42
  • 2.45
  • 2.79
  • 0.00
  • 2.55

I want to do a transform to that dataset to filter out any values that are higher than 3.00 and lower than 2.00. Essentially giving me the following desired dataset:


  • 2.34
  • 2.38
  • 2.54
  • 2.45
  • 2.79
  • 2.55

Again, the main reason I want to do this is to average the useful range within the dataset. The first dataset would give me an average of 4.18, whereas the second dataset average would be 2.51. That second dataset is a much more representative dataset of the process. Not interested in changing the logging of historian data, as I still want my historian to keep logging the 0s and the higher values because they are useful for other visualizations. Not sure if I’m going about this the best way, open to most things. Also, I’m a complete noob when it comes to the query area so I’d prefer a solution involving script transform.

Thanks!

I am a bit biased, but my view() expression function (from the free Simulation Aids module) would make short work of this, as an expression binding:

view("Select avg(myColumn) Where 2.0 <= myColumn <= 3.0 Group By 1", {path.to.dataset.property})[0, 0]
2 Likes
  • Add a script transform to your binding.
  • The binding will pass the binding data into the transform as value. Your code will look something like this (where ‘columnName’ is the actual name of the column with your values.
	filtered = []
	for val in value:
		if 2 <= val['columnName'] <= 3:
			filtered.append(['columnName'])
			
	return sum(filtered) / float(len(filtered))

You can try this test code in the Script Console:

Test code
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
m = []
for val in l:
	if 10 < val < 20:
		m.append(val)
		
avg = sum(m) / float(len(m))
		
print(m)
print(avg)
1 Like

I’m getting the following error when I try it. I think this is the right path forward though.

Try changing the Dataset Format from DATASET to DOCUMENT. The dropdown is just above the transform.

1 Like

try filtered.append(val['CT_Linear'])

2 Likes

Thanks to you both! Changing the value format to DOCUMENT and changing it to filtered.append(val['CT_Linear']) ended up fixing it!

1 Like