Skip to content
Web app (opens in a new tab)Get the kit
Web app (opens in a new tab)Get the kit

No-Code Automation

What Is a Rule Engine? How Devices Decide What to Do

A rule engine is the part of a system that checks facts against if-then rules and acts on the matches. See how one decides, how rule priority settles clashes, and how a small ESP32 board does it.

Three if-then rule cards wired to a generic ESP32 board: the crowned "If temp > 30 °C" rule drives a fan and a red colour light with a padlock, while the "Every 20 min" timer rule only gets the buzzer and its blue request is turned away
Three if-then rule cards wired to a generic ESP32 board: the crowned "If temp > 30 °C" rule drives a fan and a red colour light with a padlock, while the "Every 20 min" timer rule only gets the buzzer and its blue request is turned away

A rule engine is a piece of software that keeps a list of if-then rules, checks them against the latest facts, and carries out the actions of the rules that match. The facts can be sensor readings, a time, or a message. The actions can switch a light, sound a buzzer or move a motor. A good rule engine also decides what happens when two rules want different things at the same moment. That last part, rule priority, is where most of the interesting design lives.

You meet rule engines in banks and insurance software, in smart-home hubs, and in small boards like the ESP32. This guide explains the idea in plain words, compares a few real rule engines, and then opens up a working one: the automation rules on the MakerBuddy IoT kit. You will see its priority order, how it locks an output so two rules cannot fight over it, a second-by-second worked example, and a short Arduino sketch that builds a tiny rule engine of your own.

What a rule engine is, and how it decides

Every rule engine, big or small, works with three things.

  • Facts: what the engine knows right now. On a sensor board, the facts are the latest readings: temperature, distance, light, whether the button is pressed.
  • Rules: each rule has a condition and an action. "If the temperature is above 30 °C, switch the fan on." The condition is the if part. The action is the then part.
  • The engine: the code that keeps checking the facts against every rule's condition and runs the actions of the rules that match.

The software writer Martin Fowler describes a rules engine as a set of production rules, "each of which has a condition and an action", in his short article on rules engines (opens in a new tab). He points out the big difference from ordinary code: you do not decide the order the rules are checked in. The engine does.

So a rule engine decides by running the same cycle again and again:

  1. Read the facts.

  2. Find every rule whose condition is true right now.

  3. If two matching rules want the same thing, pick a winner.

  4. Run the winners' actions.

  5. Go back to step 1.

Step 3 is the one people forget. On a board with one buzzer, it decides who gets the buzzer. If you have read our guide to if-then statements with real sensors, a rule is exactly that if-then statement, saved so a machine can keep checking it for you.

The rule engine cycle in four boxes: 1. read the facts (a 31 °C temperature, the light level, the button), 2. find matching rules (Too hot and Drink water both match, Cool again does not), 3. pick a winner (Too hot gets the colour light, Drink water is turned away), 4. run the actions (colour light red, relay on for the fan), then 5. go back to step 1.
The rule engine cycle in four boxes: 1. read the facts (a 31 °C temperature, the light level, the button), 2. find matching rules (Too hot and Drink water both match, Cool again does not), 3. pick a winner (Too hot gets the colour light, Drink water is turned away), 4. run the actions (colour light red, relay on for the fan), then 5. go back to step 1.

Why not just write if statements?

You can, and for one or two rules you should. A rule engine earns its place when the rules change often, or when people who do not write code need to change them. The Wikipedia article on business rules engines (opens in a new tab) puts this at the centre: the rules are kept apart from the program's code, so they can be defined, tested and changed on their own.

On a learning board, it also lets a 10-year-old build "if it gets dark, turn on the light" from drop-down menus, learning the logic before the punctuation of C++. Our pillar post on how no-code rules teach real logic covers that side in depth.

Types of rule engines compared

