How to Use JavaScript for Custom Rule Conditions in Qubitro
By following this guide, you can customize Qubitro rule functions using JavaScript to create dynamic conditions.
This guide explains how to use JavaScript within Qubitro’s rule functions to define custom conditions and automate actions based on real-time data.
A few notes to remember:
Rule Functions operate only on real-time data – They cannot query historical data from the database.
Each Rule Function executes a single pre-configured action – It does not support executing multiple actions within one function.
Actions are predefined during function setup – The trigger() function executes only the action assigned when creating the rule.
Although the examples here focus on environmental monitoring and geofencing, the same approach applies to any scenario requiring event-driven automation.
Accessing Data with Predefined Keys
Qubitro provides a simple way to access sensor values in JavaScript using predefined keys. These keys allow you to reference the latest device data without complex parsing.
Syntax and Usage
Define variables using predefined keys:
function run(){
let temperature = ${{TEMPERATURE}};
let humidity = ${{HUMIDITY}};
let light = ${{LIGHT}};
let motion = ${{MOTION}};
let voltage = ${{VDD}};
}
Each variable directly pulls the corresponding real-time value from the incoming data.
Writing Rule Conditions
With variables defined, you can create logical conditions to decide when the function should trigger an action.
Example: Trigger an Action Based on Temperature and Humidity
This function triggers an action if the temperature is above 25°C and humidity exceeds 70%.
function run() {
let temperature = ${{TEMPERATURE}};
let humidity = ${{HUMIDITY}};
if (temperature > 25 && humidity > 70) {
trigger(); // Executes the pre-configured action
}
}
Utilizing the trigger() Function
The trigger() function is used to execute predefined action when conditions are met.
Example: Alert When Temperature and Humidity Are Too High
function run() {
let temperature = ${{TEMPERATURE}};
let humidity = ${{HUMIDITY}};
if (temperature > 30 && humidity > 80) {
trigger(); // This executes the pre-configured action (e.g., send a notification)
}
}
Practical Implementations
Example 1: Geofencing a Restricted Area
This example ensures that a device does not enter a restricted assembly area. If it does, the function triggers an alert.
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 restricted zone (assembly area)
const restrictedZone = {
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 }
};
// Check if the incoming coordinates fall within the restricted area
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;
};
if (isInsideRectangle(incomingLocation, restrictedZone)) {
trigger(); // Triggers the predefined alert (e.g., send a warning notification)
}
}
Example 2: Advanced Environmental Monitoring
function run() {
let temperature = ${{TEMPERATURE}};
let humidity = ${{HUMIDITY}};
let lightLevel = ${{LIGHT}};
let co2Level = ${{CO2}};
// Trigger alert if temperature is too high and humidity is too low
if (temperature > 28 && humidity < 40) {
trigger(); // Executes the pre-configured alert
}
// Trigger alert if light levels drop too low while CO2 is high
if (lightLevel < 200 && co2Level > 1000) {
trigger(); // Executes the pre-configured alert
}
// Immediate action required for extreme conditions
if (temperature > 35 || humidity > 80 || co2Level > 1500) {
trigger(); // Executes the pre-configured alert
}
}