Sum tag value for 24hr period

I have added the following code to a gateway tag change script and works as it should.

dsResults = system.tag.queryTagHistory(
    paths = ['[Weather_Stations]Rainfall'],
	aggregationMode = 'Sum',
    startDate = system.date.midnight(system.date.now()),
    rangeHours = 24,
    returnSize = 1,
	noInterpolation = False)
		
pdsResults = system.dataset.toPyDataSet(dsResults)
system.tag.writeAsync(['[Weather_Stations]RainfallTotal'],[pdsResults[0][1]])

I has hoping to run in a UDT but it will do for now.

You can run it in a UDT, but your paths will need to change for it to be dynamic.

Its got me beat.

When it runs a gateway script it works fine. But when run the script in tag it just replaces the total with the previous. Does not sum.

I have replaced some tags for testing.

Show your UDT that you're wanting to use. You should have a tag named "Rainfall" or something else for the current value, and a "RainfallTotal" tag of some sort for the total. Your value changed script on the "Rainfall" tag would look like this if you're using those 2 tags (we're cheating by appending "Total" to the "Rainfall" to get the tag path for the total:

dsResults = system.tag.queryTagHistory(
    paths = [tagPath],
	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([tagPath + 'Total'],[pdsResults[0][1]])

@michael.flagler

Now I just get random numbers in Rainfall total. They even go down when i get a zero rainfall on the update

Ok, I think I see the possible issue. You're missing the intervalMinutes parameter (I went ahead and edited my post and added it to my example as well). See if that helps.