# Calculate the Duration after Toggle switch turns on

**URL:** <https://forum.inductiveautomation.com/t/calculate-the-duration-after-toggle-switch-turns-on/107070>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [June 5, 2025, 11:12am UTC](https://forum.inductiveautomation.com/t/calculate-the-duration-after-toggle-switch-turns-on/107070 "2025-06-05T11:12:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anant\_Upadhyayula](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@Anant\_Upadhyayula](https://forum.inductiveautomation.com/u/Anant_Upadhyayula)\
**Post date:** [June 5, 2025, 11:12am UTC](https://forum.inductiveautomation.com/t/calculate-the-duration-after-toggle-switch-turns-on/107070/1 "2025-06-05T11:12:01Z")

</div>

I was thinking to use Tag.Timestamp property of a Boolean memory tag "NotScheduled" as starttime and now() as current time to get the difference.  
This script appears to work half way, when I set another integer type memory tag "notScheduledTime" the tag value to 100 and run this, it changes to 0, I think the else logic part appears to work. But it was not updating the tag with duration minutes.  
I would appreciate your help.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/b/eb70bf0afcfd67d0983d62670bc27ea7c837013d.png)

```python
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):

	import system
	# Define tag paths
	durationTag = ["[.]../Performance/notScheduledTime"]
		
	if currentValue.value == True:
		currentTime = system.date.now()
		startTime = currentValue.timestamp
		minutes = system.date.minutesBetween(startTime,currentTime)
		
		if minutes >=1:
			minutes += 1
		system.tag.writeBlocking(["[.]../Performance/notScheduledTime"], [minutes])
	else:
	    # when toggled to False, reset duration to zero
	    system.tag.writeBlocking(["[.]../Performance/notScheduledTime"], [0])

```

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [June 5, 2025, 11:37am UTC](https://forum.inductiveautomation.com/t/calculate-the-duration-after-toggle-switch-turns-on/107070/2 "2025-06-05T11:37:14Z")

</div>

1. Don't `import system`. It's built in.
2. Don't run scripts like this on tag value changed. There is a limited pool of threads (3) and `system.tag.writeBlocking` can gum it up.

Always, if you can, use an expression rather than a script. Try this expression in `notScheduled`:

```python
round(
	(now() - {[~]notScheduledTime.Timestamp}) / 60000,
	0
)

```

While you test it out set the Data Type to Float and `round()` to three decimal places so you don't have to wait a minute to see the change.

The expression will update at the default scan class rate. If every 5 s is good enough then change `now()` to `now(5000)`, etc.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [June 5, 2025, 12:18pm UTC](https://forum.inductiveautomation.com/t/calculate-the-duration-after-toggle-switch-turns-on/107070/3 "2025-06-05T12:18:38Z")

</div>

Use minutesBetween in your expression.

```python
if([~]Performance/notScheduledTime,
   minutesBetween([~]Performance/notScheduledTime.Timestamp, now()),
   0)

```

---

<div class="post-metadata">

**Author:** ![Anant\_Upadhyayula](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@Anant\_Upadhyayula](https://forum.inductiveautomation.com/u/Anant_Upadhyayula)\
**Post date:** [June 5, 2025, 1:04pm UTC](https://forum.inductiveautomation.com/t/calculate-the-duration-after-toggle-switch-turns-on/107070/4 "2025-06-05T13:04:31Z")

</div>

@Transistor Thank you for highlighting the issues with my approach and suggesting alternatives.  
@JordanCClark - thank you for simplified expression.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [June 6, 2025, 2:40pm UTC](https://forum.inductiveautomation.com/t/calculate-the-duration-after-toggle-switch-turns-on/107070/5 "2025-06-06T14:40:39Z")

</div>

> [@Transistor](#):
>
> system.tag.writeBlocking

writeAsync is OK though, if you don't need to wait for it to write before continuing. Actually, writeBlocking is fine here too as it's writing to a memory tag
