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

ConfigHome AssistantNode-Red

Written by:

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. It can connect all of your devices to make a truly smart home. 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 its 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 the addon repository here

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

Addon Reposityory List

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.

Choose the node-red-contrib-home-assistant package

Choose this one

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!

RELATED >>  Big List of Useful Home Automation Nodes for Node-Red

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

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.

Editing a Node'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.

Ready to make some simple flows? Check out my post on Basic Lighting Flows.

SETTING UP ENCRYPTION

To set up Let’s Encrypt SSL certificates for both Home Assistant and Node-Red, see this post.

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


38 Replies to “The Open Source Smart Home: Getting Started with Home Assistant & Node-Red”

  1. Tristan says:

    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.

    • brad says:

      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.

      • Tristan says:

        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 😉

  2. Curtis says:

    What is the end point of the Base URL field? I keep getting “Error calling service, home assistant api error”.

    I’ve tried
    http://[MY_HASS_IP]:8123/
    http://[MY_HASS_IP]:8123/api/
    http://[MY_HASS_IP]:8123/api/services/

    Just keep getting the same error. I’ve verified through Chrome that the GET API is working, so I don’t think it’s a HA issue. I’m just not getting the Code-Red server correct.

    Thx.

  3. Curtis says:

    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.

  4. Jeff says:

    Whenever I try to add the call service node, I get the error “Cannot GET /homeassistant/services”. I am using SSL, and have the addon configured according to the github link you posted in the comments. I am able to use the Home Assistant API from external devices successfully, but I simply cannot get Node-RED to work. Any suggestions?

    Note: I am using the fully qualified domain name (https://subdomain.duckdns.org:8123) in the config node.

    • Jeff says:

      WAIT! I got it working!

      For anyone else running into the same problem as I did, using the FQDN as I wrote in my previous comment is correct, even though it gives an error. Ignore the error, deploy your flows, and THEN it should start to successfully communicate.

  5. William says:

    Thanks for the write-up but I’m running into an issue with just the basic flow of turning on and off a light. Although it shows to have deployed successfully, I’m getting the following error: Cannot read property ‘getServices’ of undefined. Followed up by: at / shar/node-red/node_modules/node-red-contrib-home-assistant/config-server/config-server.js:44:38, etc…
    Any thoughts on what this could be?

    • brad says:

      That looks like it is not able to authenticate with your Home Assistant server. Did you enter your server and login details? Double click any of the Home Assistant nodes and there is a “Server” setting with an edit button next to it. See if updating that helps.

    • William says:

      Disregard this post. I uninstalled the HA node and added it again and it is working now!

      • brad says:

        Awesome. I had two conflicting nodes once, just for future reference for anyone reading this – you can edit the packages (ie manually remove/add something) and their settings files directly. They’re in the /share/node-red/node_modules folder in Hassio, if you enable Samba file sharing.

  6. Tim Hahn says:

    hej

    i tried installing Node-red in Hassio but i don’t see your add on being added to the list of addons.

    I put in this line in the box, hit save, it does something but when i refresh the page i don’t see the red-node 🙁
    https://github.com/notoriousbdg/hassio-addons

    What am i doing wrong?

  7. travis says:

    Hi,
    I am trying to follow the steps here however I couldn’t find node-red-contrib-home-assistant in the palette list. Any suggestions?

    thanks.

    • brad says:

      I think the search in Node-Red is kind of weird. Try searching just “assistant” or something. You can also install them manually, in Hassio all the files are in /share/node-red

  8. GS says:

    Hi,

    I’m new to HA and would like to learn from your experience.

    Is state tracking something that node-red can’t handle well?
    I’m trying to understand how much different will adding HAss make.

    As it is, we already need

    1. A MQTT broker
    2. node-red

    Thank you.

    • brad says:

      Correct, Node-Red does not do state tracking well. The main benefit of using Home Assistant is the number of devices/components supported (almost 1000) and the unified API for all those components for tracking states and calling actions. So you get all your state tracking and service calls in one place.

      MQTT is really optional, you just need NR and HA, unless you have a specific need for it. Home Assistant also has an internal MQTT broker if you’d rather not run an additional service. I have not had any issues with these 3 services running side by side though – very stable.

      • GS says:

        Hi Brad,

        I have some custom sensors, so MQTT is used to avoid reinventing a bad wheel. On Centos 6, I had trouble installing HAss and node-red gave some errors when installing the node-red-contrib-home-assistant.

        I will probably try Centos 7 before going with a RPi. Do you have a quick way of replacing your RPi when it fails? What about backing up its latest configuration? I read it takes really long to get the software installed and going.

        Thank you for sharing!

        • brad says:

          Yeah I use MQTT for the same – I have some Python scripts scraping things from the web and a bunch of esp8266 based sensors. Very easy to track states in Home Assistant for different types of data with their MQTT components.

          I’m not familiar with Centos, you may want to look into running HA+NR+MQTT in Docker containers, ie. https://community.home-assistant.io/t/my-docker-stack/43548 I think that is the way I would go if I were running on a traditional Linux type system. Docker is available on pretty much every platform I believe.

          I do really like Hass.io on the RPi. Like I detailed here, all the supporting programs (N-R, MQTT, etc.) are run in containers similar to Docker. Installing, configuring, and updating these additional programs just takes a few clicks in the web UI. It’s very easy to maintain, and it has a built-in backup system that backs up all your config files, additional programs, settings, etc. If the Pi or SD card fails, I just burn a new image of Hass.io on a new card, copy the backup (it’s just a tarball TGZ file), and hit restore. That brings back not just the HA configuration files but Node-Red, my Let’s Encrypt settings, etc.

  9. Matt says:

    Hi Brad, I am new to HA and Node-Red what I am trying to do is make a flow that will shut off my HVAC if any doors/windows are open for 5 minutes. Then I want it to turn back on once all of the doors/windows are closed. Is this possible and if so where do I begin? I’m sure once I see a flow on my screen that works I’ll get the hang on it but right now I’m lost. Help me please

    • brad says:

      Hey Matt, I think I replied to you over the on the HA forums, but for anyone else reading here’s how I would do it:

      For each window/door, use a current state node, a switch, and a stoptimer. If open, start the stoptimer. If closed, cancel the stoptimer. All of those go to a call service node to turn the HVAC off. Now if any one timer successfully completes, it will shut off the HVAC.

      To turn back on, I would put all the windows/doors in a group in Home Assistant. Then monitor the state of that group in Node-Red, when all the doors are closed, turn the HVAC back on.

      Hope that helps, if you figure it out post your JSON 🙂

  10. Dave Capamagian says:

    Hi,
    I am new to both Home Assistant and Node-Red but from following your guide I have managed to install both, so firstly a massive thanks for taking the time to write this.
    I got up to trying out the node’s but have come a little unstuck.
    When asked to enter Home Assistant server details into the service node what do I put, where do I get my details from and how do I find the api key please.

    Everything else has gone well and is all installed but when I try to use the first two nodes it keeps telling me unable to connect in the debug window.

    Any help would be awesome.
    Cheers
    Dave

    • brad says:

      Hey Dave, if you edit any of the Home Assistant nodes, there is a setting for the Home Assistant server. Click the edit button and add the details for your server. You don’t need an API key, just the URL (ie https://your.url.com:8123) and your Home Assistant password.

  11. Telegram keyboard and callbacks in Home Assistant with Node-RED – Midwinter says:

    […] Node-RED running and configured with your Home Assistant installation. If you haven’t, check out this post on DIY Futurism for how to do that. Additionally, like my previous post on this matter, I’m going to assume that […]

  12. […] Node-RED running and configured with your Home Assistant installation. If you haven’t, check out this post on DIY Futurism for how to do that. Additionally, like my previous post on this matter, I’m going to assume that […]

  13. rod says:

    HELP!! I don’t get WHAT to inject into the service node to activate the light?? Do I need to tell it what it is looking for somehow?

    • brad says:

      If you fill out the call service node then it doesn’t matter what you inject to trigger it.

      You can leave the service call blank, but then you need to put the entity_id and other information in msg.payload.data

  14. Alex says:

    Quick questions, are all of Node-Red’s automation run on top of Node Red or it some how converts them to work for HA? If Node Red is down does that mean all automations stop working? Also how do you organize the automations in Node Red? Do you just put them all into a single flow tab or you create a tab for each automation? Is there like folder structures we can use?

    • brad says:

      The automations run in Node-Red yes. I have not had any stability problems with it at all in the last year of use.

      Node-Red’s interface is tabbed into palettes, I organize my flows on different tabs.

  15. Arnout says:

    Hello,

    I’m breaking my head on the ‘Still in Bathroom’ node…
    Why do you need it? If you are still there, then there is motion, so the timer will get reset anyway and the switch will not go off…?

    I have also difficulties to understand the template in there… Is that something of NR or from HA? Can you explain that a little more?

    Thanks for this pretty perfect tutorial for NR!!

Leave a Reply

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