Step-by-Step guide for adding Tuya-bulbs, Wi-Fi smart LED (Smart Life app) to OH2 using tuya-mqtt.js by AgentK

Tags: #<Tag:0x00007fe7325bb5a0> #<Tag:0x00007fe7325bb4b0> #<Tag:0x00007fe7325bb3e8>

The procedure for bulbs and plugs is basically the same.

With a plug, the tuya topic starts with


So just replace “lightbulb” with “socket”.

Regarding metering, I guess this will not work right now, cause the script doesn’t support it yet. You may do a feature request at GitHub.


Really nice comprehensive guide.
@AgentK Nice work getting on the MQTT setup.
I’ve added links to your post and github on my post and github.

I’ve got a python script which automatically gets all the device ids and ips of the devices on your network. It removes the pain of having to manually find the id, and arp and then greping the mac. Feel free to copy or use it in your guide/github. It also gets the device state if njstuya is installed, but works fine as a standalone.

python -v

Hello everyone,

I am working on a video to show people how to use the tuya binding but, am having a little bit of trouble. I keep getting this:

(node:28317) UnhandledPromiseRejectionWarning: Error: Error communicating with device. Make sure nothing else is trying to control it or connected to it.
at Socket.client.setTimeout (/etc/openhab2/scripts/tuya-mqtt/node_modules/tuyapi/index.js:471:35)
at Object.onceWrapper (events.js:273:13)
at Socket.emit (events.js:182:13)
at Socket._onTimeout (net.js:453:8)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
(node:28317) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:28317) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I would really appreciate any help I can get. Thank you in advance!

I’ve only been using OpenHab for a few days but would really like to get my smart plugs connected to it.

Could somebody please give me some more info on how to install the TuyaAPI-MQTT Client on a windows 10 install of OpenHAB.


Sounds like you have probably are using the wrong ip or key. Getting the device status doesn’t use the key, so that’s probably your first port of call to figure out which.

There is some development going on to upload custom firmware like tasmota to tuya devices without physically opening it and bypassing tuya cloud. It’s still under heavy development.

1 Like

Does anybody know if the way to get the local key has changed?

I’ve followed the guide to connect to it with a rooted version of bluestacks and I’ve navigated to the file stated “preferences_global_keyeu1538398658843UYtHD.xml” but my file only contains this.

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map />
1 Like

Well I’ve managed to get it working. In doing so I think I’ve found an easier was to get the local key.

The below has worked for me on my android phone.

1. Install "Package Capture" from play store.
2. When you first open it it will ask you to install a certificate, this is needed for it to work.
3. In the top bar there are two "Play" symbols. The one with a 1 allows you to capture packets from certain apps.
4. Click on that and select "Smart Life" from the list of apps.
5. It will popup about setting up a vpn and will then start capturing packets.
6. Open Smart Life and on the screen showing "All Devices" pull down the screen to cause a refresh.
7. Go back to Packet Capture and hit the Stop button at the top.
8. You should now have an entry below showing x number of captures.Tap that to open it.
In mine I had to open the last packet in the list that was marked as SSL.
9. Scroll down through the first few blocks and you should see a large JSON block
This contains a lot of code but if you scroll through it you should see line like the ones below 

"devAttribute": 0
"name": Smart Socket 4"
"timezoneId": "Europe/London"

I’ve used the local keys i found doing this to setup two smart plugs and they are both working a treat.

In that large JSON code block it should list every device you have and all their local keys.


That’s what the instructions used to be and I thought they changed for a reason. But I’m going to steal your nice summary rather than referring people to potentially more complicated and less reliable methods.

I’m glad you found it helpful. I’ve since setup my 6 smart sockets after getting all the details from that one capture.

Thanks for your walk through.

Yes, monitoring the power is available. There’s a another topic called dps/. For my smart plugs, topic:

tuya/socket/1274756684f3ebb88ee7/XXXXXXX/ is the volts (e.g. 2372 - needs to be divided by 10)


tuya/socket/1274756684f3ebb88ee7/XXXXXXX/ is the watts (again, needs to be divided by 10)

I have set these up as two additional channels.

There’s also mA at dps/4, but I don’t have that configured.

For ref, I have these plugs:

Hi guys,

i have PIR Motion sensor added in the SmartLife app. The PIR Sensor it is NOT compatible with Alexa. So, is it possible to get status from the PIR Sensor, which is detected by the SmartLIfe app using this script, and then manipulate the status for other purposes? I’m planning to connect to the OpenHab 2.4, get Status and the create rule to control other devices.

Thanks in advance, Andreja

1 Like


Thanks a lot for this guide. It works great.

I have first used it on Tuya smart plugs that can be found on Amazon and then found cheap compatible devices (4.24€) on AliExpress. I have tested one and it works:

Thanks for sharing this topic. Via this wrapper I learned that my Tuya device (led dimmer switch) also has these dps values .

For some reason I can’t control my dimmer switch within OpenHAB using this tutorial, but I am able to controll it via njsTuya or even via homebridge-tuya (so my device id and key is fine). I’m also able to track OpenHAB mqtt output via a windows mqqt.fx client.

