Manage all Tuya devices with node-red
For the development of TuyaDAEMON I have collected a series of information that I believe may be of general interest.
Most Tuya-branded devices are controllable using the tuya-smart-device node. All you need is the
deviceID
, thelocalKey
, and knowing the DPs and behavior of the device.Some devices CANNOT be controlled locally with "tuya-smart-device" because they do not have a stable WiFi connection (typically battery-operated WiFi sensors). In these cases, you can use Tuya 'OpenAPI' via REST with node-red. Requires Tuya credentials and
deviceID
. Available as TuyaDAEMON extension and also as node-red library.OpenAPI can be used only in polling. It is not possible to use it for asynchronous events generated by devices (e.g. alarms). One solution is the HW/SW trigger mechanism (designed for TuyaDAEMON), which allows bidirectional communications between SmarlLife Automation and node-red. Available as TuyaDAEMON extension and also in lite version.
Many WiFi products without the Tuya logo are built by various manufacturers (Ikea, Philips, Siemens...) with their own APP, but were developed using Tuya. Some of these products CAN be managed with SmartLife. Unfortunately, this needs to be verified on a case-by-case basis (Tuya communication).
Even some ZigBee devices produced without Tuya compatibility can be associated with a ZigBee-Tuya hub (e.g. thermometers for Xiaomi's HomeKit APP).
A) how to find deviceID
, and localKey
:
deviceID
only: in SmartLife, Device Info- use Tuya IoT Development Platform for Tuya credentials
- use tuyapi_cli wizard
- use OpenAPI: 'Query Device Details' (preferred)
B) How to Find DPs
.
The simplest solution is to use the 'Send Properties' API (see here)
C) The study of the behavior and quirks of a device is greatly aided by the Log (max 7 days) present in 'Tuya Development Platform' (see here)
Caveat: Tuya OpenAPI and Automation are not very stable. Tuya occasionally changes the structure of Tuya Cloud; the latest change (June 20, 2023) made it necessary to update all Automation due to a change in the name of a condition. A change of deviceID also invalidates the Automation.
I hope this information helps someone
[{"id":"b10c24ea75862d85","type":"function","z":"4bf109a5.a97ad8","name":"","func":"var device_id = flow.get(\"device_id\");\nvar APIurl = \"/v2.0/cloud/thing/\"+device_id;\nvar t = msg.time;\nvar method = \"GET\";\n\nvar url = flow.get(\"data_center\");\nvar client_id = flow.get(\"tuya_client_id\");\nvar creds = flow.get(\"tuya\");\nvar token = creds.access_token;\nvar sign = msg.payload;\nmsg.headers ={\n \"sign_method\": \"HMAC-SHA256\",\n \"client_id\" : client_id,\n \"t\": t.toString(),\n \"mode\" : \"cors\",\n \"Content-Type\": \"application/json\",\n \"sign\": sign.toUpperCase(),\n \"access_token\" : token\n },\n msg.payload = '';\n msg.url = url + APIurl;\n msg.method = \"GET\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":460,"wires":[["040b82a0cc19a247"]]},{"id":"040b82a0cc19a247","type":"http request","z":"4bf109a5.a97ad8","name":"","method":"use","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":890,"y":460,"wires":[["e7785f1fe1518b25"]]},{"id":"05c3a0477b6f8587","type":"inject","z":"4bf109a5.a97ad8","name":"","props":[{"p":"time","v":"","vt":"date"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":55,"y":460,"wires":[["64c4f63b01ea3b48"]],"l":false},{"id":"64c4f63b01ea3b48","type":"function","z":"4bf109a5.a97ad8","name":"Create signStr","func":"var creds = flow.get(\"tuya\");\nvar access_token = creds.access_token;\nvar device_id = flow.get(\"device_id\");\nvar t = msg.time;\nvar client_id = flow.get(\"tuya_client_id\");\nvar token = access_token;\nvar method = \"GET\";\nvar sign_url = \"/v2.0/cloud/thing/\"+device_id;\nvar content_hash = \"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\";\nvar string_to_sign = method+\"\\n\"+content_hash+\"\\n\"+\"\"+\"\\n\"+sign_url;\nvar signStr = client_id+token+t+string_to_sign;\n\nmsg.payload = signStr;\nmsg.time = msg.time;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":260,"y":460,"wires":[["af49af968d68287e"]]},{"id":"60b80c53cd45f461","type":"comment","z":"4bf109a5.a97ad8","name":"Get Device Details V2.0 (Query_device_details)","info":"Get info about a device. \nYou get category, local_key, etc.\n\nnote:\n- To change the device, update the `device_id` in 'Settings: CLICK *On Start* TAB' node, and deploy.\n\n\nTuya API reference:\nhttps://developer.tuya.com/en/docs/cloud/3829469013?id=Kcp2l2v9wma0m","x":200,"y":420,"wires":[]},{"id":"af49af968d68287e","type":"hmac","z":"4bf109a5.a97ad8","name":"Sign signStr with secret","algorithm":"HmacSHA256","key":"24e1bd0159c84246a5deff4095630c99","x":490,"y":460,"wires":[["b10c24ea75862d85"]]},{"id":"e7785f1fe1518b25","type":"debug","z":"4bf109a5.a97ad8","name":"debug 10","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1140,"y":460,"wires":[]}]