Powerful Standalone Home Automation System - Pi, Sonoff, ESP8266 and Node-Red





Introduction: Powerful Standalone Home Automation System - Pi, Sonoff, ESP8266 and Node-Red

About: Engineer, retired. Have always had an interest in electronics, often related to radio control. This evolved into a part time business that I still practice. I do some voluntary work in schools to encourag...

This guide should get you to first base where you can switch a light or an appliance on/off via any device that can connect to your local network, and with a great customisable web interface. The scope for extension/ adding features is vast, including sending emails, twitter messages, reading sensors (e.g. temperature). You can easily set rules e.g. – if the temperature is below 15C at 11pm turn on the electric blanket for 30 mins. The system uses the MQTT protocol with TCP that is considerably more reliable than UDP - that is surprisingly used by some commercial home automation devices. Once the hard work of setting up the system, described here, is done the fun can start. Node Red provides a fantastic user interface that is intuitive, quick and easy to set up, and provides amazing capability.

The outcome is a powerful home automation system that is fully under one’s own control and is entirely on a local network. Many of the systems shown on Instructables work with cloud based servers and hence are a security risk and can require subscription. These are easier to set up but have a higher security risk and potentially higher cost. I should add that one can set up a VPN (virtual private network) to the Pi to enable secure connection to the home network/system if access from outside would be useful (Google Pi OpenVPN for instructions).

This system requires reprogramming Sonoff devices with a great open source system called Sonoff-Tasmoda. This can be done using a special Arduino setup and a USB to serial converter. The Sonoff devices use the ESP8266 Wi-Fi chip so you can also make your own devices using ESP8266 modules or fit them to other items to deliver wireless connectivity.

The biggest task is setting up a Raspberry Pi as the hub. This runs the MQTT broker (or server) that manages the communication with the connected devices. The Pi also runs a system called Node-Red that can intercept messages and data and output commands – such as when to turn on the electric blanket. Node-Red also provides an interface to serve the webpage that you can log on to via smartphone/tablet/PC/Laptop, to see what is going on and enable manual switching of lights and so on.

My aim with this instructable is to give every step in enough detail that a novice can get the system working.

The steps are:

  • Load a Pi with Rasbian Stretch
  • Update and add features to Node-Red
  • Install the Mosquitto MQTT broker
  • Set up the Arduino environment for uploading the Sonoff-Tasmota firmware
  • Re-program a Sonoff switch
  • Set up the Node-Red interface
  • Test it all works.

Hardware you will need:

  • Raspberry Pi and power supply (and keyboard and monitor for initial setting up) (model B used here)
  • Sonoff Switch
  • USB Serial converter
  • Optional – ESP development board such as the NodeMCU.

My experience with the Raspberry Pi is that setting up anything seems to involve considerably more pain than one could ever imagine possible with stuff not working or getting stuck on some trivial step where the explanation could have been better. The forums do not always get advice that works! I found several routes that simply did not work for one reason or another. This cost 3 days! I then wiped everything and started afresh and completed in less than a (long) day. This however used the older Raspian Jessie. After this I found a way to use the current and recently released version (Stretch) and did it all again. This guide enables copy and paste of instructions into the Pi. So you should be done in a few hours. Anyway I hope this gets you faster up the learning curve and with much less pain. Believe me it is worth it for the end result.

Step 1: Load a Pi With Rasbian Stretch

This should be pretty straightforward. Start with an 8Gb or 16Gb SD card. Download the latest version from:


The main thread here assumes that the full version is loaded. However you can use the lite version to save space. If using Raspbian Stretch Lite proceed through this step and then go to Step 9 at the end.

Unzipping the downloaded file gives a folder with the .img file. 7Zip is recommended for Windows (and The Unarchiver Mac). The image has to be burned onto the SD card – but a special program has to be used as the file system is not compatible with Windows. The recommended software is called Etcher and can be downloaded from:


The instructions for Etcher are on their website and could hardly be simpler. Select the image and the drive and click Flash.

Now with our flashed SD card with can get the Pi running.

