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

Hi GadgetAngel,
Many thanks for creating the above step by step pictorial. You and Holgi are awesome. I’ve learned so much from your posts.

Your welcome. I am sharing what I know like everyone else in this community does. I, like you, am still learning. I want to thank everyone else in this community for sharing. Also, you should know, I have only been doing this for a month and have learned the same way you have…by asking questions, reading the posts on the subject

THANK YOU CONTRIBUTORS for all your knowledge and time that you take to help us newbies!!!!

:blush:

1 Like

Hi Fraser,
Just want to say thank you. The rooted Bluestacks method in post #1 did not work for me either. I used your method in post #31 and was able to find the “localKey” for my device. Great job!

I finally got OH2.4 to control my smart plug. However, OH can only switch 1 of the 2 outlets on the plug and has zero control on the other. Using the Smart Life app, I could switch the outlets individually or both at the same time.

Any idea on how to control the 2nd outlet?

This is what I have
https://www.amazon.com/SUPERNIGHT-Upgraded-Anywhere-Required-Configuration/dp/B07K1QSFFJ
Thank you.

have you run tuya-cli to query which /dps/ topic that device uses? If you found your

tuya/<"socket" or "lightbulb">/<Virtual-id>/<localkey>/<ip-address>/command
tuya/<"socket" or "lightbulb">/<Virtual-id>/<localkey>/<ip-address>/state

with the anyproxy method then you had to install tuya-cli

This is the method of finding the keys, I was referring to:
https://github.com/codetheweb/tuyapi/blob/master/docs/SETUP.md

“oh_newbie”, I believed you used a different method. So here are the commands to install tuya-cli on a windows 10 machine (for linux use the right directory):

open a window Admin powershell at c:/openhab2/userdata/etc/scripts
>git clone git@github.com:TuyaAPI/cli
>cd cli
>npm install -g --save

Note: on linux or windows you want to install Tuya-cli GLOBALLY (-g flag on npm install line)

Here is the link to Tuya-cli setup page: https://github.com/TuyaAPI/cli

