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

Hi,

Thanks really for your help. I uploaded Tasmota on FcMila RGBW Light bulb which seems to be a Tuya. I succeeded in connecting to tasmota interface. I used generic configuration of the modula. A low blue red light always remained. when I click to toggle on, the light switches on, when I move the dark cursor, I get the small light (blue red).

How can I love it? Would it be possible to upload the original firmware? The process provided don’t work and I suppose it is because the bulb is not in flashing mode. How can I get it?

Thanks for your help

Kind regards

Claude

I have a Fcmila, GU10 bulb


flashed with Tasmota latest 6.41.19 developmental firmware
The best setting I could get under generic is this:

GPIO2 PWM4
GPIO4 PWM5
GPIO12 PWM2
GPIO13 PWM3
GPIO14 PWM1

HOWEVER, I can only get white light with those settings. Dimming works and the toggle as well. But I cannot get any RGB variation.

So still on the lookout myself.

regards

i have spent many days getting this to work on windows 10.

to get TheAgentK/tuya-mqtt installed I had to install python 3 for windows. i followed this:

install node.js (use the LTS version) from:
Node.js — Download Node.js®

i installed gitHub Desktop for windows 10, from this URL:

if you get the ERROR: Permission denied (publickey) when using gitHub Desktop go this URL
to fix it. You must have an account with github.com if you do not have an account create one.
URL:
Error: Permission denied (publickey) - GitHub Help

use gitHub Desktop: (file-> clone repository…choose URL…enter in TuyaAPI/ cli…and select c:\openhab2\userdata\etc\scripts directory). Now you have the repository in the right place. open power shell with admin and type “npm install -g” at the cli directory

to see if it installed it globally try out the command at the C:\ power shell prompt:

tuya-cli help

clone the repository unparagoned/ njsTuya and place it in c:\openhab2\userdata\etc\scripts. in power shell change dir to njsTuya and type “npm install”, leave off the -g flag. You only need to install tuya-cli as a global command.

clone the repository TheAgentK/ tuya-mqtt , and place it in c:\openhab2\userdata\etc\scripts. in a power shell window change directory to tuyaapi_mqtt and type in “npm install”, again leave off the -g flag.

Each install must not produced errors, if you get WARNing that OK. If if finds vulnerabilities do nothing.

once you get all that installed the tuya-mqtt.js file needs to be installed as a service.

so i found this topic:

From the topic you find out to just installed qckwinsvc by:

open power shell with admin, type “npm install -g qckwinsvc”

Now, after you installed qckwinsvc, ran qckwinsvc in cmd.exe with admin and answered the questions.
as follows:

qckwinsvc
tuya-mqtt
Server that converts MQTT topics into commands for tuya plugs and bulbs
c:\openhab2\userdata\etc\scripts\tuyaapi_mqtt\tuya-mqtt.js
y

before running tuya-mqtt.js make sure you copy the config.json.sample to config.json and edit it for your system settings.

Ensure the tuya-mqtt is running as a service. Go to control panel–>administrative tools–>services.
tuya-mqtt should be listed in Services and the status should be running/ automatic. If it is not showing “running” then right-click and select “start”.

The tuya-mqtt.exe is now installed as a service and will be set to run again when you log off or the system reboots.

tuya-mqtt is the client at 127.0.0.1 and will now be listening on your openhab server running on windows 10. Tuya-mqtt.exe basically converts the MQTT topic to a command that the tuya plug or tuya lightbulb can understand. So that is why you need the Tuya-mqtt.exe file running on the system always. With out it the tuya device would not understand the MQTT protocol. These tuya devices nativaly talk over TCP port 6668.

Now follow THE ABOVE GUIDE right after the tuya-mqtt.js script is running.

Find all your Tuya-Devices IP address, DeviceID and LocalKeys. I used the following technique to find mine:

it uses:

To interface with openhab2 follow the instructions for “Update for MQTT-Binding 2.4”

I have both lights and plugs so for the <tuyaAPI-type> use “lightbulb” for the lights and for smart plugs use “socket”. Also make sure you do the copy of the “config.json.sample” to “config.json”. Because the “config.json” needs to be there. I did not edit mine. I just used 127.0.0.1 which is the default.

To use the tuya-mqtt.exe server you will need to use the embedded-MQTT broker.

NOTE: THE EMBEDDED MQTT BROKER can be found at:

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

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

I used this Steve’s mosquitto files (see below). I only downloaded from his site because his mosquitto files were the only ones that passed www.virustotal.com virus scans. They had no viruses in them at all. Be care of other mosquitto files downloads. To be sure go to virustotal
and upload the file before you install it on your machine.

Here is the URL to the excellent step-by-step guide to mosquitto,

i hope this helps.

2 Likes