If you know the IP address your Pi uses or are happy to find it by logging on to your router then you can avoid the need for keyboard and monitor and use SSH immediately. Just add a blank file named SSH to the SD card, insert, connect to Ethernet and power up. Otherwise follow the guide below.

Connect the Pi to internet, screen, mouse and keyboard and connect to power. The first thing we will do is enable SSH so we can do the majority of the setup from the comfort on a PC. It can of course be done direct but it helps a great deal to be able to follow this guide on the same device as one driving the Pi and use copy and paste for most of the instructions. Also in my case my PC workstation is nice and comfortable but not big enough for a pi as well.

There is a guide for a good deal of the work on a YouTube video. This is where I started. You can run the video alongside following these instructions. However when I was running through the process again while writing this I found it easier to just follow the instructions here. There are some significant deviations. There are also some sections where I have advised following the video rather than listing the steps here. I would however recommend looking at it to get an understanding of MQTT, Node-Red and the broad setup process. The video is 38 mins long so make yourself comfortable. The video starts with showing what Node Red can do, and then covers the Pi install and setup, followed by upgrading Node Red and lastly installing Mosquitto. Key timings in case you want to go back to a section:

00:00 Introduction to Video

03:00 Node Red demonstration

14:14 Importing dashboard items into Node Red

21:05 Pi initial setup, including SSH

23:35 Installation of Node Red supporting code

27:00 Introduction to MQTT

29:12 Mosquitto (MQTT) installation (Note only works for Raspian Jessie)

33:00 Node Red examples

I will list the commands in bold italics (rather than using “”). This formatting is ignored when you copy and paste them into the Pi.

The introduction to Node Red will give you an idea of what the system can do and provide a picture of where we will end up.

The YouTube video is at: https://www.youtube.com/watch?v=WxUTYzxIDns

Follow the installation procedure from 21:05 to 23:35. Note the default login is user: pi and password: raspberry. Before restarting find the pi’s IP address. Either click the ‘up+down arrow’ icon in the desktop top bar or enter the following instruction in terminal/terminal window:

sudo ifconfig address

Then note the IP address after: inet addr: . It will be in the form 192.168.x.y

Note this address and restart (enter: sudo shutdown –r now)

The key at this point is that SSH has been enabled and one can set up a link to the Pi from a PC (Mac and Linux include SSH interfaces). A good program to do this is a free download called PuTTY that can also be used as a serial interface and can do Telnet. Putty is available from:


Download and install PuTTY.

Now, with the Pi restarted, start PuTTY and enter the IP address noted earlier. See example below:

Now click Open

After Login as: enter pi

Then enter your password.

Now follow the instructions here and optionally start the video from 23:35. You can in most cases copy and paste. Highlight the text and use ctrl+C to copy. Then right click in PuTTY to paste. Sometimes the text does not appear immediately so wait a few seconds. If you do enter twice use backspace to delete the second entry. Press enter to execute each instruction.

sudo raspi-update

I got a command not found and ignored this and the following shutdown/restart instruction:

sudo shutdown -r now

sudo apt-get -y update

sudo apt-get -y upgrade

This takes a while.....

sudo apt-get autoremove

sudo apt-get -y update

sudo shutdown -r now

At this point we have the Pi operating system loaded and updated, ready for the next step.

Step 2: Update and Add Features to Node-Red



We have been hit (1st Nov 17) by an update of Nodejs that is incompatible with npm. If node-v gives v9.0.0 then you have the incompatible version.

The procedure here has now been updated to give the latest working combination of node and npm. (8.9.0 and 5.5.1 respectively)

If node v9.0.0 loaded you can downgrade by:

sudo n 8.9.0

sudo shutdown -r now

Log on after restart and check versions by:

node -v

npm -v

If you want to be thorough now also do:

sudo n rm 9.0.0

This removes the 9.0.0 version data from the install folder.

Now go to Step 3


The PuTTY connection will have been lost by the restart at the end of the previous step. So close PuTTY and after waiting for the Pi to boot, Log on again as before.