Rule engines differ mostly in two ways: what starts a rule, and how they choose between rules that match at the same time. The Wikipedia article on business rules engines (opens in a new tab) names the main families:

  • Forward chaining: start from the facts and fire every rule that matches. The article says most rules engines used by businesses work this way.
  • Reaction rules, also called event-condition-action: wait for an event, check a condition, then act. These usually remember state between checks.
  • Backward chaining: start from a goal and work back to see whether the facts support it.

Here are four real ways to run if-then rules, from a large Java engine to a plain Arduino sketch.

SystemWhere the rules runHow you write a ruleWhat starts a ruleHow clashes are settled
DroolsInside a Java applicationText rules in .drl files with when and then partsFacts matching the when partA number called salience: a higher salience runs first
Home Assistant automationsInside Home AssistantTrigger, condition and action, in the visual editor or in YAMLA trigger, such as a door opening or the sun settingA mode per automation (single, restart, queued, parallel) decides what a second start does
MakerBuddy automation rulesOn the ESP32 board itselfDrop-down menus in a browser dashboardA loop mode: Condition, Timer, Forever or NoneA fixed priority by loop mode, plus a lock on the part each running step uses
Your own Arduino codeOn the boardif statements in C++Whatever your loop() checksWhatever you write, often the last line to run wins
System
Drools
Where the rules run
Inside a Java application
How you write a rule
Text rules in .drl files with when and then parts
What starts a rule
Facts matching the when part
How clashes are settled
A number called salience: a higher salience runs first
System
Home Assistant automations
Where the rules run
Inside Home Assistant
How you write a rule
Trigger, condition and action, in the visual editor or in YAML
What starts a rule
A trigger, such as a door opening or the sun setting
How clashes are settled
A mode per automation (single, restart, queued, parallel) decides what a second start does
System
MakerBuddy automation rules
Where the rules run
On the ESP32 board itself
How you write a rule
Drop-down menus in a browser dashboard
What starts a rule
A loop mode: Condition, Timer, Forever or None
How clashes are settled
A fixed priority by loop mode, plus a lock on the part each running step uses
System
Your own Arduino code
Where the rules run
On the board
How you write a rule
if statements in C++
What starts a rule
Whatever your loop() checks
How clashes are settled
Whatever you write, often the last line to run wins

The Drools facts come from the Drools rule language reference (opens in a new tab), which defines salience as "an integer defining the priority of the rule". The Home Assistant parts come from its automation basics page (opens in a new tab) and its automation modes page (opens in a new tab). There, single is the default: a new start is ignored while the automation is still running. Cloud platforms such as AWS IoT rules (opens in a new tab) act on the messages devices send to AWS, not on the device itself.

Automation rules on a small device: the MakerBuddy rule engine

MakerBuddy is a learning board built around an ESP32, with lights, a buzzer and sensors built in and more parts that plug in. Its rule engine runs on the board itself. You build rules in a browser dashboard with no code, and the board keeps running them with the dashboard closed, as long as it has power.

According to the automation rules documentation, a board holds up to 10 rules, and each rule has up to 10 steps. A step does one action, then waits before the next step: 1 second unless you change it, from 0 up to 1 hour. Every rule answers two questions:

  • When? The loop mode: Condition, Timer, Forever or None (shown as No Loop in the dashboard).
  • What? The steps, run in order: turn the blue light on, off or dim it, set the colour light to one of 7 colours or off, switch the buzzer or relay on or off, move the servo, or show text on the screen.

A Condition rule watches one sensor, or two joined with AND or OR. It can compare with bigger than, smaller than, at least, at most, or "about equal", which matches when the values are less than 0.1 apart. There are 10 sensors to choose from, including the DHT11 temperature, the light level, distance, soil moisture and the motion sensor. Lesson 21 introduces the four loop modes, and our post on forever, timed and conditional loops explains each one as a programming loop.

Two limits shape every MakerBuddy rule. The board reads its sensors once a second, and rules use that snapshot, so a rule reacts in about a second, not instantly. And the board has no clock, so a rule cannot run "at 8 am". A Timer rule means "every 20 minutes after the last run finished", not a time of day.

