Home Assistant and MQTT

After getting Home Assistant up and running, the next thing I wanted to do was to add MQTT so I could connect sensors. I decided to use mosquitto for MQTT.

First to install mosquitto server, client and python mosquitto packages.

Now that it’s installed, lets set it up. First lets create the directory where it will keep it’s persistence db files, not forgetting to change the directory owner to the mosquitto user.

Now lets update the configuration file. Below is what I’ve got in mine.

sudo nano /etc/mosquitto/mosquitto.conf

Now to add some usernames/passwords.

This is how you’ll create the passwd file with the first user.

After that, you add more users without the -c parameter, like this.

Now lets restart mosquitto.

After the service has restarted, verify that mosquitto has started

Alright, now it’s up and running, lets give it a test.  I tested mine connecting to my raspberry pi with two SSH sessions, one to test subscribing to messages and one to test sending messages. You’ll need to update the IP Address, port number and username/password to suit you.

Subscribe to messages with topic test_mqtt

Send a message to the topic test_mqtt

You should see the “Test Message” message arrive in your SSH session running the mosquitto subscribe.

Now lets add to Home Assistant

We now need to add some additional configuration to the Home Assistant configuration file for MQTT.

You would add similar to the following, but customise it to IP Address and port number you are running mosquitto on (from the mosquitto configuration file) and a valid username/password.

And then restart Home Assistant

This will get mosquitto up and running. You can now use MQTT with Home Assistant and send/receive message to MQTT sensors and clients.

Setting up Home Assistant on the Raspberry Pi

Lately I’ve been playing with Home Assistant (open source) on my Raspberry Pi for Home Automation.  I was surprised on the amount of support that is currently available and how flexible and easy to setup it is.  If you haven’t looked at Home Assistant yet, you can check it out here.

I’ve mainly been using a Raspberry Pi 3, but I have also tested Home Assistant on a Raspberry Pi 2 and it ran very well with no issues.

Below is some instructions for setting up Home Assistant.  These are my notes, but hopefully you might find them useful too.

Firstly, go to the Home Assistant site and download the image of Hassbian.  Grab the etcher software too for writing the image to the SD card.  After you’ve written the image to the SD card, put it in the Raspberry Pi and start it up.

NOTE: The Hassbian instructions say to wait about 5 minutes, mine took between 5-10 minutes.  During this time, Home Assistant may detect devices/sensors on the network it is connected.  It may automatically find some of your devices. e.g. it automatically found my Chromecast.

Setup the Raspberry Pi

Once Hassbian is up, SSH in using the pi user – remember the default password is raspberry.  Once in, first thing I did was setup the Raspberry Pi.  This is pretty much the same as you would do if it was running raspian.

Change the passwd for the pi user.

Then configure the Raspberry Pi settings.

I update the timezone, locale, wifi locale and expand the filesystem (so I have use of the full SD card).  Then reboot – raspi-config usually prompts you to.

After the reboot, update and upgrade the packages installed on the Raspberry Pi.

Note: This normally takes quite a while.

I usually do another reboot after the updates and upgrade just to make sure everything is running on the updated versions with no issues.

Configure Home Assistant

To configure the Home Assistant, you’ll need need to edit the Home Assistant configuration file.  In Hassbian, the Home Assistant configuration files are located in  /home/homeassistant/.homeassistant.

The Home Assistant site has resources on this.  You can check them out here.

Now lets update the Home Assistant configuration file.

It probably look something like this:

I updated latitude, longitude, elevation, unit_system and time_zone.

If you are having trouble determining the location for latitude, longitude, google maps can help you find them.  If you click on your location on the map, the location details are usually at the bottom of the browser window.  For more info, the google help page on this is here.

When updating this configuration file, it can be fussy sometimes. It’s a good idea to validate the configuration changes before restarting Home Assistant to use configuration. You can do this in GUI very easily. You can get to the GUI by going to the hostname or IP Address of you Raspberry Pi on port 8123. Something like


Once you’ve made changes to the configuration file, you’ll need to restart Home Assistant.

Security and Certificates

