# How to calculate cut times for different cycles using Tag Event Scripts?

**URL:** <https://forum.inductiveautomation.com/t/how-to-calculate-cut-times-for-different-cycles-using-tag-event-scripts/61799>\
**Category:** Ignition\
**Tags:** ignition80\
**Created:** [July 12, 2022, 2:55pm UTC](https://forum.inductiveautomation.com/t/how-to-calculate-cut-times-for-different-cycles-using-tag-event-scripts/61799 "2022-07-12T14:55:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lanutoshi\_Paul](https://avatars.discourse-cdn.com/v4/letter/l/71e660/32.png) [@Lanutoshi\_Paul](https://forum.inductiveautomation.com/u/Lanutoshi_Paul)\
**Post date:** [July 12, 2022, 2:55pm UTC](https://forum.inductiveautomation.com/t/how-to-calculate-cut-times-for-different-cycles-using-tag-event-scripts/61799/1 "2022-07-12T14:55:27Z")

</div>

Hello everyone,

I am new to Ignition/Scripting so any help extended would be greatly appreciated!

The complete cycle time on a part consists of 8 individual cuts. I am trying to calculate the cycle times for each of these 8 cuts and then historize them.  
The resources I have been given are:

1. Current\_cut tag that updates for each of the 8 cuts. This tag contains the cut times in Hr:Min:Sec for each cut. The issue is that it constantly updates and does not tell you which of these 8 cuts it is on currently.
2. Cut\_status tag that ranges from 0-7. This tells me which of the 8 cuts are currently running.

I have attempted to use a tag event script to write the cut times to memory tags that I have created based on the Cut\_Status. However, I have found that this only writes once, when the tag value changes even though I put a loop in there to write continuously(\*code only show the if statements).

Here is the code that I wrote:

status = currentValue.value  
sec = system.readBlocking([“[.]…/Springer/BUI\_Cycle\_Data\_0\_/Cut\_Cycle\_Time\_Secs”])[0].value  
min = system.readBlocking([“[.]…/Springer/BUI\_Cycle\_Data\_0\_/Cut\_Cycle\_Time\_Mins”])[0].value  
hr = system.readBlocking([“[.]…/Springer/BUI\_Cycle\_Data\_0\_/Cut\_Cycle\_Time\_Hours”])[0].value

```
if status == 0:
	system.writeBlocking(["[.]../Cut_Times/Rough_Wing_Suction/Sec"],[sec])
	system.writeBlocking(["[.]../Cut_Times/Rough_Wing_Suction/Min"],[min])
	system.writeBlocking(["[.]../Cut_Times/Rough_Wing_Suction/Hr"],[hr])

if status == 1:
	system.writeBlocking(["[.]../Cut_Times/Rough_Long_Suction/Sec"],[sec])
	system.writeBlocking(["[.]../Cut_Times/Rough_Long_Suction/Min"],[min])
	system.writeBlocking(["[.]../Cut_Times/Rough_Long_Suction/Hr"],[hr])
	
if status == 2:
	system.writeBlocking(["[.]../Cut_Times/Finish_Wing_Suction/Sec"],[sec])
	system.writeBlocking(["[.]../Cut_Times/Finish_Wing_Suction/Min"],[min])
	system.writeBlocking(["[.]../Cut_Times/Finish_Wing_Suction/Hr"],[hr])
	
if status == 3:
	system.writeBlocking(["[.]../Cut_Times/Finish_Long_Suction/Sec"],[sec])
	system.writeBlocking(["[.]../Cut_Times/Finish_Long_Suction/Min"],[min])
	system.writeBlocking(["[.]../Cut_Times/Finish_Long_Suction/Hr"],[hr])
	
if status == 4:
	system.writeBlocking(["[.]../Cut_Times/Rough_Wing_Pressure/Sec"],[sec])
	system.writeBlocking(["[.]../Cut_Times/Rough_Wing_Pressure/Min"],[min])
	system.writeBlocking(["[.]../Cut_Times/Rough_Wing_Pressure/Hr"],[hr])
	
if status == 5:
	system.writeBlocking(["[.]../Cut_Times/Rough_Long_Pressure/Sec"],[sec])
	system.writeBlocking(["[.]../Cut_Times/Rough_Long_Pressure/Min"],[min])
	system.writeBlocking(["[.]../Cut_Times/Rough_Long_Pressure/Hr"],[hr])
	
if status == 6:
	system.writeBlocking(["[.]../Cut_Times/Finish_Wing_Pressure/Sec"],[sec])
	system.writeBlocking(["[.]../Cut_Times/Finish_Wing_Pressure/Min"],[min])
	system.writeBlocking(["[.]../Cut_Times/Finish_Wing_Pressure/Hr"],[hr])
	
if status == 7:
	system.writeBlocking(["[.]../Cut_Times/Finish_Long_Pressure/Sec"],[sec])
	system.writeBlocking(["[.]../Cut_Times/Finish_Long_Pressure/Min"],[min])
	system.writeBlocking(["[.]../Cut_Times/Finish_Long_Pressure/Hr"],[hr])
	
else:
	print("Unable to calculate values")

```

My questions are:

1. Is this the correct approach?
2. Is there a simpler way to do this?
3. The script only writes once - when the value changes. How can I constantly write to the tag as long as the “If condition” is true?

Thank you!

---

<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:** [July 12, 2022, 4:21pm UTC](https://forum.inductiveautomation.com/t/how-to-calculate-cut-times-for-different-cycles-using-tag-event-scripts/61799/2 "2022-07-12T16:21:34Z")

</div>

1. No, not really. You will always be racing between the changes identifying which cut and the change for the duration. Due to the way OPC subscriptions work. You need a third tag that increments _ **after** _ the PLC updates the other two. This trigger tag can then have an event that uses `system.opc.readValues()` to obtain the two values and write them to your DB. This script can also select other memory tags to update.

2. Perhaps. If the status changes when a particular cut starts/finishes, you could log that change with a timestamp in the DB. The deltas in the DB would be your cut times. You might need an extra status code that represents not cutting at all.

3. Should not be necessary.

---

<div class="post-metadata">

**Author:** ![Lanutoshi\_Paul](https://avatars.discourse-cdn.com/v4/letter/l/71e660/32.png) [@Lanutoshi\_Paul](https://forum.inductiveautomation.com/u/Lanutoshi_Paul)\
**Post date:** [July 12, 2022, 5:46pm UTC](https://forum.inductiveautomation.com/t/how-to-calculate-cut-times-for-different-cycles-using-tag-event-scripts/61799/3 "2022-07-12T17:46:13Z")

</div>

Phil,

Thanks for your input!

Here are some more questions that popped up:

1. Would I need to create this Trigger tag or would it need to come from the PLC? Since the tags coming from the PLC: “Current\_cut” & “Cut\_status” are updating constantly, how would the new Trigger tag help me to calculate individual cut times?
2. I don’t have a lot of experience with databases but this could be a a chance to learn!

---

<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:** [July 12, 2022, 5:57pm UTC](https://forum.inductiveautomation.com/t/how-to-calculate-cut-times-for-different-cycles-using-tag-event-scripts/61799/4 "2022-07-12T17:57:43Z")

</div>

It would have to come from the PLC. The PLC program would have to ensure the status and duration are updated just before the end-of-cut trigger increments. The status and duration would not otherwise change.

Tag subscriptions are unordered. You cannot be sure that at the point one of those values changes that the other has the correct corresponding value. So you need to trigger off of _ **one** _ value, and if necessary, **poll** for the others.

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [July 12, 2022, 6:28pm UTC](https://forum.inductiveautomation.com/t/how-to-calculate-cut-times-for-different-cycles-using-tag-event-scripts/61799/5 "2022-07-12T18:28:54Z")

</div>

This might be a good use case where you create a queue/FIFO in the PLC and use a transaction group.
