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

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

@AgentK Hi, I hope you are still maintaining the MQTT code tuya-mqtt.js

I would like for you to consider my pull request. I have fixed some issues with tuya-mqtt.exe and even made it so the unhandled promise error does not get sent to the openhab2 log file. It will print out when set DEBUG=* is used when calling node to execute the code manually.

If you decide not to add it, that’s OK too. I am just trying to help out.

Here is what the update allows:

This new update allows the user to set multiple dps values via MQTT messages or topics

State Topic

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

where <tuyaAPI-type> is either socket or lightbulb

Command Topic

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


where <tuyaAPI-type> is either socket or lightbulb

The latest change: I got rid of the additional topic (dspJ TOPIC IS GONE, YOU MUST use the “multiple” format of the JSON string if you want to set multiple dps values), I use the command topic.
where the command topic can be followed by the JSON string for the dps settings or the message string can hold the JSON string of dps settings. The JSON string can also be of the format for multiple dps settings.

The “on” or “ON” or “off” or “OFF” or “1” or “0” can also follow the command topic or appear in the message area (on="" or off=""). IT all works now.

the two JSON strings are as follows:

  1. dps settings: { “dps”: 1, “set”: true } ----- this only sets on dps value
  2. multiple dps settings: { “multiple”: true, “data”: { “1”: true, “7”: true }

So to put this all together, the dps topic still returns the status of the device.

There fore,
state topic

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

where <tuyaAPI-type> is either socket or lightbulb

command topic

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


where <tuyaAPI-type> is either socket or lightbulb

IF using the on=" " or off=" " place whats in the below box in those strings

1
0
ON
OFF
{ "dps": 1, "set": true }
{ "multiple": true, "data": { "1": true, "7": true }

* don't forget to escape sequence the " marks *

so with escape sequences:

'{ \"dps\": 1, \"set\": true }' or "{ \'dps\': 1, \'set\': true }" or

 " \"{ \\\"dps\\\": 1, \\\"set\\\": true }\" "



'{ "multiple": true, "data": { "1": true, "7": true }'  or "{ 'multiple': true, 'data': { '1': true, '7': true }" or

 " \"{ \\\"multiple\\\": true, \\\"data\\\": { \\\"1\\\": true, \\\"7\\\": true }\" "

To set one or multiple dps’ the command topic can look like the following:

tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/command/ON
tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/command/OFF
tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/command/1
tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/command/0
tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/command/{ "dps": 1, "set": true } 
tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/command/{ "multiple": true, "data": { "1": true, "7": true }

NOTE: During my tests, i noticed that when the tuya-mqtt.exe program is first started (initial boot up) the program gets an unhanded promise rejection warning. I found a way to suppress the error message so that the user will not see this error in the openhab2 error log.

If you are not aware, the error message only shows up on initial boot or initial communication with a tuya device. After the first communication has been establish, [by sending the first command twice in a row,] then as long as the tuya-mqtt server is up and running, the error will not return until reboot of the tuya-mqtt server.

Therefore, I would recommend to openhab2 users that if they use tuya-mqtt.exe they should upon system reboot send two state commands to each device they plan to talk to. This can be accomplished in the Tuya.rules file.

I believe this is a TCP/IP problem. These tuya devices use a TCP and that protocol is not considered to be deterministic. The error message is coming from a routine outside the scope of tuya-mqtt code.

So the best I could do is suppress the Error from the openhab2 log file. You will git this error for every tuya device tuya-mqtt will talk to on the first try. I have 18 devices and I really do not want that many errors is my log file.

Well that all from me. :wink:

Hey @GadgetAngel,

I had a lot of work to do in the last months and couldn’t really follow the project any further. Since everything works with my 5 devices so far, I didn’t have the need to do anything.

But I am very pleased that my Scipt finds so much resonance here. I will try to look at your pull-request in the next days and integrate it if necessary.

But already in advance I thank you for the support!

Best regards
AgentK

For WINDOWS USERS or Linux USERS:

As a windows user, or Linux user, you can use the openhab2 MQTT-Embedded-broker or install Mosquitto Broker.

If you plan to use the openhab2 MQTT-Embedded-Broker than the following information is important:

NOTE: THE EMBEDDED MQTT BROKER can be found at:

ADDONS —> Misc.-----> “MQTT Broker Moquette”

One of the hardest things to get working on a windows openhab2 server is to install Mosquitto.

I thought I do this write up to help others do what I have been able to do.

To install Mosquitto on your machine. Here is an excellent step-by-step guide on how to install mosquitto on windows and linux machines

I used Steve’s mosquitto files (see below picture, just link on the picture to go to Steve’s Website, click on the link labeled Mosquitto 1.5.8 Windows Files).

I only downloaded from his site because his mosquitto files were the only ones that passed www.virustotal.com virus scans. Steve’s Files had no viruses in them at all.

NOTE:
Be care of other mosquitto files downloads. To be sure go to www.virustotal.com and upload the files you want to install before you install them on your machine, even if you download the files from the from Eclipse website.

Here is the URL to the excellent Step-By-Step Guide To Installing Mosquitto (click on the picture to go to his website):

So how would one use a dimmer on this?
OH passes a value from 0-100.
DPS needs 1-255
I assume a rule would need to be set in the sense of, if the dimmer item changes
do the transformation from 1-255 to % and or vice versa
send a payload of {multiple, data, %, DPS} and the converted number for the %.

Is my thinking right or is there a simpler way?

When you define the dimmer ‘Thing’ you can set a min of 0 amd max of 255. Then an slider of 0-100 will send a percentage of 255.