Skip to content

M7-S1 — A capability as a Function: the reading evaluator

Section titled “M7-S1 — A capability as a Function: the reading evaluator”
ModuleM7 — Extend through documented extension points (extension strand)
After this session you canBuild a parameterised Function that a builder configures in the designer, deploy it to your tenant and call it from a reaction.
Time67 min with a facilitator · about 67 min on your own
Before you startM6-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 deeperFunctions: 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 Condition Monitoring module holds two entities and two proxies:

EntityFieldsWho writes it
Threshold RulesAsset class (reference to the proxy Monitored Classes), Measure (Pressure, Vibration, Temperature), Unit, Warn above, Alarm above, ActivePlanners, in the designer’s forms
Meter ReadingsAsset (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 noteThe 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.

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:

ParameterMapped fromWhy it is a parameter
valuethe reading’s Valuethe measurement
unitthe reading’s Unitto refuse a reading in another unit than its rule
warn_above, alarm_abovethe mapped Warn above and Alarm abovethresholds are records a planner edits (test 1)
rule_unitthe mapped Rule unitthe unit the thresholds are in
deadband_percenta fixed value the builder sets, for example 2a 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.

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.

  1. 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).
  2. Open Meter Readings › Fields. Find Rule and its three mapped fields; open Warn above and see that it shows the rule’s value.
  3. 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.
  4. 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.
  5. Open Reactions on Meter Readings: none yet.
  6. 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

  1. Create reading-evaluator/ with func.yaml, package.json and tsconfig.json from the template, with the name reading-evaluator.

  2. Write params.yaml:

    version: "1"
    parameters:
    - name: value
    - name: unit
    - name: warn_above
    - name: alarm_above
    - name: rule_unit
    - name: deadband_percent
  3. Write src/index.ts:

    import { Context } from 'faas-js-runtime';
    type Level = 'normal' | 'warn' | 'alarm'; // a wire contract: never rename or reuse a value
    const 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 };
  4. 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"}}'
  5. Stop it and zip it without node_modules and build: zip -r ../reading-evaluator.zip . -x 'node_modules/*' 'build/*'.

Deploy

  1. 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.
  2. Reload until Provisioned. Its parameter list shows the six names.

Wire it

  1. On Meter Readings, add the reaction Evaluate reading: On Entry Created, Event Executor Type System, Triggering Space, Execute Function reading-evaluator.
  2. Map value to Value, unit to Unit, warn_above to Warn above, alarm_above to Alarm above, rule_unit to Rule unit, and deadband_percent to the fixed value 2. Save.
  3. 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 from message. Save.

Prove it

  1. 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.”
  2. Create a reading for P-104, Pressure, 9.1 bar. Level is Normal: 9.1 is within the 2 % dead band above 9.
  3. Create a reading for C-220, Vibration, 7.5 mm/s with the vibration rule. Level is Alarm.
  4. Create a reading with Unit psi and the pressure rule. Level is empty and the note shows the unit message.
  5. 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.
  6. Fix that: add the condition “success is true” to Store level’s criteria on the function’s output, and add a third reaction Flag evaluation problem on the same event with “success is 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.

  1. Change the pump pressure rule’s Warn above to 9.6. Create a 9.5 bar reading: Normal. Put 9 back.
  2. Write down the deployed name from the Function URL.
  3. Record in your notes the five readings and their stored levels.
  4. 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 seeWhat to do
The second local case says warnYou left out the dead band: 9.1 is above 9 but not above 9 × 1.02 = 9.18.
Provisioning does not endAsk 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-evaluatorIt is not Provisioned, or its scope is another module.
Level stays empty for every readingOpen Reaction Logs: check that Store level watches Evaluate reading on Meter Readings and maps result.level.
Level shows the code but no labelThe 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.

PracticeChoose one answer.

