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

with discovery, you need to put :

tuya/<tuyAPI-id>/<tuyAPI-key>/discover/command

Hi Friends

First excellent tutorial.

i want to order tuya bulb and led strip from ALI express

Please could you confirm if those items are supported (or if you have even better propositions)

Bulb

Led Strip

Thanks in advance

Hello,
I have MQTT running well with zigbee2mqtt and Openhab but I am struggling to add MQTT for Tuya devices.
I have followed the first steps described above:

  • I have got the device ID and Key.
  • I have tuya-mqtt.js running ok (version 2.0.1) using the default configuration
  • I could read in DEBUG mode mqtt messages coming up from human actions on the Tuya switch.
  • The format of these messages have no reference to a tuyaAPI-type
  • Typically messages are in the following format: tuya/device id/device key/device ip/action payload
  • When I send messages in the same format using mqtt.fx they are parsed by tuya-mqtt with no error but no action is taken as you could see below where I try to set up brightness from 50 to 60:
C:\Users\regis\openhab2\conf\scripts\tuya-mqtt>set DEBUG=* & node tuya-mqtt.js
  TuyAPI:mqtt MQTT-Server nicht verbunden. +0ms
  TuyAPI:mqtt Verbindung mit MQTT-Server hergestellt +13ms
  TuyAPI:mqtt receive settings {"topic":"tuya/62564885840d8e9127b6/39ca69b7e7c6a479/192.168.1.79/dps","action":"dps","message":"{\"2\":60}","options":{"id":"62564885840d8e9127b6","key":"39ca69b7e7c6a479","ip":"192.168.1.79"}} +20s
  TuyAPI:device Search device in network +0ms
  TuyAPI IP and ID are already both resolved. +0ms
  TuyAPI:device Device found in network +1ms
  TuyAPI Connecting to 192.168.1.79... +1ms
  TuyAPI Socket connected. +4ms
  TuyAPI:device Connected to device. undefined (192.168.1.79, 62564885840d8e9127b6, 39ca69b7e7c6a479) +10ms
  TuyAPI:device GET Payload: +1ms
  TuyAPI:device { gwId: '62564885840d8e9127b6', devId: '62564885840d8e9127b6' } +0ms
  TuyAPI Received response +14ms
  TuyAPI 000055aa000000000000000a00000044000000007b226465764964223a223632353634383835383430643865393132376236222c22647073223a7b2231223a747275652c2232223a35307d7db47ec3e00000aa55 +2ms
  TuyAPI Parsed response data: +1ms
  TuyAPI { devId: '62564885840d8e9127b6', dps: { '1': true, '2': 50 } } +0ms
  TuyAPI:device Data from device: { devId: '62564885840d8e9127b6', dps: { '1': true, '2': 50 } } +12ms
  TuyAPI:mqtt:device Data from device undefined : { devId: '62564885840d8e9127b6', dps: { '1': true, '2': 50 } } +0ms
  TuyAPI:mqtt:device mqtt status updated to:tuya/62564885840d8e9127b6/39ca69b7e7c6a479/192.168.1.79/state -> ON +1ms
  TuyAPI:mqtt:device mqtt dps updated to:tuya/62564885840d8e9127b6/39ca69b7e7c6a479/192.168.1.79/dps ->  {"1":true,"2":50} +0ms
  TuyAPI:mqtt:device mqtt dps updated to:tuya/62564885840d8e9127b6/39ca69b7e7c6a479/192.168.1.79/dps/1 -> dps[1] true +1ms
  TuyAPI:mqtt:device mqtt dps updated to:tuya/62564885840d8e9127b6/39ca69b7e7c6a479/192.168.1.79/dps/2 -> dps[2] 50 +0ms
  TuyAPI:mqtt receive settings {"topic":"tuya/62564885840d8e9127b6/39ca69b7e7c6a479/192.168.1.79/state","action":"state","message":"ON","options":{"id":"62564885840d8e9127b6","key":"39ca69b7e7c6a479","ip":"192.168.1.79"}} +75ms
  TuyAPI:mqtt receive settings {"topic":"tuya/62564885840d8e9127b6/39ca69b7e7c6a479/192.168.1.79/dps","action":"dps","message":"{\"1\":true,\"2\":50}","options":{"id":"62564885840d8e9127b6","key":"39ca69b7e7c6a479","ip":"192.168.1.79"}} +4ms
  TuyAPI:mqtt receive settings {"topic":"tuya/62564885840d8e9127b6/39ca69b7e7c6a479/192.168.1.79/dps/1","action":"dps","message":"true","options":{"id":"62564885840d8e9127b6","key":"39ca69b7e7c6a479","ip":"192.168.1.79"}} +1ms
  TuyAPI:mqtt receive settings {"topic":"tuya/62564885840d8e9127b6/39ca69b7e7c6a479/192.168.1.79/dps/2","action":"dps","message":"50","options":{"id":"62564885840d8e9127b6","key":"39ca69b7e7c6a479","ip":"192.168.1.79"}} +1ms
  TuyAPI Pinging 192.168.1.79 +10s
  TuyAPI Received response +17ms
  TuyAPI 000055aa00000000000000090000000c00000000b051ab030000aa55 +0ms
  TuyAPI Pong 192.168.1.79 +1ms
  TuyAPI Pinging 192.168.1.79 +10s
  TuyAPI Received response +12ms
  TuyAPI 000055aa00000000000000090000000c00000000b051ab030000aa55 +1ms

