Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 36 ยท Projects and Practice

Project: A Smart Home Rules Engine

Build a smart home automation engine: device state store, trigger-condition-action rules as data, motion lights with timers, away mode, and rule tests.

Home automation platforms (Home Assistant, openHAB, Node-RED, vendor apps) all rest on the same idea: when something happens, if a condition holds, then do something. In this project you build a small engine that reads rules written as data, keeps a state store of every device, and runs the rules when new events arrive over MQTT-style topics.

Data-driven rules matter. A user should be able to add "turn the porch light on at sunset" without anyone recompiling firmware, and you should be able to save, share and test rules.

Design

  • State store: a map from topic to the last value (like retained messages).
  • Event: a topic and a new value, plus the current simulated time.
  • Rule: { name, when: topic-filter, if: [conditions], then: [actions] }.
  • Condition: a comparison on the state store or the time of day.
  • Action: publish a value to a topic (which can trigger other rules, so protect against infinite loops).
  • Timers: rules like "turn the light off 5 minutes after the last motion" need delayed actions.
flowchart LR
  E["Event: home/hall/motion = 1"] --> M{"Rule matches?"}
  M -- "when + if" --> A["Action: publish home/hall/light = on"]
  A --> T["Schedule: light off in 5 min"]
  A --> E2["New event may trigger more rules"]

Step 1: the engine

The code below defines the engine and four rules: a motion-activated hall light that only works when it is dark, an auto-off timer, a window-open check that turns the heating off, and an "away mode" that turns all lights off and arms the alarm. The run function replays a scripted day.

Node.js The rules engine with a scripted day

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Follow the log: the noon motion event changes the state but the rule does not fire because it is not dark. In the evening the light turns on. The second motion at 19:03 restarts the timer, so the light turns off five minutes after the last motion, at 19:08. Opening the kitchen window switches heating off. The depth counter would throw if two rules kept triggering each other.

Notice how the three design choices pay off: states are only set on change (so repeated "motion=1" messages do not re-fire rules), rules are plain data you could load from a file, and every action is logged with its source, which makes automation debuggable.

Step 2: test the rules

Rules are code and need tests. A good approach is a scenario test: a list of events and the expected final state. The next block tests a rule set in isolation with a tiny assertion helper.

Node.js Scenario tests for a rule

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The "exactly at limit" test pins down the boundary behavior (> not >=), the sort of detail that otherwise causes confusing bugs months later.

Step 3: real-world concerns

  • Conflicts. Two rules may want opposite things (a schedule turns the light on, a presence rule turns it off). Define priorities or "last writer wins" and show it in the UI.
  • Manual override. If a user switches a light by hand, automation should back off for a while.
  • Failure handling. A command may not be acknowledged: use QoS 1 and verify the reported state through the shadow.
  • Safety. Never let a rule disable a safety function (smoke alarm) or unlock a door based on unauthenticated input.
  • Local first. Run the engine in the home hub so the house still works when the internet is down, as in the edge lesson.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the engine ignore a set() call whose value equals the stored value?

    Only real changes are events; repeated identical readings should not fire actions again.
  2. 2. Why did the hall light turn off at 19:08 and not 19:05?

    The rule cancels the old timer and sets a new one on each motion, so the countdown starts at the last motion.
  3. 3. What is the purpose of the depth counter?

    Actions can publish events that trigger more rules, so a limit stops infinite loops.
  4. 4. Why test the boundary 'exactly 26 degrees'?

    Edge cases around thresholds are a classic source of bugs.

Summary

  • Automation = trigger (event), condition (state, time), action (publish or timer), expressed as data.
  • A state store with change detection prevents spurious triggers; loop guards protect against rule cycles.
  • Timers need cancel and restart logic for patterns like motion lights.
  • Test rules with scenario tables; handle conflicts, overrides and safety explicitly; keep logic local.

Next: Project: A Mini MQTT Sensor Network