Sum tag value for 24hr period

I have a weather station that returns a total rainfall for a 5min period. What is the best create a tag that can sum the total rainfall from 00:00 to 12:00 (current day). I currently have the tag data in historian.
image

You can try using the system.tag.queryTagCalculations function?
image

I've never used it before. You'll probably want to set noInterpolation = True

2 Likes

Maybe something like this you can run in your script console and test.

tagPathReadList = ['[default]Folder/Tag'] #Your tagpath

# Set the start date to 12 hours ago (720 mins) from the current time
startDate = system.date.addMinutes(system.date.now(), -720)
endDate = system.date.addMinutes(system.date.now(), 0)

# Query historical tag data for the specified tag paths and time range
resultDataSet = system.tag.queryTagHistory(
            paths=tagPathReadList,
            startDate=startDate,
            endDate=endDate,
            returnSize=1,
            intervalMinutes = 144, # Number of 5-minute periods
            interapolation = False,
            aggregationMode = "Sum"
        )

pythonResultDataSet = system.dataset.toPyDataSet(resultDataSet)
print pythonResultDataSet[0][1]
1 Like

I have run the code in the console. It returns the current value instead of sum.

I have also tried the following script with no success. Its like the sum feature does not work.

For the code that you ran in the console, try changing your return size to -1, print the whole dataset, do you at least get the correct result for the period?.
As for the script on your second post, if i'm not mistaken, the paths and calculations are supposed to be lists, so maybe instead of:
tagPath = "yourTagPathHere"
Try:
tagPath = ["yourTagPathHere"]

And instead of:
calc = "sum"

Try:
calc = ["sum"]

1 Like

I have run the following script in the console, and it is returning the correct data. Excellent.

Could I get some help writing the data to the following tag path

[Weather_Stations]RainfallTotal

tagPathReadList = ['[Weather_Stations]Rainfall'] #Your tagpath

# Set the start date to 12 hours ago (720 mins) from the current time
startDate = system.date.addMinutes(system.date.now(), -720)
endDate = system.date.addMinutes(system.date.now(), 0)

# Query historical tag data for the specified tag paths and time range
resultDataSet = system.tag.queryTagHistory(
            paths=tagPathReadList,
            startDate=startDate,
            endDate=endDate,
            returnSize=1,
            intervalMinutes = 144, # Number of 5-minute periods
            interapolation = False,
            aggregationMode = "Sum"
        )

pythonResultDataSet = system.dataset.toPyDataSet(resultDataSet)
print pythonResultDataSet[0][1]

1 Like

What help do you need exactly?
system.tag.writeBlocking(["[Weather_Stations]RainfallTotal"],[pythonResultDataSet[0][1]])
This should work if you want to write exactly what you're seeing on your console.

Sorry, I should have been more specific.

When the tag "Rainfall" (highlight yellow) increases that the sum value is written to the "RainfallTotal" tag.

if you want to write to RainFallTotal every time the value at Rainfall Increases, then you should add this:

	if currentValue.value> previousValue.value:
		system.tag.writeBlocking(["[Weather_Stations]RainFallTotal"],[pythonResultDataSet[0][1]])

If you want to write to RainFallTotal every time the sum is increased, then you should add this:

	rainFallTotal = "[Weather_Stations]RainFallTotal"
	previousSum = system.tag.readBlocking([rainFallTotal])[0].value
	if previousSum < pythonResultDataSet[0][1]:
		system.tag.writeBlocking([rainFallTotal],[pythonResultDataSet[0][1]])

It's very important to note that whichever one you choose, you'll need to add it at the same indentation level as the previous code.

I think you still have some errors in your code.

  • I believe intervalMinutes should be 5 since you're looking for 5 minute intervals
  • The correct spelling is interpolation not interapolation
  • If you're looking for values since midnight, you've seemingly removed that portion from your script and now only showing the sum of the last 12 hours
1 Like

Before getting into this too far, it should be mentioned that this should not be done in a tag ValueChange script unless you understand the risks. Tag ValueChange scripts should really execute in single digit milliseconds, this script will not meet that standard. The reason for this is, because there is a dedicated thread pool for these events to run in with a size of three. That means that if you have more than three events occurring at the same time, there is a high potential for missing events.

The proper place to do this work is in a Gateway Tag Change Script.

With that said:

A few things, if you read the manual entry for the system.tag.queryTagHistory() function, you will find that your script can be simplified by understanding what the parameters are doing.

  1. Since you are providing intervalMinutes you should not provide returnSize as the two parameters conflict.
  2. Since you are using system.date.now() as your end date and you just want the last 12 hours, then you can just use rangeHours = 12 in the parameters.

If you want the sum of every 5 minute interval, then you should use intervalMinutes = 5 this will give you the sum of each 5 minute group of data, returning 145 rows in the dataset.

If you want the sum of the last 12 hours, then you would use returnSize = 1

Return the sum of each 5 min group for the last 12 hours:

resultDataset = system.tag.queryTagHistory(
    paths = ['[Weather_Stations]Rainfall'],
    aggregationMode = 'Sum',
    intervalMinutes = 5,
    rangeHours = 12,
    noInterpolation = False)

system.tag.writeAsync(['[Weather_Stations]RainfallTotal'],[resultDataset])

Return the sum of all data for the last 12 hours

resultDataset = system.tag.queryTagHistory(
    paths = ['[Weather_Stations]Rainfall'],
    aggregationMode = 'Sum',
    returnSize = 1,
    rangeHours = 12,
    noInterpolation = False)

system.tag.writeAsync(['[Weather_Stations]RainfallTotal'],[resultDataset])

Make sure that your indentation is correct. In the last screen snip you posted, your writeBlocking() call is not indented correct.

3 Likes

Nice catches there. Even I messed up on my suggestion of spelling of interpolation in that it should be noInterpolation. I could be wrong but I believe the returnSize = 1 and intervalMinutes = 5 should both be included so that the summed values are from 5 minute intevals of data and not every value stored. (Although in theory it shouldn't be storing values more frequently, but what about the potential where a value is within the deadband so isn't stored and would get skipped because of it not existing in the DB)

1 Like

If you include returnSize = 1 the function will return exactly 1 row, the sum of all 12 hours of data.

Yes, but they are wanting in from 5 minute intervals, which is why I believe both are needed. The value should only update/store once every 5 minutes, but in the event that it doesn't and stores either faster or slower depending on configuration of history parameters and deadbands, to ensure it's not skipping values in the sum or summing some values more than they should be, the interval parameter should prevent that.

1 Like

I think i have thrown you off with the 5 min intervals. Essentially this is remote weather station returning a total rainfall for a five minute period.

Essentially, I want to sum the total rainfall for the current day midnight to midnight.

Yes, that's how I was reading everything you've posted. Your scripting though doesn't match what you're asking for, which is why some of us are stating you need to change the 144 to 5 minutes. I'm also assuming that this 5 minute period resets every 5 minutes to not include previous totals (not accumulating for the day), so that value should only change once every 5 minutes.

You're also only having it pull 12 hours instead of 24 hours. Borrowing from @lrose's response, here's what I would use:

dsResults = system.tag.queryTagHistory(
    paths = ['[Weather_Stations]Rainfall'],
    aggregationMode = 'Sum',
    startDate = system.date.midnight(system.date.now()),
    intervalMinutes = 5,
    rangeHours = 24,
    returnSize = 1,
    noInterpolation = False)

pdsResults = system.dataset.toPyDataSet(dsResults)
system.tag.writeAsync(['[Weather_Stations]RainfallTotal'],[pdsResults[0][1]])
4 Likes