# Date range counter

**URL:** <https://forum.inductiveautomation.com/t/date-range-counter/37726>\
**Category:** Ignition\
**Tags:** database, ignition79, scripting\
**Created:** [August 14, 2020, 2:40pm UTC](https://forum.inductiveautomation.com/t/date-range-counter/37726 "2020-08-14T14:40:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![david26992](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@david26992](https://forum.inductiveautomation.com/u/david26992)\
**Post date:** [August 14, 2020, 2:40pm UTC](https://forum.inductiveautomation.com/t/date-range-counter/37726/1 "2020-08-14T14:40:04Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [August 14, 2020, 5:41pm UTC](https://forum.inductiveautomation.com/t/date-range-counter/37726/2 "2020-08-14T17:41:40Z")

</div>

Welcome to the forums!

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

---

<div class="post-metadata">

**Author:** ![david26992](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@david26992](https://forum.inductiveautomation.com/u/david26992)\
**Post date:** [August 14, 2020, 7:04pm UTC](https://forum.inductiveautomation.com/t/date-range-counter/37726/3 "2020-08-14T19:04:00Z")

</div>

Thanks!

I started with this to do some testing:

```auto
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:

```auto
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:

 ![template](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/d/bd22ad6e133305e7d8fce090f63fca7d1b99fae6.jpeg)

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [August 14, 2020, 7:41pm UTC](https://forum.inductiveautomation.com/t/date-range-counter/37726/4 "2020-08-14T19:41:41Z")

</div>

You may need to provide an intervalHours or Minutes to the tag History Call. Also, You may want to investigate using the [queryTagCalculations](https://docs.inductiveautomation.com/display/DOC79/system.tag.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.

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![david26992](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@david26992](https://forum.inductiveautomation.com/u/david26992)\
**Post date:** [August 14, 2020, 9:07pm UTC](https://forum.inductiveautomation.com/t/date-range-counter/37726/5 "2020-08-14T21:07:03Z")

</div>

> [@lrose](#):
>
> 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.
