---
title: "How to create Historical OEE Operations 3.0"
id: "how-to-create-historical-oee-operations-3-0"
project: "Knowledge Base"
url: "https://docs.sepasoft.com/articles/knowledge-base/how-to-create-historical-oee-operations-3-0"
updated: 2026-07-23T18:08:35Z
---

# How to create Historical OEE Operations 3\.0

When an operation is not captured due to missed operation begin and end.

Inputs required:

* Equipment Path
* Changeover Start Date
* Production Start Date
* Production End Date
* Work Order Name

## Step-by-step guide

1. Modify script to input the required items above and execute the script.

*Verify results using the MES Value Editor Tool as well as Analysis to ensure your operation data was saved and the results are accurate.*

Add Historical Operation

Code

```
def add_run(eq_path, changeover_start_date, production_start_date, production_end_date, wo_name):
    """
    add_run - Adds a production run including operations settings, production and changeover segments, and work order information.
     
    :param: eq_path - String, path to equipment where the operation took place
    :param: changeover_start_date, Date, Changeover segment start date
    :param: production_start_date, Date, Production segment start date
    :param: production_end_date, Date, Production segment start date
    :param: wo_name, String, Name of Work order
     
    """
 
    def check_then_add_tag_collector_value(eq_path, collector_name, date, value):
        """
        check_then_add_tag_collector_value - Adds a tag collector value at the specified date.  If the tag collector exists, update tag collector with new value.
         
        :param: eq_path - String, path to equipment where the tag collector is inserted
        :param: collector_name - String, Name of tag collector to input: https://help.sepasoft.com/docs/display/MHD/Tag+Collector+Types#TagCollectorTypes-TagCollectorTypes
        :param: date - Date, where to insert the tag collector value.
        :param: value - String, the value of the tag collector to insert or update.
        """
 
        prev_value = system.mes.getTagCollectorPreviousValue(eq_path, collector_name, '', date)
        prev_timestamp = system.mes.getTagCollectorPreviousTimeStamp(eq_path, collector_name, '', date)
 
        if value == None:
            value = ''
        if value != prev_value:
            if str(prev_timestamp) == str(date):
                system.mes.updateTagCollectorValue(eq_path, collector_name, '', date, value)
            else:
                system.mes.addTagCollectorValue(eq_path, collector_name, '', date, value)
        #End of check_then_add_tag_collector_value()
 
    def end_production_tag_collector(eq_path, date):
        """
        end_production_tag_collector - Adds blank tag collector values signifying the end of operation.
         
        :param: eq_path - String, path to equipment where the operation took place
        :param: date - Date, where to insert the tag collector value.
        """
         
        endProdSettings = ['Equipment Product Code', 'Equipment Operation UUID', 'Equipment Work Order']
        for collector_name in endProdSettings:
            system.mes.addTagCollectorValue(eq_path, collector_name, '', date, '')
        #End of end_production_tag_collector()
 
    def get_and_set_production_settings(response_segment, date, wo_name):
        """
        get_and_set_production_settings - Builds a list of production settings necessary for an operation.
        Calls the function: check_then_add_tag_collector_value() to configure production settings via tag collector values.
         
        :param: response_segment - MESResponseSegment object - https://help.sepasoft.com/docs/display/MHD/Response+Segment
        :param: date, Date - Date of tag collector value; pass through for check_then_add_tag_collector_value() function
        :param: wo_name, String, Name of Work order
        """
 
        prod_settings_names = response_segment.getComplexPropertyItemNames('ResponseProduction')
        response_name = response_segment.getName()
         
        for prod_setting_name in prod_settings_names:
         
            parent_path = "\".join(eq_path.split("\")[:-1])
            prod_setting_path = "%s\%s" % (parent_path, prod_setting_name.replace(":", "\"))
            prod_settings = response_segment.getProductionSettingsProperty(prod_setting_path)
             
            settings = {}
            settings['Equipment Mode'] = prod_settings.getModeRef().getMESObject().getModeCode() #both
             
            #add tag collector values on changeover
            if '_CO' in response_name and wo_name and wo_name != '':
             
                settings['Equipment Infeed Count Scale'] = prod_settings.getInfeedScale()
                settings['Equipment Infeed Units'] = prod_settings.getInfeedUnits()
                settings['Equipment Outfeed Units'] = prod_settings.getOutfeedUnits()
                settings['Equipment Package Count'] = prod_settings.getPackageCount()
                settings['Equipment Product Code'] = mat_def_name
                settings['Equipment Standard Rate'] = prod_settings.getOEERate()
                settings['Equipment Reject Units'] = prod_settings.getRejectUnits()
                settings['Line Infeed Count Equipment UUID'] = prod_settings.getInfeedEquipmentRefUUID()
                settings['Line Outfeed Count Equipment UUID'] = prod_settings.getOutfeedEquipmentRefUUID()
                settings['Equipment Operation UUID'] = response_segment.getPropertyValue('OperationsResponseRefUUID')
                 
                check_then_add_tag_collector_value(eq_path, 'Equipment Work Order', date, wo_name)
             
            for key, value in settings.iteritems():
                check_then_add_tag_collector_value(eq_path, key, date, value)  
         
        #End of get_and_set_production_settings()
      
    #Get quantity and material from work order
    wo = system.mes.workorder.getMESWorkOrder(wo_name)
    mat_def_name = wo.getMaterialRefName()
     
    #Format production and changeover segment names
    prod_seg_name = "%s-%s" % (mat_def_name, eq_path.replace('\', ':'))
    co_seg_name = "%s_CO" % (prod_seg_name)
                 
    #Create the changeover segment
    co_seg = system.mes.createSegment(co_seg_name, eq_path, False)
    co_seg = co_seg.executeImmediately(changeover_start_date)
    co_seg.setPropertyValue('EndDateTime', production_start_date)
     
    get_and_set_production_settings(co_seg, changeover_start_date, wo_name)
    system.mes.saveMESObject(co_seg)
     
    #Create the Production segment
    prod_seg = system.mes.createSegment(prod_seg_name, eq_path, False)
     
    #get quantity of counts from equipment count tag collector to set on the production response segment
    quantity = system.mes.getTagCollectorDeltaValue(eq_path, 'Equipment Count', 'Material Out', production_start_date, production_end_date)
     
    prod_seg.setMaterial('Material Out', mat_def_name, eq_path, quantity)
    prod_seg = prod_seg.executeImmediately(production_start_date)
    prod_seg.setPropertyValue('EndDateTime', production_end_date)
     
    get_and_set_production_settings(prod_seg, production_start_date, wo_name)
    #insert blank values signaling end of a run
    end_production_tag_collector(eq_path, production_end_date)
    system.mes.saveMESObject(prod_seg)
     
    ##Determine if Work Order Reference gets applied to the Operations Response
    opResp = system.mes.loadMESObject(prod_seg.getPropertyValue('OperationsResponseRefUUID'))
    woLink = system.mes.getMESObjectLinkByName('WorkOrder', wo_name)
    opResp.setWorkOrderLink(woLink)
    system.mes.saveMESObject(opResp)
     
    """
        Creating segments for both the changover and production results in two different operations
        Need to point the changeover segment to the production segment operations response.
    """
     
    #set the changeover Operations Response UUID to the Production Operations Response UUID
    co_seg.setPropertyValue('OperationsResponseRefUUID',prod_seg.getPropertyValue('OperationsResponseRefUUID'))
    system.mes.saveMESObject(co_seg)
    ##End add_run ##

eq_path = 'Enterprise\El Dorado Hills\Packaging Area\Packaging Line 3'
changeover_start_date = system.date.parse('DEC 20, 2022 18:13', 'MMMM dd, yyyy hh:mm')
production_start_date =  system.date.parse('DEC 20, 2022 18:14', 'MMMM dd, yyyy hh:mm')
production_end_date =  system.date.parse('DEC 20, 2022 19:14', 'MMMM dd, yyyy hh:mm')
wo_name = 'wo1111'

add_run(eq_path, changeover_start_date, production_start_date, production_end_date, wo_name)
```