Rule priority: who wins when two rules want the same part

Here is the problem. A Timer rule wants the colour light blue to remind you to drink water. At the same moment, a Condition rule wants it flashing red because the room is too hot. One light, two answers.

The MakerBuddy firmware settles this with a fixed rule priority based on loop mode. The rules section of the technical details states it in one line: Condition first, then Timer, then Forever, then No loop.

PriorityLoop modeWhen it startsWhen it repeatsWhat it locks
1 (strongest)ConditionWhen its sensor test has stayed true for 0.3 secondsAgain and again while the test is true; when it turns false, it finishes the current round, then stopsThe part its current step uses. A rule that is finishing keeps that part for the whole wait of its step
2TimerAt once when added, or at power-on if autostart is onWaits the interval after each run finishes, then runs againThe part its current step uses; nothing while it waits for the next run
3ForeverAt once when added, or at power-on if autostart is onStraight after the last step's waitThe part its current step uses
4 (weakest)None (No Loop)At once when added, or at power-on if autostart is onNever; it runs onceThe part its current step uses, until it ends
Priority
1 (strongest)
Loop mode
Condition
When it starts
When its sensor test has stayed true for 0.3 seconds
When it repeats
Again and again while the test is true; when it turns false, it finishes the current round, then stops
What it locks
The part its current step uses. A rule that is finishing keeps that part for the whole wait of its step
Priority
2
Loop mode
Timer
When it starts
At once when added, or at power-on if autostart is on
When it repeats
Waits the interval after each run finishes, then runs again
What it locks
The part its current step uses; nothing while it waits for the next run
Priority
3
Loop mode
Forever
When it starts
At once when added, or at power-on if autostart is on
When it repeats
Straight after the last step's wait
What it locks
The part its current step uses
Priority
4 (weakest)
Loop mode
None (No Loop)
When it starts
At once when added, or at power-on if autostart is on
When it repeats
Never; it runs once
What it locks
The part its current step uses, until it ends

The order follows urgency. A Condition rule reacts to the real world, so a heat warning should not wait for a decoration. A Timer is a promise that should still happen on time. Forever rules are usually background, like a heartbeat blink. None rules are usually a one-off setup step. The teacher's note in Lesson 21 compares this to interrupt priority in embedded systems.

One honest warning: rules of the same mode have no winner. If two Forever rules both drive the buzzer, each one writes to it in turn and the result looks random. Lesson 23 lets students see this happen on purpose. The servo is the one exception: within one priority level, the rule that already holds the servo keeps it and the other waits its turn.

A priority ladder with Condition at the top, then Timer, Forever and None. The Condition rule's "flash red" holds the colour light behind a padlock, so the Timer's "set blue" and the None rule's "set green" are skipped, while the Forever rule still blinks the blue light because only the part each current step uses is locked.
A priority ladder with Condition at the top, then Timer, Forever and None. The Condition rule's "flash red" holds the colour light behind a padlock, so the Timer's "set blue" and the None rule's "set green" are skipped, while the Forever rule still blinks the blue light because only the part each current step uses is locked.

Output locking: how the board keeps one owner per part

Priority alone is not enough. The engine also needs to know which parts are taken.

The board has six outputs a rule can drive: the blue light, the colour light, the buzzer, the servo, the relay and the screen. On every pass through the rule engine, the firmware does this:

  1. Start with all six parts unlocked.

  2. Run the Condition rules. Each running one carries out its step if its part is free.

  3. Lock the part that each running Condition rule's current step uses.

  4. Run the Timer rules, which may only touch parts that are still free. Then lock their current parts.

  5. Do the same for Forever rules, then None rules.

Three details matter.

  • Only the current step's part is locked. If a Condition rule is on its buzzer step, the colour light is free for a Timer rule, even if the Condition rule will use the colour light two steps later.
  • A blocked step is skipped, not queued. If a weaker rule reaches a step whose part is locked, that action does not happen. The weaker rule's own clock keeps running, and it carries on with its next step when that step's wait is over. It does not go back and redo the missed action later.
  • A waiting Timer locks nothing. Between runs, a Timer rule is parked and every part is free for the weaker rules.

