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

All good.

I randomly stumbled back across this thread after shelving this project last month.

I installed the most current and it seems good.

Hi everyone,
I recently accidentally upgraded my smart plugs from Costco (CE Smart home smart plug) from version 0.1.3 to version 1.0.9 and it failed to connect to openhab 2.4. So I searched and almost gave up hope, thinking may be I needed to hack with Tasmota. Since these plugs are cheaply made with glue in, it makes the job very difficult. So I read this guide again and found the upgrade from tsightler. After many days of trial and error, I finally were able to make it work. Thank you for the great work from @Fiftiz and @tsightler.

It took me a while to figure out a series steps of upgrading tuya-mqtt to version 3.3 protocal and here belows are slightly modified steps from the original post:

To start with, I assumed you already have openhab 2.4 running AgentK tuya-mqtt node and the following is simply showing how to upgrade:

Step 1:
Remove old version of tuya-mqtt:
cd /etc/openhab2/scripts
sudo rm -rf tuyaapi_mqtt

Step 2:
Get version 3.3
sudo git clone https://github.com/tsightler/tuya-mqtt.git
cd tuya-mqtt
sudo npm install

Step 3:
sudo cp config.json.sample config.json

Step 4:
Modify node service assuming you already have that with the old version, you will need to modify the file /lib/systemd/system/tuyaapi-mqtt.service as the tuya-mqtt.js is in the different folder (/etc/openhab2/scripts/tuya-mqtt/tuya-mqtt.js). Please make a careful inspection as version 3.3 have a differently directory name as compare with the one from @AgentK .

Step 5:
Kill the old service.
sudo ps aux | grep node
sudo kill -9 process_id

Step 6:
reload service.
sudo systemctl daemon-reload
sudo systemctl enable tuyaapi-mqtt.service
sudo systemctl start tuyaapi-mqtt.service

Step 7:
modify your device state and command according @Fiftiz

Step 8:
restart openhab2
sudo systemctl restart openhab2.service

That should do it. You may have to redo Step 8 and Step 5 twice to register your devices.

Hi guys,

I do not know if I have read correctly, but is it now possible to change the dps values? in my case: temperature of the thermostat (dps: 2), for example, set to the value of 250? is that possible ? if yes how? I hope someone can help me. Thanks in advance and sorry for my bad english

Hoi Rik,

Tegen welke problemen liep jij aan?

Ik krijg het niet voor elkaar om de action led-lamp zelf te besturen.
Heb veel data afgevangen, tijdens actieveren en aanmelden. Dus heb de ids en de keys.
Wat ik zie ik dat de data vanuit het script plaintext wordt verzonden en niet versleuteld. Weet niet of dat het probleem is?
Zie data naar lamp en een antwoord van de lamp. Maar bij het verzenden van het id zie ik dat het nogmaals wordt verzonden maar geen response vanuit de lamp.

Groet,
Kees

I have add dps 2 and dps 3 in the sample on.js. 2 is for the dimming and 3 the color (between the two pair leds). With njstuya you can do the same I gues. (only njstuya will not work on my ubuntu)

device.get({schema: true}).then(data => {
console.log(data);
});
let status = await device.get();
console.log(Current status: ${status}.);

await device.set( { dps:‘1’, set: true });
await device.set( { dps:‘2’, set: 200});
await device.set( { dps:‘3’, set: 0});
status = await device.get();

node on.js
{ devId: ‘01408602840d8e220e95’, dps: { ‘1’: true, ‘2’: 100, ‘3’: 0 } }
Current status: true.
New status: true.

Thank you to you.
I have been stuck for weeks.

I am using Wi-fi switch : https://www.aliexpress.com/item/33001282012.html?spm=a2g0s.9042311.0.0.2c594c4dHWKc2t

Once, I applied new script for version 3.3 now my switch now work perfect for me!

1 Like

Hi,

I’m after a little help if possible.

I have obtained the key and ID from some light globes.

Installed the latest version of tsightler tuya-mqtt and setup the service. I’ve installed the generic broker and added the thing, I’ve added a thing for generic MQTT and bridged to the broker.

I have added in a channel for an on off switch and added the appropriately formatted topic for command and state however I am unable to control the bulb.

If I use a MQTT client and subscribe to the state, it does not see any changes in the globe nor can I push commands using a MQTT client.

Am I missing a step here? I am a bit lost with how to troubleshoot this as well.

Thanks in advance

edit: even if I subscribe to the generic MQTT thing channel that is linked to a switch item, toggling the switch item doesn’t not reflect in a subscription?

I am very new to MQTT so please be gentle

edit: running the tuya-mqtt script manually rather than the service has worked. I can control the lights with the switch item created and linked. Will have to work out why the service isn’t working. cheers

edit: the file path in the system had changed per the steps, I just have no attention to detail and missed the underscore changing to a dash. I’m all up and running, thanks

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: