Use timestamp in calculations in custom Aggregation

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 and i try to convert the timestamp to int or any type of calculation the function stops working.

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.

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.

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

1 Like

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