A planner asks for a fourth level, Critical, above Alarm. How do you add it without breaking anyone?

  1. Explanation for A

    ✗ Incorrect

    Not this one: renaming or reusing a word breaks every reaction and report that tests it; the levels are a wire contract. See designing the contract.

  2. Explanation for B

    ✓ Correct

    Correct: a contract grows by addition; existing words keep their meaning for every reader. See designing the contract.

  3. Explanation for C

    ✗ Incorrect

    Not this one: changing the type of the output breaks every reader at once. See designing the contract.

Check your answer

The correct answer is B. Open the explanation under each option to see why.

PracticeChoose one answer.

Why does the evaluator receive the thresholds as parameters instead of reading Threshold Rules itself?

  1. Explanation for A

    ✗ Incorrect

    Not this one: scope decides where a Function is offered, not what data it may receive. See the model the Function serves.

  2. Explanation for B

    ✓ Correct

    Correct: the reading carries its rule’s thresholds as mapped fields, so the builder maps them and the Function needs no access. See the model the Function serves.

  3. Explanation for C

    ✗ Incorrect

    Not this one: params.yaml names parameters but does not validate them; the code must. See designing the contract.

Check your answer

The correct answer is B. Open the explanation under each option to see why.

PracticeChoose one answer.

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?

  1. Explanation for A

    ✗ Incorrect

    Not this one: any answer counts as the action having run, so the success path is taken. See designing the contract.

  2. Explanation for B

    ✓ Correct

    Correct: the engine saw a successful action; only the output says otherwise, so the chain must read it. See the lab’s step 16.

  3. Explanation for C

    ✗ Incorrect

    Not this one: there is no per-action retry in the reaction engine. See M4-S2: one run.

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”
PracticeChoose one answer.

A builder wants a quieter sensor class to use a dead band of 5 %. What must change?

  1. Explanation for A

    ✗ Incorrect

    Not this one: the dead band is a parameter, so code does not change. See designing the contract.

  2. Explanation for B

    ✓ Correct

    Correct: the dead band is a builder’s tuning knob, set where the builder maps the parameters. See designing the contract.

  3. Explanation for C

    ✗ Incorrect

    Not this one: the runtime environment holds only the tenant and the API address, nothing a builder sets. See M6-S1: a Function’s life.

Check your answer

The correct answer is B. Open the explanation under each option to see why.

PracticeChoose one answer.

Why does the Function return a level instead of creating the work order itself?

  1. Explanation for A

    ✗ Incorrect

    Not this one: with a credential it could call the API; the reason is the guarantee, not a limit. See storing the result.

  2. Explanation for B

    ✓ Correct

    Correct: keeping the Function to one decision leaves the rest as configuration, which tests 4, 8 and 10 of the guarantee need. See storing the result.

  3. Explanation for C

    ✗ Incorrect

    Not this one: reactions create work orders all the time; the point is who can change that behaviour. See storing the result.

Check your answer

The correct answer is B. Open the explanation under each option to see why.

PracticeChoose one answer.

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?

  1. Explanation for A

    ✓ Correct

    Correct: 9.1 is within the band above 9; the behaviour is the contract working. See designing the contract.

  2. Explanation for B

    ✗ Incorrect

    Not this one: values are not rounded before comparison; the dead band decides. See designing the contract.

  3. Explanation for C

    ✗ Incorrect

    Not this one: mapped fields show the rule’s current values; nothing is cached here. See the model the Function serves.

Check your answer

The correct answer is A. Open the explanation under each option to see why.

PracticeChoose one answer.

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?

  1. Explanation for A

    ✗ Incorrect

    Not this one: the level is written once, when the reading is evaluated; nothing re-evaluates old readings. See storing the result.

  2. Explanation for B

    ✓ Correct

    Correct: the level is stored at evaluation, but the mapped field shows the rule’s current value; keep that in mind when you read history, or store the thresholds used. See the model the Function serves.

  3. Explanation for C

    ✗ Incorrect

    Not this one: a reading keeps pointing at its rule; nothing fails when the rule is edited. See the model the Function serves.

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.