Raise a Work Order from a Tag
Machine 1 has a low coolant signal. When it goes true, nobody should have to notice it and fill in a form. A gateway script can raise the work order for you.
The tricky part is not raising the work order. It is raising it once. A signal can flicker or stay true for hours, and each change must not add another job to the backlog.
You will build three things:
- A work order template that the raised work orders come from.
- A script function that checks for open work and raises a work order only if there is none.
- A tag change script that calls the function when the signal goes true.
Create the Template and the Tag
Kanoa raises work orders from templates. The script needs a template to point at, so you create it first, the same way you did in Raise a One-Time Work Order.
-
Navigate to Configuration → Maintenance → Work Orders and, on the Work Orders tab, add a template.
-
Fill it in:
Field Value Title Machine 1 - Coolant lowDescription Machine 1 reported low coolant. Check the level and top up. Look for leaks.Asset CoreForge\Philadelphia\Machining\Machine 1Maintenance type Corrective Priority High Requested by Your own user Schedule type One Time -
Save it. Leave its version status as Draft for now.
You don't have a real coolant sensor, so you'll use a memory tag as the signal.
-
In the Designer Tag Browser, select the
defaulttag provider. -
Create a new tag with these settings:
Setting Value Name Machine1CoolantLowTag type Memory Tag Data type Boolean Value false
Write the Script Function
Put the logic in a script library, not in the tag script. A library function is easy to test from the Script Console and can be reused by other events.
The function does four things, in order:
- Finds the template by its title.
- Asks Kanoa for work orders from that template that are still active.
- Stops if it finds any.
- Otherwise raises a new work order.
-
In the Designer, open the project that your gateway uses for gateway event scripts. In this training that is
KANOA_SCRIPTS. -
Create a script library called
trainingMaintenance. -
Paste in this code:
trainingMaintenanceASSET_PATH = 'CoreForge\\Philadelphia\\Machining\\Machine 1'TEMPLATE_TITLE = 'Machine 1 - Coolant low'def raiseCoolantLowWorkOrder():"""Raise one corrective work order for low coolant on Machine 1,unless one from the same template is still open.Returns (workOrderEventId, message). workOrderEventId is None if nothing was raised."""logger = system.util.getLogger('trainingMaintenance')userId = system.kanoa.security.getIDPUserId({'userName': 'SYSTEM'})# 1. Find the templatetemplates = system.kanoa.cmms.getWorkOrders({'title': TEMPLATE_TITLE})if templates.getRowCount() != 1:return None, 'Expected one template titled %s, found %d' % (TEMPLATE_TITLE, templates.getRowCount())workOrderId = system.kanoa.utilities.getFieldValue('workOrderId', templates)priorityId = system.kanoa.utilities.getFieldValue('workOrderPriorityId', templates)assetId = system.kanoa.utilities.getFieldValue('assetId', templates)# 2. Is a work order from this template still active?active = system.kanoa.cmms.getWorkOrderEvents({'workOrderId': workOrderId, 'isActive': True})# 3. If so, do nothingif active.getRowCount() > 0:name = active.getValueAt(0, 'workOrderEventName')logger.info('Coolant low on Machine 1, but %s is still open. Nothing raised.' % name)return None, 'Skipped. %s is still open.' % name# 4. Raise a new work order, due nowworkOrderEventInfo = {'workOrderId': workOrderId,'workOrderPriorityId': priorityId,'requestedBy': userId,'dueDate': system.date.now(),'assetId': assetId}workOrderEventId, message = system.kanoa.cmms.addWorkOrderScheduleEvent(workOrderEventInfo, userId)logger.info('Coolant low on Machine 1. Raised work order event %s. %s' % (workOrderEventId, message))return workOrderEventId, message
A few things to notice:
getWorkOrdersreturns the template.getWorkOrderEventsreturns the work orders raised from it, each with anisActiveflag.addWorkOrderScheduleEventnames the new work order from the next serial number, such asMWO00004. It is the same function Kanoa uses when a schedule or condition generates work.SYSTEMis the user recorded as the requester, because no person raised this work order.
addWorkOrderScheduleEvent has its own safety net. If an active work order from the same template exists, it still adds a new one, but with the status Missed. Your check stops the extra record from being created at all.
Test It from the Script Console
Test the function before you wire it to a tag.
-
Open the Script Console in the Designer (Tools → Script Console).
-
Run this:
print trainingMaintenance.raiseCoolantLowWorkOrder()print trainingMaintenance.raiseCoolantLowWorkOrder() -
The first line prints a new work order event id and
None. The second printsNoneand a message saying the first work order is still open. -
Navigate to Maintenance → Work Orders and find the new
Machine 1 - Coolant lowwork order in the Open group.
If the function says it expected one template, check the title in Configuration → Maintenance → Work Orders. It must match exactly.
Call It from a Tag Change Script
-
In the Tag Browser, double-click
Machine1CoolantLowto open the tag editor. -
Select Tag Event Scripts and the Value Changed event.
-
Paste in this script:
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):# Only act when the signal goes true, and ignore the first value after a gateway startif initialChange or not currentValue.value:returnworkOrderEventId, message = trainingMaintenance.raiseCoolantLowWorkOrder() -
Click OK and save the tag.
The script runs on the gateway. Nobody has to be logged in, and no screen has to be open.
- In the Tag Browser, set
Machine1CoolantLowtotrue. - Check Maintenance → Work Orders. If you already ran the Script Console test, there is no second work order, because the first is still open.
- Set the tag to
false, thentrueagain. Still no second work order. - Open the work order and take it to Completed, as you did in Work a Work Order.
- Set the tag to
false, thentrue. Now a new work order appears, because nothing is open.
You can also check the gateway log. Search for the trainingMaintenance logger to see each decision the script made.
What You Built
You built an automatic corrective work order. A machine signal goes true, a gateway tag script calls trainingMaintenance.raiseCoolantLowWorkOrder, and Kanoa raises one work order from the Machine 1 - Coolant low template. While that work order is open, the signal can go true as often as it likes and nothing more is raised. You can copy this pattern for any signal, such as a spindle alarm, by changing the template title and the tag.