I’ve spent the last few months tinkering to take my home automation to the next level and transform a loosely connected network of sensors and IoT devices to a smart home. If you’re not sure what a smart home actually is, read this article first. The way I view it, there’s a key difference between home automation and smart home. While the former may add some convenience, it’s essentially just automating mundane, repetitive tasks such as turning lights on and off. The latter not only handles the automation but understands the context to drive the automation. For example, imagine simple home automation to turn a light on when motion is detected. Now imagine the same automation with contextual awareness to only turn the light on during certain times, if a certain device is connected to the home network, and to display the bulb color as either red, amber, or green based on the commute time to work. This is just a small example of the power and convenience that a smart home setup can bring.

Enter Node-RED

So what does all this have to do with Node-RED? Node-RED is a development team created by IBM for IoT devices and is the perfect companion to a home automation platform such as Home Assistant. At its core, it allows for easy automation of virtually any connected device or service in an easy to understand visual flow interface composed of nodes and wires. To get started, setup Node_RED based on the environment you’re using. In my case, I’m running Node-RED as a service via Docker and orchestrated with Docker Compose.

Here’s an example of my Docker Compose file:

Copy to Clipboard

Once Node-RED is set up, you’ll want to link it with your home platform.

Don’t forget to secure your Node-RED instance, especially if you plan on exposing it to the internet.

Using Home Assistant, this is pretty straight-forward and involves adding the node-red-contrib-home-assistant-websocket node to your palette. The palette in Node-RED parlance is basically a menu containing all the nodes (i.e. extensions). There are tons of nodes developed by the Node-RED community for a diverse spectrum of purposes, products, and platform integrations. Once the node is added, configure the server node to connect Node-RED to your Home Assitant instance. To do this, grab a long-lived access token from Home Assistant by navigating to the profile page. I’m running Home Assistant using Docker (not HASS.io) so your setup might look a bit different depending on how your setup.

Next, fill out the server information and add the token to Node-RED server configuration. If all goes well, Node-RED will connect to your Home Assitant instance and you’re ready to get started. If you have any issues, use the built-in debug node in Node-RED to troubleshoot.

Automation 101

Once you’re connected, it’s time to begin your smart home evolution. Three things to note before diving in:

  • Home Assitant comes with it’s on YAML based automation capabilities. You can craft automation via the front-end or directly in the automations.yaml file. There’s nothing wrong with this approach, it’s just not visual and can be quite tedious as anyone who’s worked with YAML knows… one extra space can brick your setup and force you to go line by line looking for the offense.
  • If you go the Node-RED route, it essentially just executes logic against Home Assistant’s state machine, which just keeps track of all your sensors and devices and their respective status (e.g.,kitchen motion sensor is on, bedroom light is off).
  • For those not interested in coding, Node-RED is definitely more powerful given its many nodes and powerful integrations with other platforms such as Telegram for notifications. That said, it’s never a bad idea to learn the basics of a language like Python or Java and you can create extremely sophisticated automation with a bit of code.

Here’s an example of a basic node that does the following things:

Node-RED Flow

See the JSON on Pastebin
  1. When motion is detected in the office, check to see if the office light is on. If it is, do nothing. If not, check the time. Depending on the range, turn on the office light at full brightness (during the day) or dim mode (during the evening).
  2. Once the light is on, start a timer for 10 minutes. If no additional motion is detected after the 10 minute timer, turn the light off. If motion is detected at any time, restart the timer.
  3. Finally, turn the light on or off using a Xioami wireless switch. Apply the same time-based motion logic to automatically turn the light off if no motion is detected.

One of my favorite aspects of Node-RED is its reusability. Once I have a solid lighting flow in place, I can just copy/paste the nodes for additional rooms and lights. This helps keep maintenance to a minimum and easily enables tweaks down the line if I add or remove lights or other devices.

Automation ControlCurrent-stateWithin Home Assistant, you can use input booleans as a simple way to turn off Node-RED automation. This is useful if you’re troubleshooting, or for any other reason you would want to temporarily disable your automation (e.g., guests coming over, home maintenance). Simply create an input boolean in Home Assistant. In my case, I created a boolean for each area in my home. Next, insert a Current State node that halts if the input boolean is off. In my example, each boolean defaults to “on”. I turn it “off” when I want to disable the automation.

Finally, uncheck the override box in the Current State node. Re-deploy your node and add the input boolean to your Home Assitant front-end. Now you have a simple and convenient way of controlling your automation outside of Node-RED.

Shout out to DIY Futurism for their excellent series on Node-RED and a useful list of other nodes to look at.