IOT Fishtank Part 2

In Part 1 of this post, we put together an IOT switch for my fish tank. It is powered by a Sonoff Module with a custom firmware.

V2 Open

In this post we are going to integrate the switch into an open source home automation system and do some automations.



I have decided to use Home Assistant for the automation in this case. This project has come a long way in the last few years and is now a very mature home automation suite. There is also lots of documentation online which is very helpful.

There are many options for installing Home Assistant ( on your Pi, but I chose to go with installing it on top of a generic Raspbian Stretch Lite image. The reason for this choice is that I found the suggested ResinOS install method did not play well with my webcam and I would like to use it for a future project.


To start with, you need to install a fresh version of Raspbian on your Pi. If you need some help take a look at my guide on Headless Pi Setup.

Once it is up and running, you will be able to ssh into the Pi.

SSH into Pi

Next up, you need to update your Pi. Run each line below separately.

You might want to change the hostname of your Pi too. The default is raspberrypi. I changed mine to hass. To do this run the following:

From the menu select Hostname.

Change hostname

Read the blurb about what characters are allowed, hit Enter, then change the hostname as desired.

New hostname

Once you are done, you will have to reboot the Pi. Next time you ssh, you will need to use the new hostname.

Home Assistant has some dependencies you will need to install. Run the following as one line.

To install Home Assistant, run the command below

Once everything is installed, you can browse to Home Assistant in a web browser on port 8123 using the hostname you configured earlier.


In this section we are going to install some add-ons and edit the configuration of Home Assistant.

To install add-ons, click on the menu hamburger if the menu is not already open, then the menu option. Menu

Click on the Add-ons icon on the top right.

Add-ons icon

From the Built-in Add-ons, we are going to install the following:

Click on ‘Samba share’, then click on the install button. This will take a few seconds.

Samba install

Once it is installed, you will be presented with the info screen. Here, I would suggest that you toggle Auto update on and then Start the service.

Samba Info

Do the same for the remaining packages. We can leave the configuration as default for first two, but Configurator and Mosquitto broker need changes to the options.

For Configurator set the password to something secure.

Be sure to click Save for the Options before starting the service.

Before starting the Mosquitto broker we need to edit the username and password for logins. Make this something secure and note it down as we will need it when setting up the Sonoff module.

Once you have saved the config changes, you may start the Mosquitto broker.

At this point, the Home Assistant is setup. I suggest you restart it by going to the menu and clicking the more icon (three vertical dots) in the top right. Then select Restart Home Assistant.


Before we configure Home Assistant, we are going to put new firmware on the Sonoff module.

We are going to use the fantastic Sonoff Tasmota firmware written by Theo Arends. There are quite a few steps to get this firmware building nicely in Arduino, so for detailed instructions please refer to the Arduino IDE page on the wiki.

The installation here is a little hairy, so on a PC I would go with Arduino Portable and follow the steps above. For MacOS, follow the steps below, but check the link above and make sure nothing has changed.

Firstly download and install Arduino. Then grab a zip of the latest release of Sonoff Tasmota.

Next we need to install ESP8266 in Arduino. Open up Arduino, then go to Arduino -> Preferences. Enter into Additional Board Manager URLs

Additional Board Manager

Next go to Tools -> Boards: ... -> Boards Manager.

Boards Manager

Wait for the boards list to load then scroll to the bottom and install esp8266(Mine is installed already in the image below. You should be able to click the install button.)


Next is the tricky part, but I have simplified it with terminal commands. Unzip the Sonoff Tasmota release you downloaded earlier to a known location and open a terminal window at the root of the unzipped folder. The following commands will copy the files to the correct places.

The below scripts assume that you are running a stock version of Arduino 1.8.5 with ESP8266 2.3 on MacOS. Any changes to versions may require modification. Also, if you have an existing Arduino project named sonoff it will get nuked. Do not use the below scripts blindly.

First off, the flash definition.

Next is the boards definition file.

Then we need to copy the libraries to the Arduino sketch folder.

Finally, copy the project files.

Restart Arduino and open up ~/Documents/Arduino/sonnoff/sonoff.ino. Then go to Tools -> Boards and select Generic ESP8266 Module from the list. You need to change the settings in the boards menu so that they exactly match the below image.

Board Settings

Click the Compile (tick) icon in the top left to be sure that everything works. Once you have the project building, we are going to need to edit the config. All config has been moved into a single file, user_config.h, for your convenience. There is a lot in this file, but we are only concerned about the sections below.

First section is Wifi setup. Here, you need to enter the wifi credentials the Sonoff will be connecting to. This should be the same network your Home Assistant Pi is on. You don’t need an optional STA_SSID2 or STA_PASS2 unless you have one and can leave those blank.

These strings are case sensitive and critical to things working so double check them.

Next up are the MQTT settings. Make sure that MQTT_USE is set to 1. Ignore the first bunch of host settings as we are not using TLS and skip to the host settings in the #else block.

Here, MQTT_HOST should be the same as the Home Assistant hostname set above. MQTT_USER and MQTT_PASS should be set to the credentials used in the Mosquitto broker config for the add-on.

For the USE_WEBSERVER section, set a strong WEB_PASSWORD. Then set the FRIENDLY_NAME to something that makes sense for the device. I have called mine fishtank.

Other settings you can consider changing are TIME_DSTTIME_STD and APP_TIMEZONE.

Comment out sections for USE_EMULATIONUSE_I2CUSE_IR_REMOTE and USE_WS2812. They just add code we don’t need.

With the config complete, use guide from Part 1 to flash the new firmware to your Sonoff. If you have succeeded, you should be able to browse to the IP address of the Sonnoff on your network and login to the admin page.

Admin page


The final step is configuring Home Assistant to use the recently flashed Sonoff module. Home Assistant is configured using .yamlfiles. Indentation and whitespace has meaning in yaml, so be careful.

We can edit these files using either Configurator or the Samba share we created earlier. We are going to use Configurator on port 3218, so go to http://hass.local:3218/ and login with admin and the password you used when setting up Configurator.

Open a file from the top left icon and select configuration.yaml.

Select configuration.yaml

You can now edit this file in the browser. Firstly, you will need to modify latitudelongitudeelevation and time_zone in the homeassistant section.

homeassistant section

Next, go to the http section, uncomment api_password if it is commented out and enter a strong password. This will be the password for getting into you Home Assistant dashboard in future. We don’t want lurkers in there flicking our light switches.

There should be no spaces in the password and you don’t need quotes around the string in yaml.

Http pass

For the fishtank light we are going to need a few things. A device and some automations. The device we need is an MQTT switchso at the bottom of configuration.yaml add the following:

Here, name is the friendly name we want to give this switch on the dashboard. state_topic and command_topic need to have the FRIENDLY_NAME from the Sonoff-Tasmota config. I set mine to fishtank.

Save the file in Configurator, go back to the dashboard and click Configuration -> General. Here you can check the config and Reload Core if it is valid.

Reload Core

If everything has worked, you should now see your switch in the Overview page.

Fishtank Switch

Toggling this switch should switch the fishtank on and off. Fantastic! This is real progress, but we need more than this. I want this switch to go on an hour after sunrise and two hours after sunset for example. For this we need automations.

I am going to use the sun component for triggering this automation. Here is the last bit of config to add to configuration.yaml.

This is pretty straight forward, but two things confused me initially. Services and entity_id.

Services are exposed by components in the system and allow you to call them. They are like methods and generally end up performing an action or causing an event to be fired. You can explore services by clicking the first icon in the Developer Tools.

Service Menu

For the fish tank light, we are using two services built into Home Assistant called turn_on and turn_off. These act upon an entity and do exactly what they say.

entity_id is just the name of an instance of a component in the system. You can find a list of them by going to the States menu from Developer tools.

States Menu


Well that is it. We have managed to setup a very capable home automation system using nothing more than a Raspberry Pi and a Sonoff Basic. The total cost of the project was less than $70. My fish tank now happily switches on and off without my forgetful intervention. I can now tweak the on / off times to make sure the tank gets the right amount of light.

Thanks for reading and stay tuned for further devices and automations added to the system.