vinodsr / node-red-contrib-tuya-smart-device

a node-red module for managing tuya smart devices
https://flows.nodered.org/node/node-red-contrib-tuya-smart-device
MIT License
47 stars 15 forks source link

Device connects, then disconnects; #106

Closed Saimond closed 1 year ago

Saimond commented 1 year ago

Hello;

I recently purchased 10 Switch Wifi devices;

I'm linking with NodeRED, but it's generating the following situation:

Follows device information on Tuya API - Get Device Information

{
  "result": {
    "active_time": 1672428322,
    "category": "tdq",
    "category_name": "Breaker",
    "create_time": 1672428322,
    "gateway_id": "",
    "icon": "smart/icon/ay1562830083757AoBrF/aeb390315391a67810556b3a5343310b.jpg",
    "id": "******",
    "ip": "",
    "lat": "",
    "local_key": "********",
    "lon": "",
    "model": "MYK",
    "name": "Luz",
    "online": true,
    "owner_id": "66962993",
    "product_id": "gdknjvdpiwoq6smx",
    "product_name": "Smart Switch Module",
    "sub": false,
    "time_zone": "-03:00",
    "update_time": 1672428338,
    "uuid": "d976071234a8b9c4"
  },
  "success": true,
  "t": 1672432081880,
  "tid": "74f20516888011edb95a32baeb959352"
}

Here's the debug log on nodered

30 Dec 17:40:58 - [info] [tuya-smart-device:Ambiente - Luz] Connected to device! ******
30 Dec 17:41:08 - [info] [tuya-smart-device:Ambiente - Luz] Disconnected from tuyaDevice. shouldTryReconnect = true
30 Dec 17:41:08 - [info] [tuya-smart-device:Ambiente - Luz] Will try to reconnect after 10000 milliseconds
30 Dec 17:41:18 - [info] [tuya-smart-device:Ambiente - Luz] Retrying connection...
30 Dec 17:41:18 - [info] [tuya-smart-device:Ambiente - Luz] Connected to device! ******
30 Dec 17:41:29 - [info] [tuya-smart-device:Ambiente - Luz] Disconnected from tuyaDevice. shouldTryReconnect = true
30 Dec 17:41:29 - [info] [tuya-smart-device:Ambiente - Luz] Will try to reconnect after 10000 milliseconds
30 Dec 17:41:39 - [info] [tuya-smart-device:Ambiente - Luz] Retrying connection...
30 Dec 17:41:39 - [info] [tuya-smart-device:Ambiente - Luz] Connected to device! ******
30 Dec 17:41:49 - [info] [tuya-smart-device:Ambiente - Luz] Disconnected from tuyaDevice. shouldTryReconnect = true
30 Dec 17:41:49 - [info] [tuya-smart-device:Ambiente - Luz] Will try to reconnect after 10000 milliseconds
30 Dec 17:41:59 - [info] [tuya-smart-device:Ambiente - Luz] Retrying connection...
30 Dec 17:41:59 - [info] [tuya-smart-device:Ambiente - Luz] Connected to device! ******
30 Dec 17:42:09 - [info] [tuya-smart-device:Ambiente - Luz] Disconnected from tuyaDevice. shouldTryReconnect = true
30 Dec 17:42:09 - [info] [tuya-smart-device:Ambiente - Luz] Will try to reconnect after 10000 milliseconds
30 Dec 17:42:19 - [info] [tuya-smart-device:Ambiente - Luz] Retrying connection...
30 Dec 17:42:19 - [info] [tuya-smart-device:Ambiente - Luz] Connected to device! ******
30 Dec 17:42:29 - [info] [tuya-smart-device:Ambiente - Luz] Disconnected from tuyaDevice. shouldTryReconnect = true
30 Dec 17:42:29 - [info] [tuya-smart-device:Ambiente - Luz] Will try to reconnect after 10000 milliseconds
30 Dec 17:42:39 - [info] [tuya-smart-device:Ambiente - Luz] Retrying connection...
30 Dec 17:42:39 - [info] [tuya-smart-device:Ambiente - Luz] Connected to device! ******
30 Dec 17:42:49 - [info] [tuya-smart-device:Ambiente - Luz] Disconnected from tuyaDevice. shouldTryReconnect = true
30 Dec 17:42:49 - [info] [tuya-smart-device:Ambiente - Luz] Will try to reconnect after 10000 milliseconds

Device:

At the end of the configuration, I have the field of:

Versions:

NOTE:

A1bi commented 1 year ago

I saw the same behaviour with my device I bought just today. It disconnected constantly and no request resulted in any response. By trial and error I found out that protocol version 3.4 fixes the issue. All communication seems to work now and no more disconnects. Unfortunately node-red-contrib-tuya-smart-device hasn't been updated to TuyAPI version 7.5 yet which introduces support for protocol version 3.4. Hence I wasn't able to fix it in Node-RED, but my tests with tuya-cli were successful.

iamhermes commented 1 year ago

same behaviour:, connect, disconnect, connect... without status data

working solution for me (change to latest TuyAPI):

replaced existing node_modules/tuyapi files with latest versions from TuyAPI (copy/replacing, lib folder and index.js + package.json)

Saimond commented 1 year ago

ok thanks for the answers...

after I posted it, I went after some solutions, I found tinytuya for python...

setting the device with version 3.4, it worked, so really, the problem is that you need to update to protocol 3.4...

I'm going to update node_modules/tuyapi, so I believe it will work perfectly...

