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

Hi all,

I have Teckin SB50 blubs, trying to control through openHAB using this solution. I think I did all the setup as instructed.

I got and used the ID, keys and IP address of the device. I can do the tuya-cli get command and get a successful payload from the bulb:

[root@telegramhost tuya-mqtt]# DEBUG=* tuya-cli get --ip 192.168.8.109 --id xxxxxxxxxxxxxxxxxx --key xxxxxxxxxxx --all
TuyAPI IP and ID are already both resolved. +0ms
TuyAPI Connecting to 192.168.8.109… +3ms
TuyAPI Socket connected. +79ms
TuyAPI GET Payload: +1ms
TuyAPI { gwId: ‘xxxxxxxxxxxxxxxx’, devId: ‘xxxxxxxxxxxxxxxxx’ } +0ms
TuyAPI Pinging 192.168.8.109 +10s
TuyAPI Received data: 000055aa00000000000000090000000c00000000b051bb030000ab45 +34ms
TuyAPI Parsed: +0ms
TuyAPI { payload: false, leftover: false, commandByte: 9, sequenceN: 0 } +0ms
TuyAPI Pong from 192.168.8.109 +1ms

However, when I run the DEBUG=* node tuya-mqtt.js and send mqtt commands whether through openHAB items from the control tab or mqtt.fx app using the manual method, it keep failing as shown in the debug below:

TuyAPI:mqtt receive settings {“topic”:“tuya/xxxxxxxxxxxxxxxxx/xxxxxxxxxxxxxxx/192.168.8.109/command”,“action”:“command”,“message”:“OFF”,“options”:{“id”:“xxxxxxxxxxxxxxxxx”,“key”:“xxxxxxxxxxxxxxx”,“ip”:“192.168.8.109”}} +23s
TuyAPI:device Search device in network +23s
TuyAPI IP and ID are already both resolved. +23s
TuyAPI:device Device found in network +1ms
TuyAPI Connecting to 192.168.8.109… +0ms
TuyAPI Socket connected. +121ms
TuyAPI:device Connected to device. undefined (192.168.8.109, xxxxxxxxxxxxxxxxx, xxxxxxxxxxxxx) +122ms
TuyAPI:device GET Payload: +0ms
TuyAPI:device { gwId: ‘xxxxxxxxxxxxxxxxx’, devId: ‘xxxxxxxxxxxxxxxxx’ } +0ms
TuyAPI:mqtt receive command { set: false } +124ms
TuyAPI:device set: { set: false } +1ms
TuyAPI SET Payload: +2ms
TuyAPI {
TuyAPI devId: ‘xxxxxxxxxxxxxxxxx’,
TuyAPI gwId: ‘xxxxxxxxxxxxxxxxx’,
TuyAPI uid: ‘’,
TuyAPI t: 1567712880,
TuyAPI dps: { ‘1’: false }
TuyAPI } +0ms
TuyAPI Pinging 192.168.8.109 +10s
TuyAPI Error event from socket. 192.168.8.109 Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:201:27) {
errno: ‘ECONNRESET’,
code: ‘ECONNRESET’,
syscall: ‘read’
} +46ms
TuyAPI:device:error Error: Error from socket
TuyAPI:device:error at Socket. (/etc/openhab2/scripts/tuya-mqtt/node_modules/tuyapi/index.js:365:30)
TuyAPI:device:error at Socket.emit (events.js:209:13)
TuyAPI:device:error at emitErrorNT (internal/streams/destroy.js:91:8)
TuyAPI:device:error at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
TuyAPI:device:error at processTicksAndRejections (internal/process/task_queues.js:80:21) +34s
TuyAPI Socket closed: 192.168.8.109 +1ms
TuyAPI:device Disconnected from device. undefined (192.168.8.109, xxxxxxxxxxxxxxxxx, xxxxxxxxxxxxxxx) +10s
TuyAPI:device delete Device undefined (192.168.8.109, xxxxxxxxxxxxxxxxx, xxxxxxxxxxxxxxx) +0ms

Things I tried so far:

  • restarted openHAB
  • restarted the bulb
  • closed the Smart Life app as some other people recommends
  • tried to remove the light from the Smart Life app
  • factory reset the bulb and reconfigure it which renewed the key then I reused the new key in openHAB thing and try again.

