Check Existing Entity in Orion Subflow

Introduction

This flow is a part of our Output Flows Collection. Output flows are used to export data from Node-RED to other Services running on Sandbox for further processing and display.

Prerequisites

This subflow along with other flows for Node-RED Fiware-Orion integration are available 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 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(’);

Setting Function Node for dynamic URL 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.

msg.url = "http://orion:1026/v2/entities/sensor_agriculture" + msg.payload['id'];
msg.payload_future = msg.payload;
return msg;

Inside Node-RED all data is transmitted inside the msg object. Most common object inside Node-RED is payload, be aware that most of the predefined or custom made nodes from other users write into payload property of the message object, this means it is a good practice to save important data inside another property.

Some nodes expect data such as URL in this case to be present in the specific property of the msg object. Because we use http request node URL should be defined inside the url property. dynamic URL is constructed in a way so that we make a QUERY to Fiware-Orion Context Broker if entity with specified name (sensor_agriculture + ) exists. For more information about Fiware-Orion URL endpoint structures check out this documentation: .

Setting up HTTP Request Node

This is basic preinstalled Node-RED node which is used to make HTTP Requests. User can select the request method inside configuration window, along with URL, payload (request body), authentication options and format of response to the request.

When making queries to the Orion Context broker we need to issue GET requests, URL window does not have to be set because it was already constructed in previous function node. Advantage of setting URL in function Node is that we can construct them dynamically based on our needs, compared to hardcoding it inside the URL configuration option.

For further use we want http response to be in the form of parsed JSON object.

Setting Function Node for error check

let payload = msg.payload;
if (typeof payload === 'string') {
    payload = JSON.parse(payload);
}

if (msg.payload['error'] === 'NotFound') {
    msg.check = 'true';
} else {
    msg.check = 'false';
}

msg.payload = msg.payload_future;

return msg;

HTTP Request node returns response inside the msg.payload property. First if-statement is check if the payload is really parsed JSON object, if not it parses it. Second if-else statement checks for the error property inside returned object. We were queriying for existance of entity inside Orion, if the error is NotFound (meaning object is not found), msg.check is set to true, otherwise false.

Setting up switch node to execute switch-case in Node-RED

User can change direction of the flow based on incomming messages, this switch node checks wether the msg.check from previous function is true or false and based on this changes direction of the flow.