# Best practice for tag calculating derivative (difference between values at 2 timestamps)

**URL:** <https://forum.inductiveautomation.com/t/best-practice-for-tag-calculating-derivative-difference-between-values-at-2-timestamps/54728>\
**Category:** Ignition\
**Tags:** ignition80\
**Created:** [January 6, 2022, 3:44pm UTC](https://forum.inductiveautomation.com/t/best-practice-for-tag-calculating-derivative-difference-between-values-at-2-timestamps/54728 "2022-01-06T15:44:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![djs](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@djs](https://forum.inductiveautomation.com/u/djs)\
**Post date:** [January 6, 2022, 3:44pm UTC](https://forum.inductiveautomation.com/t/best-practice-for-tag-calculating-derivative-difference-between-values-at-2-timestamps/54728/1 "2022-01-06T15:44:53Z")

</div>

I have tags that are calculating throughput from individual ports on network switches (through Kymera SNMP Driver). The way to calculate throughput is to take the difference of _ifInOctets_ over a period of time and the difference between _ifOutOctets_ over the same period of time, multiply by 800, and divide by the speed of the port and the time difference. Both _ifInOctets_ and _ifOutOctets_ are in the form of OPC tags.

I achieved this by making a _throughput_ tag that runs a script from the Project Library. That script queries history for both _ifInOctets_ and _ifOutOctets_ gets the difference in values and returns the calculated throughput. I’m starting to doubt that this is the best practice however, because with 20+ switches, 20+ ports each, the number of scripts running and queries executing against the historian every second is quite big.  
The only way that I can think of of how to do it differently is to have a temporary tag that lags 1s behind the OPC tag, and just read that tag rather than query the historian. I’m not sure how to do this properly or reliably however.

Has anyone ever dealt with tags that are calculating the derivative of other tags?  
What would be the best practice for doing this? I would appreciate advice.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 6, 2022, 4:03pm UTC](https://forum.inductiveautomation.com/t/best-practice-for-tag-calculating-derivative-difference-between-values-at-2-timestamps/54728/2 "2022-01-06T16:03:21Z")

</div>

I would use my `objectScript()` expression function’s `state` dictionary to hold the last few samples of each tag in memory (with timestamps) and calculate from those. Best if delegated to a function in the global scripting project. If you are particular about accuracy at startup, you can populate empty state from the historian, then track tag change from there.

You’ll need this free module:

> **[Ignition Third-Party Module Showcase](https://inductiveautomation.com/moduleshowcase/module/automation-professionals-llc-simulation-aids)**
>
> Easily extend Ignition's core functionality with Third-Party Modules. The Module Showcase is a great place to find a solution that best meets your needs.
