Date range counter

Hi everyone, I’m trying to do a counter for a boolean tag that show me the count when in a given date range. I did some testing using a table but I don’t care about the time when the tag was on, the I idea is to show only the count.

Any help is appreciated, thank you!

Welcome to the forums!

If you show what you’ve done so far, that will go a long way to helping you out.

Thanks!

I started with this to do some testing:

dataSet = system.tag.queryTagHistory(
	paths=['SOMETAG'],
	aggregationMode="CountOn")

for row in system.dataset.toPyDataSet(dataSet):
	print[col for col in row]

count = 0
for row in (system.dataset.toPyDataSet(dataSet)):
	for i, col in enumerate (row):
		if i == 1:
			if col > 0:
				count += 1

print count

And then, I changed it to this:

startTime = event.source.StartTime
endTime = event.source.EndTime

dataSet = system.tag.queryTagHistory(
	paths=['SOMETAG'],
	startDate = startTime,
	endDate = endTime,
	aggregationMode="CountOn")

for row in system.dataset.toPyDataSet(dataSet):
	
count = 0
for row in (system.dataset.toPyDataSet(dataSet)):
	for i, col in enumerate (row):
		if i == 1:
			if col > 0:
				count += 1

event.source.Count = count

And this is the template:

You may need to provide an intervalHours or Minutes to the tag History Call. Also, You may want to investigate using the queryTagCalculations function to avoid iterating through the dataset.

Since what you are trying to do in this instance is provide a single return value from the aggregation then I think you could just call the value directly from the data set since it should always be in the same position.

startTime = event.source.StartTime
endTime = event.source.EndTime

dataSet = system.tag.queryTagCalculations(['SOMETAG'],['CountOn'],startTime,endTime)

event.source.Count = dataSet.getValueAt(0,1)

For what it’s worth IMHO, converting to a pyDataSet is really unneeded in a situation like this. I don’t feel like it does anything to really account for the extra overhead of calling the function to convert it. I also feel that it makes the code less readable and there by less pythonic.

Perhaps someone with more understanding of whats going on behind the scenes can speak to if it is more optimal or not. In my experience it isn’t.

I personally favor using the range function instead. Something like:

count = 0
for row in range(dataSet.rowCount):
     for col in range(dataSet.columnCount):
          if dataSet.getValueAt(row,col):
               count += 1
event.source.Count = count

Your code is perfectly valid, just thought I would share a different approach.

Thank you!

I'm pretty much new into scripting so I'll try that too.