SmartLife / Tuya devices like sockets, switches and bulbs can publish to MQTT and in some cases be controlled by it. To enable this the following is required.
Prerequisites
- Connect all your SmartLife devices to the SmartLife app.
- Install Node and NPM on your target host.
- Download MQTT Explorer or a similar tool to allow you to monitor MQ and confirm that your devices are being published.
Setup
Install the TinyTuya python module with pip install tinytuya
Set up a developer account at https://platform.tuya.com/ as explained in the TinyTuya readme file available on Github https://github.com/jasonacox/tinytuya
.
Device Discovery
Once your Tuya IOT account is set up and enabled you can use tinytuya to scan for devices and their security keys:python -m tinytuya scan
if everything is set up correctly, this will provide you with a list of devices, their local IP addresses, IDs and local keys.
Now download tuya-mqtt with git clone https://github.com/TheAgentK/tuya-mqtt.git
The project is unsupported but still works as of 2025. Install the app with npm install
but don’t try to run it yet.
Configuration
In the Tuya-mqtt folder, copy config.json.example
to config.json
and update it with your MQ server details.
In the same folder, create a new file devices.conf
with the following structure, adding a new block for each device.
Once you’ve created these two files you can try running the app with node tuya-mqtt.js
. If this gives you no errors, then everything is good and you should be able to see the data in MQTT Explorer now. Press Crtl-C to stop the process running once you’re happy its working.
[
{
name: 'name of device 1',
id: 'device 1 id',
key: 'device 1 key'
},
{
name: 'name of device 2',
id: 'device 2 id',
key: 'device 2 key'
}
]
Running as a SystemD Service
Create a file tuya-mqtt.service
following the pattern below, copy, then install and run it as shown here:
sudo cp tuya-mqtt.service /etc/systemd/system
sudo systemctl daemon-reload
sudo systemctl enable tuya-mqtt
sudo systemctl start tuya-mqtt
You can confirm its working with systemctl status tuya-mqtt
and by checking in MQTT Explorer.
Example service file.
NB: Update to reflect the location of node and the tuya-mqtt source, and to reflect the user and group under which it should run.
[Unit]
Description=tuya-mqtt
[Service]
ExecStart=/usr/bin/node /home/ohuser/src/tuya-mqtt/tuya-mqtt.js
Restart=always
User=ohuser
Group=ohuser
Environment=Path=/usr/bin
Environment=NODE_ENV=Production
WorkingDirectory=/home/ohuser/src/tuya-mqtt/
[Install]
WantedBy=multi-user.target