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

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.

Tag Properties

Mysql master table.

Mysql data table.

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

Thank you,
Govind Suthar

1 Like

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.

1 Like

If you wanted to strictly use the historian, then using system.tag.queryTagCalculations | Ignition User Manual you can use the "DurationOn" calculation to determine how long that tag has been on as seen by the historian.

3 Likes

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

How to get time difference between machine_on and machine_ off time?

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.

1 Like

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

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.

I need a these value in a tag.

where to add start and end date component?

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.

1 Like

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.

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.

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:

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.

Tag change scripting: Script about calculate the cumulative downtime - #25 by Transistor.

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.

1 Like