There is a second kind of lock, aimed at people. While a rule controls a part, the dashboard locks that part's manual control, as the troubleshooting guide explains. For the servo, the board itself also refuses manual commands while a rule holds it, as the servo control notes explain.

Worked example: five rules for a classroom, second by second

Here is a set of five rules you can build on a MakerBuddy board, with a DHT11 temperature sensor plugged in and a low-voltage fan wired to the relay. The relay is for low-voltage things only. Never connect it to mains electricity.

RuleLoop modeCondition or intervalSteps, each with its wait
Too hotConditionTemperature (DHT11) bigger than 30Relay on, 0 ms. Colour light red, 500 ms. Colour light off, 500 ms
Cool againConditionTemperature (DHT11) smaller than 27Relay off, 1000 ms
Drink waterTimer, autostart onEvery 20 minutesBuzzer on, 300 ms. Buzzer off, 0 ms. Colour light blue, 5000 ms. Colour light off, 0 ms
HeartbeatForever, autostart onNoneBlue light on, 500 ms. Blue light off, 500 ms
HelloNone, autostart onNoneColour light green, 3000 ms. Colour light off, 0 ms
Rule
Too hot
Loop mode
Condition
Condition or interval
Temperature (DHT11) bigger than 30
Steps, each with its wait
Relay on, 0 ms. Colour light red, 500 ms. Colour light off, 500 ms
Rule
Cool again
Loop mode
Condition
Condition or interval
Temperature (DHT11) smaller than 27
Steps, each with its wait
Relay off, 1000 ms
Rule
Drink water
Loop mode
Timer, autostart on
Condition or interval
Every 20 minutes
Steps, each with its wait
Buzzer on, 300 ms. Buzzer off, 0 ms. Colour light blue, 5000 ms. Colour light off, 0 ms
Rule
Heartbeat
Loop mode
Forever, autostart on
Condition or interval
None
Steps, each with its wait
Blue light on, 500 ms. Blue light off, 500 ms
Rule
Hello
Loop mode
None, autostart on
Condition or interval
None
Steps, each with its wait
Colour light green, 3000 ms. Colour light off, 0 ms

Build them in the Rule Engine tab of the dashboard, then warm the DHT11 between your fingers to test the hot rules. Times below are from power-on and are rounded, because the engine works in passes and the sensors update once a second.

TimeWhat you seeWhy
0 sBlue light starts blinking, the buzzer beeps, the colour light turns greenHeartbeat, Drink water and Hello all start. Each one uses a different part, so nothing clashes
0.3 sBeep ends, colour light turns blueDrink water reaches its blue step. Timer outranks None, so it takes the colour light from Hello
3 sColour light stays blueHello reaches "colour light off", but the colour light is locked by Drink water's blue step. The off step is skipped. Hello has now finished
5.3 sColour light goes offDrink water turns it off, finishes its run and parks for 20 minutes. A parked Timer locks nothing
Room passes 30 °CAbout a second later the fan starts and the colour light flashes redThe next reading shows it, and the test holds for 0.3 seconds. Condition outranks everything
20 minutes after Drink water's last run, still hotThe buzzer beeps, but no blueThe buzzer is free, so the beep happens. The blue step is skipped because Too hot's flashing owns the colour light
Temperature falls to 29 °CRed flashing stops; the fan keeps runningToo hot finishes its round, ending on "colour light off". Its relay step left the fan on
Temperature falls below 27 °CFan stopsCool again turns the relay off
Time
0 s
What you see
Blue light starts blinking, the buzzer beeps, the colour light turns green
Why
Heartbeat, Drink water and Hello all start. Each one uses a different part, so nothing clashes
Time
0.3 s
What you see
Beep ends, colour light turns blue
Why
Drink water reaches its blue step. Timer outranks None, so it takes the colour light from Hello
Time
3 s
What you see
Colour light stays blue
Why
Hello reaches "colour light off", but the colour light is locked by Drink water's blue step. The off step is skipped. Hello has now finished
Time
5.3 s
What you see
Colour light goes off
Why
Drink water turns it off, finishes its run and parks for 20 minutes. A parked Timer locks nothing
Time
Room passes 30 °C
What you see
About a second later the fan starts and the colour light flashes red
Why
The next reading shows it, and the test holds for 0.3 seconds. Condition outranks everything
Time
20 minutes after Drink water's last run, still hot
What you see
The buzzer beeps, but no blue
Why
The buzzer is free, so the beep happens. The blue step is skipped because Too hot's flashing owns the colour light
Time
Temperature falls to 29 °C
What you see
Red flashing stops; the fan keeps running
Why
Too hot finishes its round, ending on "colour light off". Its relay step left the fan on
Time
Temperature falls below 27 °C
What you see
Fan stops
Why
Cool again turns the relay off

