I ran into an issue with a customers flow total report. My issue is that the variable used in the PLC is only a 16 bit unsigned integer so the value rolls over every 2-3 days. The report was setup to use Aggregation mode Range with an interval of 24 hours. Report runs at the beginning of a new month and reports on the flow total for each day of the month so 12am - 12am.
The Range Aggregation mode is not accounting for rollover, it just reports on the absolute difference between the values recorded at each 24hr increment at 12am.
I tried other aggregation modes but nothing is working the way I needed it to.
On your OPC cycle count tag add a script on the Value Changed event.
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
"""
Watch for the machine's counter being rolling over and, when it does,
add the rollover value to the All-Time cumulative counters.
2023-06-03 @Transistor
"""
currentCycles = currentValue.value # From PLC's resetting cycle counter
prevCycles = previousValue.value # This memory tag.
if currentCycles < prevCycles: # Batch counter has been reset.
rollover = 65535 # or whatever
paths = ["[.]allTimeBatchSum"]
values = system.tag.readBlocking(paths)
system.tag.writeBlocking(paths, [values[0].value + rollover]
Now create an expression tag totalMachineCycles to sum the allTimeBatchSum with the current count. e.g., {[.]allTimeBatchSum} + {[.]MachineCountCycles}
Log this tag in your historian or whatever and you can now get the difference between any two timestamps very easily.
Note: I've adapted this script from my application and haven't tested it but it should give you a good idea how to go about it.