Now I’m trying to figure out why my dimmer switch isn’t reacting to my command topic. Does anyone here maybe has a suggestion on where to look for a fix?

Via njs tuya I get the switch on with

node njstuya.js -ip <deviceip> -id <deviceid> -key <devicekey> set "{ \"dps\": 1, \"set\": true }"

and off with

node njstuya.js -ip <deviceip> -id <deviceid> -key <devicekey> set "{ \"dps\": 1, \"set\": false }"

Suggestions on this are very welcome.

The switch I am using (EU model):

I’ve never used tuya-mqtt and don’t have much experience with mqtt or js, so I’m probably completely wrong here. But it looks like the dps values are being displayed but you can’t set them. All the functions are already there to do it but I couldn’t see how they are joined up. I had a quick look and added a few lines, which seems like it should do what you want. But I haven’t tested it or know that it will run. So try using my modified tuya-mqtt file. I just added a couple of function to the mqtt_client.on function, it’s all below.

mqtt_client.on('message', function (topic, message) {
                 if (exec == "command") {
            if (exec == "dps") {
                var status = topic[6];
            if (exec == "dpsJ") {
                var status = topic[6];
            if (exec == "color") {

Git hub link Link

So there already are functions to publish the data, I added a couple that receive messages and send the dps commands to your device. So I used a similar format as they are published. But it’s not exactly the same I don’t know if that’s bad or will brake things.
I added two topic or whatever, dps and dpsJ. The J means that the DPSCOMMAND goes through a JSON.parse before being sent on.
I guess if you send a message to to something like this it might work.

 type + "/" + tuyaID + "/" + tuyaKey + "/" + tuyaIP + "/" +dps +"/" +DPSCOMMAND"
So something like this 
socket/32kdjtuyaIDsdfdf/3dfxtuyaKey/"{ \"dps\": 1, \"set\": true }"

socket/32kdjtuyaIDsdfdf/3dfxtuyaKey/"{ \"dps\": 1, \"set\": true }"

Hey @RikM,
if you only want to turn your switches on or off, you can do this via the MQTT-Topic "tuya/socket/<deviceid>/<devicekey>/<deviceip>/command/" with the message “on” or “off”.

The changes of @unparagoned are possible, but not fully MQTT compliant. I will probably use a similar function in the future to set other DPS values via MQTT. But for your problem this should not be necessary at the moment.
Currently you can only switch Tuya devices on and off, and change the color of light bulbs. If this really doesn’t work, I’ll be happy to help you. Just write me an issue ticket on GitHub and I’ll see what doesn’t work.
Please make sure that it’s really my script and not a misconfiguration of Openhab and MQTT.

If you want more functions, you can simply create them as issue ticket at GitHub.

I hope the information will help you.

You are correct that people will be able to use it as is to turn most devices off and on without requiring any dps options and I’m not that familiar with your script so you might be doing something really fancy. But when you TuyaDevice.set(true) using tuyapi and don’t provide a dps setting it defaults to using "dps": 1, which works for most but not all devices. So unless they can specify the dps settings, they won’t be able to toggle the power on or off. I had someone who tried dps 1-10 without any success, their devices uses "dps": 101 to do anything. You were doing some really fancy stuff with dps for the color lights but I think I think most people want the dps options just for basic power toggling rather than some kind of fancy feature. I, myself didn’t realise some devices didn’t work with the default settings until a few people brought it up. You kind of have everything there in your code already, you even get the schema and show the various dps states, but just not those last few lines to set or change those states.

Thanks, @AgentK, in the end, I got it working with your scripts.

However, the dimmer I bought is basically for dimming lights so I kept on searching. Yesterday I flashed my Tuya dimmer with Tasmota and boom, full accessible MQTT control :smiley:

These gits helped me getting to speed in no time:

After flashing the ESP I typed the following in the online Tasmota console
tasmota/sonoff-mylight/cmnd/SetOption15 1

Created the following channels in OpenHAB PaperUI, first selected ‘On/Off switch’:
Command topic: tasmota/sonoff-mylight/cmnd/Power
Set On/Open value to ON and Off/Closed value to OFF

Created a new channel and selected ‘Percentage value’
Command topic: tasmota/sonoff-mylight/cmnd/Dimmer
Set Absolute minimum to 15 and Absolute maximum to 100

I don’t have the correct state topics, so I don’t see updates when I make changes physically. Will try to find these in the near future…

Thanks for writing up this guide. I’m not very experienced with unix but was able to follow these steps and get a Tuya bulb linked up with openhab.

Looks like there is a way to control the bulb colour, do you know if there is a way to command the dimming level of the bulb through openhab?

Edit: Actually just come across an issue when adding the line to rc.local as suggested in the steps above. This seems to block any inputs received from other MQTT sensors I have hooked up. Doesn’t seem to be an issue when I launch tuya-mqtt.js from the command line.

Is support for the power monitoring outlets still far off?