M7-S1 — A capability as a Function: the reading evaluator
Section titled “M7-S1 — A capability as a Function: the reading evaluator”| Module | M7 — Extend through documented extension points (extension strand) |
| After this session you can | Build a parameterised Function that a builder configures in the designer, deploy it to your tenant and call it from a reaction. |
| Time | 67 min with a facilitator · about 67 min on your own |
| Before you start | M6-S4 (the map and the guarantee) and M6-S1 (a Function’s life). Larkspur Dev, signed in as Rami Aziz; your terminal with Node.js 20+, npm, zip, curl and jq. The seed has prepared the Condition Monitoring module’s data model, so this strand’s time goes to the extension itself. |
| Go deeper | Functions: run your own code · Execute Custom Function output in chained reactions |
◆ Orientation (1 min) — The first part of the capability
Section titled “◆ Orientation (1 min) — The first part of the capability”In M6-S4 you mapped Larkspur’s condition monitoring onto the ladder: readings in through the API, thresholds as records, a parameterised Function to evaluate each reading, reactions to act on the result, a webhook to the CMMS, a package to ship it. This session builds the core: the reading evaluator. You design its contract first, test it on your computer, deploy it, and wire it so that every new reading gets a level, Normal, Warn or Alarm, stored on the record. M7-S2 brings the readings in from the sensors and acts on the alarms; M7-S3 hands it to a builder.
▤ Concept (6 min) — A Function with a builder’s contract
Section titled “▤ Concept (6 min) — A Function with a builder’s contract”The model the Function serves
Section titled “The model the Function serves”The Condition Monitoring module holds two entities and two proxies:
| Entity | Fields | Who writes it |
|---|---|---|
| Threshold Rules | Asset class (reference to the proxy Monitored Classes), Measure (Pressure, Vibration, Temperature), Unit, Warn above, Alarm above, Active | Planners, in the designer’s forms |
| Meter Readings | Asset (reference to the proxy Monitored Assets), Measure, Value, Unit, Taken at, Source, Rule (reference to Threshold Rules), mapped fields Warn above, Alarm above and Rule unit from the rule, Level (Normal, Warn, Alarm), Evaluation note | The sensor feed (M7-S2), or a person |
The proxies are the seams you designed in ACE: Monitored Assets and Monitored Classes show Asset Maintenance’s records inside Condition Monitoring without copying them. Every reading names the rule that applies to it, and the reading shows that rule’s thresholds as mapped fields. That is what lets the Function receive the thresholds as parameters instead of looking them up: on the 1.10 line a Function has no credential of its own (M6-S1), and it needs none here.
Designing the contract
Section titled “Designing the contract”A Function’s parameters and output are a contract with every builder who maps them and every reaction that reads them. Design it before you write code:
| Parameter | Mapped from | Why it is a parameter |
|---|---|---|
value | the reading’s Value | the measurement |
unit | the reading’s Unit | to refuse a reading in another unit than its rule |
warn_above, alarm_above | the mapped Warn above and Alarm above | thresholds are records a planner edits (test 1) |
rule_unit | the mapped Rule unit | the unit the thresholds are in |
deadband_percent | a fixed value the builder sets, for example 2 | a builder’s tuning knob, not an engineer’s |
The output is {success, message, result: {level, exceeded_by}}, where level is normal,
warn or alarm. These three words are now a wire contract in the sense of
M2-S3: a reaction will store them in the Level category, and
later reactions and reports will test them. Never rename or reuse one. A new level means a new word
added, not an old one changed.
Three rules from the engine shape the code. The parameter file params.yaml names inputs but does
not type or validate them, so the code converts and checks every value. Any answer from the Function
counts as the action having run, so a problem is reported in the body with success: false and a
message a planner can read. And the answer must be JSON; text is read as an error.
The dead band. Sensors are noisy. A reading counts as above a threshold only when it exceeds it by
more than deadband_percent of the threshold, so a pressure that hovers at the limit does not raise and
clear alarms all day. The percentage is a parameter so that a builder can tune it per reaction without a
new deployment.
Storing the result
Section titled “Storing the result”The Function returns a level; it does not write anything. A second reaction, chained on Reaction Succeeded, writes the level and the message onto the reading. Two reasons. The write is plain configuration that a builder can see and change. And the level becomes data: from M7-S2 on, every further action (a work order, a notification, a webhook) is a reaction on the reading’s Level, which a builder can add, change or disable without touching the Function. Remember M4-S2: the chained reaction may run twice for one evaluation on the recovery path, and writing the same level twice is harmless.
flowchart LR R["A reading is created"] --> E["Reaction: Evaluate reading<br/>(Execute Function)"] E -->|"result.level"| S["Reaction on success: Store level<br/>(Update Data Entry, Self)"] S --> L["Reading's Level: Normal, Warn or Alarm"] L -.->|"M7-S2, M7-S3"| X["Reactions on the level:<br/>work order, CMMS, notification"]
What to notice: the Function sits in one place in the chain; everything after the stored level is configuration a builder owns.
▶ Guided demo (10 min) — The model, read as a contract
Section titled “▶ Guided demo (10 min) — The model, read as a contract”Starting state. Larkspur Dev as seeded: the Condition Monitoring module with its model, the threshold rules for the class Centrifugal pumps, and no readings.
- Open Data Entities › Threshold Rules and its list. Three rules for Centrifugal pumps: Pressure in bar (warn 9, alarm 11), Vibration in mm/s (warn 4.5, alarm 7.1), Temperature in °C (warn 80, alarm 95).
- Open Meter Readings › Fields. Find Rule and its three mapped fields; open Warn above and see that it shows the rule’s value.
- Open the field Level: a category with Normal, Warn, Alarm. Read the values’ stored codes:
normal,warn,alarm, the same words the Function will return. - Create a reading by hand: Asset P-104, Measure Pressure, Value 10.2, Unit
bar, Rule the pump pressure rule. Save. Warn above and Alarm above show 9 and 11. Level is empty. - Open Reactions on Meter Readings: none yet.
- Delete the reading.
Screenshot pending The Meter Readings form with the Rule chosen and the mapped Warn above and Alarm above fields filled from it What to notice: the thresholds are shown on the reading, but they live on the rule; change the rule and every new reading sees the new values.
⛏ Lab (40 min) — Build, test, deploy and wire the evaluator
Section titled “⛏ Lab (40 min) — Build, test, deploy and wire the evaluator”Goal. Every reading created in Meter Readings gets its Level and Evaluation note from the deployed reading-evaluator, with thresholds that come from the rule a planner edits.
Before you start. An empty working folder. The project files from
ACE M4-S1 as your template (func.yaml, package.json,
tsconfig.json).
Write and test
-
Create
reading-evaluator/withfunc.yaml,package.jsonandtsconfig.jsonfrom the template, with the namereading-evaluator. -
Write
params.yaml:version: "1"parameters:- name: value- name: unit- name: warn_above- name: alarm_above- name: rule_unit- name: deadband_percent -
Write
src/index.ts:import { Context } from 'faas-js-runtime';type Level = 'normal' | 'warn' | 'alarm'; // a wire contract: never rename or reuse a valueconst num = (v: unknown): number | null => {if (v === null || v === undefined || v === '') return null;const n = Number(v);return Number.isFinite(n) ? n : null;};export function evaluate(p: Record<string, unknown>) {const value = num(p.value);const warn = num(p.warn_above);const alarm = num(p.alarm_above);const band = num(p.deadband_percent) ?? 0;const unit = String(p.unit ?? '').trim();const ruleUnit = String(p.rule_unit ?? '').trim();if (value === null) return { success: false, message: 'The reading has no numeric value.' };if (warn === null || alarm === null) return { success: false, message: 'The reading has no rule with thresholds.' };if (unit !== ruleUnit) return { success: false, message: `Unit ${unit} does not match the rule's unit ${ruleUnit}.` };const over = (limit: number) => value > limit * (1 + band / 100);const level: Level = over(alarm) ? 'alarm' : over(warn) ? 'warn' : 'normal';const limit = level === 'alarm' ? alarm : warn;const exceeded_by = level === 'normal' ? 0 : Math.round((value - limit) * 100) / 100;const message = level === 'normal' ? 'Within limits.' : `Above the ${level} threshold by ${exceeded_by} ${unit}.`;return { success: true, message, result: { level, exceeded_by } };}const handle = async (context: Context) => {const body = (context.body ?? {}) as { data?: { parameters?: Record<string, unknown> }; parameters?: Record<string, unknown> };const input = body.data ?? body;return evaluate(input.parameters ?? {});};export { handle }; -
Install, build and start it (
npm install,npm run build,npm start). In a second terminal, send four cases:Terminal window t() { curl -s -X POST http://localhost:8080 -H 'Content-Type: application/json' -d "$1" | jq -c; }t '{"parameters":{"value":8.7,"unit":"bar","warn_above":9,"alarm_above":11,"rule_unit":"bar","deadband_percent":2}}'t '{"parameters":{"value":9.1,"unit":"bar","warn_above":9,"alarm_above":11,"rule_unit":"bar","deadband_percent":2}}'t '{"parameters":{"value":11.4,"unit":"bar","warn_above":9,"alarm_above":11,"rule_unit":"bar","deadband_percent":2}}'t '{"parameters":{"value":"7.5","unit":"mm/s","warn_above":"4.5","alarm_above":"7.1","rule_unit":"mm/s","deadband_percent":"2"}}'t '{"parameters":{"value":10,"unit":"psi","warn_above":9,"alarm_above":11,"rule_unit":"bar"}}' -
Stop it and zip it without
node_modulesandbuild:zip -r ../reading-evaluator.zip . -x 'node_modules/*' 'build/*'.
Deploy
- In Administration › Applications › Functions, select Add Function: name reading-evaluator, Reaction Custom Action, scope module Condition Monitoring, the ZIP. In its description, write the contract: the six parameters, the output and the three levels. Save.
- Reload until Provisioned. Its parameter list shows the six names.
Wire it
- On Meter Readings, add the reaction Evaluate reading: On Entry Created, Event Executor Type System, Triggering Space, Execute Function reading-evaluator.
- Map
valueto Value,unitto Unit,warn_aboveto Warn above,alarm_aboveto Alarm above,rule_unitto Rule unit, anddeadband_percentto the fixed value 2. Save. - Add the reaction Store level: Reaction Succeeded of Evaluate reading on Meter Readings,
Triggering Space, Update Data Entry on Self: Level from the function’s output
result.level, Evaluation note frommessage. Save.
Prove it
- Create a reading for P-104, Pressure, 9.5
bar, rule Centrifugal pumps / Pressure. Within seconds, Level is Warn and the note reads “Above the warn threshold by 0.5 bar.” - Create a reading for P-104, Pressure, 9.1
bar. Level is Normal: 9.1 is within the 2 % dead band above 9. - Create a reading for C-220, Vibration, 7.5
mm/swith the vibration rule. Level is Alarm. - Create a reading with Unit
psiand the pressure rule. Level is empty and the note shows the unit message. - Open Reaction Logs for that reading: Evaluate reading ran and its output carries
success: false. Store level ran too, because the action succeeded as far as the engine knows, and it wrote an empty Level. On a reading that already had a level, it would have erased it. - Fix that: add the condition “
successis true” to Store level’s criteria on the function’s output, and add a third reaction Flag evaluation problem on the same event with “successis false” that writes only the message into Evaluation note. Create the psi reading again: the note reads the unit message, Level stays empty, and Reaction Logs shows Flag evaluation problem, not Store level.
Screenshot pending Reaction Logs for the psi reading after the fix: Evaluate reading with success false in its output, then Flag evaluation problem, and no Store level What to notice: the engine counted the evaluation as a successful action; only the criteria on the output kept the empty level from being written.
- Change the pump pressure rule’s Warn above to 9.6. Create a 9.5
barreading: Normal. Put 9 back. - Write down the deployed name from the Function URL.
- Record in your notes the five readings and their stored levels.
- Delete your test readings, keeping the reactions and the function.
Check.
- The five local cases gave normal, normal, alarm, alarm and a unit refusal.
- reading-evaluator is Provisioned, scope Condition Monitoring, with its contract in the description.
- Evaluate reading maps all six parameters, with the dead band as a fixed value.
- Readings at 9.5, 9.1 and 7.5 stored Warn, Normal and Alarm.
- A unit mismatch stores no level and writes the explanation into the note.
- Changing a rule’s threshold changed the next reading’s level without any deployment.
- Your notes hold the deployed name and the five results.
Pass condition. New readings receive the level the contract predicts, the thresholds come only from the rules, and a bad reading is explained on the record instead of failing silently.
If something goes wrong
| What you see | What to do |
|---|---|
| The second local case says warn | You left out the dead band: 9.1 is above 9 but not above 9 × 1.02 = 9.18. |
| Provisioning does not end | Ask your training provider whether the function builder is available for Larkspur Dev; your local tests stand as evidence of the code. |
| Execute Function does not offer reading-evaluator | It is not Provisioned, or its scope is another module. |
| Level stays empty for every reading | Open Reaction Logs: check that Store level watches Evaluate reading on Meter Readings and maps result.level. |
| Level shows the code but no label | The category’s option codes differ from the function’s words; that is the wire contract, so fix the category, not the code. |
Model answer
The code. Every parameter is converted and checked, because params.yaml does not type them; an
empty value or threshold, or a unit mismatch, returns success: false with a planner-readable message.
The level is decided against thresholds raised by the dead band, so 9.1 bar is normal against a warn of
9 with 2 %, and 11.4 bar is alarm against 11 × 1.02 = 11.22. The words normal, warn and alarm
are the contract the Level category stores.
The wiring. Evaluate reading sends the reading’s values and the rule’s mapped thresholds as
parameters, so the Function needs no access of its own. Store level writes the output onto the record.
Because any answer counts as the action having run, Store level must test success; the separate
Flag evaluation problem puts the explanation where a planner sees it. Changing a rule changes the next
evaluation at once: the variation is data, which is the first test of the guarantee.
✓ Practice (4 min) — The contract
Section titled “✓ Practice (4 min) — The contract”A planner asks for a fourth level, Critical, above Alarm. How do you add it without breaking anyone?
Check your answer
The correct answer is B. Open the explanation under each option to see why.
Why does the evaluator receive the thresholds as parameters instead of reading Threshold Rules itself?
Check your answer
The correct answer is B. Open the explanation under each option to see why.
The evaluator answers {"success": false, "message": "The reading has no rule with thresholds."}. A
reaction chained on Reaction Succeeded writes result.level onto the reading without criteria. What
happens?
Check your answer
The correct answer is B. Open the explanation under each option to see why.
✓ Practice (5 min) — Building through extension points
Section titled “✓ Practice (5 min) — Building through extension points”A builder wants a quieter sensor class to use a dead band of 5 %. What must change?
Check your answer
The correct answer is B. Open the explanation under each option to see why.
Why does the Function return a level instead of creating the work order itself?
Check your answer
The correct answer is B. Open the explanation under each option to see why.
The reading 9.1 bar against a warn threshold of 9 with a dead band of 2 % is stored as Normal. A planner calls it a bug. What do you explain?
Check your answer
The correct answer is A. Open the explanation under each option to see why.
A planner lowers the pump pressure rule’s Warn above from 9 to 8.5. Yesterday’s reading of 8.8 bar is stored as Normal. What does that reading show today?
Check your answer
The correct answer is B. Open the explanation under each option to see why.
↺ Recap (1 min) — A decision with a contract
Section titled “↺ Recap (1 min) — A decision with a contract”You built the capability’s one piece of code as a decision with a contract: parameters a builder maps, thresholds that are records, three level words that never change meaning, and failures explained on the record. Every reading now carries a level. Next, M7-S2: readings arrive from the sensors through the API with a least-privilege credential, alarms raise work orders, and the CMMS is told.