- Installation
- Creating your first flow
- Using with MQTT on the Hub
- Example: Smart Home Integration using MQTT
- End
Run the following command to install:
bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)
After installation, to run as a service:
node-red-start
To run on startup:
sudo systemctl enable nodered.service
Once Node-RED is running, you can access the editor from another machine with the IP address of the hub: http://<ip-address>:1880
More information can be found here: https://nodered.org/docs/getting-started/raspberrypi
The inject
node allows you to inject preset messages into a flow, either by clicking the button, or setting a time interval between injects.
Drag one onto the workspace (main grid) from the palette (left sidebar).
The debug
node allows any message to be displayed in the Debug sidebar (right) which can be accessed from here:
Connect the inject
and debug
nodes by dragging from the output port of one to the input port of the other.
Click the Deploy button (top-right) to deploy the flow to the server. For each press of the inject button on the inject
node, in the Debug sidebar you should see a number pop up (timestamp in milliseconds).
The function
node allows you to pass messages through a JavaScript function.
Delete the existing wire (select and press Delete on keyboard).
Wire a function
node between the inject
and debug
nodes. Double-click on the function
node to bring up the edit dialog. Enter the following code into the function field:
// Create a Date object from the payload
var date = new Date(msg.payload);
// Change the payload to be a formatted Date string
msg.payload = date.toString();
// Return the message so it can be sent on
return msg;
Click Done to close the edit dialog and deploy the flow.
Now when you click the inject button, the function will take in the raw timestamp from the inject
node, format it as a date, and send it to the debug
node to be logged in the Debug sidebar.
Add an mqtt in
node and double-click it to edit the properties. Next to "Add new mqtt-broker..." click the pencil icon. Add a new broker with the following settings:
Then, go to the Security tab and enter the username and password for the internal broker.
Configure an mqtt in
node to subscribe to the events/object/occupantTemperature
topic:
Connect this to a debug
node and you should see the temperature being logged when you deploy the flow. Any time a change in temperature is observed, it should be logged as well.
Configure an mqtt out
node to publish to the commands/object/lightringPattern
topic:
Connect this to an inject
node with msg.payload
set to a JSON value. Enter the following as the value:
{
"data": "2"
}
Now deploy the flow. When you press the inject button you should see the 2nd light ring pattern (blue swirl) light up on the sensor hub. If nothing is showing up, ensure that your login for the internal broker was entered correctly.
For this example, we will be using 3rd-party nodes to handle the integration with Google Home and Amazon Alexa.
Select the Manage Palette
option from the main menu (top-right) to open the Palette Manager. Go to the Install tab and install the following packages:
- node-red-contrib-nora
- node-red-contrib-alexa-smart-home
This can take a few minutes. Click View log
to check the status.
We will be adding the Sensor Hub as a thermostat to our Google Home using the NORA nodes that we added earlier.
Add a nora thermostat
node to your flow, go into the node's properties, and edit the Config (nora config). Sign in here to get a token and copy it into your nora config.
With the node selected, you can view the acceptable inputs in the Help sidebar:
Create an inject
node for the thermostat's mode. Connect it to the thermostat
input. Be sure "Inject once after 0.1 seconds" is checked so that it is set on startup. Set msg.payload
to a JSON value. Enter the following as the value:
{
"mode": "cool"
}
Create mqtt in
nodes for setpoint (topic: events/object/tempSetPoint
) and temperature (topic: events/object/occupantTemperature
).
At this point, your flow should look like this:
As you saw previously, the mqtt in
node outputs something like:
{
"Present_Value": 25.32,
"Units": "°C",
"updated": "31-07-2020 02:18:43.0",
"status": 0
}
But the thermostat
node would expect this as its input (for temperature):
{
"temperature": 25.32
}
So we can add a function
node between the mqtt in
and thermostat
nodes to handle this. The code is as follows for the temperature:
var jsonPayload = JSON.parse(msg.payload);
var temperature = jsonPayload.Present_Value;
return {"payload": {"temperature": temperature}};
And for the setpoint:
var jsonPayload = JSON.parse(msg.payload);
var setpoint = jsonPayload.Present_Value;
return {"payload": {"setpoint": setpoint}};
You can also add an mqtt out
node for the setpoint (topic: events/object/tempSetPoint
). Add a function
node to convert the thermostat
node output to what the MQTT topic expects. The code is as follows:
var jsonPayload = JSON.parse(msg.payload.setpoint);
return {"payload": {"Present_Value": jsonPayload, "Units": "", "updated": "31-07-2020 01:15:23.73", "status": 0}};
Now, your flow should look like this:
Deploy the flow. Now all that's left is to add the device to your Google Home. Open the Google Home app on your phone and follow the these steps:
You can now assign the Sensor Hub to any room. Try asking Google Assistant "Ok Google, what is the temperature inside?" to get the current temperature from the Hub. You can also ask "Ok Google, set the temperature to 21 degrees" to publish the setpoint over MQTT.
For Amazon Alexa, we will be adding temperature and motion sensors to our smart home using the Alexa nodes that we added earlier.
First, create an account here and create both a temperature sensor and a motion sensor here.
In Node-RED, add two alexa smart home v3 state
nodes, one for each sensor. Double-click on them and add your account. You should then see your devices populate here:
With the nodes selected, you can view their acceptable input in the Help sidebar:
Create mqtt in
nodes for temperature (topic: events/object/occupantTemperature
) and occupancy (topic: events/object/combinedOccupancy
).
At this point, your flow should look like this:
As you saw previously, the mqtt in
node outputs something like:
{
"Present_Value": 25.32,
"Units": "°C",
"updated": "31-07-2020 02:18:43.0",
"status": 0
}
But the temperature sensor would expect this as its input:
{
"state": {
"temperature": 25.32
}
}
So we can add a function
node between the mqtt in
and temperature sensor nodes to handle this. The code is as follows:
var jsonPayload = JSON.parse(msg.payload);
var temperature = Number(jsonPayload.Present_Value);
return { "acknowledge" : true, "payload" : { "state" : { "temperature" : temperature } } };
The motion sensor is a bit different though as it is doesn't give the number of occupants, but whether it is occupied or not instead.
We can use a function
node between the mqtt in
node and motion sensor with the following code to deal with this:
var jsonPayload = JSON.parse(msg.payload);
var combinedOccupancy = jsonPayload.Present_Value;
if (combinedOccupancy === 0) {
return { "acknowledge" : true, "payload" : { "state" : { "motion" : "NOT_DETECTED" } } };
} else {
return { "acknowledge" : true, "payload" : { "state" : { "motion" : "DETECTED" } } };
}
Now, your flow should look like this:
Deploy the flow. Now all that's left is to add the device to Amazon Alexa. Open the Alexa app on your phone and follow the these steps:
You can now assign the Sensor Hub to any room/group. Try asking "Alexa, what is the temperature inside?" to get the current temperature from the Hub. You can also try setting up routines using the motion sensor and other smart devices. In the following example, I created a routine to turn off the light if no motion has been detected in the last 30 minutes during the day on weekdays:
Here is the complete flow for this tutorial (you will have to configure the internal broker login, nora token, and alexa account for it to work correctly).