Bin Count Subflow

Introduction

This subflow is part of our Internal flows collection. Internal flows are used for manipulating and storing data inside Node-RED.

Prerequisites

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

@senlab/node-red-statistics-senlab

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

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 2 environmental variables named range and count, working of the subflow strongly depends on these 2 variables, count string defines number of bins inside array and range is used to define expected range of incomming data.

Setting Function Node for dynamic bin generation

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 node utilizes context which is a powerful something inside Node-RED (or js in general maybe?). Variables inside function nodes are usually overriden each time message comes inside the function. With context we can make variables persistent through infinite iterations and make our custom logic for reseting their value.

Context get:

Context get is used to assign value which holds context to some variable in our case to binArray, || after context.get is OR operation which initializes variable in case this context is not yet present in the flow.

let binArray = context.get("binContext") || Array(count).fill(0);

Context.set is a method used to assign specific value to context, which can be later aquired using context.get

context.set('binContext', binArray);

Predefined Range and Count:

Range should be constructed in the following form: X-Y, where X is minimum value and Y maximum. Range is passed inside the function from environmental variable, using get method. Both values are parsed using split and parseFloat method. Split gives us array with values left of -'' and right of -'', parseFloat makes string into float value.

Bins are all the same size, number of bins is defined with count variable. They can be used for Data presentation with histograms.