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
| 字段 | Description | Required |
|---|---|---|
id | Unique identifier, e.g. px4-cpu-load | Yes |
group | Group name; must be registered in facts.yaml group_order | Yes |
slot | Execution slot; determines which raw topics are available | Yes |
triggers | List of required raw topics | Yes |
compute | Data-flow expression (the core) | Yes |
condition | Python expression; fires when True | No |
title | Conclusion title (supports {var} placeholders) | Yes |
severity | critical / warning / info | No |
tags | Tag list for fault KB matching | No |
suggestion | Diagnostic suggestion text | No |
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 thebattery_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 voltagebattery_status[0].voltage_cell_v— Per-cell voltage array
Choose operator and params
We want the minimum ofremaining, 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
Usecondition 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 inknowledge/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 does | Example |
|---|---|---|
max | Maximum | op: max, of: cpuload.load |
min | Minimum | op: min, of: battery_status[0].remaining |
mean | Mean | op: mean, of: sensor_accel.x |
ratio_equal | Ratio equal to value | op: ratio_equal, of: airspeed_validated.valid, value: 0 |
worst_reject_ratio | EKF rejection ratio | See ekf-innovation rule |
head_tail_median_drop | Head-tail median voltage drop | See power-sag rule |
Run build validation
After writing, run the build command to validate:pnpm build:kb # --check 模式只校验不写入
The build checks:
-
groupregistered infacts.yaml -
tagsvalues defined in facts -
triggerstopics in the whitelist - Operator name exists, params complete
- Template placeholders are all resolvable
Build-time rejections
-
groupnot registered →group 'xxx' not registered -
Operator typo →
unknown operator 'xxx' -
Multiple pipes in
compute→only one compute key-value pair allowed
Debugging tips
Adddisplay: {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.