there are many Tuya apps to use on the App Store. He uses Smart Life by Tuya Inc. I use Smart life+ by RSH - TECH Company. Not all Tuya Apps will place the information in the “shared_prefs” folder. For Smart life+, I had to go to /data/data/com.rshsmartlife/shared_prefs/preferences_global_keyxxxxxxxxxxxxx.xml

you just need to install the app you use and check in the right folder to see if the information is in the .xml file. If not I would delete the devices from your App and use the Smart Life App for Tuya Inc. Then follow his directions

Hello all,

Today i have added a new device on my smart life aplication, and i have followed the steps to get de global key of the device but, the preferences_global_keyxxxxxxxxxxxxx.xml does not have the information of any device.
Something changed in the last days?


Thanks.

1 Like

I had the same issue. I posted above the way I got my details using an android phone, I didn’t use bluestatcks as I was unable to install the certificate.

2 Likes

It worked! Many thanks!

Hi!)
I’m very new with Nodejs. I tried to do each step as right as in the instruction, but I have stopped at the starting “tuya-mqtt.js” with an error:

/etc/openhab2/scripts/tuyaapi_mqtt/node_modules/tuyapi/index.js:4
const {EventEmitter} = require('events');
      ^

SyntaxError: Unexpected token {
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:374:25)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Module.require (module.js:354:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/etc/openhab2/scripts/tuyaapi_mqtt/tuya-device.js:1:78)
    at Module._compile (module.js:410:26)
    at Object.Module._extensions..js (module.js:417:10)

What it might be?

Hello,
Will this guide work with a Wifi Smart Plug which uses Smart Life APP?
Such as this one
https://www.amazon.com/SUPERNIGHT-Upgraded-Anywhere-Required-Configuration/dp/B07K1QSFFJ

If not please give me some ideas on how to control such a plug with OH. I scanned the binding list but could not find one to use with this device.
Many thanks!

I know it works with these smart plugs, i run openhab 2.5.0 on a windows 10 machine. I use a MQTT broker from a raspberry pi , just make sure you use “socket” instead of “lightbulb” in the tuya/ topic

my smart plug is this one:
https://www.amazon.com/gp/product/B072ZX8RTZ/ref=ppx_yo_dt_b_asin_title_o01_s00?ie=UTF8&psc=1

Thank you GadgetAngel.

I have zero MQTT experience and this will be my first integration with MQTT. I’m not yet familiar with the MQTT lingo and it would be very helpful for me if I could see how different pieces connected together. Based on the guide above and your message, I guess this is how the pieces are connected together:

OpenHAB <> MQTT-Binding(aka MQTT broker?) <> Tuya-MQTT-Client(aka topic?) <> Smart Plug

Does the above diagram have any resemblance to reality?

I still don’t know what node.js ( a prerequisite) is and how it fits in the above diagram. I have lots of learning to do.

yes, thats the flow of information. You need to decide if you will use the OpenHAB embedded MQTT broker or have one in your network somewhere. I was having trouble with the embedded option so i build a raspberry pi magic mirror and its running my MQTT Broker.

so OPENHAB <> MQTT-Binding[Embedded MQTT Broker Thing]<>MQTT-Generic-Thing<>Tuya-mqtt-Client(aka topic)<>Smart Plug

what you need to see here is that After you install the MQTT Binding the first thing you do is ADD the MQTT misc Embedded MQTT Broker then Define the MQTT Broker to use the embedded broker as a bridge broker. Then you finally define a MQTT-Generic-Thing that links to your smart plug and tells the system to use the MQTT Broker with commands like this

one for state:
tuya/socket/XXXXXXXXXXXXXXXXXXXXXXX/192.168.0.19/state

one for command:
tuya/socket/XXXXXXXXXXXXXXXXXXX/192.168.0.19/command

where XXXXXXXXXXXXXXXXXX is the secret Key ID.

Have fun!!!

Thank you for sharing this. Could you give an example of your channel settings? Do you use OH2.4 with tuya-mqtt from AgentK or maybe also with the fix from tsightler?

I’m not able to get any dps value shown in OH2.
What I have done so far:

  1. I have added a Channel(Channel Type:Text Value) to my Generic MQTT Thing.
  2. In the channel I set MQTT State Topic to:
    “tuya/socket/82141708dc4f22905494/XXX/192.168.178.38/dps/6”
  3. Linked this channel to an item.

In PaperUI the value for the item is only “-”… Thats all I see

I can switch Power ON/OFF with tuya-mqtt from tsightler and OH2.4. But I am not able to get any other dps value shown in PaperUI or BasicUI.

Btw. thank you to all other developers and to the community. I am very new to OH, but I found many helpful explanations and add-ons here.

Hi,

I was using the one from AgentK. I’ve actually moved over to tasmota now, but this is how I had it configured.

The only possible difference that may be relevant is I used the ‘number’ type instead of string.

