# Down time timer

**URL:** <https://forum.inductiveautomation.com/t/down-time-timer/64315>\
**Category:** Ignition\
**Created:** [September 21, 2022, 1:51am UTC](https://forum.inductiveautomation.com/t/down-time-timer/64315 "2022-09-21T01:51:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benjamin\_Wagner](https://avatars.discourse-cdn.com/v4/letter/b/b5ac83/32.png) [@Benjamin\_Wagner](https://forum.inductiveautomation.com/u/Benjamin_Wagner)\
**Post date:** [September 21, 2022, 1:51am UTC](https://forum.inductiveautomation.com/t/down-time-timer/64315/1 "2022-09-21T01:51:29Z")

</div>

I have a UDT that has a Boolean tag for when a piece of equipment goes down. I want to have a tag that also tracks how long it is down retentively and then zero it at end of shift. Looking for some ideas on how to do this. I tried several things that are not working correctly and seem to be much more complex then I think it should be.

---

<div class="post-metadata">

**Author:** ![YF129701](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/yf129701/32/60498_2.png) [@YF129701](https://forum.inductiveautomation.com/u/YF129701)\
**Post date:** [September 21, 2022, 3:01am UTC](https://forum.inductiveautomation.com/t/down-time-timer/64315/2 "2022-09-21T03:01:56Z")

</div>

Do you store tag history for the state tag? This is the way I’ve approached this problem in the past. Then you can query for state changes throughout the day and get the total amount of time the equipment is in each state.

---

<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:** [September 21, 2022, 4:09am UTC](https://forum.inductiveautomation.com/t/down-time-timer/64315/3 "2022-09-21T04:09:05Z")

</div>

I agree on using tag history.

---

<div class="post-metadata">

**Author:** ![nideyijuyidong](https://avatars.discourse-cdn.com/v4/letter/n/7cd45c/32.png) [@nideyijuyidong](https://forum.inductiveautomation.com/u/nideyijuyidong)\
**Post date:** [September 21, 2022, 9:50am UTC](https://forum.inductiveautomation.com/t/down-time-timer/64315/4 "2022-09-21T09:50:25Z")

</div>

I think there is no timer like that in ignition. I have a similiar request. Using Gateway Events Timer script, check the Boolean tag per second, if True, the timer tag add 1. And a Gateway Schedule script, reset the timer tag to zero at midnight.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [September 21, 2022, 9:53am UTC](https://forum.inductiveautomation.com/t/down-time-timer/64315/5 "2022-09-21T09:53:06Z")

</div>

I’d use a tag change script instead, and do a timestamp comparison…

---

<div class="post-metadata">

**Author:** ![nideyijuyidong](https://avatars.discourse-cdn.com/v4/letter/n/7cd45c/32.png) [@nideyijuyidong](https://forum.inductiveautomation.com/u/nideyijuyidong)\
**Post date:** [September 21, 2022, 9:59am UTC](https://forum.inductiveautomation.com/t/down-time-timer/64315/6 "2022-09-21T09:59:45Z")

</div>

Well, yes, I have hundreds of these devices, so I opted for the gateway script so I don’t have to configure the script for each device tag.  
UDT is a better choise, but I have some historical issues.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [September 21, 2022, 10:44am UTC](https://forum.inductiveautomation.com/t/down-time-timer/64315/7 "2022-09-21T10:44:02Z")

</div>

There’s a tag change script gateway event that you can use. There’s no need to configure a value change script on each tag.

---

<div class="post-metadata">

**Author:** ![Benjamin\_Wagner](https://avatars.discourse-cdn.com/v4/letter/b/b5ac83/32.png) [@Benjamin\_Wagner](https://forum.inductiveautomation.com/u/Benjamin_Wagner)\
**Post date:** [September 21, 2022, 11:17am UTC](https://forum.inductiveautomation.com/t/down-time-timer/64315/8 "2022-09-21T11:17:19Z")

</div>

pascal - Do you have an example of how you would use a tag change script. I have an expression tag I am using now, that does not work due to how I am trying to add the previous to the current. It also seems overly complex.  
if({[.]Shift} = 0, 0, if({[.]Down} = True, if(isNull({[.]DownTime}), 0, {[.]DownTime} + secondsBetween({[.]TempDownTime}, now())), {[.]DownTime}))

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [September 21, 2022, 11:57am UTC](https://forum.inductiveautomation.com/t/down-time-timer/64315/9 "2022-09-21T11:57:57Z")

</div>

Something like that will trigger when a tag becomes true. It reads the current timer from a tag called `active_time` in the same folder as the tag that changed, compute the difference in seconds between the previous timestamp and the current timestamp, add it to the previous active\_time value and write it back to the tag:

```python
if not initialChange and newValue.value:
	timer_path = "{}/active_time".format(event.tagPath.parentPath)
	current_timer = system.tag.readBlocking([timer_path])[0].value
	new_timer = (newValue.timestamp.getTime() - previousValue.timestamp.getTime()) / 1000
	system.tag.writeBlocking([timer_path], [current_timer + new_timer])

```

You can monitor as many tags as you want with it, just add them to the tag list. As long as they have a tag called `active_time` in the same folder, it will work.

PS: You might need to rework it a bit to better handle the initialChange.

---

<div class="post-metadata">

**Author:** ![Benjamin\_Wagner](https://avatars.discourse-cdn.com/v4/letter/b/b5ac83/32.png) [@Benjamin\_Wagner](https://forum.inductiveautomation.com/u/Benjamin_Wagner)\
**Post date:** [September 21, 2022, 3:50pm UTC](https://forum.inductiveautomation.com/t/down-time-timer/64315/10 "2022-09-21T15:50:11Z")

</div>

This works well for getting the total downtime throughout the day. Would it be possible to make this work to show it as it is ticking off the time by seconds? I want to be able to show it in real time as they are down.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [September 28, 2022, 5:07pm UTC](https://forum.inductiveautomation.com/t/down-time-timer/64315/11 "2022-09-28T17:07:53Z")

</div>

You could do this where you display it.  
First, a clarification: the code in my previous message counts active time, not down time. Adapt as you see fit, but for the rest of this post I’ll use an `active_time` tag that times… active time.  
For example you’d bind a label to `active_time`, which would get you the time it’s been active for this day (assuming you reset it at midnight).  
If the machine is currently down, then there’s nothing to change.  
If the machine is currently running, then you’ll need to add the time between when the tag was last updated to now.  
So, an expression like this would do the job:

```auto
if ({the_tag_you_monitor},
    {active_time},
    {active_time} + secondsBetween({active_time.timestamp}, now())
)

```

I haven’t tested it, take it as conceptual code

```auto

```
