Use Tuya-Convert to Free Devices From the Cloud

This post may contain affiliate links. Please read my disclaimer for more info.

Have you seen cheap smart plugs on Amazon like these and wondering if you can use them with Home Assistant? Don’t want to rely on some cloud service to toggle your outlets? Well if the answer is yes to these questions you’re in luck, Tuya-Convert to the rescue!

In today’s article, I’m walking through a project called tuya-convert which allows you to replace the firmware on these plugs with whatever you want. I’m a big fan of esphome, so that’s what I’ll be using to build the custom firmware.

So what can we do once the device is freed from the firmware? Use it for automation of course! I’m using one of these plugs to control a lamp next to my desk that notifies when my wife comes home. The light toggles to give me a visual indicator that she opened the garage so I can go help her with groceries or my son. Skip to the end for the automation if you’re already familiar with tuya-convert.

esphome

Before we start changing out the firmware on the device, we need to decide what to put on it. I’m a big fan of the esphome project. It’s a command-line tool that allows you to customize an ESP8266 firmware image using a YAML file. Check out the installation page to install the tool.

ESPHome allows you to enable all sorts of hardware components by just writing some YAML configuration. I’ve used it before in my DIY Sprinkler Controller and ESP8266 Sound Machine to quickly develop firmware images that integrate nicely with Home Assistant. For plugs, we’ll use the basic GPIO switch component and some components to perform power monitoring.

Around the esphome project, there have popped up a community of people collaborating on device configurations for popular smart home devices. The esphome-configs.io website collects these configuration files for the community. This allows you to download a configuration file without having to learn all the components of esphome.

Writing a Configuration File

For the smart plugs I’m using, someone has already written a great esphome config file. On the back of those smart plugs you can see they have the model number AWP04L. Searching on the esphome-configs.io page there is already a config for the AWP04L.

If you’ve got a bunch of plugs you want to flash, it’s best to use a “split configuration”. That means putting all the common configuration into a common YAML file (I called mine awp04l.yaml) and then creating another YAML file for each plug you are going to convert (mine are just called plug1.yaml, plug2.yaml, etc..).

My plug specific YAML file just looks like:

Modify the first few lights for the plug you want to build firmware for. I kept the calibration data the same as the sample code as I’m not too concerned with the accuracy of power measurements right now. If you are, you can later attach a known load to the plug and tweak these values to match what you expect.

The last line of the file includes the “common” YAML file. Here you can see I include awp04l.yaml which contains all the common esphome configuration for these plugs that I got from esphome-configs.io.

Smart Plug

Building a Firmware Image

You’ve got esphome installed and your configuration files are written. Now it’s time to actually build the firmware image. This should be pretty straightforward. Just run:

You should see esphome download some libraries and build the firmware according to the YAML configuration files. If everything worked, you should see a firmware.bin file in the plug1/.pioenvs/plug1 directory. That means the firmware compiled successfully and that’s what we’ll load on the smart plug.

Tuya-Convert

To actually run Tuya-Convert you need access to a Linux computer that as a WiFi adapter that can be put into access point mode. The easiest route is to just use a Raspberry Pi 3, which is what I’m doing today.

Get Raspberry Pi Ready

Download the latest Raspbian Lite image from the Raspberry Pi website. Raspbian Lite is a lightweight image for the Raspberry Pi that only has the barebones packages needed. I copied it over to a new SD card using dd. You can also use a GUI tool like etcher if you prefer a GUI or are on Windows.

After writing the image to the SD card. I like to enable SSH out of the box so I don’t need to plug the Raspberry Pi into a monitor. You can do this by creating a file named ssh or ssh.txt to the boot partition of the disk. The Raspberry Pi will see this file during the bootup process and will automatically enable an SSH server.

Once your Raspberry Pi boots up, log in using SSH or just by hooking it up to a monitor. On a fresh Raspberry Pi, I like to do a full package update. You should also run sudo raspi-config and go under “Advanced Configuration” and expand the filesystem to fit the whole SD card. Finally, you’ll need the git package to download the latest version of tuya-convert, so install that too.

 Once that’s all done you’ll have a fresh Raspberry Pi set up and ready to start running Tuya-Convert.

Install Tuya-Convert

