The Open Source Smart Home: Getting Started with Home Assistant & Node-Red

Home Assistant is one of the most interesting open source projects I’ve ever come across. It interfaces with any device, platform, or service you can think of and allows you to perform actions based on the data you monitor. It has a great community and is under extremely active development. With a little creativity almost anything is possible with Home Assistant, and best of all it’s private and totally under your control.

After using Hass to control my smart home for the last year, I started to hit the limitations of it’s YAML-based configuration. Any automation that was even moderately complicated  required a lot of pieces spread out through the configuration files (see the sprawling “Creating an Alarm Clock” thread on the HA forums for an example). Doing simple things like if-then or a  loop required awkward workarounds. As my automations (and ambitions) increased in complexity, so did the time I spent  trying to figure out what was going on.

That’s when I discovered Node-Red, a visual programming tool developed by IBM. Node-Red is the perfect complement to Home Assistant, allowing for very complicated logic to be constructed visually through a simple “flow” interface. It integrates seamlessly with Home Assistant. Let’s set it up.

STEP ONE: Installing Home Assistant

The easiest way to get up and running with Home Assistant is to install Hass.io on a Raspberry Pi. All you need to do is download the image, flash it to an SD card, and plug it in to your network-connected Pi. Soon you will be looking at the Home Assistant web interface. For more detailed instructions, refer to the Home Assistant documentation or watch this great video from BRUH Automation:

STEP TWO: Installing Node-Red

Hass.io provides a kind of “app store” for additional software. The addons are pre-configured for use with Home Assistant. To add Node-Red, click the hamburger menu in the Home Assistant Web UI and select “Hass.io”. From there select the Addon Store icon in the top right. You’ll see a list of default addons, but we need to add another repository to get Node-Red.

Enter https://github.com/notoriousbdg/hassio-addons and hit save. Refresh the page and you should see Node-Red available for install.

Install Node-Red, Start the service, and then hit the “Open Web UI” to access it. That’s it. The default login is admin/password, which is configurable in the Options section for the Node-Red addon.

STEP THREE: Integrating Home Assistant & Node-Red

Node-Red uses “nodes” to send messages from an input, through a series of flows that you diagram, and then it ends in an “output”.

In our case, the inputs will come from Home Assistant (a state change, an event), the message will be routed according to our needs, and then Node-Red will output back to Home Assistant by making a service call.

So how do we get these two things talking to each other?

In Node-Red, select the hamburger menu in the top right and click “Manage Palette”. This shows us a list of installed nodes, and a way to search for new ones contributed by the Node-Red community.

As you can see, there are several user contributed Home Assistant nodes. I tried all of them, and the one you want is node-red-contrib-home-assistant – not the “-ws” websocket one or the POST one!

We will now have a useful set of nodes for interfacing with Home Assistant available in the sidebar.

Optionally, we can also add Node-Red to the Home Assistant menu for easy access. To do this, add the following lines to your configuration.yaml file:

panel_iframe:
 nodered:
   title: 'Node-Red'
   icon: 'mdi:shuffle-variant'
   url: 'http://YOURIPADDRESS:1880/'

STEP FOUR: Putting it All Together

Now that everything is installed, let’s make a simple flow and make sure it works.  I am going to use an inject node as an input, and a Home Assistant service node as the output to toggle a light.

Drag the two nodes into your palette and connect them with a wire.

The simplest flow, an input and an output.

Clicking once on either node will show it’s documentation in the “info” panel to the right. Note the red triangle on the Home Assistant node – this means the node needs to be configured. Double clicking it brings up it’s options.

Node Options

Enter your Home Assistant server details if you haven’t already. Then fill in the appropriate data for the service call. In this case we use the “toggle” service from the “light” domain on the “light.bedroom” entity. The Data field is filled out in JSON format, exactly the same as the Service Call page in the Home Assistant developer tools.

All filled in correctly, we must hit the DEPLOY button to activate the flow. Now when you press the button on the inject node, Home Assistant will toggle the light on and off.

FURTHER RESOURCES

This guide is just to get you set up quickly and does not even begin to scratch the surface of what is possible running Home Assistant and Node-Red together. Next, you may want to set up some basic flows to automate lighting or look at some of the other useful nodes available to get your own ideas.

For a general overview of Node-Red and it’s interface, see this video

8 Comments

  1. You might not remember me but I was the one asking you on Reddit for this blog’s URL. Great job, will definitely read on here 🙂

    One thing I want to throw in:
    Since I (and you too for sure) already use an MQTT broker, I was wondering if I could decouple HA and NR via that route. So I looked at the components list and there is indeed an EventStream and StateStream MQTT component. NR has a great MQTT node built in.

    Just played with it and it is awesome!

    Now I can setup separate Docker containers for Mosquitto, HA and NR and everybody is just talking to Mosquitto. That way I can have HA’s excellent integration with all my lamps and what not using it as a “state machine” and having NR doing the heavy logic lifting. Previously I went down the track of doing everything in NR but state is not its strength, one has to pollute the global namespace to keep things for later (perks of leveraging the functional approach).

    I don’t know if that would be something for you since you already integrated it but I think this way it is just one moving part (well, two in this case) less to break.

    Reply

    1. Thanks for the reply! I am basically doing exactly as you described, using Home Assistant as the “state machine” with NR on top. That is a good way of putting it. I was also looking at the MQTT state/event stream, it seems ideal to me to just use MQTT for everything as well.

      I’m currently working on seeing if I can make a dumb IR fan into a Hass mqtt_fan using NR to read sensor values and output appropriate MQTT. So using NR’s better logic to maintain the state in Hass.

      I’m also looking at converting iBeacon signals to MQTT, to then format for the new mqtt_room component. (I have some ESP32’s but haven’t found any reliable BLE->MQTT code for it yet).

      So you are using MQTT as inputs and outputs directly? The one thing about the state stream is that it doesn’t appear the states are published with the retain tag, so it seems it it could only be used for inputs/outputs? Nice thing about the Home Assistant nodes I install here is that you can get a state or render a template (super useful) mid-flow. I guess you could also use the HTTP API to get a state mid-flow, but that’s basically what these nodes are doing I think.

      Reply

      1. Well I did not think about that for sure^^

        However I tried just now on one of my test devices and it seems that HA pulishes every state on startup at least once. So any consecutive state change would be covered by NodeRed. The persistence tag seems to be set as well as I was able to get all the published states:
        starting Mosquitto -> starting HA -> killing HA -> mosquitto_sub -v -t “#”

        If I enable persistence in the Mosquitto server I could even preserve states beyond complete server reboot without restarting HA. That opens a whole lot of new possibilities as the streams are designed for HA to sync over instances and we could publish via NR to the same state attributes while HA is down (for some reason). Therefore if HA comes up again it could(?) update itself from the states previously published – and changed by NR in the meantime? So effectively Mosquitto is the “real” state machine with two or more clients reading and publishing from it.

        Btw. you were right, I just dug through the source of the plugin as well and in the config-server.js the node calls the REST API of HA, so I think using that is feasible as there seems no magic hidden behind some covers. Nevertheless I will continue to try to find a way to completely separate the “state machine” from the logic which gives me some “reward” as a hobby dev 😉

        Reply

  2. Thanks. I got it working by changing my ip to 127.0.0.1. No idea why that worked.

    I wasn’t using https, but am now switching over so that I can integrate Google Assistant with HA. So I’ll need to use https and an external domain (DuckDNS).

    Thanks for the link.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *