Extend Measurements Object Subflow

Introduction

This subflow is part of our Input flows collection. Input flows are used for importing data inside Node-RED environemnt for further processing.

Extend Measurement Object Subflow can be used in various scenarios, where we need to extend our measurement object with various data.

Prerequisites

This flow along with other data generating nodes and flows is available for user in the following package:

@senlab/node-red-generate-random-senlab

To learn more about Verdaccio and how to install packages, please refer to this documentation: Verdaccio

IMPORTANT: While not a strict dependency, this subflow is frequently used in our example flows in conjunction with the senlab-rand-obj node, which generates a random object filled with configured data. The Extend Measurement Object Subflow expects input in the form of a JSON object.

Setting I/O nodes and environmental variables

When creating a subflow user needs to define input node and one or more output nodes, so the messages can pass through our custom implementation. In the top bar there are configuration options enabling us to do that.

image::subflows-config.png

Environmental variables can be added to the subflow using the edit properties window, where user can also change the name of the subflow. Environmental variables can be used to make subflow configurable (make subflow reusable in different configurations). For example user can specify environmental variable named addID which is of type boolean and build custom implementation of the code based on this environmental variable where subflow outputs JS object which does or doesnt include ID based on selected option when deploying the subflow.

Inside the subflow environmental variables are accessible with: env.get(’);

This subflow uses multiple environmental variables, among them location (for defining location of measurement station), multiple fields for measurement names and ranges (this defines names of measurements and their ranges, user can modify this to change types of data). NoOfUsed, this field defines how many measurements user wants to add to the object.

Prerequisites

This subflow extends incomming object with more additional measurements. This requires that the input to the subflow is JSON object with fields id and timestamp. For generating random JSON object you can use our custom node named Generate-rand-meas from @senlab/package.

To learn more about Verdaccio and how to install packages, please refer to this documentation: Verdaccio

Setting Function Node for extending JSON object

let count = parseInt(env.get('noOfUsed'));
let dataObject = {};

for (let i = 1; i <= count; i++) {
    let sensorName = env.get('measurement' + i);
    let range = env.get('range' + i);
    let splitrange = range.split(",");
    let min = splitrange[0];
    let max = splitrange[1];
    let random =  Math.random() * (max - min) + min;
    dataObject[sensorName] = random;
}

Code above is for parsing name and range of measurements from environmental variables, range should be in specified format: X,Y where X is minimal possible value and Y maximal. For loop is used to iterate through all environmental variables, their name is dynamically constructed each iteration using concatenation of strings.

Each measurement is random using Math.random() method. Measurement is added inside the data object using this line: dataObject[sensorName] = random; Where sensorName is key which will represent name of measurement and random is randomly generated value from previous step.

This subflow returns extended JSON object inside the msg.payload property.