Getting Started With Home Assistant and Node-RED

Navigate to:

Howdy, smart-home enthusiasts! If you’re fascinated by the world of home automation but find yourself wrestling with complex scripts and codes, you’re in for a treat! Today, we will delve into the amazing combo of Home Assistant and Node-RED. Home Assistant is a fantastic open-source home automation platform, while Node-RED is a visual tool for wiring the Internet of Things. Together, they make home automation a breeze.

All right, let’s roll up our sleeves and get started!

Step 1: setting up Home Assistant

First, we need Home Assistant running. You can install it on Raspberry Pi, a Linux machine, or even a Windows PC using VirtualBox. You can find detailed instructions here. Once installed, set up some devices. Lights, switches, sensors, you name it!

Step 2: installing Node-RED

Next up, we’re bringing Node-RED into the mix. Within Home Assistant, navigate to Supervisor > Add-on Store. Find Node-RED, install it, and then hit Start. It’s that easy!

Step 3: Node-RED & Home Assistant, best buddies

After Node-RED starts, click Open Web UI. The Node-RED interface will greet you. Now, ensure that Node-RED and Home Assistant are on speaking terms. Next, check the sidebar for a small Home Assistant icon. If it’s green, bingo! If it’s red, check the add-on log back in Home Assistant. It usually sorts out any connection issues.

Step 4: your first flow

Time to create your first flow, Node-RED lingo for automation. Hit the + icon to create a new tab. Drag an events: state node (from the Home Assistant section) onto the workspace. Double-click it, name it Turn on light, choose your light entity, and set the If State to on.

Then, drag a call service node onto the workspace. Name it Notify, set Server to Home Assistant, Domain to notify, Service to your notification service, and Data to a message like {“message”: “The light is on”}.

Connect the two nodes together, hit Deploy, and voila! You’ve created an automation that sends a notification whenever your chosen light turns on!

Step 5: installing InfluxDB

You’ll need a robust solution for data storage and that’s where InfluxDB comes in.

Developed specifically for managing heavy write and query loads, InfluxDB is a time-series database that boasts exceptional performance. In essence, it’s a database specially made for handling time-stamped or time series data sets of data points indexed in time order. Sounds complex, right? Don’t worry. It’s simpler than you might think.

Time series data can be envisioned as a chain of data points that commonly comprises consecutive measurements taken over a period of time. This type of data pops up all over the place. Stock market data, energy load data from a power grid, server metrics, sensor data, and (drum roll, please) home automation data are all examples of time series data.

The brilliance of InfluxDB lies in its purpose-built design. It provides incredible speed and flexibility when dealing with time series data. Think of it as a highly skilled specialist compared to a general practitioner.

InfluxDB features a SQL-like query language, making it easier to interact with your data. You can do all sorts of things, like find a specific data point or find a range of data points between particular timestamps.

InfluxDB really shines when used in a home automation scenario, like with Home Assistant and Node-RED. Sensor data readings, like temperature or humidity over time, or switches’ on/off state, are time series data. InfluxDB allows you to efficiently store, retrieve, and analyze this data, making your home automation setup even more powerful.

Like Node-RED, you can install InfluxDB directly from the Home Assistant Add-on Store. Once installed, hit Start, and ensure it’s running smoothly.

Once InfluxDB is installed, ensure the Watchdog slider is on and then click Start. You can verify it’s running by checking the logs at the bottom of the page.

To access the InfluxDB dashboard, click OPEN WEB UI at the top. This will take you to the InfluxDB dashboard, where you can create databases and users and perform other management tasks.

Step 6: linking Node-RED to InfluxDB

Next, we’ll connect Node-RED to InfluxDB. In Node-RED, you’ll need to install the InfluxDB nodes. Go to Manage Palette > Install, search for node-red-contrib-influxdb, and install it.

Create a new flow, drag an influxdb out node onto the workspace, and double-click it to configure the connection to your InfluxDB server. Use localhost for the host if InfluxDB and Node-RED are on the same machine.

Step 7: storing data in InfluxDB

Ready to log some data? Great! Let’s create a flow that logs temperature data from a sensor to InfluxDB. Drag a state changed node, configure it for your temperature sensor, and link it to your influxdb out node.

Set the measurement as temperature in the influxdb out node. Now, whenever your temperature sensor updates, Node-RED logs the data into InfluxDB. Pretty nifty, huh?

Step 8: practical examples

Let’s look at a practical example. A picture containing circle, wall
Description automatically generated

Example 1: smart temperature control

Let’s say you have a smart thermostat, and you’d like to adjust the temperature based on the time of day and whether you’re home. Here’s how you can achieve this with Node-RED.

First, create a flow with a time-range-switch node (you might need to install the node-red-contrib-light-scheduler from Manage Palette). Then, set different ranges for morning, day, evening, and night. Each range connects to a call service node that sets the thermostat to the desired temperature.

Next, add an events: state node that triggers the state change of a device tracking your presence (like your smartphone). For example, if you’re not home, it can trigger a call service node that sets the thermostat to an energy-saving temperature.

Example 2: intrusion alert

You can set up a security alert when motion is detected at home while you’re away. Begin by using an events: state node for your presence detection. Then, when you’re away, it sets a global variable Away to true.

Then, create a flow triggered by an events: state node from a motion sensor. Next, add a switch node checking the Away variable. If it’s true, it connects to a call service node that sends a notification or triggers an alarm.

Example 3: tracking power consumption with InfluxDB

You can log this data into InfluxDB for analysis if you have smart plugs measuring power consumption.

Set up a flow using a state changed node configured for your smart plug. Connect this to an influxdb out node with the measurement set as power. You’re now logging power data into InfluxDB every time it changes.

Using InfluxDB’s query capabilities, you can analyze this data to understand your power consumption patterns and even identify energy-saving opportunities.

These are just a few examples of what you can achieve with Home Assistant, Node-RED, and InfluxDB. The possibilities are practically limitless, so let your creativity shine and have fun automating your smart home!

Summary

Well, there you have it! You’re now well on your way to automating your home like a pro with Home Assistant, Node-RED, and InfluxDB. Start experimenting with different flows and you’ll soon realize the potential of this terrific trio.

Remember, every smart home is unique. So keep tinkering, keep exploring, and most importantly, have fun with it! You’ll be amazed at the clever, creative, and convenient automations you can come up with. So enjoy your journey in the world of home automation!

This post was written by Juan Reyes. As an entrepreneur, skilled engineer, and mental health champion, Juan pursues sustainable self-growth, embodying leadership, wit, and passion. With over 15 years of experience in the tech industry, Juan has had the opportunity to work with some of the most prominent players in mobile development, web development, and e-commerce in Japan and the US.