This is my things file:

Bridge mqtt:broker:openhabBroker [ host="127.0.0.1", secure=false ]
{
    Thing topic tuya_teckin_plug_2 "Tenkin Smart Plug 2" {
    Channels:
        Type switch : powerswitch "Power Switch" [ stateTopic="tuya/socket/XX/YY/192.168.1.71/state", commandTopic="tuya/socket/XX/YY/192.168.1.71/command", on="ON", off="OFF" ]
        Type number : watts "Watts" [ stateTopic="tuya/socket/XX/YY/192.168.1.71/dps/5", transformationPattern="JS:tuya-energy.js"]
        Type number : volts "Volts" [ stateTopic="tuya/socket/XX/YY/192.168.1.71/dps/6", transformationPattern="JS:tuya-energy.js"]
    }
    Thing topic tuya_teckin_plug_1 "Tenkin Smart Plug 1" {
    Channels:
        Type switch : powerswitch "Power Switch" [ stateTopic="tuya/socket/XX/YY/192.168.1.184/state", commandTopic="tuya/socket/XX/YY/192.168.1.184/command", on="ON", off="OFF" ]
        Type number : watts "Watts" [ stateTopic="tuya/socket/XX/YY/192.168.1.184/dps/5", transformationPattern="JS:tuya-energy.js"]
        Type number : volts "Volts" [ stateTopic="tuya/socket/XX/YY/192.168.1.184/dps/6", transformationPattern="JS:tuya-energy.js"]
    }
}

And my items file:

Number In_Tuya_Tenkin_Smart_Plug_1_Watts "Tuya Tenkin smart plug 1 watts [%d]" <energy> (gElectric) { channel="mqtt:topic:openhabBroker:tuya_teckin_plug_1:watts" }
Number In_Tuya_Tenkin_Smart_Plug_1_Volts "Tuya Tenkin smart plug 1 volts [%d]" <energy> (gElectric) { channel="mqtt:topic:openhabBroker:tuya_teckin_plug_1:volts" }

The tuya-energy.js was just to divide the value by 10:

(function(i) {
      return Math.ceil(i / 10)
})(input)

It’s probably worth using a tool like mqtt.fx to check which mqtt topics are getting updated. I think the dps values can be different per plug.

Many thanks for showing your implementation.

That’s an interesting way to handle it. So far, I have specified everything for tuya in the PaperUI, nothing really worked fine. But I will try this also and specify everything in files.

Which version of AgentK’s implementation are you using?

Hi,

I was using master branch, commit 53811376843cca325fee83cd935c645c0c1863f4 to be specific.

The reason I did it in files was just because it was easier to then add new plugs - it was a case of copy/paste and change a few values. I think PaperUI should work, but I haven’t tried it.

Now I’am totally confused…

It’s not working for me. I handled it the same way. But my smart plugs won’t switch reliable if I use OH2.4.
I tried it with the old 1.x-Binding and Mosquitto and everthing is working fine. On is on, Off is off, absolutly reliable and without any delay.

I tried many different settings with OH2.4 and MQTT. In one setup I was able to switch the plugs, but there was ab big delay and it was not reliable. In this setup I connected the things driectly to the embedded broker of OH2.4.

In the other settings I often received an error from the tuya-mqtt.js. Sounds like something is preventing the communication to the device:

"at TCP.onStreamRead (internal/stream_base_commons.js:111:27)
(node:22334) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing i
nside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (reje
ction id: 5) "

Another stupid thing is. I have two smart plugs and I can switch the one more reliable than the other… Dont know why. The Id and the key of both are correct, they work with 1.x and Mosquitto.

After frustrating hours of trying I went back to my 1.x setup with Mosquitto… But with this setup I’m not able to observe the dps output of the devices… Or?

Maby somebody has an idea? I’m grateful for any help!

I still don’t know what node.js ( a prerequisite) is and how it fits in the above diagram. I have lots of learning to do.

To put it simple, node.js is required for running Java Script.

First, check if node is present on your system:

which node

Output should be something like

/usr/sbin/node

If not, install it by:

sudo apt-get install nodejs

Hope I could help.

1 Like

Thanks Holgi. I just checked and node was already installed on my RPi :smiley:.

Currently, I’m having the "“Permission denied (publickey)” problem when trying to download AgentK’s Tuya-MQTT-Client. I saw your #5 post above on how to fix that but have not yet implemented it.

Thanks to this Project.
I was now able to handle my GOSUND SP111 Socket with openHAB.

For those who still trying to get the localkey and do not have root access on android.
Check the man in the middle method, using Burp Suite as Proxy with your Smartphone to capture the traffic and find the localKey.

In openHAB I needed to extend the Things Settings to work with the GOSUND:

Maybe this helps somone.

Keep up the great work