Generate Random Line

generate line flow

Introduction

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

Generating Random Line flow can be used in various scenarios, where we dont yet have stream of data comming to Node-RED, but we want to test if our other flows work.

This flow simulates production lines which are common in industrial processes.

Prerequisites

This flow 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

Setting Up The Inject Node

We initiate our flow with the Inject Node, which is a basic node and already preinstalled inside Node-RED. It can be found under the common tab.

The Inject Node can function as a button or a signal generator. Users need to specify which property and value the node will inject into the flow. For the purposes of this flow, the specific configuration of the property and value is not critical, as the primary goal is to activate other nodes within the flow. In the configuration panel, users have the option to set the Inject Node to either inject the value just once or to do so repeatedly at a specified interval.

For this example, it is important that users set the Inject Node’s interval to 1 minute. The logic following the Inject Node is designed with the assumption that it will be triggered every minute.

step1 inject

Setting up the generate time strings function

Function Node is powerful Node-RED node which enables us to add our Custom Javascript code to Node-RED without the need to make custom nodes.

This function is used to generate 5 random values, one for each line. This value represents number of products each line will produce in a minute.

for (let i = 0; i < 5; i++) {
    count[i] = Math.floor(Math.random() * (6) + 5);
}

The function generates a timestamp for each product manufactured. It utilizes a for-loop, which iterates as many times as the maximum number of products that can be produced on one line. This maximum number is determined using the Math.max() method. In each iteration, an offset timestamp is added to an array. The offset is calculated to ensure that the time intervals between the production of each product within a minute are consistent.

if (i < count[0]) {
        let offset_part0 = offset[0] * i;
        let entryTime = new Date(starttime.getTime() + offset_part0);
        timestamps1.push(entryTime.toISOString().replace('T', ' ').substring(0, 19));
    }

Each array of timestamps is saved into its own property of msg object and sent out of the function for further processing.

Set Node

The Set Node is used for reassigning values to other properties of the msg object. In this setup, where the flow branches into five distinct directions, the Set Node assigns each timestamp array to the payload property for each respective branch.

set node

Debug Node

The Debug Node enables users to inspect properties of the msg object. Users can choose to view the entire msg object or only a selected property. This node is crucial for development within Node-RED and should be utilized frequently to ensure optimal function and debugging.

Split Node

The msg.payload property, set in the previous steps, contains an array of timestamps. To distribute this data, the array needs to be separated into individual elements. The Split Node accomplishes this by allowing users to specify criteria for how the data should be divided. Given that the structure is solely an array, only the array configuration matters for this purpose. To split the array into all individual elements, select a fixed length of 1.

split node

Delay Node

The "Generate Random Line" flow is often utilized in conjunction with the Odoo Example for simulating line operations and displaying data on the dashboard. Due to limitations in the Odoo API regarding the number of measurements that can be saved within a certain timeframe, the Delay Node is employed to manage data transmission without errors. This node adjusts the rate at which messages are passed through; in the current setup, one message is sent every three seconds across all five lines. However, this rate can be adjusted—increasing the frequency of messages per second is possible if fewer lines are being simulated.

Setting up Function Node for Odoo

This flow is designed under the assumption that lines are being simulated for integration with Odoo, though the function can be modified. It constructs a simple JavaScript object containing three properties: name, timestamp, and line number. This object is designed to integrate seamlessly with a custom module created within Odoo. For more information about Odoo and its custom modules, refer to this documentation: Odoo

Sending The Data Through MQTT

MQTT is our protocol of choice inside our Node-RED examples and you will see it being used many times. To find out what MQTT is and how to use it in the scope of Node-RED, please refer to this documentation: MQTT

MQTT Node uses outMSG variable to be sent out as message body, in this case previously created object needs to be assigned to the outMSG variable.