Look at the 3-second row. The green welcome was meant to last 3 seconds, but you only saw it for a fraction of a second. Nothing is broken. The Timer rule had higher priority and wanted the same light. If the welcome matters, give Hello a part no other rule uses, such as the screen.

Now look at the gap between 27 and 30 °C. Between those numbers, neither Condition rule changes the relay, so the fan keeps whatever state it had. That gap stops the fan flicking on and off when the reading wobbles around one number, and our upcoming post on hysteresis and why a fan keeps switching on and off explains it. Do not expect a switch at exactly 30.0 °C either: the Adafruit DHT guide (opens in a new tab) gives the DHT11 an accuracy of ±2 °C. Lesson 26 builds this smart fan in class, and Lesson 27 adds ranges and a second sensor.

The worked example drawn part by part. In the first 6 seconds the blue light blinks, the colour light shows a sliver of green, then blue until 5.3 s, and Hello's "off" at 3 s is skipped. Later, once the temperature passes 30 °C the fan starts and the colour light flashes red; 20 minutes on, the buzzer beeps but the blue step is skipped because it is locked; the red stops at 29 °C and the fan stops below 27 °C.
The worked example drawn part by part. In the first 6 seconds the blue light blinks, the colour light shows a sliver of green, then blue until 5.3 s, and Hello's "off" at 3 s is skipped. Later, once the temperature passes 30 °C the fan starts and the colour light flashes red; 20 minutes on, the buzzer beeps but the blue step is skipped because it is locked; the red stops at 29 °C and the fan stops below 27 °C.

Build a tiny priority rule engine in Arduino code

Reading about a rule engine is one thing. Writing one shows you how simple the core idea is. The sketch below builds a three-rule engine with the same "claim and lock" idea, using only parts built into the MakerBuddy board: the blue light on GPIO 12, the buzzer on GPIO 4 and the knob on GPIO 34.

cpp
// A tiny priority rule engine for the MakerBuddy board.
// Board: "ESP32 Dev Module" with "esp32 by Espressif Systems" 3.x.
// Uploading this sketch replaces the MakerBuddy firmware until you upload it again.

const int LED_PIN = 12;      // built-in blue light
const int BUZZER_PIN = 4;    // built-in buzzer
const int KNOB_PIN = 34;     // built-in turning knob

enum Device { DEV_LIGHT, DEV_BUZZER, DEVICE_COUNT };
const int pins[DEVICE_COUNT] = {LED_PIN, BUZZER_PIN};

struct Rule {
  const char *name;
  Device device;                      // the one part this rule drives
  bool (*wants)(unsigned long now);   // does it want to act right now?
  int (*value)(unsigned long now);    // HIGH or LOW for that part
};

int knobPercent() {
  return map(analogRead(KNOB_PIN), 0, 4095, 0, 100);
}