sudo apt-get install npm


sudo npm i -g npm@2.x

Note this takes a while…..

npm -v

- should give 1.4.21

node -v

- should give 4.8.2

sudo npm install -g n

sudo n 8.9.0

sudo shutdown -r now

The PuTTY connection will be lost. So close PuTTY and after waiting for the Pi to boot, Log on again as before.

node -v

- should give v8.9.0

npm -v

- should give 5.5.1

These latter instructions show the installed versions and that our upgrades have been installed.

So at this point we have our pi loaded and updated and with the updates needed for Node Red. It is no harm to do a restart before the next stage.

sudo shutdown -r now

Step 3: Mosquitto MQTT Installation

If you have not already done so it is worth viewing the video introduction to MQTT on the video from 27:00.

This is where we need to take a different path. The procedure outlined in the video only works for the older Jessie version of Raspian. There is a discussion on the mosquitto site but no clear outcome shown and hence will stick to a simpler and safer route.

So log on using PuTTY and enter the following:

sudo apt-get update

sudo apt-get install mosquitto mosquitto-clients


sudo /etc/init.d/mosquitto stop

sudo /etc/init.d/mosquitto start

The latter two instruction stop and start mosquitto and show that our MQTT broker is working.

For a quick test open two more PuTTY sessions and log on to each.

You will know by now that MQTT works by the device the needs data subscribing to a 'topic'. The broker will send any data with the same 'topic'. Then the device wanting to send data/instruction publishes this to the broker using the same 'topic'.

So in one PuTTY session enter:

mosquitto_sub -d -t hello/world

This is an instruction to subscribe to the topic: hello/world.

In the other enter:

mosquitto_pub -d -t hello/world -m "Hello from Terminal window 2!"

This is a publish instruction with the same topic with a message. The text: “Hello from Terminal window 2!” should now appear in the other terminal.

Well done getting this far. We now have the Pi loaded and updated with updates needed for Node-Red and with the mosquito MQTT broker installed and tested. From now on life gets easier a bit more fun. Close the two PuTTY sessions used for the MQTT test.

Step 4: Setting Up the Node Red Interface

First we need to start Node Red. Enter the instruction:

node-red-pi --max-old-space-size=256

Wait for it to get running and you see the text ‘Started flows’.

Now open a browser, I use Chrome, and enter the pi IP address noted earlier followed by :1880 i.e. something like

You should now have the Node Red programming page in view as below:

You can now follow the Node Red examples starting at 33:00 or move straight on to a tiny bit of additional setup and loading the first flow that will show the link to MQTT and be ready to drive our switch.

The additional setup needed is to add the dashboard items that enable a web interface.

These are shown on the video at 14:14.

Follow the instructions to load node-red-dashboard.

Now we can have a little play and show MQTT working and being driven by Node-Red. This is not on the video but is key to this project. You can either follow my instructions and/or import the flows from the NRtest1.txt file attached.

First add an input inject node and an output mqtt node and link them together.

Double click the inject node (that initially is labelled timestamp). In the Payload section use the twiddly to change to string and enter: Hello from me. Leave topic blank as we can enter this in the MQTT node. Click Done

Now double click the MQTT node. Click the pencil icon to the right of the Server section. This opens a new dialog. Enter: localhost in the Server section. Click Add. Now, back in the Edit mqtt out node, enter our topic in the Topic section: hello/world . Set QoS to 1. Click Done. Now click Deploy. You should see a green blob and ‘connected’ below the mqtt node.

Now add two more nodes – input mqtt and output debug, and connect these together. Now double click the input mqtt node and enter hello/world in the topic section. The server should already show localhost:1883. Set QoS to 1. Click Done. Then click Deploy and click the debug tab on the right hand pane. Now click the grey square on the left of the ‘Hello from me’ inject node. This sends the text payload to the MQTT broker with the topic hello/world. The broker knows that the mqtt input node has subscribed to the same topic and so forwards the payload. The mqtt input node then sends this to the debug tab (RHS) and the text 'Hello from me' should appear.