Thanks!.

iamhermes commented 1 year ago

Issue closed to early, this nodered plugin needs to be adjusted. In the package.json file the dependency is tuyapi 7.3.0 (with exact version 7.3.0) I think it should adjusted to greater or equal 7.5.1 (>=7.5.1 for future updates in tuyapi) to provide tuya 3.4 protocol to everyone

My nodered reboot in this day has broken my copied over files to temporary fix this.

vinodsr commented 1 year ago

I will try to update the version asap

VVerlog commented 1 year ago

Hello vinodsr,

do you plan an update to the tuyapi 7.5.1? thanks br. JR

vinodsr commented 1 year ago

Merged the changes with PR . #110

Changes are now available with V5.1.0

mrmaximas commented 4 months ago

@vinodsr how about update tuya api to 7.5.2 https://www.npmjs.com/package/tuyapi ??

mrmaximas commented 4 months ago

@vinodsr I have several tuya devices. Some of them have the same problem. All of my devices have excellent wifi signal. All of my devs use protocol version 3.3. After initial connection all devices are detected and I receive data from them (e.g. voltage from sockets or humidity from HT sensor), after time (5-10-20 mins) some devices fail with this errors.

8 Jul 00:04:22 - [info] [tuya-smart-device:HOME OUTDOOR HT] Disconnected from tuyaDevice. shouldTryReconnect = true
8 Jul 00:04:23 - [info] [tuya-smart-device:Home Snaige] Retrying connection...
8 Jul 00:04:23 - [info] [tuya-smart-device:Home Snaige] Connected to device! name : Home Snaige, ip : 192.168.1.191
8 Jul 00:04:23 - [error] [tuya-smart-device:Home Snaige] Error from tuyaDevice. shouldTryReconnect = true, error  = {}
8 Jul 00:04:23 - [error] [tuya-smart-device:Home Snaige] Error: Error from socket: read ECONNRESET
    at Socket.<anonymous> (/Users/m/.node-red/node_modules/tuyapi/index.js:627:26)
    at Socket.emit (node:events:519:28)
    at emitErrorNT (node:internal/streams/destroy:169:8)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
8 Jul 00:04:23 - [info] [tuya-smart-device:Home Snaige] Disconnected from tuyaDevice. shouldTryReconnect = true
8 Jul 00:04:32 - [error] [json:d2ffc2c8.f52098] Unexpected token '<', "<!DOCTYPE "... is not valid JSON
8 Jul 00:04:32 - [info] [tuya-smart-device:HOME OUTDOOR HT] Retrying connection...
8 Jul 00:04:32 - [info] [tuya-smart-device:HOME OUTDOOR HT] Connected to device! name : HOME OUTDOOR HT, ip : 192.168.1.209
8 Jul 00:04:32 - [error] [tuya-smart-device:HOME OUTDOOR HT] Error from tuyaDevice. shouldTryReconnect = true, error  = {}
8 Jul 00:04:32 - [error] [tuya-smart-device:HOME OUTDOOR HT] Error: Error from socket: read ECONNRESET
    at Socket.<anonymous> (/Users/m/.node-red/node_modules/tuyapi/index.js:627:26)
    at Socket.emit (node:events:519:28)
    at emitErrorNT (node:internal/streams/destroy:169:8)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
8 Jul 00:04:32 - [info] [tuya-smart-device:HOME OUTDOOR HT] Disconnected from tuyaDevice. shouldTryReconnect = true
8 Jul 00:04:33 - [info] [tuya-smart-device:Home Snaige] Retrying connection...
8 Jul 00:04:34 - [info] [tuya-smart-device:Home Snaige] Connected to device! name : Home Snaige, ip : 192.168.1.191
8 Jul 00:04:34 - [error] [tuya-smart-device:Home Snaige] Error from tuyaDevice. shouldTryReconnect = true, error  = {}
8 Jul 00:04:34 - [error] [tuya-smart-device:Home Snaige] Error: Error from socket: read ECONNRESET
    at Socket.<anonymous> (/Users/m/.node-red/node_modules/tuyapi/index.js:627:26)
    at Socket.emit (node:events:519:28)
    at emitErrorNT (node:internal/streams/destroy:169:8)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
8 Jul 00:04:34 - [info] [tuya-smart-device:Home Snaige] Disconnected from tuyaDevice. shouldTryReconnect = true
8 Jul 00:04:42 - [info] [tuya-smart-device:HOME OUTDOOR HT] Retrying connection...
8 Jul 00:04:42 - [info] [tuya-smart-device:HOME OUTDOOR HT] Connected to device! name : HOME OUTDOOR HT, ip : 192.168.1.209
8 Jul 00:04:42 - [error] [tuya-smart-device:HOME OUTDOOR HT] Error from tuyaDevice. shouldTryReconnect = true, error  = {}
8 Jul 00:04:42 - [error] [tuya-smart-device:HOME OUTDOOR HT] Error: Error from socket: read ECONNRESET
    at Socket.<anonymous> (/Users/m/.node-red/node_modules/tuyapi/index.js:627:26)
    at Socket.emit (node:events:519:28)
    at emitErrorNT (node:internal/streams/destroy:169:8)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

It can reconnect them only by switching off the power or wi-fi. What to do?

vinodsr commented 4 months ago

"dependencies": { "tuyapi": "~7.5.1" },

patches are auto included as per the package json. a reinstall may add the latest tuya cli