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

tuya
smart-life
arilux
Tags: #<Tag:0x00007f1e5b98a190> #<Tag:0x00007f1e5b989ce0> #<Tag:0x00007f1e5b989970>
(Rik Muilwijk) #42

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…

0 Likes

(Coops213) #43

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.

0 Likes

(Stopeject) #44

Is support for the power monitoring outlets still far off?

0 Likes

(Elenio) #46

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

(Claude) #47

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

0 Likes

(J Wee) #48

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

0 Likes

(Gadget Angel) #49

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:
https://nodejs.org/en/download/

i installed gitHub Desktop for windows 10, from this URL:
https://help.github.com/en/desktop/getting-started-with-github-desktop/installing-github-desktop

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:
https://help.github.com/en/enterprise/2.15/user/articles/error-permission-denied-publickey

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,

com

i hope this helps.

1 Like

(Gadget Angel) #50

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

0 Likes

(Luis Cardoso) #51

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

(Fraser Howell) #52

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

Merkury Wifi smart bulbs
(Luis Cardoso) #53

It worked! Many thanks!

0 Likes

(Иван Полуда) #54

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?

0 Likes

(oh_newbie) #55

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!

0 Likes

(Gadget Angel) #56

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

0 Likes

(oh_newbie) #57

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.

0 Likes

(Gadget Angel) #58

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!!!

0 Likes

(Niklas) #59

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.

0 Likes

(Andy) #60

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.

0 Likes

(Niklas) #61

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?

0 Likes

(Andy) #62

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.

0 Likes