This ticks another box as we have Node Red talking to our MQTT broker. Note that Node Red is just a client to the broker – like the Sonoff devices we will connect later. It however enables sophisticated automation and provides a great user interface.

Now we can make a couple of small changes and set up the flows for our Sonoff switch.

So delete the input inject node (click to highlight and press delete key). Now add a switch from the dashboard section and connect it to the mqtt output. Double click switch. Click the pencil on the right of Group. Enter into Name:Light. Then click the pencil to the right of Tab and enter into the Name section: Lounge . Click Add/Update and Add/Update again. Now, back in the Edit switch node, set the on and off Payloads. Use the twidlys to select string and enter ON for the On payload and OFF for the OFF payload. Click Done

Now go to each of the mqtt nodes and change the Topic to cmnd/sonoff/POWER . If copying and pasting, check you have not included a space at the end. This is a different topic and will not work with the Sonoff. Finding a stray space can take an hour or so – believe me! I also went to the dashboard>Theme and selected: Dark. Click Deploy and select the debug tab.

Now open a new browser session in a new window and size it like a mobile phone over the Node Red session. Enter the address : your Pi IP address:1880/ui/#/0 i.e. something like . You should see a screen with Lounge and Light and switch. Click the switch on and then off. The debug window should show the ON and OFF payloads. Now if you wish also log on via a mobile as well. Note that the switch position is synchronised. It does not matter that our Sonoff is not yet connected. When it is, by subscribing to the Topic, it will pick up the message/payload and act on it.

A last little step is to make Node Red start automatically after the Pi boots up.

Node Red has a guide at: https://nodered.org/docs/hardware/raspberrypi

However the files needed are already loaded so no installation is required.

To then enable Node-RED to run automatically at every boot and upon crashes enter (open PuTTY session):

sudo systemctl enable nodered.service

If you ever need to disable this enter:

sudo systemctl disable nodered.service

Now shutdown the pi with sudo shutdown now and remove power.

This now has our Pi locked and loaded ready for action. We have our PC/mobile phone connecting to Node Red and this talking to our MQTT server. This has been a long haul and worth a big pat on the back. Well done. I found the next, Arduino bit, a good deal easier!

Step 5: Setting Up the Arduino System for Reprogramming Sonoff Devices

All the info to do this is at the Sonoff-Tasmota GitHub. The only part I had difficulty with was the Topic text – but I have cunningly got you to enter this already!

Go to https://github.com/arendst/Sonoff-Tasmota

You will find the setup instructions on the Wiki tab in the Upload tools section: https://github.com/arendst/Sonoff-Tasmota/wiki/Arduino-IDE

Rather than go through every step I will just note key aspects that I felt were important or got stuck on.

The instructions are good but need careful attention to detail. I was tripped up by there being a need for an ESP8266 folder within a folder that was within another folder named ESP8266 and hence missed out two levels.

I followed the recommendation for an entirely separate Arduino setup. I created a new folder called ‘ArduinoSonoff’ that is separate to my existing Arduino folder. The setup is quite advanced and hence keeping it separate is a very good idea. If this is your first Arduino setup make sure you install it a second time in an ‘Arduino’ or other folder for any other Arduino work, including work on ESP8266s.

Download the latest Arduino IDE from https://www.arduino.cc/en/Main/Software . Unzip the file into your new folder.

The instructions include download the Sonoff-Tasmoda system from https://github.com/arendst/Sonoff-Tasmota main page via: Clone or Download>Download ZIP . Unzip the file into your new folder.

Follow the instructions carefully. I didn’t do the Optional: Prepare for OTA upload section. We will leave this for another day.

Now start the Arduino IDE (double click arduino.exe). Load the Sonoff-Tasmota sketch via File>Sketchbook>sonoff . There is no need to do any edits. All the settings are done via a serial connection after the firmware is loaded. These are stored in EEPROM. Hence the firmware can be updated and all the settings retained. This is pretty smart stuff. However you can avoid the need for a couple steps here by going to the user-config.h file and entering your wifi SSID and password and MQTT_HOST (replace ‘domus1’– second reference, with your Pi IP address). Later you may want to enter a MQTT user and password as well. Before downloading check the board settings under Tools. These will need a couple of changes to match the requirements set out in the Wiki. Now click compile (tick icon). It should compile OK. If it does not or if the required board settings are not available then go back and check each step of the setup.