// Condition rule: knob past 80 -> blue light fully on.
bool knobHigh(unsigned long now) { return knobPercent() > 80; }
int lightOn(unsigned long now) { return HIGH; }

// Timer rule: a 200 ms beep at the start of every 5 seconds.
bool always(unsigned long now) { return true; }
int beep(unsigned long now) { return (now % 5000) < 200 ? HIGH : LOW; }

// Forever rule: blink the blue light, 500 ms on, 500 ms off.
int blink(unsigned long now) { return (now % 1000) < 500 ? HIGH : LOW; }

// The order of this list IS the priority: strongest first.
Rule rules[] = {
  {"Knob alarm", DEV_LIGHT,  knobHigh, lightOn},  // like a Condition rule
  {"Beep timer", DEV_BUZZER, always,   beep},     // like a Timer rule
  {"Heartbeat",  DEV_LIGHT,  always,   blink},    // like a Forever rule
};
const int RULE_COUNT = sizeof(rules) / sizeof(rules[0]);

void setup() {
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);
}

void loop() {
  unsigned long now = millis();
  bool locked[DEVICE_COUNT] = {false, false};   // fresh every pass

  for (int i = 0; i < RULE_COUNT; i++) {
    Rule &r = rules[i];
    if (!r.wants(now)) continue;        // nothing to do this pass
    if (locked[r.device]) continue;     // a stronger rule owns this part
    digitalWrite(pins[r.device], r.value(now));
    locked[r.device] = true;            // weaker rules must keep off
  }
  delay(20);                            // about 50 passes a second
}
  1. Facts.

    knobPercent() turns the knob's raw reading into 0 to 100. In the Arduino-ESP32 core (opens in a new tab) that the firmware uses, analogRead gives a 12-bit value from 0 to 4095, which is why the map call starts from 4095.

  2. Rules.

    Each Rule names the one part it drives, a wants test (the condition) and a value (the action).

  3. Priority.

    The order of the rules list is the priority. A bigger engine would store a number, like Drools salience, and sort by it.

  4. Locking.

    The locked array starts empty on every pass. The first rule that acts on a part locks it, and every weaker rule that wants the same part is skipped for that pass.

  5. Try it.

    Upload the sketch and the blue light blinks while the buzzer beeps every 5 seconds. Turn the knob towards its top end. Past 80, the blue light stays fully on because the Knob alarm rule has claimed it, while the beep carries on. If nothing changes, turn the knob the other way.

The MakerBuddy firmware adds steps, waits, the 0.3-second condition check and rules saved on the board, but the idea underneath is the same. You can read the real thing, the processSequences function, in the public MakerBuddy firmware source (opens in a new tab).

When a rule engine is the wrong tool

  • Chains of rules get hard to follow. In his rules engine article (opens in a new tab), Fowler warns that when one rule's action changes the facts that other rules check, the system "can easily end up being very hard to reason about and debug".
  • Fowler's advice is to keep the number of rules small and to test them with real data, in the same rules engine article (opens in a new tab). MakerBuddy's cap of 10 rules with 10 steps each is tight on purpose. If a project needs more, it has outgrown rules.
  • Some logic needs memory. A rule engine reacts to what is true now. A parking gate that must remember "a car went in, so wait for it to come out" is easier as a state machine. Our upcoming guide to state machines with traffic lights and parking gates shows the difference.
  • Some timing needs a clock. "Water the plant at 7 am" needs a real-time clock. The MakerBuddy board has none, so a Timer rule set to 24 hours drifts from the time you wanted, and a power cut restarts its count. Lesson 25 works within that limit.
  • Safety systems need certified parts. A rule that sounds the buzzer when the MQ-2 gas reading rises is a great lesson. It is never a real gas or smoke alarm.

Common rule engine mistakes, with causes and fixes

A welcome colour flashes for a moment, then disappears

Symptom: a None rule sets the colour light at power-on, but you barely see it.

Cause: a stronger rule, often a Timer or Forever rule, uses the same light and takes it over.