I think I’m in a rabbit hole now. Appreciate your help here.

Thanks.

Hello, When you try the tuya-cli get command, I don’t see the list of DPS… try to put in MQTT : tuya/Tuya-id/tuya-localkey/discover/state and tuya/Tuya-id/tuya-localkey/discover/command

work or not ?

Make sure you kill the tuya app on any devices that may have it open.
That caught me out with a similar error
Cheers

Hi Fiftiz, thank you for your comment.

I tried and failed. When I used the MQTT style you shared, I receive this:

TuyAPI:mqtt receive settings {"topic":"tuya/xxxxxxxxxxxxxxxxxxxx/xxxxxxxxxxxxxxxx/discover/command","action":"command","message":"OFF","options":{"id":"xxxxxxxxxxxxxxxxxxxx","key":"xxxxxxxxxxxxxxxx","ip":"discover"}} +8s
  TuyAPI:device Search device in network +8s
  TuyAPI IP and ID are already both resolved. +8s
  TuyAPI:device Device found in network +0ms
  TuyAPI Connecting to discover... +0ms
(node:120583) UnhandledPromiseRejectionWarning: Error: connection timed out
    at Socket.<anonymous> (/etc/openhab2/scripts/tuya-mqtt/node_modules/tuyapi/index.js:305:18)
    at Object.onceWrapper (events.js:298:28)
    at Socket.emit (events.js:209:13)
    at Socket._onTimeout (net.js:468:8)
    at listOnTimeout (internal/timers.js:531:17)
    at processTimers (internal/timers.js:475:7)
(node:120583) 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: 15)
  TuyAPI Socket closed: discover +5s
  TuyAPI:device Disconnected from device. undefined (discover, xxxxxxxxxxxxxxxxxxxx, xxxxxxxxxxxxxxxx) +5s
  TuyAPI:device delete Device undefined (discover, xxxxxxxxxxxxxxxxxxxx, xxxxxxxxxxxxxxxx) +0ms
  TuyAPI:mqtt:error { status: 'disconnect', device: null } +13s

However, you mentioned about the tuya-cli command, I did the get option and manged to receive the dps.

[root@telegramhost tuya-mqtt]# tuya-cli get  --id xxxxxxxxxxxxxxxxxxxx--key xxxxxxxxxxxxxxxx -a 
{
  devId: 'xxxxxxxxxxxxxxxxxxxx',
  dps: {
    '1': true,
    '2': 'scene',
    '3': 255,
    '4': 184,
    '5': 'f0f8ff00d00fff',
    '6': 'fffcf70168ffff',
    '7': 'ffff500100ff00',
    '8': 'ffff8003ff000000ff000000ff000000000000000000',
    '9': 'ffff5001ff0000',
    '10': 'ffff0505ff000000ff00ffff00ff00ff0000ff000000'
  }
}

Also managed to use it to control the bulb from the CLI, weird and good news at the same time, the tuya-cli set command succeeded to control the bulb!

tuya-cli set --set true --id xxxxxxxxxxxxxxxxxxxx --key xxxxxxxxxxxxxxxx

Any idea what I’m facing here, do you think the tuya repo I used has some issue like compatibility or missing thing on my machine?

I used this one which is mentioned in the post

Well, that’s part of the troubleshooting list I tried as mentioned in my comment. Same problem :pensive:

Use this version : https://github.com/tsightler/tuya-mqtt

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

I think your tuya devise use the new protocol v3.3 , you need to install the version of tsightler

2 Likes

I think your tuya devise use the new protocol v3.3 , you need to install the version of tsightler

You know what? You are the BEST! I owe you a fix :smile:

2 Likes

Sorry, late night I didn’t read your post properly… glad to hear you got it sorted!

1 Like

For anyone using Kogan light bulbs (not sure which model Tuya) you can control the brightness using dps 2. The warmth is dps3.

You don’t need to use transform you can use the outgoing format in the channel. I used a number type on the channel and set min to 0 and max to 255. I used a setpoint item in the sitemap as slider wouldn’t work with custom min and max values.

Looking for some help on this.
I can turn on off my plugs using
tuya-cli set --set false --id xxxx --ip 192.168.0.140 --key xxxxx --protocol-version 3.3

