Is there a way to determine the timestamp in scripting for the maximum value in a tag history query? I have a tank with a level measurement, and I would like to calculate current consumption rate. I know that I can use the minmax function in the system.tag.queryTagCalculations to get the minimum and maximum, and that works if the tank has not been refilled during that time period. If I could get the timestamp from the maximum value, then I can just subtract the current value from the maximum and divide by the difference in time.
rows = [
(historyDataset.getValueAt(r, 1), historyDataset.getValueAt(r, 0))
for r in range(historyDataset.getRowCount())
if historyDataset.getValueAt(r, 1) is not None
]
maxValue, maxTimestamp = max(rows)
If I’m understanding correctly and you’re in a script already anyway, the above might help.
Thanks for the help. I had thought about iterating through myself, but I was hoping there was a built in solution. Your algorithm is more efficient than mine, so I went ahead and used it.
Untested, but this or something like it should be a little bit more efficient:
maxValue, maxTimestamp = max(system.dataset.toPyDataSet(historyDataset), key=lambda row: row[1])
The other approach is creating an extra copy of all the values in a list (which is not free) and iterating through them all twice. If you're on 8.3 you can drop the toPyDataSet call.
I would use my toolkit's orderBy() function (expression or scripting) to get the dataset in descending order by value, then the entire first row is easily accessible. (FWIW. the toolkit's asMap() function turns the first row of any dataset into a dictionary, which makes it really handy in Perspective.)