My first question is to know what tuyaAPI-type is referring to?
I also thank you in advance for helping me identifying what’s wrong in my setup

I am on 2.5.M3 and all my setup is on windows 10

you use paper ui and mqtt 2 ? or you want change brightness by a rule ?

try with rules , for example :

val mqttActions = getActions("mqtt", "mqtt:broker:6de5ece8") mqttActions.publishMQTT("tuya/ver3.3/XXXXXXXXXXX/XXXXXXXXXXXX/192.168.1.8/command","{ \"multiple\": true, \"data\": { \"1\": true, \"2\": \"60\" } }")

and in paper UI :

{ “multiple”: true, “data”: { “1”: true, “2”: 60 } }

Thank you Yannis, it works !

I had to read the full thread to understand that I needed first to update to tsightler/tuya-mqtt. It will be great if Holgi could update his original post with a reference to the tsightler version.

Also in your example below the “quotes” are not the right ones (“ or ” must be "). The issue seems to come from this editor that forces the wrong quotes.

Now I am trying to set the Tuya thing as a dimmer in PaperU.
I have tried to set a new channel in PaperUI with the following MQTT command topic:
tuya/device ID/Device Key/Device IP/command with a min of 0, a max of 255 and the Outgoing value format set to { “dps”: 2, “set”: %s}.
The mqtt message is properly received (“action”:“command”,“message”:“{ "dps": 2, "set":28.05}”) except that the dimmer value is not an integer and therefor not processed

I have tried to set the Outgoing value format to { “dps”: 2, “set”: %d} but in that case the mqtt message is truncated (and still not an integer) “action”:“command”,“message”:“28.05”

Any advice on how to properly send the dimmer value as an integer?

As per the following discussion,

the proper solution to control the tuya brightness should be to add { “dps”: 2, “set”: %d} to the Outgoing Value Format (OVF) but it seems that the %d is not properly processed by OVF at that stage.

The temporary workaround is to not use OVF but to add the following Javascript transform

(function(brightness) {
    var tuyaobj = {"dps": 2, "set": Math.floor(brightness)};
    var data = JSON.stringify(tuyaobj);
    return data;
})(input)

JSONPATH:$.2 must also be added to the channel “Incoming value transformations” to process properly the dimmer state.
The complete channel set up is:

with the following item setup (including Alexa control for my specific need)

Dimmer TableLight “Table Light [%d %%]” {channel=“mqtt:topic:d2cfb0fd:Dimmer”, alexa=“Lighting”}

1 Like

Welcome

Has anything changed in terms of obtaining LocalKey?
I have 2 rooted devices. In the file preferences_global_key_ some chars and numbers.xml
there is no information besides:

<? xml version = '1.0' encoding = 'utf-8' standalone = 'yes'?>
<map />

Identical result for 2 applications: Smart Live - Smart Living and Tuya Smart.

I checked the solution given in # 31 post “Install” Package Capture “from play store …”
I used the Pocket Capture application by Gray Sh. as a result, I’ve got captures (mostly without data) Several of them contain some RAW data that totally doesn’t match the pattern.

How to obtain LocalKEY ?!

Thanks this worked for me as well (Tuya wall Switch)
Here is the way i made

You will need to download old version of the SmartApp however the key you will see is wrong and will not work…
I followed this guide and find the real key


I also document the steps that worked for me…

1 Like

I sort of successfully set up these bulbs from Amazon:

I was able to get the bulbs to turn on and off but when I added a separate MQTT color channel, the bulb would shut off and the local key would change. I can get the new key (again) and restore on/off functionality. I then tried again, except completely deleting the Smart Life app from my phone (thinking they still had a cloud connection through the app) but experienced the same result. The bulb would stop functioning and the key would change if I tried to change the color using a color channel.

Has anyone else seen these devices change their local key like that, and if so, any suggested work around?

Hi there, I have the same issue as some of you guys.
I am not able to obtain ther Key, because the preferences-file is nearly empty and all other methods I try do not work (proxy, paket capture).

Is there a way to get the Key I dont know yet?

I have an rooted Android Phone with the latest Tuya App installed.

Or is there an other way to get Tuya work with openhab2?

Thanks Nils

Hi all,

just fyi: I ran into the Github-PERMISSION DENIED-issue as well.
As Github recommends to use https instead of ssh, just clone the repo with

git clone GitHub - TheAgentK/tuya-mqtt: Nodejs-Script to combine tuyaapi and openhab via mqtt

This worked for me as the easiest solution.

Cheers
Matthias.

Another one from me: as I did not manage to get the LocalKey for the Tuya-Plugs, I decided to flash a plug with Tasmota via OTA, so no opening of the plug or soldering needed.
I used the Tuya-Convert-scripts from c’t Magazine/ VTRUST (How-To here in german: https://www.heise.de/ct/artikel/Tuya-Convert-IoT-Geraete-ohne-Loeten-vom-Cloud-Zwang-befreien-4283623.html).

Even the Power Consumption information is available. The Plugs are not connected anymore to the Tuya-Cloud and MQTT works well to have them switched via Openhab.

Only to get the correct GPIO-Configuration might be tricky if the device is not mentioned in the Wiki: https://github.com/ct-Open-Source/tuya-convert/wiki.

@HolgiHab: many thanks for the Step-by-Step guide. Was a good starting point for me, even when I decided to use another solution.

Best regards,
Matze.

With the New OH 2.4 is Mosquito really necessary?

Use Smart Life version 3.6.1. In newer version preferences have been encrypted.

1 Like

Hi,

i have bought those two lightbulbs
https://fr.aliexpress.com/item/4000071916147.html?spm=a2g0o.detail.100009.5.4c4519a07614uo&gps-id=pcDetailLeftTopSell&scm=1007.13482.95643.0&scm_id=1007.13482.95643.0&scm-url=1007.13482.95643.0&pvid=80dd6538-bb1a-4270-b84e-4650ab25be69

https://fr.aliexpress.com/item/33018729697.html?spm=a2g0o.detail.100009.1.4c4519a07614uo&gps-id=pcDetailLeftTopSell&scm=1007.13482.95643.0&scm_id=1007.13482.95643.0&scm-url=1007.13482.95643.0&pvid=80dd6538-bb1a-4270-b84e-4650ab25be69

  1. I have used an android rooted phone with Smartlife 3.6 to extract local key

  2. I have well configured the tuya-mqtt service

it’s well working for the first lightbulb

tuya/lightbulb/83051002cc50e343f55c/4acc76c34273ae63/192.168.1.140/command/on

but for the second lighbulb, it’s not working. in spite that i can see that the mqtt commands are well sent from openhab but the bulb do not get turned off or on

tuya/lightbulb/550700402462ab453e00/c86653bc8dd7b695/192.168.1.141/command/on

I dont get it. When I set the channel as in the tutorial shown, it dont work.

Now I found a solution from the beginning of this topic with a rule. I get an error with no clue for me.

My MQTT Statements:
State -> tuya/socket/20856001840d8e9aec7e/4294933610b7c006/192.168.2.131/state
Command -> tuya/socket/20856001840d8e9aec7e/4294933610b7c006/192.168.2.131/command

The command line is not working an for the rule I deleted it from the channel.

Here is the rule

rule "Test MQTT ON"
when
    Item TestPlug received command
then
    switch(receivedCommand){
        case ON : actions.publishMQTT("tuya/socket/20856001840d8e9aec7e/4294933610b7c006/192.168.2.131/command/on","")
       case OFF : actions.publishMQTT("tuya/socket/20856001840d8e9aec7e/4294933610b7c006/192.168.2.131/command/off","")
    }
end

I am going crazy with this.

Here is the error message:
2020-01-08 20:19:29.738 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Test MQTT ON': An error occurred during the script execution: index=0, size=0

Hi,

thanks for the great tutorial.

Ffinally I was able to set everything up.

But after trying to start the script with

absolutely nothing happens.

I am a total newbie to Linux.
Do I have to install anything else? Do I do anything wrong? Or is it just normal behaviour?

Thank you!

Hi I finally got this working but I needed a few more steps to get it working with BlueStacks on Windows.

When you have BlueStacks up and running then:
Download SmartLife 3.6.1 apk (old version may no longer be available in the play store)
Download Root Certificate Manager
Scan the QR code with your phone and enter the web address into Chrome in BlueStacks
Save it in the download folder (automatic)
Open Root Certificate Manager, find the ca.pem file (just downloaded) and install it

You need to change the proxy for BlueStacks, to do this
In windows command prompt enter:
> cd c:\Program Files\BlueStacks_bgp64
then
HD-ConfigHttpProxy.exe set <ip address in QR window> <port in QR window>

Restart BlueStacks
Open SmartLife 3.6.1 app
Pull down to refresh and the keys are shown in the QR window, copy them for later

Once complete reset the BlueStacks proxy by entering the following in windows command prompt:
HD-ConfigHttpProxy.exe reset

And remove the certificate with Root Certificate Manager, it is called “Node MITM Proxy CA”, select it and delete.

Now continue with the MQTT as above.

I’m having such a headache with all this… I’ve tried all the other methods for getting these keys and hit a dead end and now I’m getting the same thing here.

I’ve followed the steps up to and including installing Root Certificate Manager, but I can’t figure out where the QR code is coming from. All I can see in RCM is a list of 150-odd certs, all of which I can view or download to SD card, but no QR codes to be found anywhere. What am I missing??

EDIT Scratch that, I was able to locate the keys using a different root folder explorer. Phew!