Skip to main content

12.09 Using global variables

As an example of using global variables, let's create two scenarios:

  • The first scenario will, three times a day based on a timer, record the current temperature in London. A global variable will be created for each temperature value, and the obtained value will be stored.
  • The second scenario will calculate the average daily temperature in London based on the obtained values of global variables once a day and send a notification to Telegram.

Scenario for obtaining three temperature values

For the successful execution of the Three temperature measurements in London scenario, it is necessary to add 9 nodes and configure 3 routes:

  • (1) Trigger on Schedule node to initiate the scenario at 1 AM, 9 AM, and 5 PM, i.e., three times a day;
  • (2) HTTP request node with a GET request to send a query to the OpenWeather service to obtain the temperature parameter in London;
  • (3) JavaScript node to calculate the time of day (morning, day, night) with the following code:
export default async function run({execution_id, input, data, store}) {
// Create a Date object for UTC
const now = new Date();

// Define the offset for Istanbul timezone (UTC+3)
const timezoneOffset = 3 * 60;

// Adjust the current time for the Istanbul timezone
now.setMinutes(now.getMinutes() + now.getTimezoneOffset() + timezoneOffset);

// Get the hours, adjusted for the Istanbul timezone
const hours = now.getHours();

let timeOfDay; // Variable for the time of day

// Determine the time of day
if (hours >= 4 && hours < 12) {
timeOfDay = "morning";
} else if (hours >= 12 && hours < 20) {
timeOfDay = "day";
} else {
timeOfDay = "night";
}

// Return an object with the time of day
return {
timeOfDay: timeOfDay
};
}
  • (3.1) Set up the morning route from the JavaScript node, where the scenario execution will go if the calculated time of day in the JavaScript node is morning. To do this, add a condition to the route:
  • (3.2) Set up the day route from the JavaScript node, where the scenario execution will go if the calculated time of day in the JavaScript node is day. To do this, add a condition to the route:
  • (3.3) Set up the night route from the JavaScript node, where the scenario execution will go if the calculated time of day in the JavaScript node is night. To do this, add a condition to the route:
  • (4) SetGlobalVariables node to create a global variable morningTemp containing the parameter of the morning temperature obtained during the morning time from the HTTP request node;
  • (5) If notifications about the morningTemp variable recording are needed, you can add a Send a Text Message or Reply node, sending a Telegram message with the variable's value. To configure the node, establish a authorization and fill in the fields with the chat identifier to which messages should be sent:
  • (6) SetGlobalVariables node to create a global variable dayTemp containing the parameter of the daytime temperature obtained during the day from the HTTP request node;
  • (7) If notifications about the dayTemp variable recording are needed, you can add a Send a Text Message or Reply node, sending a Telegram message with the variable's value. To configure the node, establish a authorization and fill in the fields with the chat identifier to which messages should be sent:
  • (8) SetGlobalVariables node to create a global variable nightTemp containing the parameter of the nighttime temperature obtained during the night from the HTTP request node;
  • (9) If notifications about the nightTemp variable recording are needed, you can add a Send a Text Message or Reply node, sending a Telegram message with the variable's value. To configure the node, establish a authorization and fill in the fields with the chat identifier to which messages should be sent:

The completion of the scenario results in the recording of values for three global variables. These values can be viewed in the variable table:

If nodes for sending Telegram notifications are added, a message is sent with each variable recording:

Scenario to get average daily temperature

For the successful execution of the scenario, it is necessary to add 3 nodes:

  • (1) Trigger on Schedule node to initiate the scenario at 8 PM, i.e., once a day;
  • (2) JavaScript node to calculate the average daily temperature with the code below. To calculate the average daily value, variables morningTemp, dayTemp, nightTemp obtained and recorded in the Three temperature measurements in London scenario are used:
export default async function run({execution_id, input, data, store}) {
// Initialize variables for temperatures and a counter for valid values
let totalTempCelsius = 0;
let count = 0;

// Function to convert temperature from Kelvin to Celsius
function kelvinToCelsius(kelvin) {
return kelvin - 273.15;
}

// Function to process temperature
function processTemperature(tempStr) {
if (tempStr !== "null") {
const tempKelvin = parseFloat(tempStr);
if (!isNaN(tempKelvin)) {
const tempCelsius = kelvinToCelsius(tempKelvin);
totalTempCelsius += tempCelsius;
count++;
} else {
// If the value cannot be converted to a number, return an error
return "Temperature value is not a valid number.";
}
}
}

// Process each temperature value
let error = processTemperature(data["{{%.morningTemp}}"]);
if (error) return { error: "Morning " + error };

error = processTemperature(data["{{%.nightTemp}}"]);
if (error) return { error: "Night " + error };

error = processTemperature(data["{{%.dayTemp}}"]);
if (error) return { error: "Day " + error };

// If no valid temperature values were obtained, return an error
if (count === 0) {
return {
error: "No valid temperature values provided."
};
}

// Calculate the average temperature in Celsius and round to two decimal places
const averageTempCelsius = parseFloat((totalTempCelsius / count).toFixed(2));

// Return the result
return {
averageTemperatureCelsius: averageTempCelsius
};
}
  • (3) To receive notifications about the average daily temperature, you need to add a Send a Text Message or Reply node, sending a Telegram message with the value obtained in the JavaScript node. To configure the node, establish a authorization and fill in the fields with the chat identifier to which messages should be sent:

The result of the scenario execution, obtaining the average daily temperature, is a message sent once a day by the bot to Telegram.