When adding to the channel it appears to connect to the plug and send the command but the plug doesn’t switch.
Using
DEBUG=* node tuya-mqtt
the debug logshows

 TuyAPI:mqtt receive settings {"topic":"tuya/socket/82180707c44f33879d0c/a9c357e5203f210c/192.168.0.140/command","action":"command","message":"ON","options":{"id":"82180707c44f33879d0c","key":"a9c357e5203f210c","ip":"192.168.0.140","type":"socket"}} +12s
  TuyAPI:mqtt receive command { set: true } +2ms
  TuyAPI:device set: { set: true } +12s
  TuyAPI SET Payload: +415ms
  TuyAPI { devId: '82180707c44f33879d0c',
  TuyAPI   gwId: '82180707c44f33879d0c',
  TuyAPI   uid: '',
  TuyAPI   t: 1568026720,
  TuyAPI   dps: { '1': true } } +1ms
  TuyAPI Pinging 192.168.0.140 +10s
  TuyAPI Received data: 000055aa00000000000000090000000c00000000b051ab030000aa55 +19ms
  TuyAPI Parsed: +1ms
  TuyAPI { payload: false, leftover: false, commandByte: 9, sequenceN: 0 } +0ms
  TuyAPI Pong from 192.168.0.140 +1ms
  TuyAPI Pinging 192.168.0.140 +10s
  TuyAPI Received data: 000055aa00000000000000090000000c00000000b051ab030000aa55 +14ms
  TuyAPI Parsed: +0ms
  TuyAPI { payload: false, leftover: false, commandByte: 9, sequenceN: 0 } +1ms
  TuyAPI Pong from 192.168.0.140 +0ms
  TuyAPI Pinging 192.168.0.140 +10s
  TuyAPI Received data: 000055aa00000000000000090000000c00000000b051ab030000aa55 +12ms
  TuyAPI Parsed: +1ms

Does anyone have any ideas?

try in MQTT :
tuya/ver3.3/82180707c44f33879d0c/a9c357e5203f210c/192.168.0.140/state
And
tuya/ver3.3/82180707c44f33879d0c/a9c357e5203f210c/192.168.0.140/command

On/open value = 1
Off/closed value = 0

and use this version of tuya mqtt : https://github.com/tsightler/tuya-mqtt

Thanks for the quick response. I had that tuya-mqtt version installed now passing the 3.3.
Not sure what failed with the first test. I just edited this post as it is working now.
Not sure what failed with the first test. Great advice.
I can now configure all my plugs.

Thanks Again.

yes the first time the MQTT command don’t work (same if you reboot OH2, first command don’t work) and after , it work :slight_smile:

Thanks,
I am still a littel confused about few things.

  1. The swtch doesn’t update when changed externally. How do you get it to map back. It works to send from the gui but not when changed externally. I can see the change in the debug mode of tuya-mqtt

  2. How do you configure in an items things/items file instead of Paper UI?

  3. How do you send a value or receive from one of the dps values?

Any help or pointing me to somewhere that explains would be great.

Hello, try with the DPS directly on the state :

tuya/<typeofprotocol>/<tuyaid>/<tuyakey>/dps/1

, or DPS/2 if you want the state of DPS number 2 …

hi how i can controle a wall switch of 3 gangs only turn on 1 gang

Thanks for the tutorial, I’m having some trouble finding the .ssh folder to save my public/private key to. In V 2.4, is is named something like “org.apche.karaf.shell.ssh”?

check here : https://help.github.com/en/github/authenticating-to-github/checking-for-existing-ssh-keys

Hi. I’ve installed the MQTT Binding (2.4.0) and the things that you said in the tutorial (MQTT Broker, Embedded MQTT and the Generic MQTT). Also, I’ve installed the tuyaaì_mqtt.js and it’s running.

In the other hand, I’ve installed a contact sensor in Tuya App and got the deviceId, the localKey and the local IP, and I’ve added the channel with the state and the command topic like that:

tuya/{deviceId}/{localKey}/{localIP}/

The problem is that OpenHAB doesn’t seem connect with my device.

What’s wrong? Have I forgotten anything?

Regards

what is the version of tuya_mqtt ?