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.
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.
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.
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.
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.
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.
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.
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.
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.