# How long has my machine been stopped?

**URL:** <https://forum.inductiveautomation.com/t/how-long-has-my-machine-been-stopped/58256>\
**Category:** Ignition\
**Created:** [April 9, 2022, 1:55pm UTC](https://forum.inductiveautomation.com/t/how-long-has-my-machine-been-stopped/58256 "2022-04-09T13:55:38Z")\
**Posts on this page:** 5\
**Page:** 1

<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:** [April 9, 2022, 1:55pm UTC](https://forum.inductiveautomation.com/t/how-long-has-my-machine-been-stopped/58256/1 "2022-04-09T13:55:38Z")

</div>

I should be able to figure this out but my head is a little muzzy today.

- I have the machine status logged on-change in the default historian.
- I want my Perspective view to display how long the machine has been stopped (when stopped and blank when running).
- The tag’s history setting has its Max Time Between Samples set to 1 hour so there could be multiple stop records and we would need to retrieve the first to know how long the machine is stopped.

How can I retrieve this t\_stamp value efficiently? I could create a tag binding and run a script transform to figure it out but I’d have to retrieve quite a lot of records to be sure that I got the first stop event. That doesn’t sound smart.  
I can disable the visibility if the machine is running so that part is OK.

---

<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:** [April 9, 2022, 2:34pm UTC](https://forum.inductiveautomation.com/t/how-long-has-my-machine-been-stopped/58256/2 "2022-04-09T14:34:55Z")

</div>

I prefer using a dedicated table for events, not tag history. Multiple sources can share the table using a source device column. Then use a conventional query the “TOP 1” or “LIMIT 1” record from the given source device using “ORDER BY t\_stamp DESC”. The primary key should be t\_stamp+source.

---

<div class="post-metadata">

**Author:** ![schenk](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@schenk](https://forum.inductiveautomation.com/u/schenk)\
**Post date:** [April 9, 2022, 5:07pm UTC](https://forum.inductiveautomation.com/t/how-long-has-my-machine-been-stopped/58256/3 "2022-04-09T17:07:18Z")

</div>

If all you want to do is display the time the equipment has currently been down for, I would create a binding to the tags TimeStamp, then use an expression binding like  
`secondsBetween(now(), {view.custom.tagTimeStamp})`.  
I use this script in a project library and a transform to display the time values as a string

```auto
def convertSecondsToDisplayTime(seconds):
	minutes, seconds = divmod(seconds, 60)
	hours, minutes = divmod(minutes, 60)
	days, hours = divmod(hours, 24)

	if days > 0 and hours > 1:
		display = '%d Day%s, %d Hr%s' %(days, 's' if days>1 else '', hours, 's' if hours>1 else '')
	elif days > 0:
		display = '%d Day%s' %(days, 's' if days>1 else '')
	elif hours > 0:
		display = '%d:%02d Hr%s' %(hours, minutes,'s' if hours > 1 else '')
	elif minutes > 0:
		display = '%d:%02d Min%s' %(minutes, seconds, 's' if minutes > 1 else '')
	else:
		display = '%d Sec' %seconds
	return display

```

---

<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:** [April 9, 2022, 7:37pm UTC](https://forum.inductiveautomation.com/t/how-long-has-my-machine-been-stopped/58256/4 "2022-04-09T19:37:18Z")

</div>

> [@schenk](#):
>
> I would create a binding to the tag's TimeStamp ...

That will do! I forgot that the timestamp doesn't get updated if the value hasn't changed.

 ![Time since run-stop](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/9/89c56570452e455325c10e0b3724a64f240441dd.png)

Now I'm just giving the time since the run status last changed state. Set the text color to CSS variable `--success` for elapsed run-time and `--error` for elapsed stop-time. Nice!

Thank you.

---

<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:** [April 9, 2022, 9:11pm UTC](https://forum.inductiveautomation.com/t/how-long-has-my-machine-been-stopped/58256/5 "2022-04-09T21:11:05Z")

</div>

I modified your code as follows:

```auto
def format_seconds_to_hhmmss(seconds):
	minutes, seconds = divmod(seconds, 60)
	hours, minutes = divmod(minutes, 60)

	if hours:
		return "%1i:%02i:%02i" % (hours, minutes, seconds)
	elif minutes:
		return "%02i:%02i" % (minutes, seconds)
	else:
		return "%02i" % (seconds)

```

This results in the following:

```auto
                      27 s --> 27
                      95 s --> 01:35
                     120 s --> 02:00
                    3700 s --> 1:01:40
170 * 3600 + 2 * 60 + 13 s --> 170:02:13

```

So it handles more that 24 hours nicely.

Thanks for the inspiration.
