Most efficient way to return maximum values of tag trend history aggregated by day over a time period?

What would be the most efficient way to return a table/list-of-dicts of the maximum value recorded each day over a time period for a number of tags using tag history?

My initial thought would have been via SQL, but SQL is hard for tag history since they’re partitioned.

My next thought is to use sytem.tag.queryTagCalculations(..., calculations=['Maximum']) for each day within the time period selected to get each tag’s maximum value for that day. This doesn’t seem too efficient though…

Is there another way that I’m missing?

What about something like this, I’m using an example for of 5 days:

tp = [your tag list]
e = system.date.now()
s = system.date.addDays(system.date.midnight(e),-5)
d = system.date.daysBetween(s,e)
v = system.tag.queryTagHistory(paths=tp,startDate=s,endDate=e,returnSize=d,aggregationMode='Maximum')
2 Likes

Just have to be careful with daylight savings when relying on returnSize or intervalHours=24.

It’s not the most efficient, but I tend to use intervalMinutes=1 (could also use intervalHours=1 to speed it up, but you might miss some data if you add additional conditions) and then loop through all the data to find what I’m looking for, for each date. I like this approach as it gives me the flexibility to add additional conditions (i.e. - if some sensors fault high, you can set a limit to exclude these fault values from the end result, etc).

Ah cool, I didn’t realise returnSize would work like that. Haven’t had to do much with history calcs as of yet. Cheers!