# How to calculate machine total hours and working hours using the tag history?

**URL:** <https://forum.inductiveautomation.com/t/how-to-calculate-machine-total-hours-and-working-hours-using-the-tag-history/93596>\
**Category:** Ignition\
**Tags:** ignition81, vision\
**Created:** [July 23, 2024, 10:42am UTC](https://forum.inductiveautomation.com/t/how-to-calculate-machine-total-hours-and-working-hours-using-the-tag-history/93596 "2024-07-23T10:42:41Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Govind\_Suthar1](https://avatars.discourse-cdn.com/v4/letter/g/a87d85/32.png) [@Govind\_Suthar1](https://forum.inductiveautomation.com/u/Govind_Suthar1)\
**Post date:** [July 23, 2024, 10:42am UTC](https://forum.inductiveautomation.com/t/how-to-calculate-machine-total-hours-and-working-hours-using-the-tag-history/93596/1 "2024-07-23T10:42:41Z")

</div>

Hello all,

I have a require to calculate machine total hours and working hours using the tag historian. created a two Boolean memory tags and log in the MySQL database on on-change sample mode.

I want to calculate the machine working and total hours on daily basis. (every 24 hour)

PFA screenshots for reference.

Tags are logged in the database using the "on change" sample mode.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/3/53ba1cd7be18c48e07dc393f285d1ee61246d3e7.jpeg)

Tag Properties

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/a/facd201a41c93912fb3784ed3af8261a5c89fd49.jpeg)

Mysql master table.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/3/139caada94ebb77dd85d33317b1d4a2940ae883e.png)

Mysql data table.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/7/c7ae5a869e608eed9408d5514e6907a5f3956307.jpeg)

Is it possible to calculate the total hours and working hours of the machine using the tag history?

Thank you,  
Govind Suthar

---

<div class="post-metadata">