Step 6: Reprogramming a Sonoff Switch

We are now ready to download. At this stage one can either go straight ahead and flash a Sonoff switch or one can first flash an ESP8266 module. I did the latter, partly because my switches had not yet arrived (just arrived as I type this!) but also as a precaution as the flashing of the Sonoff switch is a one way step as the original firmware is not publicly available as far as I am aware. I have a couple of NodeMCU boards. These are easy to connect, having their on-board USB to serial converter. However the nodemcu reset method does not work with this system. So leave the Tools>Reset Method set to “ck”. Do the normal manual flash setup by holding the Flash button (GPIO 0 to ground) while pressing and releasing Reset (Reset to ground). I am not sure if this has a timeout or maybe I didn’t hold GPIO 0 low for long enough but I did need several tries, including doing this while the Arduino IDE is compiling!

If you want to check the responses - the relay output is D6 on NodeMCU boards. On ESP12’s this is GPIO 12. The LED output is D7 (NodeMCU) or GPIO 13 (ESP12’s).

Sonoff switch.

WARNING: I have to say “do not connect to mains under any circumstances where the enclosure is open”. Note that the PCB (at least on the Sonoff Basic (in-line switch) only has ‘single isolation’ distances between the low voltage section and the mains. So one should treat any part of the Sonoff circuit as mains voltage. The Sonoff-Tasmota GitHub does show connection of a temperature and humidity sensor to a Sonoff S20. I would not do this because of isolation concerns. – so if you want to do this get an ESP12 or NodeMCU module and set this up separately with a proper double isolated or earthed power supply.

The Sonoff S20 plug-in switch is a good starting point as it does not need any mains wiring. This can be opened by removing one screw (under the security seal) and prising the case open. The photo below shows where the tags are. Squeezing the case at these points helps.

USB serial converter

My favourite converter is the FTDI version. However this does not have adequate capability to supply the Sonoff 3.3v needs. The FTDI specification says maximum 50ma. The next best alternative is one using the CP2102 chip. This however has a 100ma limit that is still not enough. Clearly many are using this converter direct but there are also reports of loading failing. I would limit the time it is connected as it will warm up under the load. Do this at your own risk. The ideal solution is to have a 3.3v regulator as well, e.g. an AMS1117 3.3. I made up a small PCB to enable this. See Programmer for Sonoff devices.

My sequence for programming is as follows:

Open the Arduino IDE.

Under Tools check the settings are as on the Wiki.

Make any edits required to user_config.h . I set the Wifi SSID and password and MQTT broker address and timezone/daylight savings details.

Click ‘verify’ to check it compiles OK.

Plug the USB serial converter (on its own) to the PC. Note the Port number.

Now disconnect the USB serial lead from the PC and connect it to the Sonoff switch. Check the ground and 3v3 connections are the right way around (ground is connected to the ground plane on the Sonoff PCB).

Hold the programmer so the contacts are secure while also pressing the button.

Now plug the USB lead into the PC, check the port number is correct (in Tools), then click download.

I continue to hold the button throughout the programming because I don’t want to disturb the connections.

When done you should see a screen as below:

The Sonoff needs a couple of bits of information to connect to our system: the local network wifi SSID and password, and the Pi IP address. There are several ways to do this, one of which is to modify the config.ino file as noted earlier. If you have a smartphone you can (after reassembling the switch) press the Sonoff button 4 times fairly quickly to put it into a web-server mode. The LED will flash. I had to try several times to get this to work. Then, on your smartphone look for the new Sonoff network and connect. A webpage will appear where you can set the data required. The Pi IP address goes into hostname. I also changed the second SSID and password to something long and basically unusable.

Alternatively it can be setup via the serial connection just after loading. Open the Arduino Serial Monitor (under Tools).

Commands to enter:


Password yourWiFiPassword

MqttHost 192.168.x.y (PI IP address)

You can also enter SSId1 and Password1 followed by something long and unusable to effectively disable this.

Now you can box up the Sonoff switch, open Node-Red and the Node-red dashboard and click the switch button and look at the debug tab that should now include the responses from the Sonoff. So we have another major step achieved - our first switch being driven from a PC/smartphone.

Up to now we have not mentioned security. There is the possibility to use encrypted communications. This is quite complex to set up and probably more appropriate where a cloud based broker is being used. There is also the option to add usernames and passwords for all the connected devices and disallowing anonymous users. This is pretty simple to set up. And so now to Security.

Step 7: Security

MQTT allows usernames and passwords for each client. This is easy to setup. It is probably easier to first rename each device and then set their usernames and passwords. This can be done via MQTT commands, and Node-Red is probably the easiest way to send these. First decide on a naming convention. One option is to base names on location and function. Then you will want to record the name(Topic) username and password along with the fallback topic. Note there is also a ‘reset option’ to reset the Sonoff settings to original download (see Wiki Usage>Button Functionality).

Power up the pi and after a few seconds open a browser to Node-Red (IP address:1880).

In Node-Red set up an inject node and link this to an mqtt output and set the mqtt server to localhost . Leave topic, user and password blank as we will set these in the inject node. Also setup an mqtt input node and connect this to a debug node so we can see the responses. Set the mqtt input note to localhost (should already be set) and enter +/+/+ for topic so it catches all traffic.

Enter the following sequence of settings in the inject node.

First check connectivity with

Topic: cmnd/sonoff/Status

Message: 6

click the ‘Inject once on start’. Deploy. We should see debug including 7 lines of data from stat/sonoff/STATUS6

Enter Topic: cmnd/sonoff/Topic andMessage: loungelight. Deploy. This changes the switch name from sonoff to loungelight

The debug tab should show the device restarting with its new name/topic

Topic: cmnd/loungelight/MqttUser

Message: loungelight

Click deploy. A debug should be seen from stat/loungelight/RESULT with {“MtqqUser”:”loungelight”}

Topic: cmnd/loungelight/MqttPassword

Message: loungelightPW (note be more imaginative than this!)

Clear the debug tab and Deploy.

A reply should be seen from stat/loungelight/RESULT with {"MqttPassword":"loungelightPW"}

Now enter NodeRed and NodeRedPW as username and password in the mqtt out node. This is via the Server pencil icon and security tab in the window that opens. This automatically gets copied over to the other MQTT nodes.

Check again with

Topic: cmnd/loungelight/Status and Message: 6 . Deploy.

And that the response is sent.

So at this point we have renamed our Sonof device so it will listen to cmnd/loungelight/…… topics and will log onto the MQTT sever with username loungelight and password loungelightPW. We have also set a username and password for Node-Red.

Next we have to tell the mosquito MQTT server to only accept clients with usernames and to list the usernames and passwords to be accepted.

The procedure is:

  • Stop mosquito
  • Create own config file
  • Edit config file
  • Create password file
  • Add users/passwords.

So log in with a new PuTTY session and run through the following commands:

sudo /etc/init.d/mosquitto stop

cd /etc/mosquitto/conf.d/

sudo nano mosquitto.conf This starts the editor.

Add lines:

allow_anonymous false

password_file /etc/mosquitto/conf.d/passwd

require_certificate false

Save and exit (Ctrl+X), Y, enter .

sudo touch passwd This creates a password file and the following instructions add names and passwords.

sudo mosquitto_passwd -b /etc/mosquitto/conf.d/passwd loungelight loungelightPW

sudo mosquitto_passwd -b /etc/mosquitto/conf.d/passwd NodeRed NodeRedPW

sudo /etc/init.d/mosquitto restart

Note when adding a new device you will have to either enter the user and password via the serial port and add these to the password file or temporarily change the mosquitto config file and comment out (add # at start of line) the lines "allow_anonymous false" and "password_file /etc/mosquitto/conf.d/passwd" and then reset these when the details have been sent to the device and added to the password file as above.

We have changed the sonoff name to loungelight and so update the mqtt output node (connected to the switch) to use the topic cmnd/loungelight/POWER .

Click Deploy and check the mqtt nodes show 'connected'.

Next try the switch button and look for the debug showing the Sonoff switch responding. You will notice that the device shows the change with a topic: stat/loungelight/POWER. So now change the input node that was set to cmnd/sonoff/POWER to stat/loungelight/POWER . We can use this cover a gap in our functionality. The system as initially set up will be synchronised with all logged on users but will not synchronise with switch changes made by pressing the button on the Sonoff switch. So now connect the output of the stat/loungelight/POWER mqtt input node to the switch input (LHS). Now double click the switch and untick the "if msg arrives on input, pass through to output". This brings up some new options - select 'switch icon shows state of the input'. Deploy. So now we have a good feedback loop. The dashboard switch position will always change when the Sonoff switch changes, regardless of where the change was initiated.

So we now have a secure, standalone home automation system up and running and ready for expansion to whatever you want it to do. In the next section I will cover some of my experimentation so far and challenges I plan to tackle.

Step 8: Initial Steps for Expansion

I have another Instructable Home Automation Sonoff-Tasmota Sensors LEDs Development Board that shows some of the further capabilities of the Sonoff-Tasmota firmware:

Temperature and Humidity measurement

Intruder detection (switch input)

IR remote (for TVs etc)

LED strings - both RGB and NeoPixel (individually addressable)

I2C sensors

For the above I am using a ESP12F and a custom PCB. A NodeMCU and breadboard could equally be used. This enables these additional functions without wiring into a Sonoff device, and hence is a much safer approach. With the temperature input I have been able to complete my electric blanket automation:

I have also been exploring Node-Red, including sending emails and making voice alerts. There is also the potential for the system to detect when you are in/out - by pinging your mobile phone IP address. Node-Red can also access weather and news - so one can add information as well as do automation.

There are a few tricks to be learned - but these become dead easy second time around.

Another avenue is adding a display to the pi to show the dashboard. This is 'work in progress' - or in other words I am not too happy. The display I got is difficult to rotate into portrait mode and the Chromium browser is painfully slow. An alternative would be to pick up an old tablet on ebay and use that. I may try with a Pi 2 and see if that delivers enough of an improvement (model B used for this development).

I hope this gets you started and has your imagination buzzing. The potential scope is huge. One could even modify the Sonoff code for other sensors if needed.

Overall I have been amazed at what this system can do. My original objective was to just drive a switch from a smartphone in a reliable manner via a standalone system. I had had visions of needing to manage servers and clients and writing html for a user interface. Where this ended up is way ahead of this, with greater security, excellent reliability, fantastic user interface, drag and drop programming and huge potential for expansion. And all this with much less effort.


Step 9: Addendum - Loading From Raspbian Stretch Lite

This option avoids the bloatware that comes with the full Raspbian Stretch version. Most of this will not be needed when using a Pi for home automation. However Node-Red does have to be installed.

Proceed as in Step 1 but using Raspbian Stretch Lite rather than Raspbian Stretch.

Instead of Step2 do the following:

sudo apt -y install npm

npm -v should return: 1.4.21 or later

sudo npm install -g n

sudo n 8.9.0

We can now use node packet manager to instal Node-Red:

sudo npm install node-red --global --unsafe-perm

This will give a couple of error messages because of an incorrect address. The system however does a ‘source compile’ to correct this problem. If you repeat the instruction above (not necessary) the errors do not occur.

We now have Node-Red and its supporting packages installed and can move on to Step 3, loading mosquitto.

Wireless Contest

Runner Up in the
Wireless Contest



  • Clocks Contest

    Clocks Contest
  • Planter Challenge

    Planter Challenge
  • Oil Contest

    Oil Contest

We have a be nice policy.
Please be positive and constructive.




Lovely instructable Mike, i too had several attempts to get various flavours of linux on my OrangePi along with all the dependancies and applications.. then I found DietPi , which, as its name suggests, is a light Debian-based Linux distribution and it comes with Mosquitto and Node-Red already to install via the DietPi installer. It makes for a compact and simple (ie error free) install.

3 replies

Look like an interesting option + the fit with other SBCs. I have periodically looked at the Pi alternatives and decided to stick with the Pi system as there is help around if one gets stuck. The Pi approach also gets more up-to-date software (but not sure what difference it makes). My system is now a fair bit more complicated and hence the hurdle to change is getting bigger all the time. Keep us posted with how you get on. Thank you. Mike

I notice that the OrangePi is not directly supported on the DietPi site anymore, instead they have downloads available for the NanoPi range. As an exercise I downloaded the image for the NanoPi Neo and tried it on a OrangePi Zero 256MB (H2 core) and it works fine. The OrangePi's are generally cheaper than the NanoPi's :-)

you probably are aware of the scargill 'script' for DietPi, I take it? if not it is worth checking out

running your system locally has many advantages. I am using openhab +mosquitto on a Pi, with a lot of ' satellites' such as the sonoffs, but the principle is the same

Hi Mike, Happy New Year.

I've come across your instructable by accident after trying (and failing) to follow the instructions given on many other websites to set up my home automation system. All my other attempts failed when I come across instructions that are out of date, links that don't work anymore etc so it was heartening to read your similar experiences caused your instructable.

I have installed all the software on my raspberry pi as you described, all fine until node-red version 8.9.0 and npm version 5.5.1 are installed when I get responses from the pi that they are incompatible and npm should be updated.

My question is should I ignore this and carry on following your instructable or attempt to update node-red and npm to the latest versions?



3 replies


Strange. I have loaded several times now and not seen this. Have these loaded OK and shown correct versions from node -v and npm -v?

My inclination would be to continue.

Are you starting from Stretch full or lite?

What version Node-Red have you got? (see version when Node-Red is started or from the top RHS icon when running). I have v0.17.5 on a system I loaded 6 days ago from lite and recall the full came with v0.17.4. Both worked fine with node and npm as above.


Hi Mike, Thanks for your very quick response on News Years Day.

After reading your reply I decided to reinstall the whole setup on my pi and I followed your instructions to the letter. It worked perfectly first time. This is further than I've ever got following one of these home automation tutorials by the way.

I'm at Step 5 now and hoping it goes as well. Thanks goodness for your instructable.



Mike I have found the flow thankyou
On the top row there is a time stamp linking to another node then on to a time node. The middle one says it’s a unknown moment. Can you tell me what node this is please as the flow won’t work

Thanks nigel

1 reply


To what extent have you followed the instructions (detailed in my other instructable)? I believe that if you run through these you will find the answer to your problem. If you get get stuck then please ask again. Might be best to post on the other instructable. Glad you are following this. Node-Red is a fantastic tool with breathtaking capabilities. The more you learn about it the more you will be able to do with it.


Hi Mike

I was talking about your node-red flows of the blanket sketch

very good write up thankyou

could you post your Node-red sketch please



1 reply

Hi, When I get to Step 2: update and add features to node red, the command node -v gives me a command not found error, and the command npm -v gives me no such file or directory error. Any suggestions?

Note I started with a unmodified install of stretch and copied and pasted your commands exactly.

5 replies

I have just added a method to install Node-Red onto Stretch Lite - prompted by your comment. I guess you will have solved this by now but thought I should let you know just in case you hadn't.

This looks like they did not install. Did you start from the full version of Stretch or Stretch Lite? Mike

I started with stretch lite, because I wanted as small a software footprint as possible.

Addendum: nodejs -v runs. Is there supposed to be a symbolic link from node to nodejs?

I understand that node and nodejs are the same. Have tried following the steps in Step 2, ignoring the responses to the first node -v and npm -v? If you can get npm loaded you can load node. Then you also have to load Node-Red. Mike