I would also recommend that you password protect your Home Assistant.  It’s good practice even if you aren’t exposing it directly to the Internet.  To do this, update the http: section of the configuration.yaml file and add api_password: PASSWORD

It should look like this:

You’ll need to restart Home Assistant for this to take effect.

Going to the Home Assistant web gui after this change will prompt for a password.


You should note however that this option still transmits the password insecurely over HTTP.  You’ll need to add certificates if you want it securely transfer your traffic.

IMPORTANT NOTE: If your Rapsberry Pi has a private IP Address (e.g. and get a certificate for a domain with a public IP address, you browser will give you a warning that the Raspberry PI is not trusted if you go to it from your private network.  If you go to it from the Internet, you will not get this message.

If you decide you want a certificate, Let’s Encrypt provide a fantastic service where you can get free certificates.  You need a domain for this as the Let’s encrypt certificates require that you can prove ownership of your domain.  The easiest way to prove this is to port forward ports 80 & 443 temporarily to your raspberry pi while you run the script which sets up, verifies and obtain a certificate.

To get the certificates, after you’ve put the port forwards in place, you run the following commands to get obtain certificate.  Make sure you update the email address and hostname to suit that of your raspberry pi.

Note: These scripts will install python if it’s not already installed.

This will take a while.  Once this has completed, it should provide you with information about the certificate you just obtained, similar to below:

Now, to use the certificate you just got in Home Assistant, we’ll need to edit the configuration.yaml file again.

Make sure you update the path of the key and certificate to match your domain.

Once you’ve made changes to the configuration file, you’ll need to restart Home Assistant.

As the certificates from Lets Encrypt expire in 90 days, it’s important to renew the certificate.

The previous script secures the certifcates for only the root user, so we’ll first need to update the permissions.

Once this has been done, add the auto-renewal setup into cron.

(select nano, its easiest)

This crontab will attempt to renew the certificate on a daily basis, but you could go weekly or monthly if you prefer.


If you have any issues with your Home Assistant, (maybe a typo in the configuration file) checking the home assistant log file can give you information on what is wrong.

That’s it.  Home Assistant is now configured.  The next step is to attach devices/sensors and add automation.

Upgrading Nodejs on Raspberry Pi 2

I read about using the n helper for node to perform an upgrade, however this did not work for me.  Instead, I added another source and upgraded with apt-get.

If you see another way which consistently works, please let me know.  This worked for me, I had version 0.10 and this upgraded me to 0.12.

NOTE: I ran this as root, as sudo did not work.

You may want to perform an upgrade of packages before you do this.

The process should look something like this.

Then use apt-get to perform the upgrade.

The upgrade should look similar to this.


As I use node for node_red, I restarted node_red after the upgrade.  I experienced no issues with node_red after the upgrade to 0.12.


UPDATE 29 April 2016: Upgrading to other newer versions of node.js works this way too.  I recently just upgraded to v6 this same way, just use a different url.  You can find the urls for different versions here.  Although make sure that you check dependencies for other programs which depend on it, like node_red.



Installing Emoncms on Raspberrry Pi 2

Emoncms can do some great input processing and visual dashboards, along with many other things. Below is the process I used to install it on a Raspberry Pi 2 using Raspian.

Install Dependencies

Install all the dependencies.

During this install, it will prompt you for further information. Install pecl dependencies (redis and swift mailer)

Add pecl modules to php5 config

Configure dependencies


Configure redis to run without logging or data persistence.

Comment out redis log file and all the redis saving.

Then restart the Redis service


Emoncms uses Apache to route requests, so modrewrite needs to be enabled.

Modify the Apache2 config to allow rewrite.

Change (line 7 and line 11), AllowOverride None to AllowOverride All. Comment out line # CustomLog ${APACHE_LOG_DIR}/access.log combined Comment the access log to other-vhosts (add #)

Now Reboot the Pi

Install Emoncms

For the install Emoncms, it will be installed from the git repository for Emoncms. First, lets setup the web directory and permissions.

Download Emoncms from the git repository. For this we’re using a cloning a specialised version of Emoncms which has been designed to write less to the SD Card.

Configure Emoncms


Lets setup the mysql database for Emoncms. If you would like to move the mysql database to another location (for easier backup,etc), now is the time to do it. For this, I’ve chosen to move the Mysql database to /home/pi/data/mysql. I first create the directory and then copy the mysql data accross.

Now update the mysql configuration file to use the new location.

change line datadir to /home/pi/data/mysql Create the Emoncms database.

This will prompt you for a password and the following SQL will create the database.

Now create a user and assign permissions that Emoncms will use to access the mysql databse. In the commands below, update username and password to suit your choice.

Data directory

Now we create some directories were the Emoncms’ feed engines ( phpfina and phptimeseries) will store data. I chose /home/pi/data/.

Emoncms Configuration settings

Lets setup the configuration file for Emoncms. We copy the default.settings.php to settings.php.

Now we update the settings to suit this installation of Emoncms.

Update the following things: Database Settings. Update these to the username and password you created previously when setting up Mysql.

If you chose a different location to store your data directory earlier, update these lines to suit your data directories.

Setup the feedwriter script as a service.  Copy the service script to /etc/init.d/, update permissions and enable for service.

Now go to a web browser and go to the emoncms website. Update the IP-Address below to your host.

Click Register and create yourself a new Admin user. After you have successfully logged in and do not want to create any other users for this Emoncms, it is best to disable the register functionality. To do this, we edit the settings again.

And change $allowusersregister to FALSE.

As you have now gone through the initial setup for Emoncms, you can also disable the dbtest.


The following log entries should be created and help with this.



Installation of Pywws on Raspberry Pi 2 for Weather Station

This is the process I followed to install pywws on a Raspberry Pi 2 on Raspian.  I have a 3080 compatible weather station connected to the Raspberry Pi 2 via USB.

Install Dependencies

As pywws is written in Python, it has a few dependencies which need to be installed on the Raspberry Pi 2.  I like to make sure that before this I update to the latest updates.

Now install the python, libusb and gnuplot.  Libusb as we need to access the weather station via USB and gnuplot in order for draw graphs with pywws.

There are other dependencies if you want to use other features or pywws (eg. twitter), but these will give you the basics to get pywws up and running.  Other dependencies can be found here.

Install Pywws and permissions

After all the required software is installed, you can install pywws.

After it has installed, plug in the internal weather station unit to the Raspberry Pi 2 via USB. Now you can test to ensure it can talk to the weather station.

This command should produce some output that looks like this.

If this does not work, further information is available at http://jim-easterbrook.github.io/pywws/doc/en/html/guides/getstarted.html which may help. You have to run pywws in python using sudo. It is much better to run it as the user Pi and the give the Pi user access to the USB port. First create a group to grant the access and add the Pi user to the group

You need to identify the idVendor and idProduct for your WeatherStation that is plugged into the USB port. Look into the kernal log file.

Look for idVendor and idProduct, mine were idVendor=1941 and idProduct=8021. My kern.log look like this.

To modify the USB, create a rule that sets the usb port to the group weather

Copy and paste the following into this file. Make sure you update the idVendor and idProduct to suit your USB.

After you’ve saved and exit, reboot the Pi. Once the Pi has started again, test connecting to the weatherstation as user Pi, without sudo.

You should get the same results as you did above when you tested using the command with sudo. As we’ll be running this as user Pi, ensure that all the weather data is owned by user Pi.

Configuration of Pywws

I wanted readings every 5 minutes from the weather station and as pywws can easily read and log the weather station data every 5 minutes (recommended setting is 5min too), this was ideal. So, I set the weather station to log every 5 minutes.

It’s worth noting that the memory in the internal unit is not very large and although it will buffer some data, it cannot hold very much weather data. Now to setup where to log the data.  I chose /home/pi/data/weather, but it could be anywhere.  If a different directory is used, update the url through the rest of this page to the select directory.

All the weather station’s logged data should go into this directory. As per the getting started doco above for pywws, we now need to setup the timezone and weather station type. We will need to run the following command and it will create the configuration files the first time it is run.

The configuration files are found in the data directory /home/pi/data/weather/data. I adjusted the following in the weather.ini configuration file.

There is a full page in the doco about the weather.ini configuration settings here. I also update the configuration paths in the weather.ini file.

My full weather.ini looks like this.

Now create these directories and copy the pywws examples into them.

Then we run the command again.

This will probably need to run for a while to download the data. It should have some output similar to this.

So now that the weather station data can be downloaded, I want to setup a service that will run to download the weather data. There are two options, an hourly update or a live logger. I opted for the live logger as I wanted data updated as close to every 5min as possible. Pywws comes with a livelogger than can run as a daemon.  It can be run like this.

While this works well, it will not start automatically. A service init script is much easier to manage for me. Create a new init.d file by running the following.

Now copy and paste the following into this file. Update the DAEMON, DATADIR and LOGFILE to suit your installation.

Once the init script has been saved, it needs to be made executable and added to rc.d so it will start and stop at startup and shutdown.

You can also start/stop pywws as a service.



Weather Station

I recently purchased a weather station from ebay, model WS-3081.  The weather station has an internal and external unit and transmits via wireless between them – 433Mhz.  The internal unit runs on batteries and although unverified, it appears like it will also run off power supplied via USB.  The external unit has rechargeable batteries and a solar panel to recharge the batteries.

WH3080 Weather StationThe weather station can measure wind speed, wind direction, rainfall, temperature, humidity, solar UV and lux from the external unit.  The internal unit can measure temperature, humidity and pressure.

This is great, but I want to a way to get that information into the computer so that I can more interesting things with it.  The weather station comes with Windows software, which will let you download the data over USB to the internal unit.  I had a look at this and although it works, it doesn’t give me the flexibility I want to download the data quickly, easily and export in an automated way.

Further posts coming on the weather station as I work on getting it automated.

Raspberry Pi 2 Node-Red setup

In order to setup node-red, node.js is required.  Node.js runs on javascript and is one of the building blocks of node-red.  The install processes for Raspberry Pi and Raspberry Pi 2 are different and have different instructions.  Below are instructions for Raspberry Pi 2 and I have tested this process on Raspian wheezy 2015-20-16.  These are my own notes for my own purposes, but hopefully somebody else will find them useful too.

Before running these, update Raspian first.


To install node-red for the Raspberry Pi 2, the node-red guys have made it much easier.

Run the following command. This will update the raspian package cache and the source list.

Now to install all the packages.

Verify that node and npm have installed correctly. Run the following.

You should get results similar to the following.

Once these are installed, run the following command to install node-red.

This takes quite some time.

I got a few warnings and the build of the node-icu-charset-detector node failed completely, however node-red still built successfully.

Now to test node-red. Run the following to start node-red from the command line.

Node-Red should start and should look something like this.

You should get the node-red web interface by going to the following page in your web browser.

Now to install an initialisation script so that node-red can run as a service and start/stop automatically on startup/shutdown.  Create a new init.d file by running the following.

Then copy and paste the script below into this file and save.  This script will run node-red as the user pi.  It’s very simliiar to the Adafruit (link below) except updated for the new installs for Raspberry Pi 2.

Once the init script has been saved, it needs to be made executable and added to rc.d so it will start and stop at startup and shutdown.

You can also start/stop/restart the node-red service using this script.

node-red addons

There are many addons for node-red.  Some of my favorites are ping, wol, emoncms, prowl, mysql and pushbullet – but there is many others. To install them, use npm from the node-red directory.  The following will install my favorite addons.

There are many other addons and you can find other packages at https://www.npmjs.com/


I found that the install didn’t always successfully compile all addons. If there is an addon you expect to see in the node-red GUI that is not there, you can enable more verbose logging.  It should tell you if it is getting any errors or if it cannot load a specific module.  The below will run node-red manually from the command line.  Ctrl-C to break out.

Useful node-red sites & references

Adafruit has a very nice guide for setting up node-red on a Raspberry Pi (version1). Very useful.

Has many npm packages which can be installed.

The Node-Red site.

Interesting demo. Twitter, Mongo REST API and sentiment Analytics in 3 minute demo.

The Git Hub repository for Node-Red.