**Author:** ![liam1](https://avatars.discourse-cdn.com/v4/letter/l/e68b1a/32.png) [@liam1](https://forum.inductiveautomation.com/u/liam1)\
**Post date:** [July 23, 2024, 11:30am UTC](https://forum.inductiveautomation.com/t/how-to-calculate-machine-total-hours-and-working-hours-using-the-tag-history/93596/2 "2024-07-23T11:30:49Z")

</div>

You can use the use Timestamp property of the machine\_working\_hour to calculate the time delta between the last state change and current time, and have this time added to a accumulation tag (memory tag that stores seconds or minutes ).

You can use a tag onChange script perform the calculation and storage of the time elapsed to the accumulation tag.

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

---

<div class="post-metadata">

**Author:** ![bschroeder](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bschroeder/32/13260_2.png) [@bschroeder](https://forum.inductiveautomation.com/u/bschroeder)\
**Post date:** [July 23, 2024, 11:37am UTC](https://forum.inductiveautomation.com/t/how-to-calculate-machine-total-hours-and-working-hours-using-the-tag-history/93596/3 "2024-07-23T11:37:18Z")

</div>

If you wanted to strictly use the historian, then using [system.tag.queryTagCalculations | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-tag/system-tag-queryTagCalculations) you can use the "DurationOn" calculation to determine how long that tag has been on as seen by the historian.

---

<div class="post-metadata">

**Author:** ![Govind\_Suthar1](https://avatars.discourse-cdn.com/v4/letter/g/a87d85/32.png) [@Govind\_Suthar1](https://forum.inductiveautomation.com/u/Govind_Suthar1)\
**Post date:** [July 23, 2024, 12:50pm UTC](https://forum.inductiveautomation.com/t/how-to-calculate-machine-total-hours-and-working-hours-using-the-tag-history/93596/4 "2024-07-23T12:50:01Z")

</div>

> [@liam1](#):
>
> You can use a tag onChange script perform the calculation

Here , I added the new accumulation tag and log in the database.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/0/40f1886d8a7a61f25d85cfc60f6679ed7685f6e3.png)

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/a/8a4d9a3c531051eaa1fe3118ea5744478c03a8d8.png)

How to get time difference between machine\_on and machine\_ off time?

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [July 23, 2024, 1:00pm UTC](https://forum.inductiveautomation.com/t/how-to-calculate-machine-total-hours-and-working-hours-using-the-tag-history/93596/5 "2024-07-23T13:00:01Z")

</div>

I wouldn't use that method and follow what @bschroeder is telling you. There's a built-in function to calculate the Duration On from history that will work much easier than doing it manually and will be more accurate as well.

---

<div class="post-metadata">

**Author:** ![Govind\_Suthar1](https://avatars.discourse-cdn.com/v4/letter/g/a87d85/32.png) [@Govind\_Suthar1](https://forum.inductiveautomation.com/u/Govind_Suthar1)\
**Post date:** [July 23, 2024, 1:06pm UTC](https://forum.inductiveautomation.com/t/how-to-calculate-machine-total-hours-and-working-hours-using-the-tag-history/93596/6 "2024-07-23T13:06:19Z")

</div>

Is this syntax **system.tag.queryTagCalculations** defined in the gateway events ?

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [July 23, 2024, 1:10pm UTC](https://forum.inductiveautomation.com/t/how-to-calculate-machine-total-hours-and-working-hours-using-the-tag-history/93596/7 "2024-07-23T13:10:25Z")

</div>

It's a scripting function as linked by @bschroeder. Are you needing these values in your tag or on a screen? With the script, you can pick any start/end date and get the total running time between those times. Are you wanting the current running total? If so, how often does it need to update? If only needing the running time for the last 24 hours or yesterday, you'll have to set the appropriate start/end times or duration when calling the script.

---

<div class="post-metadata">

**Author:** ![Govind\_Suthar1](https://avatars.discourse-cdn.com/v4/letter/g/a87d85/32.png) [@Govind\_Suthar1](https://forum.inductiveautomation.com/u/Govind_Suthar1)\
**Post date:** [July 24, 2024, 10:22am UTC](https://forum.inductiveautomation.com/t/how-to-calculate-machine-total-hours-and-working-hours-using-the-tag-history/93596/8 "2024-07-24T10:22:28Z")

</div>

> [@michael.flagler](#):
>
> Are you needing these values in your tag or on a screen?

I need a these value in a tag.

where to add start and end date component?

---

<div class="post-metadata">

**Author:** ![bschroeder](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bschroeder/32/13260_2.png) [@bschroeder](https://forum.inductiveautomation.com/u/bschroeder)\
**Post date:** [July 24, 2024, 11:49am UTC](https://forum.inductiveautomation.com/t/how-to-calculate-machine-total-hours-and-working-hours-using-the-tag-history/93596/9 "2024-07-24T11:49:04Z")

</div>

If you need them in a tag you will need to setup a gateway timer event, and then use the function supplied to get the data from the historian and then write it to a tag.

---

<div class="post-metadata">

**Author:** ![Govind\_Suthar1](https://avatars.discourse-cdn.com/v4/letter/g/a87d85/32.png) [@Govind\_Suthar1](https://forum.inductiveautomation.com/u/Govind_Suthar1)\
**Post date:** [July 25, 2024, 7:25am UTC](https://forum.inductiveautomation.com/t/how-to-calculate-machine-total-hours-and-working-hours-using-the-tag-history/93596/10 "2024-07-25T07:25:25Z")

</div>

> paths = [  
> "[Sample\_Tags]Machine\_working\_hour",  
> "[Sample\_Tags]Total\_hour"  
> ]
> 
> calc = ["DurationOn"]
> 
> end = system.date.now()  
> start = system.date.parse("2024-07-19 00:00:00")
> 
> data = system.tag.queryTagCalculations(paths, calc, start, end)

I took a trail above script in script console, but, it did not work it.

---

<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:** [July 25, 2024, 11:17am UTC](https://forum.inductiveautomation.com/t/how-to-calculate-machine-total-hours-and-working-hours-using-the-tag-history/93596/11 "2024-07-25T11:17:29Z")

</div>

You've posted your code as a quotation. You should have posted it as \</\> preformatted text. See [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831).

I (and many others) gave up on tag history for things like hourly and daily reports. The historian doesn't inter/extrapolate to give you the edge count values. e.g., If you have a count value for 09:50 and one for 10:05 and ask for an aggregate function of 10:00 to 11:00 it will start its calculations on the 10:05 value. If you tried to manipulate the data by script to generate the 10:00 value you'd have the problem of how far back to go in history to ensure you got the previous value.

From my response to [https://forum.inductiveautomation.com/t/includeboundingvalues-not-working/90821:](https://forum.inductiveautomation.com/t/includeboundingvalues-not-working/90821:)

> I found Historian OK for general trending and useful for checking back on what happened at a certain time. For anything like production reports I recommend doing your own history. We logged each machine with a gateway script running on the hour and every 15 minutes as well as on status change of the "running" tag. With columns for t\_stamp, running, cycle count1, good parts count, SKU (recipe) and FirstOutAlarm we were able to generate excellent historical and realtime views.  
> 1 A never-resetting counter.

More discussion: [Save machine count on each change of run status - #16 by josborn](https://forum.inductiveautomation.com/t/save-machine-count-on-each-change-of-run-status/58492/16).

Tag change scripting: [Script about calculate the cumulative downtime - #25 by Transistor](https://forum.inductiveautomation.com/t/script-about-calculate-the-cumulative-downtime/82940/25).

If you do this and also scheduled logging on the hour or every 15 minutes using gateway scheduled events then the SQL and reporting for any interval becomes very simple.
