Script about calculate the cumulative downtime

Write a piece of code in Python to calculate the cumulative downtime. When the parameter stop is 0, start accumulating the timer. When the parameter stop is 1, stop the timer in minutes. When the stop is 0, always display the accumulated time instead of displaying the total downtime when it is 1
could anybody help me?

Write the change in values into a database table, then use a query to calc the diff between your dates. Then you have the ability to look at historic downtime as well.
I would also write the timestamp of the currentValue into a custom prop on the tag as well that stores the timestamp when it goes 0, set to None when 1. Then current downtime can simply be the difference between now and the tag's custom prop

2 Likes

The equipment shuts down irregularly, and all the time differences add up, which is a bit difficult. Can Python scripts implement this function

Just calculate the total downtime of the shift, it will be reset to zero at 8 am and 8 pm, and then recalculate

Yes, but I imagine that Python isn't the most efficient approach. Just store uptime and downtime as a one or a zero Boolean tag value, and let your query sum the amount of time the tag was a zero between your specified timestamps.

Do you perform operations in TagHistory?

Tag History is not a good solution for this because you want values at 08:00 and 20:00 and the historian doesn't interpolate well.

The best solution is to record the timestamp, runStatus and count (and the product code, the machine ID and anything else that may prove useful in future) in a separate data table. I recommend using a non-resetting counter so that you can very easily calculate the production between any two dates.

  • You can trigger a record using Gateway Event Scripts, Tag Change event.
  • You can also schedule records using the Gateway Event Scripts, Scheduled. Use this to ensure a count value at 08:00 and 20:00. (In one of my applications I record on the hour and every 15 minutes. This ensures that I have counts for all sorts of weird production schedules - subject to 15 minute start time.)

For calculated uptime / downtime you need to run a query for the machine and time period of interest and then loop through the results to calculate the total uptime / downtime.

This data structure also makes it very easy to produce Time Series charts showing the production count through the shift. Downtime will appear as flat-lines on the chart.

...or perhaps SUM the intervals in the query, so the query returns the end value you are after.

That would require some SQL trickery to present the data in the right format. Not so easy - particularly when successive records might have the same runStatus.

I imagine that this could also be set up as am alarm on the tag, and the alarm duration times could be summed, but some logic would have to be developed to handle alarms that overlap the beginning and end of the shift.

i write it by python expect the 8:00am and 8:00 pm
this is the code

import time
total = 0
tempa = 0
oldtotal = 0
def calculate_downtime(start):    
    global total, tempa, start_time, end_time,oldtotal
    if start == False and tempa == 0:  
        start_time = time.time()  
        tempa = 1  
    elif start == True:
         tempa = 0   
    if tempa == 1:  
        end_time = time.time()  
        total = (end_time - start_time) + oldtotal
        return total/60
    elif tempa == 0:
         oldtotal = total
         return oldtotal/60

But there are still some issues. I used global variables, and when I save the project, the global variables are reset to zero, and the result is also reset to zero. Is there any way to solve the problem of global variables

Where does this script live? How does it get called? Instead of using global variables (which in many applications is considered to be a bad practice) why not write the values to a tag or a database, and use the tag or data table as your ongoing storage point for the information?

1 Like

And why are you using import time.
Ignition has system.date - Ignition User Manual 8.1 - Ignition Documentation functions which will be faster and should do everything you require.

1 Like

how to write the values to a tag in script

system.tag.writeBlocking(tagpaths, values)

runscript("DownTime.calculate_dowmtime",5000,start)

thanks,i will try

Out of curiosity, where does the runscript live? I don't normally see runscript used for this type of task.

Chen, I think we are worn out trying to help on this problem which you have posted in several threads. You are insisting on using global variables and, as you have seen, this is not reliable. You need to forget this idea and do one of the recommended solutions.

  1. Use a database to record machine events.
  2. Use dedicated tags. I think you would need one to record the last time the machine stopped / started, another for the cumulative time. This is messy and you lose all the advantages of solution 1 where you have history and the ability to easily plot graphs.

What is the reason you will not create a database and table for this application? It is the professional, maintainable and useful solution to your problem.