Fix: give the one-off rule a part nobody else uses, such as the screen, or move the stronger rule's colour step later.

Two rules make an output flicker

Symptom: the buzzer or light switches on and off in a random-looking pattern.

Cause: two rules of the same loop mode drive the same part. Same-mode rules have no winner, so each one writes in turn.

Fix: give each part one owner per mode, or merge the two rules into one rule with more steps. Lesson 22 shows how one rule can drive several outputs in order.

The fan stays on after the room cools down

Symptom: the Condition rule stopped, but the relay is still on.

Cause: when a Condition turns false, the rule finishes its round and stops. It leaves every output the way its last step set it. It does not undo anything.

Fix: add an off step at the end of the rule, or add a second Condition rule, like Cool again in the worked example.

A rule reacts late, or misses a quick event

Symptom: you wave at the distance sensor and nothing happens.

Cause: rules read the once-a-second sensor snapshot, and a Condition must stay true for 0.3 seconds before the rule starts. An event shorter than that can slip between readings.

Fix: design for slow changes, such as light, temperature or soil, or hold the test for longer. For fast events, write your own sketch.

If you teach this in a classroom, the flow is the same as in the free lessons: plan the logic as a flowchart in Lesson 20, meet the rule engine in Lesson 21, then build up from one rule to several. The MakerBuddy page for teachers shows how the kit and the free lessons fit a class. More guides like this one are on the MakerBuddy blog, and our upcoming explainer on how IoT works, from sensors to dashboards and rules puts the rule engine into the bigger picture.

FAQ

What does a rule engine do?

A rule engine checks a list of if-then rules against the latest facts and runs the actions of the rules that match. A good rule engine also settles what happens when two matching rules want different things.

What is a rule engine with an example?

A smart fan is a simple example. The facts are temperature readings, the rule is "if the temperature is above 30 °C, switch the fan on", and the engine checks it every second. On the MakerBuddy board, a Condition rule with a relay-on step does exactly this.

Is a rule engine the same as a rules engine?

Yes. "Rule engine" and "rules engine" name the same idea, and both spellings are common. Business software often says "business rules engine" for an engine that holds company policies, such as pricing or approval rules.

How do you implement a rules engine?

Start with three pieces: a list of rules, each with a condition and an action; a loop that reads the facts and checks each condition; and a way to pick a winner when two rules want the same output. The Arduino sketch in this post does all three in under 60 lines. In his rules engine article (opens in a new tab), Fowler suggests prototyping with both a product and a hand-made approach before you choose.

What is rule priority in automation?

Rule priority decides which rule wins when two rules want the same output at the same time. Drools uses a number called salience, where higher runs first, as its rule language reference (opens in a new tab) explains. MakerBuddy uses a fixed order by loop mode: Condition, then Timer, then Forever, then None.

Is Drools a rules engine?

Yes. Drools is a rule engine where you write rules in .drl files, each with a when part for the conditions and a then part for the actions. Its salience attribute sets each rule's priority, according to the Drools rule language reference (opens in a new tab).

Do rules keep running when the dashboard is closed?

On MakerBuddy, yes. The rules are saved on the board and run there, so they keep working with the dashboard closed or the laptop off, as long as the board has power. After a power cut, Condition rules start watching again, and the other types start again if autostart is on, unless someone stopped them by hand.

What happens when two rules have the same priority?

On MakerBuddy, rules of the same loop mode have no winner. If both drive the same part, each one writes to it in turn and the output flickers or behaves unpredictably. The servo is the exception: the rule already holding it keeps it, and the other waits its turn.

Sources

From the blog

Keep reading

No-Code Automation

Block Coding vs Text Coding: When Should Students Switch?

Block coding and text coding teach the same logic in different clothes. See what research says about switching, an age guide, five signs a student is ready, and one sensor project built three ways.

19 min read

Learn with MakerBuddy

Start building today.

Every part in these guides has its own lesson. Read them free, or get a kit for your class.