Run this tuya-cli command at any command prompt to see if it installed correctly (

Important
If tuya-cli throws an error under the following condition (#1 - somethimes, #2 always):

  1. try shutting down you OpenHAB2 server before using the tuya-cli command
  2. Also, BE-AWARE that while you are using the tuya-cli command DON’T have the Smart Life App (or which ever Tuya Smart App you use) OPEN while you are running the tuya-cli command on your server (tuya-cli will definitely through an unhanded error). If you must you the APP, access it before running the tuya-cli then make sure you exit the APP (don’t leave it running in the background on your phone)
tuya-cli help

or to find info on your plug:
tuya-cli get --id <Virtual-id> --key <localkey> --<ip-address> --all

example:
tuya-cli get --id 67833830cc50e3d4c9cf --key 559b607b2f0bac09 --ip 192.168.0.37 --all

tuya-cli produced the following output:
{ devId: '67833830cc50e3d4c9cf',
  dps: { '1': true, '7': true, '11': 0, '101': 0 } }

From that output I see I have two plugs on my unit that can be controlled.  The default, dps 1, can be controlled by using the MQTT state and command Topic below:

tuya/socket/67833830cc50e3d4c9cf/559b607b2f0bac09/192.168.0.37/state
tuya/socket/67833830cc50e3d4c9cf/559b607b2f0bac09/192.168.0.37/command, on="1", off="0"

I am presently testing this my self (I will let you know if this works) but I believe the second power port in the example can be controlled by the following MQTT state and command Topics:

tuya/socket/67833830cc50e3d4c9cf/559b607b2f0bac09/192.168.0.37/state
tuya/socket/67833830cc50e3d4c9cf/559b607b2f0bac09/192.168.0.37/dps/7, on="true", off="false"

I know this works:
tuya-cli set --id 67833830cc50e3d4c9cf --key 559b607b2f0bac09 --ip 192.168.0.37 --dps 1 --set false

result: the power turns off on dps 1 (the main socket)

tuya-cli set --id 67833830cc50e3d4c9cf --key 559b607b2f0bac09 --ip 192.168.0.37 --dps 7 --set false

result the power on the USB port turns off

This is my tuya smart plug:
https://www.amazon.com/Compatible-Function-Required-Allliances-Overcharging/dp/B07G78YSQS

So, now I just need to figure out how to translate the tuya-cli command into the MQTT topic. I am in the process of testing this out now, but I am in the middle of reinstalling Openhab2 first. I will let you know if I guessed wrong

1 Like

Thanks for the tuya-cli suggestion. Will try to install it later and let you know how it goes.
I used Packet Capture App (see post #31) on my cell phone to find the key which (as you mentioned in earlier post) changed when I reconfigured the plug’s IP address.

Hi GadgetAngel
Success!!! dps 1 and 2 control outlets 1 and 2, respectively. I was able to turn them ON/OFF using the cli set command. Here is a complete list of my dps

dps:
{ ‘1’: true,
‘2’: true,
‘9’: 0,
‘10’: 0,
‘18’: 0,
‘19’: 0,
‘20’: 1205,
‘21’: 1,
‘22’: 591,
‘23’: 24902,
‘24’: 14371,
‘25’: 1450 }

9-25 look like a bunch of status which I have yet known what they are. Someone(s) in earlier post(s) mentioned that either dps 9 or 10 being energy related parameters. I need to go back and study those posts.

Please let me know when you succeed translating the cli commands into MQTT topics. However, based on post #39 by @AgentK, it sounds like control via DPS has not yet implemented.

Again, a big THANK YOU. You are my Angel.

Well I am not having much luck, I hope the guy that wrote the tuya-mqtt.exe is watching this thread. I had success with turning off the USB ports on my plug via dps/7 but I had to use the tuya-cli command to do it.

Here is the problem, with tuya-cli, if I have a MQTT broker running on my machine (talks out port 1883) and I have a tuya-mqtt.exe which basically takes the openHAB2 topic and writes them to the plug (the program converts the information and sends the appropriate command) the tuya-cli command will error out. The minute I shut down the MQTT broker and tuya-mqtt.exe, tuya-cli command will work. On top of it all,for me the tuya-cli is not totally reliable, i have had it error out when it should not. The other thing is the tuya-cli talks over TCP port 6668. So I have no idea why the command line stops working when Mosquitto Broker and tuya-mqtt.exe use port 1883. So I am trying to see if I can change the code to tuya-mqtt.exe to set dps values.

Apparently, it only reads the values! I could be wrong I hope, TheAgent , is reading this and can set me straight. I just looked at the code and it appears he does parse the dps string using JASON.parse() so I do not understand why I can not get it to work… … It might be because I have not tried it inside openHAB2 yet. I have been trying to set the dps values from the command line or from a MQTT Client, like MQTT.fx, and having Mosquitto act as a Broker. I am missing something. I know to send the string like this:

tuya/lightbulb/xxxxxxxxxxxxxxx/yyyyyyyyyyyyyy/XXX.XXX.XXX.XXX/dps", on="{ “dps”: 1, “set”: true }", off="{ “dps”: 1, “set”: false }", i even tried it with “socket”.

was trying to go through tuya-mqtt code to figure out what he did. He sets the color for lightbulbs but I do not see where he is setting dps values.

NOTE
If your device has a true or false setting in dps=1 then the MQTT method will work. But if you devices’ 1st index (dps=1) is not a boolean value than this MQTT method will not work.

After much thought, I think I remember that even thou tuya-mqtt talks on a different port than tuya-cli, the tuya devices can only respond to ONE port. So, basically which every method you choose you will have to use it for the whole device. So, I have a plug that has a main power outlet and a USB outlet. I can not use MQTT for the mains and use the command line to control the usb port, because which ever port I use first the device will only listen on that port. So using two different methods to talk to one device will NOT work. It’s a trade off, give up the ability to change color, dimness on the bulb, so that i can control turning it on and off; give up control over the usb port so I can control the main power on the plug. If I do this I can use the MQTT method. The MQTT method, for me, is easier than doing everything with the command line interface.

I hope this helps

if you look at this URL:
https://github.com/clach04/python-tuya/wiki

You will see that the dps numbers mean different things on different devices. In fact, that have not created a standard for them yet. So please do not assume dps 5 and dps 6 do power and amps…I might be different for your device.

one way you can tell is to see which dps values are Boolean, which are strings, and which are numbers. At least that reduces the possibilities

@GadgetAngel
Have you looked at https://community.openhab.org/t/tuya-switch-control-jinvoo-smart-life/38015/113 ?
It looks like the author @unparagoned does allow some dps control but I’m not 100% sure. See his/her post #113.

The project is here https://github.com/unparagoned/njsTuya
This might be something we could try?

have you used the code yet? I tried and i can not get it to run

@GadgetAngel Please check your Inbox. I PMed you.

I will look again, now that I have finished reinstalling openhab2 build 1561 to only find that the MQTT binding is really a mess!!

OK, I actually attempted add dps functionality to the tuya-mqtt, but I don’t use mqtt, and didn’t test if it works, but you should be able to see my commit and it shouldn’t be that hard to fix any issues or bugs.
Read my post about it, I don’t know if things have changed since

But you can try my branch, it probably won’t work for the reasons AgentK mentioned but it should be fairly trivial to fix them if you know how mqtt works. AgentK has everything already there except for a few lines joining it all together.

I’m pretty busy atm so if you want to get tuya-mqtt working with dps setting, just tell AgentK to merge and fix my branch.

I don’t want to hijack this thread but you did mention my scripts.

My njstuya script has a js script, and example, rules, items to get it working with your system. It’s more of a manual process and no-where as good as a properly working mqtt setup. But I give you pretty much complete control to send the exact dps setting you want. It works very similar to tuya-cli, but I have some rules and items which means it’s simpler to integrate into openhab. It’s up to you what you do, they both are based on tuyapi, which I’ve also started making contributions to.

The new things I was working on is cloud based control, so you don’t have to perform a mitma, all you need to do is register the devices like normal with your smartlife app, and use those credentials to control your device. I’ve added those features to njsTuya, so it can either work locally only using tuyapi, or via the cloud using GitHub - unparagoned/cloudtuya: Tuya control over the cloud Now cloudtuya doesn’t let you directly set the dps value, and the api is a bit strange. It’s also may be a bit buggy and there are various options and settings I haven’t tested.

1 Like

This is probably due to the fact the device can only have one connection. So if MQTT has a persistent connection to the device, tuyapi won’t work. It’s like if you have the smartlife app open it won’t work.

If you have any reliability issues, make sure you have the lastest version of tuya-cli and tuyapi, then post an issue on github with details and we’ll have a look to fix it for you. Append command with DEBUG=* to get the all the debug data.

See my post above and branch where I effectively add that functionality to tuya-mqtt, it will need some tweaking to work properly,
unparagoned/tuya-mqtt

Your analysis is pretty much exactly what I thought. I’ve never used this tuya-mqtt but I did look through the code, and some of the code is a copy and paste from my project, which was flattering. I guess technically I’m kind of a contributor to this project :stuck_out_tongue: I’m not an expert but it looks like AgentK does use dps for setting color, in a very fancy way, but there is no way for you to set the dps. That’s why I made a branch showing how to add that feature. I may be wrong but it does look like it’s a required feature or if it’s there it’s not clear how to use it.

See my post above about how I just have items and rules to integrate the script nicely into OH. It doesn’t have scratch on this awesome mqtt implementation but if you need dps settings and options it’s not bad.

I will check it out. But at the moment my HUSBZB-1 Zigbee port is not getting recognized and I need my zigbee motion sensors back… I will get back to this so, I have not given up on getting the MQTT working with the dps values.

Update: I found your fork. I will down load it after I wake up tomorrow… I finally got all my zigbee stuff back, so next it’s seeing if I can figure out JAVA SCRIPT. I am a VB programmer so the syntax is different and I need to learn more JAVA Script. Can someone recommend a good reference book to read on how to do asynchronous programming with JAVA Script? I need books to learn.

Any advice on a good book would be appreciated. My background is instrumentation and system programming. I also enjoy compiler theory so I know how to program I just need to get familiar with Java object oriented programming.

Good night, I am going to bed for the next 10 hours!!:sleeping::smirk:

Here is what I am talking about, I ran these two commands back to back. I do not understand why it states “undefined” and then I run it again and it does what it should had done the first time

PS C:\openhab2\userdata\etc\scripts> node  c:/openhab2/userdata/etc/scripts/node_modules/njstuya/njstuya.js -ip 192.168.0.30 -id 5846055184f3eb20bfaf -key afb9b013346054e6 -set '{ \"dps\": 2, \"set\": \"white\" }' DEBUG=*
(node:12400) UnhandledPromiseRejectionWarning: undefined
(node:12400) 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:12400) [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.
PS C:\openhab2\userdata\etc\scripts> node  c:/openhab2/userdata/etc/scripts/node_modules/njstuya/njstuya.js -ip 192.168.0.30 -id 5846055184f3eb20bfaf -key afb9b013346054e6 -set '{ \"dps\": 2, \"set\": \"white\" }' DEBUG=*
OFF
PS C:\openhab2\userdata\etc\scripts>

I tried stepping thru your code but you are more familiar with the code then me, since their is alot of timing stuff going on. It take me to long to figure out what variables I need to look at.

But if you could figure out why the above happens, I would appreciate it.

I am leaning toward using your code but I have to know if it executed correctly. I hate the idea of having to send a command twice just to make sure the command goes thru to the bulb

BTW it reported that the bulb is OFF when it was ON

I don’t want to hijack this thread. So keep this thread about the mqtt script by AgentK. If you want to talk about njsTuya please use the other thread Tuya Switch Control (Jinvoo, Smart Life)

OK, sorry about that

TheAgent/tuya-mqtt

I have submitted a pull request to TheAgent/tuya-mqtt.

I have added the ability for users to set multiple dps in the MQTT topic.

The new MQTT topic is dpsJ

so to use the topic, the JSON string is different than what you are use to. The format is as follows:

{ “dps as index”: dpsvalue, “dps as index”: dpsvalue }

Here are examples of the JSON string:

JSON string for the topic dpsJ

{ "1": false }
{ "1": true }
{ "2": "scene_1" }
{ "2": "scene_2" }
{ "2": "scene_3" }
{ "2": "scene_4" }
{ "2": "scene", "6": "c479000025ffc3" )
{ "2": "scene", "6": "fffcf7002607ff" }
{ "2": "scene", "6": "d438000010ffd3" }
{ "2": "scene", "6": "3a59bd00e2afbc" }
{ "2": "colour", "5": "ff0033015cffff" }
{ "2": "colour", "5": "ffc400002effff" }
{ "2": "colour", "5": "000dff00edffff" }
{ "2": "colour", "5": "00ff040079ffff" }
{ "2": "white", "3": 255, "4": 255 }
{ "2": "white", "3": 138, "4": 255 }
{ "2": "white", "3": 27, "4": 255 }

NOTE:
this JSON string can be placed in the ON=" on string " or OFF=" off string" in the Generic MQTT Thing or this JSON string my be placed as the next topic behind the dpsJ topic. Here is what I mean.

command Topic:

tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/dpsJ/{ "2": "white", "3": 27, "4": 255 }

or 

tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/dpsJ

and     on="{ '2': 'white', '3': 27, '4': 255 }" is set 

A side note:

I noticed, while testing the code, that the software on initial start up gets an “Error: connection not made to device”. I believe it is a timing problem. If I submit the command again (and this includes the command topic also) the ERROR goes away and tuya-mqtt.exe can now talk to the device. So, if any of you, have seen error messages in your log , I would not worry to much. You will notice it only occurs when the system reboots and it will only occurr on the very first communication between tuya-mqtt.exe and a Tuya Plug/Bulb. On the second communication the problem goes away. I am sorry I could not figure out how to fix it because the error originates in the tuyapi code not in the tuya-mqtt.exe code.

TheAgent/tuya-mqtt, if you have any question please contact me.

thanks for considering my addition to your code :sunglasses:

1 Like