The Tuya-Convert project is hosted on GitHub and easily installed by cloning the repository. Just run the following command wherever you want to download Tuya-Convert.

Now, we need to install the rest of the dependencies needed for Tuya-Convert. Luckily, the project comes with a convenient script to install all our dependencies. Go into the newly created tuya-convert directory and run the setup script.

Let it run for a bit, installing everything necessary to run the application. Eventually, you’ll see the following message meaning you’re good to go.

Running Tuya-Convert

Now it’s time to start running Tuya-Convert and actually reflash your device! But first, we need to put the new firmware we want in the tuya-convert directory. Go back to the machine you compiled the esphome firmware on and copy it over to the Raspberry Pi with Tuya-Convert. You can use the scp command to copy the file over ssh. Or you can use a USB drive or something like that.

Place the firmware file in the files directory within the tuya-convert installation. You should see some other firmware files already there that are provided by Tuya-Convert.

Now it’s really time to get started with flashing your device. Start by running ./start_flash. This kicks off the tool and first prints a warning message that this might render the device unusable.

Type yes and press enter to continue. Depending on how you have your Raspberry Pi set up, it might detect that dnsmasq is running. If it is, no big deal just allow it to terminate dnsmasq by typing Y so it can continue.

You also might get a similar message about mosquitto. Type Y again to terminate the process.

Next, tuya-convert will start using port 8886. It should start up an access point called vtrust-flash. Connect to that network using your phone.

After your phone is connected, you need to put the plug into autoconfig/smartconfig/pairing mode. For the plug I’m using, you hold down the button for 6 seconds until the LED starts to blink fast. Once that happens go back to your terminal and press “enter”.

After a bit of output, it should back up the existing firmware from the device and prompt you to choose a new firmware.

Type the number next to the firmware you want to flash and press enter. In my case, you can see firmware.bin in the output above as option #2. Let tuya-convert flash the firmware. Once completed, you’ve got a cloud-free smart plug!

Home Assistant

Adding esphome devices to Home Assistant is done completely through the integration page through the UI.

Open up Home Assistant and choose “Configurations” on the left-hand side panel.

Configuration

After that, choose “Integrations” and click the big “Plus Sign” button in the bottom right of the screen.

Integrations

You’ll be greeted with a new screen to add an integration. Find “esphome” and type in the IP address of the device that was just flashed.

Add esphome

Click “Submit” and that should be it! The device is now added to your Home Assistant configuration. If you are using the configuration I used, there are light entities for the red and blue LEDs on the device. Sensor entities for power monitoring and a switch entity for toggling the plug itself.

If you are using the switch to control a lamp, consider using the Light Switch integration to tell Home Assistant that a light is controlled by the outlet. My plug is controlling a lamp to the left of my desk so I created a light entity out of it.

Automations

There’s no limit to what automations you can write for your new plug. For mine, I’m going to use it to alert myself whenever my wife gets home. I’m often working in the office on the other side of the house so I toggle the light on and off 6 times as a visual indicator that my wife is home and I can go greet her and my son coming home from work. If I know she’s coming from the grocery store, I can start walking to the car to help her if I’m not in the middle of something.

To achieve this, I’ll be using AppDaemon to write my automation. You could complete something like this using the built-in Home Assistant method to write automations. Or you could use something like NodeRed if you’re more comfortable with that. For me, I’m already handy with Python and prefer to write my automations that way. If you’re interested in AppDaemon, check out my article on Create Telegram Bot for Home Assistant where I go over the basics of setting up AppDaemon.

Below is the little automation I wrote:

Hopefully, the code is pretty straightforward. It waits until my garage door opens, then starts toggling the lamp. Every second it toggles it again for 6 seconds. You’ll want to choose an even number for the number of toggles so the light ends up in the same state as you started.

Conclusion

I hope you enjoyed my walkthrough of using Tuya-Convert to free your smart plugs from the cloud. It’s a great tool to have to take full control of cheap smart plugs and run esphome or other firwmare on them.

If you liked this article, you might some of these other ones from my blog:

If you found this tutorial helpful, please consider supporting the blog by joining my mailing list, following the blog on social media or directly through Buy Me a Coffee. Thanks for reading!