# Use timestamp in calculations in custom Aggregation

**URL:** <https://forum.inductiveautomation.com/t/use-timestamp-in-calculations-in-custom-aggregation/101651>\
**Category:** Ignition\
**Tags:** python, scripting, ignition81, perspective\
**Created:** [January 30, 2025, 10:16pm UTC](https://forum.inductiveautomation.com/t/use-timestamp-in-calculations-in-custom-aggregation/101651 "2025-01-30T22:16:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastian\_Rojas](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Sebastian\_Rojas](https://forum.inductiveautomation.com/u/Sebastian_Rojas)\
**Post date:** [January 30, 2025, 10:16pm UTC](https://forum.inductiveautomation.com/t/use-timestamp-in-calculations-in-custom-aggregation/101651/1 "2025-01-30T22:16:06Z")

</div>

I'm bulding a custom aggregation function for the tag historian, and for this function I need to make some calculations with the timestamp, but for some reason i'm not getting the desired value I don't know why when I try to make a calculation using the timestamp something seems to crash.

For example, if i use the sample function in this [page](https://docs.inductiveautomation.com/docs/8.1/ignition-modules/tag-historian/custom-tag-history-aggregates) and i try to convert the timestamp to int or any type of calculation the function stops working.

```python
start_date = system.date.getDate(2025, 0, 27)
end_date = system.date.addDays(start_date, 1)

wrapper = """\
python:def myCount(qval, interpolated, finished, blockContext, queryContext):
	cnt = blockContext.getOrDefault('cnt',0)
	if qval.quality.isGood():
	    blockContext['cnt']=int(cnt)+1
	 
	if finished:
	    return blockContext.getOrDefault('cnt', 0)
"""

ds = system.tag.queryTagHistory(
	paths = [tag_path]
	,startDate = start_date
	,endDate = end_date
	,aggregationMode = wrapper
	,intervalHours = 24
)

print [ds.getValueAt(row,col) for row in range(ds.rowCount) for col in range(ds.columnCount)]

[Mon Jan 27 00:00:00 EST 2025, 23592L, Tue Jan 28 00:00:00 EST 2025, 33958L]

```

if add `int(qval.timestamp)` or `a = qval.timestamp - 1 ` it breaks, and the counter stops working, there are no problems when using `qval.value` but for some reason i can't use the timestamp.

```python
start_date = system.date.getDate(2025, 0, 27)
end_date = system.date.addDays(start_date, 1)

wrapper = """\
python:def myCount(qval, interpolated, finished, blockContext, queryContext):
	cnt = blockContext.getOrDefault('cnt',0)
	int(qval.timestamp)
	if qval.quality.isGood():
	    blockContext['cnt']=int(cnt)+1
	 
	if finished:
	    return blockContext.getOrDefault('cnt', 0)
"""

ds = system.tag.queryTagHistory(
	paths = [tag_path]
	,startDate = start_date
	,endDate = end_date
	,aggregationMode = wrapper
	,intervalHours = 24
)

print [ds.getValueAt(row,col) for row in range(ds.rowCount) for col in range(ds.columnCount)]

[Mon Jan 27 00:00:00 EST 2025, 0L, Tue Jan 28 00:00:00 EST 2025, 0L]

```

Why does this happen? and for the calculation I'm assuming the historian retrieves values in order from oldest to the most recent.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 31, 2025, 12:42pm UTC](https://forum.inductiveautomation.com/t/use-timestamp-in-calculations-in-custom-aggregation/101651/2 "2025-01-31T12:42:52Z")

</div>

Timestamps are not integers. They are instances of `java.util.Date`.

---

<div class="post-metadata">

**Author:** ![Sebastian\_Rojas](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Sebastian\_Rojas](https://forum.inductiveautomation.com/u/Sebastian_Rojas)\
**Post date:** [January 31, 2025, 1:09pm UTC](https://forum.inductiveautomation.com/t/use-timestamp-in-calculations-in-custom-aggregation/101651/3 "2025-01-31T13:09:43Z")

</div>

Ok, Tahnk you.  
I used `system.date.toMillis(qval.timestamp)` and evrything is working now, i thought it was a long.

Thank you for the help
