# Sum tag value for 24hr period

**URL:** <https://forum.inductiveautomation.com/t/sum-tag-value-for-24hr-period/92724>\
**Category:** Ignition\
**Created:** [July 3, 2024, 1:23am UTC](https://forum.inductiveautomation.com/t/sum-tag-value-for-24hr-period/92724 "2024-07-03T01:23:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karl\_Eckermann](https://avatars.discourse-cdn.com/v4/letter/k/839c29/32.png) [@Karl\_Eckermann](https://forum.inductiveautomation.com/u/Karl_Eckermann)\
**Post date:** [July 3, 2024, 1:23am UTC](https://forum.inductiveautomation.com/t/sum-tag-value-for-24hr-period/92724/1 "2024-07-03T01:23:44Z")

</div>

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](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/3/737b71a6acbd872a7780ad4f23f4a831a700ae90.png)

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [July 3, 2024, 7:43am UTC](https://forum.inductiveautomation.com/t/sum-tag-value-for-24hr-period/92724/2 "2024-07-03T07:43:15Z")

</div>

You can try using the `system.tag.queryTagCalculations` function?  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/b/bbdc3ae6ba9b67e5a81097d917eeb7e00e08a1d7.png)

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

---

<div class="post-metadata">

**Author:** ![Saad\_Ali\_Hassan](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/saad_ali_hassan/32/52363_2.png) [@Saad\_Ali\_Hassan](https://forum.inductiveautomation.com/u/Saad_Ali_Hassan)\
**Post date:** [July 4, 2024, 3:35am UTC](https://forum.inductiveautomation.com/t/sum-tag-value-for-24hr-period/92724/3 "2024-07-04T03:35:58Z")

</div>

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

```python
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]

```

---

<div class="post-metadata">

**Author:** ![Karl\_Eckermann](https://avatars.discourse-cdn.com/v4/letter/k/839c29/32.png) [@Karl\_Eckermann](https://forum.inductiveautomation.com/u/Karl_Eckermann)\
**Post date:** [July 7, 2024, 10:58pm UTC](https://forum.inductiveautomation.com/t/sum-tag-value-for-24hr-period/92724/4 "2024-07-07T22:58:41Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/7/370d5c0a29fd2b75b7baa37e725c568d7cbd32ac.png)

---

<div class="post-metadata">

**Author:** ![Karl\_Eckermann](https://avatars.discourse-cdn.com/v4/letter/k/839c29/32.png) [@Karl\_Eckermann](https://forum.inductiveautomation.com/u/Karl_Eckermann)\
**Post date:** [July 7, 2024, 11:06pm UTC](https://forum.inductiveautomation.com/t/sum-tag-value-for-24hr-period/92724/5 "2024-07-07T23:06:15Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/f/0fb0207bdf125002844f26cc903d60022a19efc5.png)

---

<div class="post-metadata">

**Author:** ![leonardo.godoi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/leonardo.godoi/32/27178_2.png) [@leonardo.godoi](https://forum.inductiveautomation.com/u/leonardo.godoi)\
**Post date:** [July 8, 2024, 12:28am UTC](https://forum.inductiveautomation.com/t/sum-tag-value-for-24hr-period/92724/6 "2024-07-08T00:28:46Z")

</div>

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"]**

---

<div class="post-metadata">

**Author:** ![Karl\_Eckermann](https://avatars.discourse-cdn.com/v4/letter/k/839c29/32.png) [@Karl\_Eckermann](https://forum.inductiveautomation.com/u/Karl_Eckermann)\
**Post date:** [July 8, 2024, 2:42am UTC](https://forum.inductiveautomation.com/t/sum-tag-value-for-24hr-period/92724/7 "2024-07-08T02:42:12Z")

</div>

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

```python
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]

```

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/2/12ee24877e6faafe2ed3749c9f747be5f03a822a.png)

---

<div class="post-metadata">

**Author:** ![leonardo.godoi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/leonardo.godoi/32/27178_2.png) [@leonardo.godoi](https://forum.inductiveautomation.com/u/leonardo.godoi)\
**Post date:** [July 8, 2024, 2:52am UTC](https://forum.inductiveautomation.com/t/sum-tag-value-for-24hr-period/92724/8 "2024-07-08T02:52:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Karl\_Eckermann](https://avatars.discourse-cdn.com/v4/letter/k/839c29/32.png) [@Karl\_Eckermann](https://forum.inductiveautomation.com/u/Karl_Eckermann)\
**Post date:** [July 8, 2024, 3:15am UTC](https://forum.inductiveautomation.com/t/sum-tag-value-for-24hr-period/92724/9 "2024-07-08T03:15:31Z")

</div>

Sorry, I should have been more specific.

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

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/b/bb27a49330d4df63ea4059f2116fda85ef9f4ced.png)

---

<div class="post-metadata">

**Author:** ![leonardo.godoi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/leonardo.godoi/32/27178_2.png) [@leonardo.godoi](https://forum.inductiveautomation.com/u/leonardo.godoi)\
**Post date:** [July 8, 2024, 11:30am UTC](https://forum.inductiveautomation.com/t/sum-tag-value-for-24hr-period/92724/10 "2024-07-08T11:30:07Z")

</div>

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

```python
	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:

```python
	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.

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [July 8, 2024, 4:13pm UTC](https://forum.inductiveautomation.com/t/sum-tag-value-for-24hr-period/92724/11 "2024-07-08T16:13:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [July 8, 2024, 4:58pm UTC](https://forum.inductiveautomation.com/t/sum-tag-value-for-24hr-period/92724/12 "2024-07-08T16:58:32Z")

</div>

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:

```python
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

```python
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.

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [July 8, 2024, 6:07pm UTC](https://forum.inductiveautomation.com/t/sum-tag-value-for-24hr-period/92724/13 "2024-07-08T18:07:57Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [July 8, 2024, 6:14pm UTC](https://forum.inductiveautomation.com/t/sum-tag-value-for-24hr-period/92724/14 "2024-07-08T18:14:33Z")

</div>

> [@michael.flagler](#):
>
> I could be wrong but I believe the `returnSize = 1` and `intervalMinutes = 5`

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

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [July 8, 2024, 6:19pm UTC](https://forum.inductiveautomation.com/t/sum-tag-value-for-24hr-period/92724/15 "2024-07-08T18:19:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Karl\_Eckermann](https://avatars.discourse-cdn.com/v4/letter/k/839c29/32.png) [@Karl\_Eckermann](https://forum.inductiveautomation.com/u/Karl_Eckermann)\
**Post date:** [July 8, 2024, 10:08pm UTC](https://forum.inductiveautomation.com/t/sum-tag-value-for-24hr-period/92724/16 "2024-07-08T22:08:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [July 8, 2024, 10:56pm UTC](https://forum.inductiveautomation.com/t/sum-tag-value-for-24hr-period/92724/17 "2024-07-08T22:56:18Z")

</div>

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:

```python
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]])

```

---

<div class="post-metadata">

**Author:** ![Karl\_Eckermann](https://avatars.discourse-cdn.com/v4/letter/k/839c29/32.png) [@Karl\_Eckermann](https://forum.inductiveautomation.com/u/Karl_Eckermann)\
**Post date:** [July 25, 2024, 7:14am UTC](https://forum.inductiveautomation.com/t/sum-tag-value-for-24hr-period/92724/18 "2024-07-25T07:14:52Z")

</div>

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.

```python
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]])

```

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [July 25, 2024, 1:13pm UTC](https://forum.inductiveautomation.com/t/sum-tag-value-for-24hr-period/92724/19 "2024-07-25T13:13:57Z")

</div>

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:

```python
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]])

```

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [July 25, 2024, 1:46pm UTC](https://forum.inductiveautomation.com/t/sum-tag-value-for-24hr-period/92724/20 "2024-07-25T13:46:46Z")

</div>

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.

[Next page](https://forum.inductiveautomation.com/t/sum-tag-value-for-24hr-period/92724.md?page=2)
