# Tag Calculation (Current Value - Previous Value)

**URL:** <https://forum.inductiveautomation.com/t/tag-calculation-current-value-previous-value/92619>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [July 1, 2024, 12:48pm UTC](https://forum.inductiveautomation.com/t/tag-calculation-current-value-previous-value/92619 "2024-07-01T12:48:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maryam\_Saeed](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Maryam\_Saeed](https://forum.inductiveautomation.com/u/Maryam_Saeed)\
**Post date:** [July 1, 2024, 12:48pm UTC](https://forum.inductiveautomation.com/t/tag-calculation-current-value-previous-value/92619/1 "2024-07-01T12:48:35Z")

</div>

I have 300 Energy tags with history enabled that need calculations on it.  
My values are being stored every 60 mins. I want my value calculated as  
Current Value- Previous Value.  
In another post on the forum the solution for 3 tags (1. Original, 2.Previous value, 3. Derived tag with calculated value) works. But this solution is not effective for 300 tags or more. Creating 3 tags for each energy tag will give me (3 x 300) = 900 tags for my system is way too much  
Is there a way a script or any other solution would work here?

---

<div class="post-metadata">

**Author:** ![Hayden\_Watson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/hayden_watson/32/48792_2.png) [@Hayden\_Watson](https://forum.inductiveautomation.com/u/Hayden_Watson)\
**Post date:** [July 1, 2024, 12:53pm UTC](https://forum.inductiveautomation.com/t/tag-calculation-current-value-previous-value/92619/2 "2024-07-01T12:53:50Z")

</div>

You could create your tag for current, and add a change script on it then write that value to a memory tag called whatever.  
Put these in some UDTs and configuring 300 copies won't take very long.

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [July 1, 2024, 1:13pm UTC](https://forum.inductiveautomation.com/t/tag-calculation-current-value-previous-value/92619/3 "2024-07-01T13:13:49Z")

</div>

900 tags is not a large number of tags overall. Yes, it would take some time to create 900 tags. However, you can do this with a UDT and save some time.

Are the tag names formatted in a predictable way?

What is the use case? Are you displaying the difference somewhere?

---

<div class="post-metadata">

**Author:** ![Maryam\_Saeed](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Maryam\_Saeed](https://forum.inductiveautomation.com/u/Maryam_Saeed)\
**Post date:** [July 1, 2024, 3:03pm UTC](https://forum.inductiveautomation.com/t/tag-calculation-current-value-previous-value/92619/4 "2024-07-01T15:03:50Z")

</div>

What kind of change scripts in UDT? Making derived tags?

---

<div class="post-metadata">

**Author:** ![Maryam\_Saeed](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Maryam\_Saeed](https://forum.inductiveautomation.com/u/Maryam_Saeed)\
**Post date:** [July 1, 2024, 3:06pm UTC](https://forum.inductiveautomation.com/t/tag-calculation-current-value-previous-value/92619/5 "2024-07-01T15:06:03Z")

</div>

Yes my tags are formatted in a predictable way , I need final kWh reading on my dashboard.  
I don't understand the UDT point. Could you elaborate.

---

<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 1, 2024, 3:12pm UTC](https://forum.inductiveautomation.com/t/tag-calculation-current-value-previous-value/92619/6 "2024-07-01T15:12:41Z")

</div>

Create a UDT to hold your current and previous tags. You don't need the derived value as a tag unless that is what you are recording historically. Otherwise, just do the calculation at display time.

Once you've created the UDT definition, use the Multi-Instance Wizard to quickly create all of your needed tags.

> **[UDT Multi-Instance Wizard | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/platform/tags/user-defined-types-udts/udt-multi-instance-wizard)**
>
> The Multi-Instance Wizard provides a powerful, but simple mechanism for rapidly generating many instances of a UDT at the same time by specifying patterns for UDT parameters.

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [July 1, 2024, 3:21pm UTC](https://forum.inductiveautomation.com/t/tag-calculation-current-value-previous-value/92619/7 "2024-07-01T15:21:58Z")

</div>

Create a UDT with a couple of parameters. Generate the OPC path based on those parameters.

What do your tag paths look like?

---

<div class="post-metadata">

**Author:** ![Hayden\_Watson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/hayden_watson/32/48792_2.png) [@Hayden\_Watson](https://forum.inductiveautomation.com/u/Hayden_Watson)\
**Post date:** [July 2, 2024, 6:32am UTC](https://forum.inductiveautomation.com/t/tag-calculation-current-value-previous-value/92619/8 "2024-07-02T06:32:56Z")

</div>

You can right click the tag, and add a change script. This will just be a bit of python code to do the writing.  
Just make sure to use `system.tag.writeAsync()` as that will cause less potential tag threading issues.
