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

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/192.168.1.222/dps/6 is the volts (e.g. 2372 - needs to be divided by 10)

and

tuya/socket/1274756684f3ebb88ee7/XXXXXXX/192.168.1.222/dps/5 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: https://www.amazon.co.uk/TECKIN-Outlet-Wireless-Control-Required/dp/B07CVJYV3G/ref=sr_1_3?ie=UTF8&qid=1549473531&sr=8-3&keywords=teckin+smart+plug

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

Hi,

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:
https://www.aliexpress.com/item/APP-Control-Basic-Wireless-Wifi-Switch-For-Smart-Home-Automation-Relay-Module-Remote-Controller-10A-90/32961149555.html

Thanks for sharing this topic. Via this wrapper https://github.com/unparagoned/njsTuya 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): https://nl.aliexpress.com/item/Hot-WiFi-LED-Dimmer-220-v-110-v-Dimmen-Panel-Schakelaar-Aangesloten-op-Alexa-Google-Thuis/32910295055.html?spm=a2g0z.search0104.3.1.1edc55ebZaUZIK&transAbTest=ae803_5&ws_ab_test=searchweb0_0%2Csearchweb201602_1_10065_10068_319_317_10696_10084_453_10083_454_10618_10304_10307_10820_10821_10301_537_536_10902_10843_10059_10884_10887_321_322_10103%2Csearchweb201603_57%2CppcSwitch_0&algo_pvid=44a4e116-2614-4ae6-aa16-f1851bf490e3&algo_expid=44a4e116-2614-4ae6-aa16-f1851bf490e3-0

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];
                device.set(status);
            }
            if (exec == "dpsJ") {
                var status = topic[6];
                device.set(JSON.parse(status));
            }
            if (exec == "color") {
          ...

Git hub link Link https://github.com/unparagoned/tuya-mqtt/blob/master/tuya-mqtt.j

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/123.123.1.0.1/dps/"{ \"dps\": 1, \"set\": true }"

socket/32kdjtuyaIDsdfdf/3dfxtuyaKey/123.123.1.0.1/dpsJ/"{ \"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?

Hi, have you got it?
Me too have a PIR sensor tuya and wonder how integrate it in OH
Does anybody out there, knows how to do?

1 Like

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?