Define and execute an Action in Node-RED

Define Actions on your devices and call them from the cloud. For example, use actions to turn on/off a lock or call a function that reboots your device.

Prerequisites

For this section, you'll first need to connect your device to the cloud, create a remote connection for Node-RED, and create a Device API Key.

In case you have a Node-RED already installed manually and only need to exchange real-time data without managing your device, you can add IoTFlows Node-RED Nodes manually to your Node-RED. Learn how to add nodes to Node-RED.

Create an Action

From the side navigation tab of the device page, click on Create Action. Set the name, description and hit submit. For this example, we will name the action "Bash Command" since we want to execute a bash command from another device or from the cloud on this device.

Connect to Node-RED

Ensure your device is connected to the cloud by checking if you see the green light on the device page, Enable the Node-RED connection in the Remote Connections tab, and hit Connect.

IMPORTANT: We strongly recommend that you change the default username and password for Node-RED (admin, password). Learn how to change the Node-RED credentials.

Define an Action on your device in Node-RED

To define an action in Node-RED, drag and drop an action start and an action finish. Every action start node needs to be finished with an action finish node to confirm that the action was properly executed. When an action is triggered with an HTTP request, this will result in an HTTP response code of 200 that confirms the request has been fulfilled properly

Double-click on the action start node, and then click on the edit button to add the device API Key credentials if you haven't configured the connection yet.

Next, we will click the refresh button in the Topic and select the Action that we will like to execute, in this case "Bash Command", and click Done.

Connect the action start node to your the function / actuator that needs to be triggered when the action is called. In this case, we will connect it to an exec node to execute a command. Drag and drop an exec code and connect its threads similar to the following figure. Also, double click on the exec node and check mark the Append option to pass the msg.payload (the HTTP request body) to this node. This way, once we execute this Action from the cloud, our command will be executed on the device and its stdout results will be sent back to us.

Execute/Call an Action

You can send a HTTP Post request to execute / call an Action. You can find the action uuid from the right hand side navigation.

post
Execute a Device Action

https://api.iotflows.com/v1/actions/:action_uuid
When executing a device action, the Body of the HTTP request will be sent in a string format to the device action. Once the device sends back the confirmation message, this http request will return a success (200) message back.
Request
Response
Request
Headers
Authorization
required
string
Basic Authentication (base64-encoded string of client_username:password)
Response
200: OK
[successful operation] OR [response of the action]

get
Read Last Status of a Device Action

https://api.iotflows.com/v1/actions/:action_uuid
You can read the last status of a device action by sending this request.
Request
Response
Request
Path Parameters
required
string
Basic base64-encoded string of client_id:password
Response
200: OK
{
"action_last_confirmed_status": "string",
"action_last_confirmed_status_timestamp": "string"
}

‚Äč