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

Hi Holig,

Thanks for updating your guide with instructions for 2.4. One question I was wondering about as I noticed you dumped mosquitto and went with the embedded MQTT broker. I did the same at first, but ended up reverting back to mosquitto as I could not get the embedded broker to save state. For example, when I would restart OpenHab, which restarts the embedded broker, some of my lights would randomly turn on, as if they remembered a prior state.

When using mosquitto I never had this issue. I spent 15-20 minutes trying to understand this behavior, but eventually gave up and just went back to mosquitto while still using the 2.4 MQTT bindings. This is probably OK for me anyway as I have several other tools integrated with MQTT so using a broker outside of OpenHAB probably makes sense anyway, but mostly I was just curious if you’ve seen this behavior.

Hi Tom,

first of all thank you for sharing your experience about the embedded broker.

As I actually got it finally working only today, I did not have many restarts so far. But I will keep an eye on it and let the community know if I run into the same issues with the embedded broker.

One question, when using MQTT Generic Things, you configure them to the Bridge or the Embedded Broker? I actually wonder what would make the difference. For your understanding, is it necessary to use the bridge at all when using embedded broker?

Hmm…I did not use a bridge when I attempted to use the embedded broker since the embedded broker was listed I assumed it was OK to use it directly.

Yeah, if I understand right, the bridge is only necessary when “talking” to an external broker. So actually one could skip this step when using internal broker.

Is there any other way to find the Tuya Localkey.?
I have my device flashed with tasmota, and Module type defined as Magic Home(34).

Yes, with the so-called “man-in-the-middle”-method. In a nutshell, it includes setting up a proxy on a smartphone and let the bulbs connect to the phone during the initial setup, while you capture the information sent by the bulbs with the proxy.

Find a instruction here:

In my case, the bulbs already have been set up, and I did not have a iPhone I found the method with emulator more convenient as you have a plain text file where you just need to copy&paste the local keys.

2 Likes

Thank for this great work.

Did anybody try to include a tuyaapi smart plug with power metering. they are around 15 Euro (Wlan Smart Steckdose AISIRER).

Regards

1 Like

The procedure for bulbs and plugs is basically the same.

With a plug, the tuya topic starts with

tuya/socket/22345555...

So just replace “lightbulb” with “socket”.

Regarding metering, I guess this will not work right now, cause the script doesn’t support it yet. You may do a feature request at GitHub.

Cheers

Really nice comprehensive guide.
@AgentK Nice work getting on the MQTT setup.
I’ve added links to your post and github on my post and github.

I’ve got a python script which automatically gets all the device ids and ips of the devices on your network. It removes the pain of having to manually find the id, and arp and then greping the mac. Feel free to copy or use it in your guide/github. It also gets the device state if njstuya is installed, but works fine as a standalone. https://github.com/unparagoned/njsTuya

python njstuyamonitor.py -v
2 Likes

Hello everyone,

I am working on a video to show people how to use the tuya binding but, am having a little bit of trouble. I keep getting this:

(node:28317) UnhandledPromiseRejectionWarning: Error: Error communicating with device. Make sure nothing else is trying to control it or connected to it.
at Socket.client.setTimeout (/etc/openhab2/scripts/tuya-mqtt/node_modules/tuyapi/index.js:471:35)
at Object.onceWrapper (events.js:273:13)
at Socket.emit (events.js:182:13)
at Socket._onTimeout (net.js:453:8)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
(node:28317) 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:28317) [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.

I would really appreciate any help I can get. Thank you in advance!

I’ve only been using OpenHab for a few days but would really like to get my smart plugs connected to it.

Could somebody please give me some more info on how to install the TuyaAPI-MQTT Client on a windows 10 install of OpenHAB.

Thanks.

Sounds like you have probably are using the wrong ip or key. Getting the device status doesn’t use the key, so that’s probably your first port of call to figure out which.

There is some development going on to upload custom firmware like tasmota to tuya devices without physically opening it and bypassing tuya cloud. It’s still under heavy development.

https://github.com/ct-Open-Source/tuya-convert

https://github.com/SynAckFin/TuyOTA

1 Like

Does anybody know if the way to get the local key has changed?

I’ve followed the guide to connect to it with a rooted version of bluestacks and I’ve navigated to the file stated “preferences_global_keyeu1538398658843UYtHD.xml” but my file only contains this.

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

Well I’ve managed to get it working. In doing so I think I’ve found an easier was to get the local key.

The below has worked for me on my android phone.

1. Install "Package Capture" from play store.
2. When you first open it it will ask you to install a certificate, this is needed for it to work.
3. In the top bar there are two "Play" symbols. The one with a 1 allows you to capture packets from certain apps.
4. Click on that and select "Smart Life" from the list of apps.
5. It will popup about setting up a vpn and will then start capturing packets.
6. Open Smart Life and on the screen showing "All Devices" pull down the screen to cause a refresh.
7. Go back to Packet Capture and hit the Stop button at the top.
8. You should now have an entry below showing x number of captures.Tap that to open it.
In mine I had to open the last packet in the list that was marked as SSL.
9. Scroll down through the first few blocks and you should see a large JSON block
This contains a lot of code but if you scroll through it you should see line like the ones below 

"devAttribute": 0
"name": Smart Socket 4"
"timezoneId": "Europe/London"
"localKey": "XXXXXXXXXXXXXXX"

I’ve used the local keys i found doing this to setup two smart plugs and they are both working a treat.

In that large JSON code block it should list every device you have and all their local keys.

10 Likes

That’s what the instructions used to be and I thought they changed for a reason. But I’m going to steal your nice summary rather than referring people to potentially more complicated and less reliable methods.

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