Build your Smart Home using Node-Red

Keith Tay
6 min readMay 30, 2019


The use of Internet-of-Thing (IoT) brings about myriad benefits and is increasingly transforming the way we live, making our life easier. Traditionally, the control of our home devices such as turning on or off the lights, boiling water in a kettle or cooling the house with an air conditioner requires manual human intervention. However, with the use of IoT, we can remotely control or even automate these operations in our home.

Imagine your alarm clock ringing in the morning, your room light brightness gets brighter every 2 minutes, your room’s blind is slowly letting sun in, your water heater gets switched on, hot water starts boiling in your kitchen (and that list goes on….), how amazing will that be?

In this article, I will touch on a software tool titled Node-Red and share two flows on how you can control your smart devices and even automating them.

Set up:

For this article, the following hardware were used:

  1. Raspberry Pi
  2. CC 2531 (Coordinator)
  3. Xiaomi Aqara smart LED bulb
  4. Xiaomi Aqara wireless switch

The following software were installed on the raspberry pi:

  1. Mosquitto — The MQTT protocol provides a lightweight method of carrying out messaging using a publish/subscribe model. This makes it suitable for Internet of Things messaging such as with low power sensors or mobile devices such as phones, embedded computers or microcontrollers.
  2. ZigBee2Mqtt — Allows you to use your ZigBee devices without the vendors bridge or gateway. It bridges events and allows you to control your ZigBee devices via MQTT. In this way you can integrate your ZigBee devices with whatever smart home infrastructure you are using.
  3. Node-Red — Node-Red is a programming tool for wiring together hardware devices, APIs and online services in new and interesting ways. It provides a browser-based editor that makes it easy to wire together flows using the wide range of nodes in the palette that can be deployed to its runtime in a single-click. Node-Red allows the creation of a user interactive dashboard to remotely control or monitor your IoT devices.

*I will like to stress on the importance of having a secure set up for all the software tools required for installation. Ensure that you secure your Smart Home set up by having authentication for Mosquitto and Node-Red, SSL for transport, replace default network keys etc. In a separate post, i shared on the potential danger associated to ZigBee IoT devices.


Figure 1: Architecture set-up

About Node-Red Flows:

Figure 2: Node-Red flows

Every flow in Node-Red requires the use of an input and output node. In between the input and output node, you could have functions that checks the data and react differently depending on the input. For example, if the wireless switch is clicked once, the smart light bulb will turn on and if the wireless switch is clicked twice, the smart light bulb will turn off.

JSON (JavaScript Object Notation) is a syntax for exchanging data. In the ZigBee2MQTT set up, JSON is used to control the smart devices. For example, to turn on the smart light bulb, set the inject node (Figure 3: On Smart Light) properties to send the following JSON data {“State”:”ON”}. The MQTT publisher node (Figure 3: Light Toggle) will publish the JSON data to the MQTT broker which will be forwarded to the smart light bulb, turning on the light.

Figure 3: Using inject node to turn on/off a smart light

When building your smart home with Node-Red, there may be times where error is encountered. To aid with resolving errors, subscribe to all topics using the # sign and output them to a debug message node.

Figure 4: Setting debug messages for all topics

Now you should have a better idea on how Node-Red works, and with that knowledge, we will move on to 2 simple flows.

Flow 1: Using wireless switch to control your smart light

In this flow, we will learn how to control the smart light bulb using a portable wireless switch. After fiddling around with the wireless switch, from the ZigBee2MQTT logs, the JSON data contained a key titled ‘click’, with values being either ‘single’, ‘double’ or ‘triple’. This means that we can program up to a maximum of three different outcome of our choice.

Figure 5: Flow to use a portable wireless switch to control a smart light bulb

To begin, insert a MQTT input node. Under the properties, subscribe to the portable wireless switch device ID. This helps filter other ZigBee IoT device messages and execute the flow only when the wireless switch is clicked.

Figure 6: Properties of MQTT input node

Place a JSON message converter. I’ve noted that in the absence of this JSON converter node, the subsequent nodes in the flow will not be able to interpret my message.payload data.

Figure 7: JSON node property

Thirdly, add a switch node that reads the value of the ‘click’ property. The switch node have two branches that will flow accordingly depending on the value ‘click’ property.

Figure 8: Properties of switch node

If the consumer performs a single click on the wireless switch, the smart light bulb will turn on. To do so, send the {“State”:”ON”} JSON data to the light bulb, and for double click, we will send a {“State”:”OFF”} to turn the light bulb off.

Figure 9: Properties to turn on/off the lights

Lastly, we will want to publish the JSON data to control the Smart light bulb.

Figure 10: Properties of MQTT output node

Flow 2: Wake up lights

In this wake up lights flow, we will learn how can one automate smart devices.

Figure 11: Flow to automate wake up light at a specific time interval

To begin, we will start off with the creation of an inject node, setting its properties to turn on the light and repeat at a specific time interval each day. You can also replace the input node using a dashboard input field, allowing end users to fill up a form that contains the desired time, which days of the week and any other options for the your home automation. For the dashboard, you have to install an optional module titled node-red-dashboard.

Figure 12: Inject node property with specific time variable set

Subsequently, I used a function node to declare a new flow variable, setting the initial light bulb brightness.

Figure 13: Function node property to declare flow variable

In my case, I created a loop that executes subsequent nodes in the flow for 5 consecutive times with a 5 second interval.

Figure 14: Loop node property

The first function it will perform is to increase the brightness of the light bulb. Using the flow variable we created earlier, we will continuously increase its brightness by 35. We will then publish the JSON data to ensure the light state is on and updating the bulb brightness.

Figure 15: Function node property to iteratively increase the light bulb brightness

The second function it will perform is playing an audio. In this flow, I used the UI audio output which required a string as an input. You can send string messages by installing an optional module titled node-red-contrib-string.

Figure 16: UI audio output node to play a background sound


As you have seen from this article, setting up your smart home is affordable, easy to do so using Node-Red and brings convenience to end users. Node-Red offers a lot of other optional modules created by the community that can help with the design and creation of your smart home flows. I hope this article found you well and do share any interesting flows!



Keith Tay

Cyber-Enthusiast | IoT Specialist | Penetration Testing | Red Teaming