From an hour meter to a PM you can't miss
Say Pump P-101 needs its bearings greased and its seal flush checked every 2,000 run hours. The run-hours tutorial gave it an hour meter you can trust: an integer odometer that never resets and survives power cycles. That's the input. What it doesn't give you yet is the part maintenance actually lives with:
- Enough warning to plan the work. A "PM due" alarm that shows up the hour it's due is too late to order parts or schedule a shutdown.
- Due and overdue that mean different things. Due is "do it now". Overdue is "we're past the grace period, and somebody needs to know".
- A sign-off people can trust. It records when the work was done and at what hours, without wiping lifetime hours. It can't happen by accident, can't happen while the pump is running, and can't be done by just anyone.
- No false alarms from bad data. A blank interval, a meter someone reset, or a program download shouldn't make every pump in the plant go overdue at once, or quietly stop a PM from ever coming due.
This page builds that in Studio 5000 (one UDT and one routine, in ladder and structured text for every rung) and in Ignition (a UDT, three alarm priorities, a Perspective card, a secured sign-off, and a PM log). Then it covers testing, the mistakes we see most, and how to grow it to a whole plant.
How it works
- Count hours since the PM, one at a time. The run-hours page worked out hours since PM by subtraction: the odometer minus the reading at the last PM. That works until the odometer is ever reset or restored to a lower number. Then the subtraction goes negative, and the PM silently stops coming due. Here the PM logic keeps its own trip counter,
HrsSincePM, which only goes up when the odometer goes up by exactly one. Any other change gets flagged, not counted. - Compare against three points: a warning point (a percentage of the interval), the interval itself, and an overdue point (the interval plus grace hours). Each stage turns the one before it off.
- Sign off deliberately. The HMI sets a request bit. The PLC clears it immediately, turns it into a one-scan pulse, and only accepts it with the pump proven stopped. Accepting it records the odometer reading, the hours this interval actually ran, the PM count and the clock, then zeroes the trip counter. The odometer is never touched.
- Don't trust typed-in numbers. Settings out of range are flagged and ignored, and the last good settings stay in use.
The data: one UDT per asset
Everything for one asset's PM lives in one tag of a user-defined type, PM_Hours. Ignition gets one matching UDT, so adding the next pump means one PLC tag, one copy of the routine (or one AOI call, see extensions), and one Ignition instance. The two inputs come from the run-hours logic, renamed per asset:
The members of PM_Hours, grouped by who writes them:
Everything that counts is a DINT. A DINT holds whole numbers up to 2,147,483,647, and an hour count will never get there. PctUsed is the only REAL, and it's recalculated from integers every scan, so its rounding never builds up. (The run-hours page shows what happens when you accumulate hours in a REAL: it stops counting at 8,192.)
The logic in Studio 5000
One routine, P101_PM_Logic, in the same task as the run-hours logic and called right after it. It's split into three parts below so each can be explained, but the rung numbers run straight through, 0 to 16. The ladder and the structured text do the same thing in the same order with the same tag names. Pick whichever is your plant standard.
Part 1: count hours since the PM (rungs 0 to 3)
Ladder
Four rungs. Open "as text" under any rung to copy it into the Studio 5000 rung editor.
Drawn the way the Studio 5000 ladder editor lays it out: inputs on the left, outputs pushed to the right rail. The small numbers with arrows are where the editor shows live values; they read 0 here because nothing is online.
On a phone, swipe sideways to see the whole rung.
Rung 0 as text
[MOV(P101_RunHrs,P101_PM.RunHrsNow),SUB(P101_PM.RunHrsNow,P101_PM.LastRunHrs,P101_PM.Delta),MOV(P101_PM.RunHrsNow,P101_PM.LastRunHrs)];Rung 1 as text
EQU(P101_PM.Delta,1)ADD(P101_PM.HrsSincePM,1,P101_PM.HrsSincePM);Rung 2 as text
NEQ(P101_PM.Delta,0)NEQ(P101_PM.Delta,1)[OTL(P101_PM.MeterJump),MOV(P101_PM.Delta,P101_PM.LastJump)];Rung 3 as text
LES(P101_PM.HrsSincePM,0)CLR(P101_PM.HrsSincePM);Rungs 0 to 3 as text
[MOV(P101_RunHrs,P101_PM.RunHrsNow),SUB(P101_PM.RunHrsNow,P101_PM.LastRunHrs,P101_PM.Delta),MOV(P101_PM.RunHrsNow,P101_PM.LastRunHrs)];
EQU(P101_PM.Delta,1)ADD(P101_PM.HrsSincePM,1,P101_PM.HrsSincePM);
NEQ(P101_PM.Delta,0)NEQ(P101_PM.Delta,1)[OTL(P101_PM.MeterJump),MOV(P101_PM.Delta,P101_PM.LastJump)];
LES(P101_PM.HrsSincePM,0)CLR(P101_PM.HrsSincePM);Structured Text
The same four steps in structured text:
(* Part 1: count run hours since the last PM, one hour at a time *)
P101_PM.RunHrsNow := P101_RunHrs; (* read the odometer once *)
P101_PM.Delta := P101_PM.RunHrsNow - P101_PM.LastRunHrs;
P101_PM.LastRunHrs := P101_PM.RunHrsNow; (* remember it for next scan *)
IF P101_PM.Delta = 1 THEN
P101_PM.HrsSincePM := P101_PM.HrsSincePM + 1; (* a normal run hour *)
END_IF;
IF P101_PM.Delta <> 0 AND P101_PM.Delta <> 1 THEN
P101_PM.MeterJump := 1; (* went back or jumped: not counted *)
P101_PM.LastJump := P101_PM.Delta;
END_IF;
IF P101_PM.HrsSincePM < 0 THEN
P101_PM.HrsSincePM := 0; (* a hand-typed negative starts over *)
END_IF;
What each rung does
- Rung 0 copies the odometer into
RunHrsNowonce, works outDelta, and stores the reading for the next scan. Everything after this uses the copy. An HMI or another task can change a tag partway through a scan, so two reads of the same tag in one routine can disagree. One read means one answer. - Rung 1 adds one hour when the odometer moved by exactly one. The run-hours logic adds at most one hour per scan, so this is the only normal change.
- Rung 2 catches every other non-zero change: a reset to 0 (big negative), a restored backup (negative), or starting hours typed in (big positive). None of them are counted.
MeterJumplatches so somebody looks, andLastJumpkeeps the size of the jump. - Rung 3 guards a value people type in during commissioning. A negative
HrsSincePMwould push the PM out past its interval.
In the structured text, the three assignments at the top are rung 0, and each IF block is one of rungs 1 to 3, in the same order.
Part 2: the sign-off (rungs 4 to 8)
Ladder
Five rungs. Rung 5 is the one-shot; rungs 6 and 7 are the accept and refuse paths.
Drawn the way the Studio 5000 ladder editor lays it out: inputs on the left, outputs pushed to the right rail. The small numbers with arrows are where the editor shows live values; they read 0 here because nothing is online.
On a phone, swipe sideways to see the whole rung.
Rung 4 as text
XIC(S:FS)OTU(P101_PM.ResetReq);Rung 5 as text
XIC(P101_PM.ResetReq)[OTU(P101_PM.ResetReq),ONS(P101_PM.ResetStor)OTE(P101_PM.ResetOS)];Rung 6 as text
XIC(P101_PM.ResetOS)XIO(P101_RunFbk)[MOV(P101_PM.RunHrsNow,P101_PM.LastPM_RunHrs),MOV(P101_PM.HrsSincePM,P101_PM.LastPM_HrsUsed),CLR(P101_PM.HrsSincePM),ADD(P101_PM.PM_Count,1,P101_PM.PM_Count),GSV(WallClockTime,,DateTime,P101_PM.LastPM_Date[0]),OTU(P101_PM.MeterJump),OTU(P101_PM.ResetRefused)];Rung 7 as text
XIC(P101_PM.ResetOS)XIC(P101_RunFbk)OTL(P101_PM.ResetRefused);Rung 8 as text
XIC(P101_PM.AckReq)[OTU(P101_PM.MeterJump),OTU(P101_PM.ResetRefused),OTU(P101_PM.AckReq)];Rungs 4 to 8 as text
XIC(S:FS)OTU(P101_PM.ResetReq);
XIC(P101_PM.ResetReq)[OTU(P101_PM.ResetReq),ONS(P101_PM.ResetStor)OTE(P101_PM.ResetOS)];
XIC(P101_PM.ResetOS)XIO(P101_RunFbk)[MOV(P101_PM.RunHrsNow,P101_PM.LastPM_RunHrs),MOV(P101_PM.HrsSincePM,P101_PM.LastPM_HrsUsed),CLR(P101_PM.HrsSincePM),ADD(P101_PM.PM_Count,1,P101_PM.PM_Count),GSV(WallClockTime,,DateTime,P101_PM.LastPM_Date[0]),OTU(P101_PM.MeterJump),OTU(P101_PM.ResetRefused)];
XIC(P101_PM.ResetOS)XIC(P101_RunFbk)OTL(P101_PM.ResetRefused);
XIC(P101_PM.AckReq)[OTU(P101_PM.MeterJump),OTU(P101_PM.ResetRefused),OTU(P101_PM.AckReq)];Structured Text
Structured text has no ONS, so the one-shot is written out: true on the first scan the request is seen, false after that, and the storage bit cleared when the request is gone. That's exactly what the ladder ONS does. (OSRI works too, see the one-shot tutorial, but the written-out version matches the ladder line for line.)
(* Part 2: the PM sign-off *)
IF S:FS THEN
P101_PM.ResetReq := 0; (* drop a request left over from before a power cycle *)
END_IF;
(* Same as the ONS rung: true for one scan per request *)
IF P101_PM.ResetReq THEN
P101_PM.ResetReq := 0; (* hand the bit back to the HMI *)
P101_PM.ResetOS := NOT P101_PM.ResetStor;
P101_PM.ResetStor := 1;
ELSE
P101_PM.ResetOS := 0;
P101_PM.ResetStor := 0;
END_IF;
(* Accepted only with the pump proven stopped *)
IF P101_PM.ResetOS AND NOT P101_RunFbk THEN
P101_PM.LastPM_RunHrs := P101_PM.RunHrsNow; (* odometer at service; never zeroed *)
P101_PM.LastPM_HrsUsed := P101_PM.HrsSincePM; (* how long this interval really ran *)
P101_PM.HrsSincePM := 0; (* start the trip meter over *)
P101_PM.PM_Count := P101_PM.PM_Count + 1;
GSV(WallClockTime, , DateTime, P101_PM.LastPM_Date[0]); (* UTC on v16+ *)
P101_PM.MeterJump := 0;
P101_PM.ResetRefused := 0;
END_IF;
(* Pump running: refuse, and say why *)
IF P101_PM.ResetOS AND P101_RunFbk THEN
P101_PM.ResetRefused := 1;
END_IF;
(* Maintenance acknowledges a meter jump or a refused sign-off *)
IF P101_PM.AckReq THEN
P101_PM.MeterJump := 0;
P101_PM.ResetRefused := 0;
P101_PM.AckReq := 0;
END_IF;
What each rung does
- Rung 4 throws away a request that was sitting in the controller when it lost power or went to Program mode. A sign-off should be something a person just did, not something left over from yesterday. In ladder, the
ONSalready ignores a request that's on at the first scan (prescan sets its storage bit1) and rung 5 then clears it, so this rung is belt and braces. In structured text it's what makes the two versions behave the same, because a plain:=keeps its old value through prescan. - Rung 5 does two jobs with one request. The
OTUhands the bit back right away, so the HMI knows the PLC got it and the request can't sit there and fire later. TheONSturns the request intoResetOS, a pulse that's on for exactly one scan. The PLC clears the request in the same rung that reads it, not at the bottom of the routine. Clear it at the bottom, and a write that lands mid-scan, after this rung, gets wiped before anything sees it. - Rung 6 is the sign-off. It needs the pulse and the pump proven stopped (
XIOon the run feedback). It saves the odometer reading and the hours this interval actually ran, starts the trip meter over, counts the PM, and reads the controller clock.P101_RunHrsis never written. That's the lifetime number, and it stays. - Rung 7 is the refusal: same pulse, pump running, so
ResetRefusedlatches for the HMI to show. The request was already cleared in rung 5, so a refused sign-off is simply gone. The tech stops the pump and signs off again. - Rung 8 clears the two latched messages and hands
AckReqback, the same way rung 5 hands backResetReq.
If your plant uses a lockout status or a local maintenance key switch, put that contact in series with the XIO on rung 6, and in the IF on the structured text side.
One sign-off, scan by scan
Part 3: settings and the three stages (rungs 9 to 16)
Ladder
Eight rungs, then the end of the routine.
Drawn the way the Studio 5000 ladder editor lays it out: inputs on the left, outputs pushed to the right rail. The small numbers with arrows are where the editor shows live values; they read 0 here because nothing is online.
On a phone, swipe sideways to see the whole rung.
Rung 9 as text
[LES(P101_PM.IntervalHrs,1),GRT(P101_PM.IntervalHrs,100000),LES(P101_PM.WarnPct,1),GRT(P101_PM.WarnPct,99),LES(P101_PM.GraceHrs,0),GRT(P101_PM.GraceHrs,100000)]OTE(P101_PM.CfgFault);Rung 10 as text
XIO(P101_PM.CfgFault)[MOV(P101_PM.IntervalHrs,P101_PM.IntervalAct),CPT(P101_PM.WarnAtHrs,P101_PM.IntervalHrs*P101_PM.WarnPct/100),ADD(P101_PM.IntervalHrs,P101_PM.GraceHrs,P101_PM.OverdueAtHrs)];Rung 11 as text
GEQ(P101_PM.IntervalAct,1)OTE(P101_PM.CfgActive);Rung 12 as text
XIC(P101_PM.CfgActive)[SUB(P101_PM.IntervalAct,P101_PM.HrsSincePM,P101_PM.HrsLeft),CPT(P101_PM.PctUsed,P101_PM.HrsSincePM*100.0/P101_PM.IntervalAct)];Rung 13 as text
[CLR(P101_PM.State),XIC(P101_PM.CfgActive)MOV(1,P101_PM.State),XIC(P101_PM.CfgActive)GEQ(P101_PM.HrsSincePM,P101_PM.WarnAtHrs)MOV(2,P101_PM.State),XIC(P101_PM.CfgActive)GEQ(P101_PM.HrsSincePM,P101_PM.IntervalAct)MOV(3,P101_PM.State),XIC(P101_PM.CfgActive)GEQ(P101_PM.HrsSincePM,P101_PM.OverdueAtHrs)MOV(4,P101_PM.State)];Rung 14 as text
EQU(P101_PM.State,2)OTE(P101_PM.Warn);Rung 15 as text
EQU(P101_PM.State,3)OTE(P101_PM.Due);Rung 16 as text
EQU(P101_PM.State,4)OTE(P101_PM.Overdue);Rungs 9 to 16 as text
[LES(P101_PM.IntervalHrs,1),GRT(P101_PM.IntervalHrs,100000),LES(P101_PM.WarnPct,1),GRT(P101_PM.WarnPct,99),LES(P101_PM.GraceHrs,0),GRT(P101_PM.GraceHrs,100000)]OTE(P101_PM.CfgFault);
XIO(P101_PM.CfgFault)[MOV(P101_PM.IntervalHrs,P101_PM.IntervalAct),CPT(P101_PM.WarnAtHrs,P101_PM.IntervalHrs*P101_PM.WarnPct/100),ADD(P101_PM.IntervalHrs,P101_PM.GraceHrs,P101_PM.OverdueAtHrs)];
GEQ(P101_PM.IntervalAct,1)OTE(P101_PM.CfgActive);
XIC(P101_PM.CfgActive)[SUB(P101_PM.IntervalAct,P101_PM.HrsSincePM,P101_PM.HrsLeft),CPT(P101_PM.PctUsed,P101_PM.HrsSincePM*100.0/P101_PM.IntervalAct)];
[CLR(P101_PM.State),XIC(P101_PM.CfgActive)MOV(1,P101_PM.State),XIC(P101_PM.CfgActive)GEQ(P101_PM.HrsSincePM,P101_PM.WarnAtHrs)MOV(2,P101_PM.State),XIC(P101_PM.CfgActive)GEQ(P101_PM.HrsSincePM,P101_PM.IntervalAct)MOV(3,P101_PM.State),XIC(P101_PM.CfgActive)GEQ(P101_PM.HrsSincePM,P101_PM.OverdueAtHrs)MOV(4,P101_PM.State)];
EQU(P101_PM.State,2)OTE(P101_PM.Warn);
EQU(P101_PM.State,3)OTE(P101_PM.Due);
EQU(P101_PM.State,4)OTE(P101_PM.Overdue);Structured Text
The same eight steps in structured text:
(* Part 3: check the settings, then work out the stage *)
P101_PM.CfgFault := (P101_PM.IntervalHrs < 1) OR (P101_PM.IntervalHrs > 100000)
OR (P101_PM.WarnPct < 1) OR (P101_PM.WarnPct > 99)
OR (P101_PM.GraceHrs < 0) OR (P101_PM.GraceHrs > 100000);
IF NOT P101_PM.CfgFault THEN (* bad entries keep the last good settings *)
P101_PM.IntervalAct := P101_PM.IntervalHrs;
P101_PM.WarnAtHrs := P101_PM.IntervalHrs * P101_PM.WarnPct / 100; (* DINT math, truncated *)
P101_PM.OverdueAtHrs := P101_PM.IntervalHrs + P101_PM.GraceHrs;
END_IF;
P101_PM.CfgActive := P101_PM.IntervalAct >= 1;
IF P101_PM.CfgActive THEN
P101_PM.HrsLeft := P101_PM.IntervalAct - P101_PM.HrsSincePM; (* negative = overdue *)
P101_PM.PctUsed := P101_PM.HrsSincePM * 100.0 / P101_PM.IntervalAct; (* 100.0 forces REAL *)
END_IF;
(* 0 not set up, 1 OK, 2 warning, 3 due, 4 overdue: the last true test wins *)
P101_PM.State := 0;
IF P101_PM.CfgActive THEN
P101_PM.State := 1;
IF P101_PM.HrsSincePM >= P101_PM.WarnAtHrs THEN P101_PM.State := 2; END_IF;
IF P101_PM.HrsSincePM >= P101_PM.IntervalAct THEN P101_PM.State := 3; END_IF;
IF P101_PM.HrsSincePM >= P101_PM.OverdueAtHrs THEN P101_PM.State := 4; END_IF;
END_IF;
(* One stage bit per alarm, so only one can ever be on *)
P101_PM.Warn := P101_PM.State = 2;
P101_PM.Due := P101_PM.State = 3;
P101_PM.Overdue := P101_PM.State = 4;
What each rung does
- Rung 9 checks the three settings someone can type in. An interval of 0 is the classic one: "hours used ≥ 0" is always true, so every asset with a blank interval goes overdue the moment it's downloaded. Negative numbers, and percentages of 0 or 100 and up, are just as wrong. The upper limits (100,000 hours is over 11 years of running) also keep the math in rung 10 far away from a
DINToverflow. - Rung 10 copies good settings into the active ones and works out the two hour points. A bad entry changes nothing: the asset keeps its last good schedule, and
CfgFaulttells everyone the new number wasn't taken. The three settings are taken as a set, so if the new interval is fine but the percentage isn't, neither is used until both are right.WarnAtHrsisDINTmath, and with all-integer operands Logix truncates the division,2 so 90% of 2,005 is 1,804, not 1,804.5. A PM schedule doesn't care about half an hour. - Rung 11 says whether there's anything to compare against. A new tag starts at 0, and until someone enters a good interval the stages stay off and
Statereads 0, "not set up", rather than OK. - Rung 12 is for the screen.
HrsLeftgoes negative when the PM is late, which is a useful number to show ("157 h overdue"). InPctUsed, the100.0makes itREALmath, and the divide only runs whenIntervalActis at least 1, so it can never divide by zero. - Rung 13 works out the stage as one number,
State. The legs of a branch run top to bottom, so each leg that's true overwrites the one above it and the last true leg wins: 1 if there's a good interval, 2 once past the warning point, 3 once past the interval, 4 once past the overdue point. The structured text does the same with anIFper step, in the same order. - Rungs 14 to 16 turn
Stateinto the three stage bits for the alarms. Because they all come from one number, only one can ever be on, so Ignition shows one PM alarm, not three, however the settings are entered.
With GraceHrs set to 0, the overdue point is the interval itself, so the PM goes straight from warning to overdue and the "due" stage never shows. That's deliberate: zero grace means late is late.
The whole routine in structured text
(* Part 1: count run hours since the last PM, one hour at a time *)
P101_PM.RunHrsNow := P101_RunHrs; (* read the odometer once *)
P101_PM.Delta := P101_PM.RunHrsNow - P101_PM.LastRunHrs;
P101_PM.LastRunHrs := P101_PM.RunHrsNow; (* remember it for next scan *)
IF P101_PM.Delta = 1 THEN
P101_PM.HrsSincePM := P101_PM.HrsSincePM + 1; (* a normal run hour *)
END_IF;
IF P101_PM.Delta <> 0 AND P101_PM.Delta <> 1 THEN
P101_PM.MeterJump := 1; (* went back or jumped: not counted *)
P101_PM.LastJump := P101_PM.Delta;
END_IF;
IF P101_PM.HrsSincePM < 0 THEN
P101_PM.HrsSincePM := 0; (* a hand-typed negative starts over *)
END_IF;
(* Part 2: the PM sign-off *)
IF S:FS THEN
P101_PM.ResetReq := 0; (* drop a request left over from before a power cycle *)
END_IF;
(* Same as the ONS rung: true for one scan per request *)
IF P101_PM.ResetReq THEN
P101_PM.ResetReq := 0; (* hand the bit back to the HMI *)
P101_PM.ResetOS := NOT P101_PM.ResetStor;
P101_PM.ResetStor := 1;
ELSE
P101_PM.ResetOS := 0;
P101_PM.ResetStor := 0;
END_IF;
(* Accepted only with the pump proven stopped *)
IF P101_PM.ResetOS AND NOT P101_RunFbk THEN
P101_PM.LastPM_RunHrs := P101_PM.RunHrsNow; (* odometer at service; never zeroed *)
P101_PM.LastPM_HrsUsed := P101_PM.HrsSincePM; (* how long this interval really ran *)
P101_PM.HrsSincePM := 0; (* start the trip meter over *)
P101_PM.PM_Count := P101_PM.PM_Count + 1;
GSV(WallClockTime, , DateTime, P101_PM.LastPM_Date[0]); (* UTC on v16+ *)
P101_PM.MeterJump := 0;
P101_PM.ResetRefused := 0;
END_IF;
(* Pump running: refuse, and say why *)
IF P101_PM.ResetOS AND P101_RunFbk THEN
P101_PM.ResetRefused := 1;
END_IF;
(* Maintenance acknowledges a meter jump or a refused sign-off *)
IF P101_PM.AckReq THEN
P101_PM.MeterJump := 0;
P101_PM.ResetRefused := 0;
P101_PM.AckReq := 0;
END_IF;
(* Part 3: check the settings, then work out the stage *)
P101_PM.CfgFault := (P101_PM.IntervalHrs < 1) OR (P101_PM.IntervalHrs > 100000)
OR (P101_PM.WarnPct < 1) OR (P101_PM.WarnPct > 99)
OR (P101_PM.GraceHrs < 0) OR (P101_PM.GraceHrs > 100000);
IF NOT P101_PM.CfgFault THEN (* bad entries keep the last good settings *)
P101_PM.IntervalAct := P101_PM.IntervalHrs;
P101_PM.WarnAtHrs := P101_PM.IntervalHrs * P101_PM.WarnPct / 100; (* DINT math, truncated *)
P101_PM.OverdueAtHrs := P101_PM.IntervalHrs + P101_PM.GraceHrs;
END_IF;
P101_PM.CfgActive := P101_PM.IntervalAct >= 1;
IF P101_PM.CfgActive THEN
P101_PM.HrsLeft := P101_PM.IntervalAct - P101_PM.HrsSincePM; (* negative = overdue *)
P101_PM.PctUsed := P101_PM.HrsSincePM * 100.0 / P101_PM.IntervalAct; (* 100.0 forces REAL *)
END_IF;
(* 0 not set up, 1 OK, 2 warning, 3 due, 4 overdue: the last true test wins *)
P101_PM.State := 0;
IF P101_PM.CfgActive THEN
P101_PM.State := 1;
IF P101_PM.HrsSincePM >= P101_PM.WarnAtHrs THEN P101_PM.State := 2; END_IF;
IF P101_PM.HrsSincePM >= P101_PM.IntervalAct THEN P101_PM.State := 3; END_IF;
IF P101_PM.HrsSincePM >= P101_PM.OverdueAtHrs THEN P101_PM.State := 4; END_IF;
END_IF;
(* One stage bit per alarm, so only one can ever be on *)
P101_PM.Warn := P101_PM.State = 2;
P101_PM.Due := P101_PM.State = 3;
P101_PM.Overdue := P101_PM.State = 4;
Educational example. This code has not been compiled, downloaded, or tested on a controller. It is not production-ready or drop-in code. Treat it as a starting point to review, adapt, and test on your own system. It is standard control logic, not safety logic.
Edge cases, traced by hand
These are the situations that break most PM logic. Each one was traced through both the ladder and the structured text above, with the example settings (interval 2,000 h, warn at 90%, grace 100 h).
A DINT odometer won't roll over in practice. The meters that do roll over are external ones: a drive's elapsed-hours parameter, an INT counter at 32,767, a mechanical meter at 99,999. If you feed one of those in, a rollover shows up as one MeterJump and one lost hour, which is the safe direction. If you know the rollover value, add a rung that treats that exact Delta as +1.
In Ignition
This is written for Ignition 8.1; the same pieces exist in 8.3. Make one UDT definition, PM_Hours, with two parameters: PLC (the device connection name) and Asset (for example P101). Each pump is then one instance.
Set the read-only members to Read Only in the tag editor, so a binding can't write to them by mistake. Write Permissions on a tag apply everywhere the tag is used, so they're the real lock. The button logic only makes it friendly.
Alarms: three priorities, one at a time
Keep Critical for process and safety alarms. A PM that's 100 hours late matters, but it shouldn't share a color with a high-pressure trip. Set the Display Path to {Asset} so the alarm table reads "P101 · PM overdue". Because the PLC only ever has one stage on, the alarm journal reads as a clean history: warning on, warning off as due comes on, due off as overdue comes on, overdue off at the sign-off.
The Perspective card
- Bindings: the view takes one parameter,
tagPath(for example[default]Pumps/P101/PM), and every value uses an indirect tag binding to{tagPath}/Member. Then the same view works for every asset. - Status pill: text bound to
StateText, with the background mapped fromState(gray, green, amber, orange, red). - Progress bar: bound to
PctUsedwith the expression transformmin({value}, 100)and a max of 100, so an overdue pump shows a full bar, not a broken one. - Hours left: bound to
HrsLeftwith the transformif({value} < 0, abs({value}) + " h overdue", {value} + " h left"). - Banners: one shown while
ResetRefusedis on ("Sign-off refused: the pump was running. Stop it and sign off again.") and one whileCfgFaultis on ("New PM settings were out of range. Still using the last good ones."). - Sign off PM opens a confirmation popup (below). Its
enabledproperty is bound toRunFbkwith the transform!{value}, so it greys out while the pump runs. The PLC still checks; the greyed-out button is only a hint. - Acknowledge is a One-Shot Button:
valuebound bidirectionally toAckReq,setValuetrue, andconfirm.enabledon.5 It shows its writing state until the PLC clearsAckReq, which is positive proof the PLC got it. - Settings opens a small popup with numeric entry fields bound bidirectionally to
IntervalHrs,WarnPctandGraceHrs, shown only to the planner role.
The sign-off: confirmation, security, and who did it
The sign-off uses a normal button and a popup instead of a One-Shot Button, because we want to record who signed off and why. The popup has a note field, a Confirm button and a Cancel button. The Sign off PM button opens it:
# Card > "Sign off PM" button > onActionPerformed
def runAction(self, event):
system.perspective.openPopup("pmSignOff", "PM/SignOffPopup",
params={"tagPath": self.view.params.tagPath},
title="Sign off PM", modal=True)
The Confirm button in the popup does the rest. The order matters: it writes who and why first, then the request, so the log script always finds the name waiting for it.
# PM/SignOffPopup > "Confirm" button > onActionPerformed
def runAction(self, event):
base = self.view.params.tagPath # for example "[default]Pumps/P101/PM"
user = self.session.props.auth.user.userName
roles = self.session.props.auth.user.roles
msg = self.getSibling("Message")
if "Maintenance" not in roles:
msg.props.text = "Only maintenance can sign off a PM."
return
note = self.getSibling("Note").props.text or ""
system.tag.writeBlocking([base + "/SignOffBy", base + "/SignOffNote"], [user, note])
q = system.tag.writeBlocking([base + "/ResetReq"], [True])[0]
if not q.isGood(): # for example, no write permission
msg.props.text = "Sign-off not sent: " + str(q)
return
system.perspective.closePopup("pmSignOff")
The role check is there to give a clear message. The tag's Write Permissions are what actually stop a user without the role, whatever screen they try it from. Gateway-scoped scripts run without a user, so Write Permissions don't limit them: keep anything that writes ResetReq out of gateway scripts. And none of this replaces the PLC's own check that the pump is stopped.
The PM log
Log the PM when the PLC accepts it, not when someone clicks. That way a refused sign-off never shows up as a completed PM. The trigger is PM_Count going up by exactly one. A download or a hand edit changes it by some other amount, or backwards, and isn't logged. A table like this works on any database Ignition supports (adjust the ID column for yours):
CREATE TABLE pm_log (
id SERIAL PRIMARY KEY, -- IDENTITY or AUTO_INCREMENT on other databases
t_stamp TIMESTAMP NOT NULL, -- gateway time of the sign-off
asset VARCHAR(40) NOT NULL, -- "P101"
pm_number INT NOT NULL, -- PM_Count after the sign-off
run_hrs_at_pm INT NOT NULL, -- LastPM_RunHrs (lifetime odometer)
hrs_since_last_pm INT NOT NULL, -- LastPM_HrsUsed (shows late PMs)
interval_hrs INT NOT NULL, -- IntervalAct at the time
signed_by VARCHAR(80),
note VARCHAR(400)
);
Then add a Value Changed script on PM_Count in the UDT definition, so every instance logs itself:6
# UDT PM_Hours > PM_Count > Tag Events > Value Changed
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
if initialChange or previousValue is None:
return
if not (currentValue.quality.isGood() and previousValue.quality.isGood()):
return
if currentValue.value != previousValue.value + 1:
return # download or hand edit, not a sign-off
base = str(tagPath).rsplit("/", 1)[0]
# Read the record straight from the PLC, so nothing is a poll behind PM_Count
names = ["LastPM_RunHrs", "LastPM_HrsUsed", "IntervalAct"]
items = [qv.value for qv in system.tag.readBlocking([base + "/" + n + ".OpcItemPath" for n in names])]
hrs_at, hrs_used, interval = [qv.value for qv in system.opc.readValues("Ignition OPC UA Server", items)]
asset, by, note = [qv.value for qv in system.tag.readBlocking(
[base + "/Asset", base + "/SignOffBy", base + "/SignOffNote"])]
now = system.date.now()
system.db.runPrepUpdate(
"INSERT INTO pm_log (t_stamp, asset, pm_number, run_hrs_at_pm, hrs_since_last_pm, "
"interval_hrs, signed_by, note) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
[now, asset, currentValue.value, hrs_at, hrs_used, interval, by, note], "PlantDB")
system.tag.writeBlocking(
[base + "/LastPM_By", base + "/LastPM_Time", base + "/SignOffBy", base + "/SignOffNote"],
[by, now, "", ""])
"Ignition OPC UA Server" is the default name of the gateway's built-in OPC connection, and "PlantDB" stands in for your database connection. Tag event scripts run on the gateway in a shared thread pool, so keep them short like this one. For dozens of assets, a Gateway Tag Change script on all the PM_Count tags does the same job. The log is also your backup: if a download ever puts old values back in the PLC, the last row for each asset tells you what HrsSincePM should be.
For history, turn on the historian for HrsSincePM, State and RunHrs, on change. They change at most once an hour, so they cost almost nothing to store. A trend of HrsSincePM is a sawtooth: every drop is a PM, and the height of each tooth is how long that interval really ran.
Commissioning and testing
Test it on the bench, or with the pump locked out, before you trust it. The quickest way to walk the stages is to write HrsSincePM directly from Studio 5000 while online.
- Before the first download, go online and upload tag values if this controller already counts run hours, so the download doesn't put old numbers back. On 5380 and 5580 controllers you can also turn on Preserve online tag values in the Download dialog's options.
- Seed the counts once. Rung 0 overwrites
LastRunHrsevery scan, so writing it online does nothing. Either set its value in the offline project to the current odometer reading before the download, or expect oneMeterJumpon the first scan (that's correct: the odometer "jumped" from 0) and acknowledge it. Then writeHrsSincePMto the hours since the last real PM from the paper log. Save the project online with tag values. - Blank settings: with
IntervalHrsat 0, check thatCfgFaultis on,Stateis 0, and no PM alarm is active in Ignition. - Enter 2000, 90 and 100. Check
IntervalAct2000,WarnAtHrs1800,OverdueAtHrs2100, andCfgFaultoff. - Walk the stages. Write
HrsSincePMto 1799, 1800, 1999, 2000, 2099 and 2100, and check thatStatereads 1, 2, 2, 3, 3, 4, with exactly one PM alarm in Ignition at each step, at the right priority. - Bad entries: type 0, −5 and 150000 into the interval, and 0 and 100 into the percentage. Each should raise
CfgFaultand leaveIntervalActat 2000. Put the good values back. - Normal counting: with the pump running, write the run-hours
RunSecto 3590 and watchHrsSincePMgo up by exactly one about ten seconds later. - Sign-off while running: sign off from a maintenance login with the pump running. Expect
ResetRefusedon, the refused banner, nothing new inpm_log, andPM_Countunchanged. Stop the pump and check that the old request doesn't fire on its own. - Sign-off while stopped: expect
HrsSincePM0,PM_Countup by one,LastPM_RunHrsequal to the odometer,LastPM_Datefilled in, the alarm cleared, one new row inpm_logwith your user name, andP101_RunHrsunchanged. - Security: log in as an operator. Sign off PM should show the role message, and a write to
ResetReqfrom anywhere else should be denied. - Meter jump: on a test controller, write
P101_RunHrslower. ExpectMeterJumpon, the jump size inLastJump, andHrsSincePMunchanged. Acknowledge it from the card. - Power cycle: note the values, cycle power, and check that every count,
PM_Countand the latched bits came back the same. If anything reset, check the controller's SD card load setting (the run-hours page covers it).
Common mistakes
Zeroing the odometer at each PM
It's the quickest way to build it, and it throws away the motor's lifetime hours. Reset the trip counter, record the odometer, and leave RunHrs alone.
Alarming on "hours left ≤ 0" with no guard
A blank or zero interval makes every asset overdue the moment it's downloaded, and then people learn to ignore PM alarms. Check the settings, and keep the last good ones.
Security only on the button
Hiding a button doesn't stop another screen, a script, or a tag browser from writing the bit. Put Write Permissions on the tag, and put the "pump stopped" check in the PLC.
Clearing the request at the bottom
An HMI write can land partway through a scan. Clear the request in the same rung that reads it, or a request that arrives after that rung gets wiped unseen.
Logging the click, not the PM
If the button writes the log, a refused sign-off still looks like a completed PM. Log when PM_Count goes up, which only the PLC can make happen.
Three alarms for one problem
"Hours ≥ warning" and "hours ≥ due" are both true once it's due, so the operator sees two alarms for one PM. Work out one stage number and drive the alarm bits from it, so only one is ever on.
Counting the command
The hour meter has to count run feedback, not the run command, and the sign-off has to check feedback too. A tripped starter with the command still on isn't running.
Downloading over a year of counts
A download puts back whatever was in the offline file. Upload tag values before you download, or use Preserve online tag values, and keep the Ignition log as the record.
Extensions
Several assets
Copying the routine for each pump works for three pumps, not thirty. Wrap it in an Add-On Instruction, PM_Track, with an InOut parameter PM of type PM_Hours and two Inputs, RunHrs (DINT) and RunFbk (BOOL). The logic is the same, with P101_PM. replaced by PM.. Because the data stays in the PM_Hours tag, not inside the AOI, the Ignition UDT doesn't change. Each asset is then one rung:
// One call per asset. P101_PMTrk is the AOI's own instance tag.
PM_Track(P101_PMTrk,P101_PM,P101_RunHrs,P101_RunFbk);
PM_Track(P102_PMTrk,P102_PM,P102_RunHrs,P102_RunFbk);
Two changes inside the AOI: clear PM.ResetReq in the AOI's Prescan routine instead of using S:FS, and check 1756-PM010 for your version's AOI rules, such as which GSV classes are allowed.4 Then test the AOI once, thoroughly, with the checklist above, before it goes into thirty places.
Calendar-based and hours-based, whichever comes first
Some PMs are "every 2,000 hours or every 12 months". A standby pump might only run 300 hours a year, but its seals still age. The calendar half is easiest in Ignition, because the gateway already has the last sign-off time in LastPM_Time. Add a memory tag IntervalDays and an expression tag DaysSincePM with dateDiff({[.]LastPM_Time}, now(60000), "day"), and alarm on it the same way. If the PLC has to own both, add IntervalDays to the UDT, store the sign-off time as a LINT from the WallClockTime object's CurrentValue (microseconds since 1970), and compare days on controllers and firmware that support LINT math. Either way, OR the two together into the stages, and record which one triggered.
Handing off to a CMMS
The PLC knows when a PM is coming due. Your CMMS knows the procedure, the parts and who's on shift. Let each do its own job:
- Create the work order at the warning, not at due. A Value Changed script on
Warn(rising, good quality, not the initial change) can post a work request with the asset,HrsSincePMandHrsLeftto the CMMS's API usingsystem.net.httpClient(). Store the work order number it sends back in a memory tag, so only one is created per cycle. - Close the loop in the plant. When the work order is done, the sign-off still happens at the pump, with the pump stopped, by a maintenance user. Put the work order number in the sign-off note so it lands in
pm_log. Don't let the CMMS writeResetReqremotely unless you're happy for a closed ticket alone to count as a PM (the PLC's stopped check would still apply). - Send hours, not just alarms. Many CMMS packages can schedule from a meter reading. A nightly job that posts
RunHrsfor each asset lets the CMMS run its own meter-based schedule as a cross-check on the PLC.
The short version
Keep a trip counter that only moves forward one run hour at a time. Compare it against a warning point, the interval, and an overdue point so only one stage is ever on, and ignore settings that make no sense. Let the PLC accept a sign-off only as a one-scan pulse with the pump stopped, and record the odometer instead of zeroing it. Let Ignition handle who's allowed, the confirmation, and the log.
Sources
- Rockwell Automation, Studio 5000 Logix Designer online help, "One Shot (ONS)" (prescan sets the storage bit): rockwellautomation.com.
- Rockwell Automation, Logix 5000 Controllers General Instructions, publication 1756-RM018 (CPT and DIV integer truncation, math status flags, S:FS): PDF.
- Rockwell Automation, Logix 5000 Controllers Information and Status, publication 1756-PM015 (GSV and the WallClockTime object): PDF.
- Rockwell Automation, Logix 5000 Controllers Add-On Instructions, publication 1756-PM010 (parameters, Prescan routine, restrictions): PDF.
- Inductive Automation, Ignition 8.1 User Manual, "Perspective - One-Shot Button": docs.inductiveautomation.com.
- Inductive Automation, Ignition 8.1 User Manual, "Tag Event Scripts" and "system.opc.readValues": tag events, readValues.
Instruction details were checked against the vendor documentation linked above. Behavior can change between software and firmware versions, so check the help for the version you run.