Functions

April 18, 2024

How to Use Custom JavaScript Logic in Qubitro Rule Functions

This tutorial will guide you through the steps to integrate custom JavaScript logic into Qubitro's rule functions, enabling advanced data processing and automated responses based on incoming device data.

Introduction

In Qubitro, Rule Functions automate actions based on the data transmitted by IoT devices. By utilizing predefined keys for instant data access and the internal trigger() function, you can efficiently manage and respond to real-time data conditions.

Accessing Data with Predefined Keys

To simplify the creation of conditions within rules, Qubitro allows the direct access of data fields using the ${{key}} syntax. This method streamlines the process of fetching data for condition checks.

Syntax and Usage

Here’s an example of how to set up variables using predefined keys:

function run(){
    let temperature = ${{TEMPERATURE}};
    let humidity = ${{HUMIDITY}};
    let light = ${{LIGHT}};
    let motion = ${{MOTION}};
    let voltage = ${{VDD}};
    // Additional variables can be defined similarly
}

Each variable directly accesses the corresponding value from the sensor data, facilitated by its predefined key.

Writing Conditions

With variables prepared, you can define conditions to trigger actions. These conditions can range from basic comparisons to complex logical statements.

Example Condition: Temperature and Humidity Check

This example demonstrates how to trigger an action if the temperature exceeds 25 degrees Celsius and humidity is above 70%:

function run(){
    let temperature = ${{TEMPERATURE}};
    let humidity = ${{HUMIDITY}};

    if (temperature > 25 && humidity > 70) {
        trigger();  // Activates the trigger function to execute an action
    }
}

Utilizing the trigger() Function

The trigger() function executes predefined actions when the conditions set in the rules are met.

How to Use trigger()

  • Purpose: To execute actions based on rule conditions.
  • Example Usage: Triggering an alert under specific environmental conditions.
function run(){
    let temperature = ${{TEMPERATURE}};
    let humidity = ${{HUMIDITY}};

    if (temperature > 30 && humidity > 80) {
        trigger();  // This could, for example, send a notification to a maintenance team
    }
}

Practical Implementation: Environmental Monitoring

Applying the principles learned to a practical scenario in environmental monitoring:

function run(){
    let temperature = ${{TEMPERATURE}};
    let humidity = ${{HUMIDITY}};
    let light = ${{LIGHT}};
    let motion = ${{MOTION}};
    let voltage = ${{VDD}};

    if (temperature > 30 && humidity > 85) {
        trigger();  // Initiates notification to relevant personnel via email or SMS
    }
}

Example Usage: Restricted Zone Alert

Monitoring specific geographical areas is crucial in many applications, such as ensuring personnel remain within safe zones in a facility. The following example shows how to set up a rule that triggers an alert if a device enters a restricted assembly area.

Scenario: Monitoring an Assembly Area

function run (){
    let coordinates = ${{coordinates}}; // Assuming format [latitude, longitude]

    // Convert coordinates array to an object
    let incomingLocation = {
        lat: coordinates[0],
        lon: coordinates[1]
    };

    // Define the corners of the assembly area
    const assemblyArea = {
        A: { lat: 40.822608, lon: 29.352864 },
        B: { lat: 40.822652, lon: 29.353246 },
        C: { lat: 40.822289, lon: 29.352890 },
        D: { lat: 40.822327, lon: 29.353346 }
    };

    // Function to check if a point is inside a given rectangle
    const isInsideRectangle = (point, rect) => {
        const minLat = Math.min(rect.A.lat, rect.B.lat, rect.C.lat, rect.D.lat);
        const maxLat = Math.max(rect.A.lat, rect.B.lat, rect.C.lat, rect.D.lat);
        const minLon = Math.min(rect.A.lon, rect.B.lon, rect.C.lon, rect.D.lon);
        const maxLon = Math.max(rect.A.lon, rect.B.lon, rect.C.lon, rect.D.lon);

        return point.lat >= minLat && point.lat <= maxLat && point.lon >= minLon && point.lon <= maxLon;
    };

    // Check if the incoming location is inside the assembly area
    if (isInsideRectangle(incomingLocation, assemblyArea)) {
        trigger();
    }
}

Example Usage: Advanced Environmental Monitoring

Imagine managing an advanced environmental control system where temperature, humidity, light levels, and CO2 concentrations are monitored. Alerts need to be sent when any of these parameters deviate significantly from their target ranges.

Scenario Setup

function run(){
    let temperature = ${{TEMPERATURE}};
    let humidity = ${{HUMIDITY}};
    let lightLevel = ${{LIGHT}};
    let co2Level = ${{CO2}};

    // Complex condition to manage temperature and humidity
    if (temperature > 28 && humidity < 40) {
        trigger();  // Pre-configured to send an SMS
    }

    // Additional conditions to manage light and CO2 levels
    if (lightLevel < 200 && co2Level > 1000) {
        trigger();  // Pre-configured to send an SMS
    }

    // Check for extreme conditions that require immediate attention
    if (temperature > 35 || humidity > 80 || co2Level > 1500) {
        trigger();  // Pre-configured to send an SMS
    }
}

This example demonstrates the use of multiple sensor variables to monitor and react to changes in environmental conditions. By setting up complex logical conditions that combine several parameters, the system can send targeted alerts to address specific scenarios.

Conclusion

By employing predefined keys and the trigger() function, Qubitro's rule functions provides powerful tools for managing real-time data efficiently. This capability enhances operational efficiency and ensures rapid responses to environmental changes.

For further information and advanced functionalities, refer to the Qubitro Documentation.

Couldn't find the guide you need?
    Qubitro Logo

    Product

    Resources

    Industries

    Decision Aids

    Solutions

    Company

    © 2024 Qubitro, Inc. All rights reserved