DE6412 Computer Programming 2 - Task 4

Industrial Temperature Sensor Monitoring using n8n Automation

Introduction

In this task, you will develop an automated monitoring system for industrial temperature sensors using n8n (a workflow automation tool) and API integration. You will create a workflow that continuously monitors two temperature sensors and sends alerts to Telegram when anomalies are detected. This simulates real-world industrial monitoring scenarios where engineers need to track sensor data and receive immediate notifications when equipment operates outside safe parameters.

The temperature sensors should operate between -100°C and +100°C. Values outside this range indicate potential equipment failure or dangerous conditions requiring immediate attention.

Part A: System Analysis and Setup (LO1)

Learning Outcome 1: Analyse an engineering application to facilitate the development of a software solution using rapid application development (RAD) techniques.

In this section, you will analyze the industrial monitoring requirements, understand the API structure, and plan your n8n workflow architecture. This analysis phase is crucial for identifying system requirements, data flow patterns, and potential failure points before implementation.

Step 1: Install and Setup n8n

First, install n8n on your system. n8n is a powerful workflow automation tool that allows you to connect different services and APIs without extensive coding.


# Install n8n globally using npm
npm install n8n -g

# Start n8n (this will open the interface in your browser)
n8n
        

Step 2: Analyze the Temperature Sensor API

Test the provided API endpoint to understand its structure and response format. The API simulates two industrial temperature sensors.

API Endpoint: https://fakerapi.it/api/v2/custom?_quantity=1&sensor_voltage=longitude&sensor_current=longitude

Note: The API returns "sensor_voltage" and "sensor_current" but we'll treat these as temperature readings from sensor_1 and sensor_2 respectively. The values represent temperature in Celsius and should be between -100 and +100 for normal operation.

Step 3: Setup Telegram Bot

Create a Telegram bot that will receive alert notifications when temperature anomalies are detected.

  1. Open Telegram and search for "@BotFather"
  2. Send /newbot to create a new bot
  3. Follow the prompts to name your bot (e.g., "TempMonitorBot")
  4. Save the Bot Token provided by BotFather
  5. Start a conversation with your bot and send any message
  6. Get your Chat ID by visiting: https://api.telegram.org/bot[YOUR_BOT_TOKEN]/getUpdates

Part B: Workflow Development (LO2)

Learning Outcome 2: Develop a software solution for an engineering application using RAD techniques.

You will now implement the monitoring workflow using n8n's visual workflow builder. This demonstrates RAD principles by using pre-built components and visual programming to rapidly create a functional monitoring system.

Step 4: Create the n8n Workflow

Access n8n interface (usually at http://localhost:5678) and create a new workflow with the following components:

4.1: Add Schedule Trigger

Add a Schedule Trigger node to call the API every 5 seconds:

4.2: Add HTTP Request Node

Add an HTTP Request node to fetch temperature data:

4.3: Add Code Node for Temperature Analysis

Add a Code node to analyze temperature readings and detect anomalies:


// Temperature analysis code for n8n Code node
const inputData = $input.all();

for (const item of inputData) {
  const sensorData = item.json.data[0];
  const sensor1_temp = sensorData.sensor_voltage;
  const sensor2_temp = sensorData.sensor_current;
  
  // Check if temperatures are within safe range (-100 to +100)
  const sensor1_alert = sensor1_temp < -100 || sensor1_temp > 100;
  const sensor2_alert = sensor2_temp < -100 || sensor2_temp > 100;
  
  // Prepare output data
  const outputItem = {
    timestamp: new Date().toISOString(),
    sensor1_temperature: sensor1_temp,
    sensor2_temperature: sensor2_temp,
    sensor1_status: sensor1_alert ? 'ALERT' : 'NORMAL',
    sensor2_status: sensor2_alert ? 'ALERT' : 'NORMAL',
    alert_required: sensor1_alert || sensor2_alert,
    alert_message: ''
  };
  
  // Generate alert message if needed
  if (outputItem.alert_required) {
    let alertMsg = '🚨 TEMPERATURE ALERT 🚨\n';
    if (sensor1_alert) {
      alertMsg += `Sensor 1: ${sensor1_temp.toFixed(2)}°C (OUT OF RANGE)\n`;
    }
    if (sensor2_alert) {
      alertMsg += `Sensor 2: ${sensor2_temp.toFixed(2)}°C (OUT OF RANGE)\n`;
    }
    alertMsg += `Safe Range: -100°C to +100°C\nTime: ${outputItem.timestamp}`;
    outputItem.alert_message = alertMsg;
  }
  
  $return.push({json: outputItem});
}
        

4.4: Add IF Node for Conditional Logic

Add an IF node to check if an alert is required:

4.5: Add Telegram Node

Add a Telegram node connected to the "True" output of the IF node:

Step 5: Test and Deploy the Workflow

Test your workflow by manually executing it and then activate it for continuous monitoring:

  1. Click "Execute Workflow" to test manually
  2. Verify that data flows through all nodes correctly
  3. Check that you receive test messages in Telegram when alerts occur
  4. Click "Active" toggle to enable automatic execution

Testing Tip: Since the API generates random values, you may need to run the workflow multiple times to see alert conditions. You can temporarily modify the temperature thresholds in the code node for testing purposes.

Part C: Performance Evaluation (LO3)

Learning Outcome 3: Evaluate the performance of a RAD software solution to an engineering application.

In this section, you will critically evaluate your n8n workflow solution, identify its strengths and limitations, and propose improvements. This evaluation helps you understand the practical considerations of deploying RAD solutions in real industrial environments.

Step 6: System Evaluation and Analysis

Create a file called evaluation_report.md and provide detailed answers to the following questions based on your workflow implementation and testing:

Questions for Evaluation:

  1. Appraise Performance and Deficiencies: Technically evaluate the performance of your n8n workflow solution. Explain what happens when the external API is slow, returns errors, or becomes unavailable?
  2. Informed Judgement on Failure Points: Consider the broader system reliability from an engineering perspective. What would happen if the n8n service crashes, the server loses internet connectivity, or Telegram's API becomes unavailable? How would changes in the sensor API format (e.g., different field names or additional sensors) affect your workflow?
  3. Identify Areas for Improvement: Based on your evaluation, propose specific improvements to enhance the monitoring system.

Step 7: Document Your Workflow

Take screenshots of your complete n8n workflow showing:

Submission

To complete this lab, please submit the following items:

Security Note: When submitting your work, ensure you mask or remove actual bot tokens and sensitive credentials. Include only the configuration structure and setup steps.