Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Rule engine for IoT data routing
📖 Scenario: You are managing a smart home system where multiple IoT sensors send data. You want to create a simple rule engine that routes sensor data to different actions based on sensor type and value.
🎯 Goal: Build a basic rule engine that reads sensor data, applies routing rules, and outputs the action to take for each sensor reading.
📋 What You'll Learn
Create a dictionary with sensor data entries
Add a configuration variable for a threshold value
Write a loop that applies routing rules based on sensor type and threshold
Print the routing decisions for each sensor
💡 Why This Matters
🌍 Real World
IoT devices send data continuously. A rule engine helps decide what actions to take automatically, like turning on air conditioning or sending alerts.
💼 Career
Understanding how to route IoT data with simple rules is useful for roles in IoT development, DevOps automation, and smart system management.
Progress0 / 4 steps
1
Create sensor data dictionary
Create a dictionary called sensor_data with these exact entries: 'temperature': 22, 'humidity': 55, 'motion': 1
IOT Protocols
Hint
Use curly braces to create a dictionary with keys and values separated by colons.
2
Add threshold configuration
Create a variable called temp_threshold and set it to 25 to use as the temperature threshold.
IOT Protocols
Hint
Just assign the number 25 to the variable named temp_threshold.
3
Apply routing rules
Write a for loop using variables sensor and value to iterate over sensor_data.items(). Inside the loop, create a variable action and set it to 'Turn on AC' if sensor is 'temperature' and value is greater than temp_threshold. Otherwise, set action to 'No action'.
IOT Protocols
Hint
Use an if-else statement inside the loop to decide the action based on sensor type and value.
4
Print routing decisions
Inside the existing for loop, add a print statement that outputs the text "Sensor: {sensor}, Value: {value}, Action: {action}" using an f-string.
IOT Protocols
Hint
Use print(f"Sensor: {sensor}, Value: {value}, Action: {action}") inside the loop to show the routing results.
Practice
(1/5)
1. What is the main purpose of a rule engine in IoT data routing?
easy
A. To monitor battery levels of IoT devices
B. To store IoT device firmware updates
C. To encrypt IoT data for security
D. To automatically route data based on defined conditions
Solution
Step 1: Understand the role of a rule engine
A rule engine evaluates data against rules to decide actions automatically.
Step 2: Identify the main function in IoT routing
It routes data based on conditions without manual intervention.
Final Answer:
To automatically route data based on defined conditions -> Option D
Quick Check:
Rule engine = automatic routing [OK]
Hint: Rule engines automate decisions based on conditions [OK]
Common Mistakes:
Confusing routing with data storage
Thinking rule engine handles encryption
Assuming it monitors device hardware
2. Which of the following is the correct syntax to define a rule that routes temperature data above 30°C to an alert system?
easy
A. when temperature > 30 then route to 'alert_system'
B. if temperature > 30 then send alert_system
C. rule temperature > 30 route alert_system
D. on temperature > 30 send to alert_system
Solution
Step 1: Identify correct rule syntax keywords
The standard syntax uses 'when' for condition and 'then' for action.
Step 2: Match syntax with routing action
when temperature > 30 then route to 'alert_system' correctly uses 'when temperature > 30 then route to 'alert_system''.
Final Answer:
when temperature > 30 then route to 'alert_system' -> Option A
Quick Check:
Correct syntax = when...then... [OK]
Hint: Look for 'when' condition and 'then' action keywords [OK]
Common Mistakes:
Using 'if' instead of 'when'
Missing 'then' keyword
Incorrect action verbs like 'send' or 'on'
3. Given the rule: when humidity < 20 then route to 'dry_alert' and input data humidity=15, what will be the routing result?
medium
A. Data ignored, no routing
B. Data routed to 'dry_alert'
C. Error due to syntax
D. Data routed to default storage
Solution
Step 1: Evaluate the condition with input data
Humidity is 15, which is less than 20, so condition is true.
Step 2: Determine the action based on true condition
Rule says to route data to 'dry_alert' when condition is true.
Final Answer:
Data routed to 'dry_alert' -> Option B
Quick Check:
Condition true routes data [OK]
Hint: Check if condition matches input data to find routing [OK]
Common Mistakes:
Ignoring condition evaluation
Assuming syntax error without cause
Routing to default when condition matches
4. Identify the error in this rule: when temperature => 25 then route to 'cooling_system'
medium
A. Missing 'then' keyword
B. Missing quotes around 'cooling_system'
C. Incorrect comparison operator used
D. Rule should start with 'if' instead of 'when'
Solution
Step 1: Check the comparison operator syntax
The operator '=>' is invalid; correct operator is '>=' for 'greater or equal'.
Step 2: Verify other syntax parts
Quotes and 'then' keyword are correct; 'when' is the right keyword.
Final Answer:
Incorrect comparison operator used -> Option C
Quick Check:
Use '>=' not '=>' for comparisons [OK]
Hint: Remember '>=' means greater or equal, not '=>' [OK]
Common Mistakes:
Confusing '>=' with '=>' operator
Omitting quotes around routing target
Replacing 'when' with 'if' incorrectly
5. You want to route IoT data to 'high_temp_alert' if temperature > 50 and humidity < 30. Which rule correctly implements this?
hard
A. when temperature > 50 and humidity < 30 then route to 'high_temp_alert'
B. when temperature > 50 or humidity < 30 then route to 'high_temp_alert'
C. when temperature > 50 then route to 'high_temp_alert' if humidity < 30
D. when temperature > 50 and humidity > 30 then route to 'high_temp_alert'
Solution
Step 1: Understand the condition requirements
Both temperature > 50 and humidity < 30 must be true to route data.
Step 2: Identify the rule with correct logical AND
when temperature > 50 and humidity < 30 then route to 'high_temp_alert' uses 'and' to combine both conditions correctly.
Final Answer:
when temperature > 50 and humidity < 30 then route to 'high_temp_alert' -> Option A
Quick Check:
Use 'and' for both conditions true [OK]
Hint: Use 'and' to combine multiple conditions in rules [OK]