Write check rules

Write a log check rule from scratch — pick fields, write expressions, set thresholds, run validation.

Check rules are the core of the platform's log analysis: each rule defines a diagnostic pipeline from raw data to conclusion. This tutorial walks you through writing one from scratch.

Rules live in knowledge/px4/rules/*.yaml and are compiled into the Python engine by build-knowledge.mjs. The syntax is a safe subset of Python expressions; thresholds, fields, and operators are declared in YAML and fully validated at build time.

Anatomy of a rule

Take the simplest CPU load rule as an example:
id: px4-cpu-load
group: cpu
slot: cpu
triggers: [cpuload]
compute:
    cpu_max:
        op: max
        of: cpuload.load
condition: cpu_max >= 0.90
title: "CPU 负载关注"
severity: warning
tags: []
suggestion: "CPU 负载关注,关注是否因为开启了过多的模块或日志"
What this rule does: take the max of cpuload.load → if >= 0.90 → emit a warning.

Quick field reference

字段DescriptionRequired
idUnique identifier, e.g. px4-cpu-loadYes
groupGroup name; must be registered in facts.yaml group_orderYes
slotExecution slot; determines which raw topics are availableYes
triggersList of required raw topicsYes
computeData-flow expression (the core)Yes
conditionPython expression; fires when TrueNo
titleConclusion title (supports {var} placeholders)Yes
severitycritical / warning / infoNo
tagsTag list for fault KB matchingNo
suggestionDiagnostic suggestion textNo
Full field-level reference: Rule schema reference.

Write your first rule

Let's write a rule to check if battery remaining is too low. Four steps:

Identify the data source

Battery data lives in the battery_status topic. Open a ULog to see available fields:
  • battery_status[0].remaining — Remaining percentage (0.0-1.0)
  • battery_status[0].voltage_v — Total voltage
  • battery_status[0].voltage_cell_v — Per-cell voltage array

Choose operator and params

We want the minimum of remaining, using the min operator:
compute:
    rem:
        op: min
        of: battery_status[0].remaining
rem is the variable name you choose; reference it as {rem} in title and suggestion.

Set the judgment logic

Use condition to define when to fire, and severity for the level. Multiple branches match top-down:
condition:
    - expr: rem <= 0.10
      severity: critical
      title: "电池剩余电量极低({rem:.0%})"
      suggestion: "立即降落,检查电池健康度"
    - expr: rem <= 0.20
      severity: warning
      title: "电池剩余电量偏低({rem:.0%})"
      suggestion: "尽早返航,关注电压变化"

Complete the metadata

Put it all together in knowledge/px4/rules/my-battery.yaml:
id: px4-battery-remaining
group: battery
slot: battery
triggers: [battery_status]
compute:
    rem:
        op: min
        of: battery_status[0].remaining
condition:
    - expr: rem <= 0.10
      severity: critical
      title: "电池剩余电量极低({rem:.0%})"
      suggestion: "立即降落,检查电池健康度。最低剩余 {rem:.0%}"
    - expr: rem <= 0.20
      severity: warning
      title: "电池剩余电量偏低({rem:.0%})"
      suggestion: "尽早返航,关注电压变化。最低剩余 {rem:.0%}"
tags: [battery_voltage_drop]

Common operators

算子What it doesExample
maxMaximumop: max, of: cpuload.load
minMinimumop: min, of: battery_status[0].remaining
meanMeanop: mean, of: sensor_accel.x
ratio_equalRatio equal to valueop: ratio_equal, of: airspeed_validated.valid, value: 0
worst_reject_ratioEKF rejection ratioSee ekf-innovation rule
head_tail_median_dropHead-tail median voltage dropSee power-sag rule
Full operator catalogue: Rule schema reference §4.

Run build validation

After writing, run the build command to validate:
pnpm build:kb    # --check 模式只校验不写入
The build checks:
  • group registered in facts.yaml
  • tags values defined in facts
  • triggers topics in the whitelist
  • Operator name exists, params complete
  • Template placeholders are all resolvable
Build-time rejections
  • group not registered → group 'xxx' not registered
  • Operator typo → unknown operator 'xxx'
  • Multiple pipes in compute → only one compute key-value pair allowed

Debugging tips

Add display: {log: true} to your rule to print intermediate values in the browser console. Add display: {plots: ["cpu_max"]} to plot the computed result for visual inspection.

Next step

  • 规则清单 — Browse all built-in rules with detailed descriptions
  • 规则编写参考 — Field-level authoritative reference and full operator catalogue