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.
You can try using the system.tag.queryTagCalculations
function?
I've never used it before. You'll probably want to set noInterpolation = True
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]
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"]
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]
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
notinterapolation
- 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
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.
- Since you are providing
intervalMinutes
you should not provide returnSize as the two parameters conflict. - Since you are using
system.date.now()
as your end date and you just want the last 12 hours, then you can just userangeHours = 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.
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)
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.
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]])
I still cannot get the result I am looking for. The current code I am using only moves the current value "Rainfall" to the "RainfallTotal" field. It does not appear to add the rainfall values.
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
if not initialChange:
# Query historical tag data for the specified tag paths and time range
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]])
What is being written to the RainfallTotal in your code should be the aggregate sum between midnight and the current time. I believe you want to use the endDate
not the startDate
as midnight.
Try this:
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
if not initialChange:
# Query historical tag data for the specified tag paths and time range
dsResults = system.tag.queryTagHistory(
paths = ['[Weather_Stations]Rainfall'],
aggregationMode = 'Sum',
endDate = 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]])
But if midnight is the end date, that would get yesterday's 24-hour total, not today's running total.
On the test that I ran with the script I shared, it worked fine. Where is your tag change script? If you're still using the tag structure from your first post, the tag paths are wrong.