Skip to main content

12.08 Using global variables across different scenarios

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

  • The first scenario will record the current temperature in London when manually triggered. A global variable will be created for each temperature value, and the value will be recorded.
  • The second scenario will calculate the daily average temperature in London based on the recorded global variables. This scenario will run once a day when manually triggered.

Getting and writing three global variables

To set up the scenario Getting and writing three global variables, you need to add six nodes and configure three routes:

  • (1) Trigger on Run once: This node initiates the scenario when you click the Run Once button;
  • (2) HTTP request: This node sends a GET request to the OpenWeather service to retrieve the temperature in London;
  • (3) JavaScript: This node calculates 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 time 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) Route morning: Configure this route from the JavaScript node to proceed if the calculated time of day is morning. Add the condition:
  • (3.2) Route day: Configure this route from the JavaScript node to proceed if the calculated time of day is day. Add the condition:
  • (3.3) Route night: Configure this route from the JavaScript node to proceed if the calculated time of day is night. Add the condition:
  • (4) SetGlobalVariables: This node creates the global variable morningTemp, containing the morning temperature value obtained during morning from the HTTP request node;
  • (5) SetGlobalVariables: This node creates the global variable dayTemp, containing the daytime temperature value obtained during day from the HTTP request node;
  • (6) SetGlobalVariables: This node creates the global variable nightTemp, containing the nighttime temperature value obtained during night from the HTTP request node;

The result of this scenario is the recording of three global variable values. You can view these values in the variables table.

Processing Global Variables

To set up the scenario Processing Global Variables, you need to add three nodes:

  • (1) Trigger on Run once: This node initiates the scenario when you click the Run Once button;
  • (2) JavaScript: This node calculates the daily average temperature using the code below. It uses the morningTemp, dayTemp, nightTemp variables obtained and recorded in the Getting and writing three global variables scenario:
import axios from 'axios';

export default async function run({execution_id, input, data}) {
// Retrieve temperature values in Kelvin and convert to Celsius
const dayTempCelsius = parseFloat(data["{{%.dayTemp}}"]) - 273.15;
const morningTempCelsius = parseFloat(data["{{%.morningTemp}}"]) - 273.15;
const nightTempCelsius = parseFloat(data["{{%.nightTemp}}"]) - 273.15;

// Calculate the arithmetic average in Celsius
let averageTempCelsius = (dayTempCelsius + morningTempCelsius + nightTempCelsius) / 3;

// Round to two decimal places
averageTempCelsius = parseFloat(averageTempCelsius.toFixed(2));

// Return the result
return {
averageTempCelsius: averageTempCelsius
};
}
  • (3) Webhook response: This node returns the result upon successful completion of the scenario:

The result of this scenario is a response with the processed data from the three global variables, for example: The average temperature is 17.260000 degrees.