Skip to content

Commit 44d426a

Browse files
authored
Tuya integration. Updated guide (#2572)
* Updated guide for Tuya integration * Updated Uplink and Downlink converters
1 parent b15f982 commit 44d426a

34 files changed

Lines changed: 122 additions & 353 deletions

_includes/docs/pe/user-guide/integrations/tuya.md

Lines changed: 77 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -3,226 +3,169 @@
33
* TOC
44
{:toc}
55

6-
## Overview
6+
Tuya is an IoT platform that enables device manufacturers and solution providers to connect, manage, and control smart devices through the Tuya Cloud.
77

8-
Tuya is a cloud platform that connects a range of devices via the IoT. After integrating Tuya with the ThingsBoard, you can connect, manage, communicate, process and visualize data from your devices in the ThingsBoard IoT platform.
8+
The Tuya Cloud Integration in ThingsBoard allows you to:
9+
- Connect Tuya-managed devices to ThingsBoard
10+
- Receive telemetry and device state updates
11+
- Send control commands from ThingsBoard to Tuya devices
12+
- Visualize device data and manage devices centrally
913

10-
## Tuya Integration Tutorial
14+
This guide describes how to configure an end-to-end integration using a real device - **Tuya Smart Plug**.
15+
Also, you can use a [virtual Tuya device](https://developer.tuya.com/en/docs/iot/manage-virtual-devices?id=Ka4725tiyfhg0){:target="_blank"} for testing.
1116

12-
In this tutorial we will use a real device - Smart Plug.
17+
## Prerequisites
1318

14-
Also, you can use a [**virtual device**](https://developer.tuya.com/en/docs/iot/manage-virtual-devices?id=Ka4725tiyfhg0). Virtual devices help you to perform cloud development without an actual IoT device.
19+
Before starting, ensure you have:
20+
- A ThingsBoard account with permission to create integrations
21+
- A Tuya IoT Platform account
22+
- At least one Tuya device (physical or virtual)
23+
- Tuya Smart App installed and logged in
1524

16-
### Prerequisites
25+
## Install smart device control application
1726

1827
The first step is to install the smart device control application (Smart Life, Tuya Smart, or other) on your mobile device and register your Smart Plug device in the application.
1928

2029
{% include images-gallery.html imageCollection="tuya-application-add-device" %}
2130

22-
### Tuya setup
31+
## Tuya cloud configuration
2332

24-
#### Create cloud project
33+
### Create cloud project
2534

26-
The next step is to register an account on [Tuya](https://www.tuya.com/) and create cloud project.
35+
The next step is to register an account on [Tuya](https://www.tuya.com/){:target="_blank"} and create cloud project.
36+
- Go to the **Cloud** tab ⇾ **Project Management**. Click the **Create Cloud Project** button.
37+
- In a pop-up window, fill required fields and click "Create".
38+
- Make additional settings in the **Authorize API Services** window and click **Authorize**.
39+
- Now your cloud project is created. In this window, remember the **Access ID/Client ID** and **Access Secret/Client Secret** values. These values will be needed during the Tuya Integration setup.
2740

28-
{% include images-gallery.html imageCollection="tuya-create-cloud-project" showListImageTitles="true" %}
29-
30-
#### Enable Tuya message service
41+
### Enable Tuya message service
3142

3243
Enable the message service to timely receive messages about device registration, data reporting, and status change.
44+
- Go to the **Message Service** tab.
45+
- Toggle to enable Message Service.
46+
- In the pop-up window, set up the messaging service. Configure the settings for **Message Service Type** and **Alert Contact**. Click **Ok**.
47+
- Message Service enabled.
3348

34-
{% include images-gallery.html imageCollection="tuya-message-service-enable" showListImageTitles="true" %}
35-
36-
#### Link Tuya App Account
49+
### Link Tuya App account
3750

3851
You need to link your devices to this project using your Smart Life app account.
52+
- Go to **Cloud** tab -> **Project management**. Select your project.
53+
- Navigate to the **Devices** tab -> select the **Link App Account** tab. Click **Add App Account**.
54+
- Select Tuya Account Authorization option.
55+
- Scan the QR code with Smart Life App to authorize.
56+
- In the pop-up window, select Automatic Link and Click **Ok**.
57+
- Now your devices under the mobile app account have been added to the project.
58+
- Navigate to the "All Devices" tab. You can see your device added to the project.
3959

40-
{% include images-gallery.html imageCollection="tuya-add-smart-life-app" showListImageTitles="true" %}
41-
42-
<br>
43-
**Note:**
44-
<br>
45-
Make sure you enabled Messaging rule (filter) to receive uplinks. The very basic filter (statusReport) should be sufficient for testing purposes
60+
Make sure you enabled Messaging rule (filter) to receive uplinks. The very basic filter (statusReport) should be sufficient for testing purposes:
4661

4762
{% include images-gallery.html imageCollection="tuya-enable-rules-environment" showListImageTitles="true" %}
4863

49-
## Tuya Integration Configuration
64+
## ThingsBoard integration configuration
5065

51-
### Uplink Converter
66+
Let&#39;s move on to setting up the integration between the ThingsBoard platform and Tuya.
5267

53-
Before setting up a **Tuya integration**, you need to create an **Uplink Converter** which is a script for parsing and transforming the data received by Tuya integration to a format that ThingsBoard can consume.
68+
<b><font size="4">1. Basic settings</font></b>
5469

55-
To create an **Uplink Converter**, go to the **Data Converters** section and click **Add new data converter —> Create new converter**, name it **"Tuya Uplink Converter"** and select type **Uplink**. Use debug mode for now.
70+
- Log in to your ThingsBoard account.
71+
- Go to the **Integrations center** &#8702; **Integrations**.
72+
- Click **+** (**plus**) button.
73+
- From the list, select **Tuya** integration type.
74+
- If you&#39;d like to monitor events and troubleshoot, enable **debug mode**.
75+
> Enabling debug mode allows you to track events, states, and potential errors related to the execution of . This greatly simplifies development and troubleshooting.
76+
- Click **Next**.
5677

57-
{% assign feature = "integrations" %}{% include templates/debug-mode.md %}
78+
<b><font size="4">2. Uplink data converter</font></b>
5879

59-
**Choose device payload type to for decoder configuration:**
80+
Uplink is necessary in order to convert the incoming data from the device into the required format for displaying them in ThingsBoard.
81+
- Enter a name for the converter. It must be unique.
82+
- To view the events, enable debug mode.
83+
- In the **Main decoding configuration** section, provide your own script or use the script below.
6084

6185
{% include templates/tbel-vs-js.md %}
6286

6387
{% capture tuyauplink %}
6488
TBEL<small>Recommended</small>%,%accessToken%,%templates/integration/tuya/tuya-uplink-tbel.md%br%
6589
JavaScript<small></small>%,%anonymous%,%templates/integration/tuya/tuya-uplink-java.md{% endcapture %}
66-
6790
{% include content-toggle.liquid content-toggle-id="tuyauplink" toggle-spec=tuyauplink %}
6891

69-
### Downlink Converter
92+
- Once the uplink converter is set up, click **Next**.
7093

71-
The Downlink converter transforming outgoing RPC message and then the Integration sends it to your device.
94+
<b><font size="4">3. Downlink data converter</font></b>
7295

73-
Create another converter with the name **"Tuya Downlink Converter"** and type **Downlink**. To see events - enable Debug.
96+
The Downlink converter transforming outgoing RPC message and then the Integration sends it to your device.
97+
You can use our example of Downlink Converter, or write your own according to your configuration:
7498

7599
{% capture tuyadownlink %}
76100
TBEL<small>Recommended</small>%,%accessToken%,%templates/integration/tuya/tuya-downlink-tbel.md%br%
77101
JavaScript<small></small>%,%anonymous%,%templates/integration/tuya/tuya-downlink-java.md{% endcapture %}
78-
79102
{% include content-toggle.liquid content-toggle-id="tuyadownlink" toggle-spec=tuyadownlink %}
80103

81-
### Tuya Integration Setup
104+
- Click **Next**.
82105

83-
Go to **Integrations** section and click Add new integration button. Name it **Tuya Integration**, select type **Tuya**;
84-
85-
{% if docsPrefix == "pe/" %}
86-
![image](/images/user-guide/integrations/tuya/tuya-create-integration-1-pe.png)
87-
{% endif %}
88-
{% if docsPrefix contains "paas/" %}
89-
![image](/images/user-guide/integrations/tuya/tuya-create-integration-1-pe.png)
90-
{% endif %}
91-
92-
In this step, you can select the recently created **Tuya Uplink Converter** or create a new uplink data converter;
93-
94-
{% if docsPrefix == "pe/" %}
95-
![image](/images/user-guide/integrations/tuya/tuya-create-integration-2-pe.png)
96-
{% endif %}
97-
{% if docsPrefix contains "paas/" %}
98-
![image](/images/user-guide/integrations/tuya/tuya-create-integration-2-pe.png)
99-
{% endif %}
100-
101-
Add **Tuya Downlink Converter** to the integration or create a new downlink data converter;
102-
103-
{% if docsPrefix == "pe/" %}
104-
![image](/images/user-guide/integrations/tuya/tuya-create-integration-3-pe.png)
105-
{% endif %}
106-
{% if docsPrefix contains "paas/" %}
107-
![image](/images/user-guide/integrations/tuya/tuya-create-integration-3-pe.png)
108-
{% endif %}
106+
<b><font size="4">4. Connection</font></b>
109107

110108
In the last step, fill in the following fields:
111-
112109
- **Region** - specify your region;
110+
- **Environment**:
111+
- Choose **PROD** for real devices.
112+
- Select **TEST** if you want to connect a **virtual device** to Thingsboard and test its operation before you buy it.
113+
- **Access Id** and **Access Key** is an authorization certificate distributed by Tuya. Paste [previously copied Access Id and Access Key](#create-cloud-project) into the integration.
114+
- Click **Add** to create an integration.
113115

114-
- **Environment:** choose **PROD** or **TEST**. Choose **PROD** for real devices. Select **TEST** if you want to connect a [**virtual device**](https://developer.tuya.com/en/docs/iot/manage-virtual-devices?id=Ka4725tiyfhg0) to Thingsboard and test its operation before you buy it.
115-
116-
- **Access Id** and **Access Key** is an authorization certificate distributed by Tuya. Paste previously copied **Access Id** and **Access Key** into the integration.
117-
118-
Click "Add" to create an integration.
119-
120-
{% if docsPrefix == "pe/" %}
121-
![image](/images/user-guide/integrations/tuya/tuya-create-integration-4-pe.png)
122-
{% endif %}
123-
{% if docsPrefix contains "paas/" %}
124-
![image](/images/user-guide/integrations/tuya/tuya-create-integration-4-pe.png)
125-
{% endif %}
126-
127-
### Rule Chain configuration
116+
## Rule Chain configuration
128117

129118
When integration configured and ready to use, we need to go to **Rule Chains**, choose **"Root Rule Chain"** and here create rule node **Integration Downlink**. Input some name here, choose earlier created Tuya integration, and tap **Add**.
130119

131-
{% if docsPrefix == "pe/" %}
132-
![image](/images/user-guide/integrations/tuya/tuya-rule-chain-downlink-1-pe.png)
133-
{% endif %}
134-
{% if docsPrefix contains "paas/" %}
135120
![image](/images/user-guide/integrations/tuya/tuya-rule-chain-downlink-1-pe.png)
136-
{% endif %}
137121

138-
After these steps, we need to tap on a right grey circle of rule node **message type switch** and drag this circle to left side of **Integration Downlink**. In pop-up window add **"RPC Request to Device"** linl, and tap "Add". Save the Root Rule Chain.
122+
After these steps, we need to tap on a right grey circle of rule node **message type switch** and drag this circle to left side of **Integration Downlink**. In pop-up window add **"RPC Request to Device"** link, and tap **Add**. **Save** the Root Rule Chain.
139123

140-
{% if docsPrefix == "pe/" %}
141124
![image](/images/user-guide/integrations/tuya/tuya-rule-chain-downlink-2-pe.png)
142-
{% endif %}
143-
{% if docsPrefix contains "paas/" %}
144-
![image](/images/user-guide/integrations/tuya/tuya-rule-chain-downlink-2-pe.png)
145-
{% endif %}
146125

147-
### Uplink message
126+
## Uplink message
148127

149-
Once ThingsBoard **Tuya Integration** has been created, you must disconnect Smart Plug from power and reconnect. The device will send an uplink message with telemetry and attributes to the integration
128+
Once ThingsBoard Tuya Integration has been created, you must disconnect Smart Plug from power and reconnect. The device will send an uplink message with telemetry and attributes to the integration.
150129

151-
Go to **Device Groups** -> **All** you should find your device provisioned by the Integration. In my case it is - **SmartPlug268970**.
130+
Go to **Entities &#8702; Devices**. You should find your device provisioned by the Integration.
152131

153-
{% if docsPrefix == "pe/" %}
154-
![image](/images/user-guide/integrations/tuya/tuya-integration-create-device-pe.png)
155-
{% endif %}
156-
{% if docsPrefix contains "paas/" %}
157-
![image](/images/user-guide/integrations/tuya/tuya-integration-create-device-pe.png)
158-
{% endif %}
132+
In my case it is - **SmartPlug268970**.
159133

160-
### Tuya Smart Plug Dashboard
134+
## Tuya Smart Plug Dashboard
161135

162136
To visualize the Smart Plug data and test RPC commands, we will create the **Tuya Smart Plug** dashboard.
137+
- Download the [tuya_smart_plug_dashboard.json](/docs/user-guide/resources/tuya_smart_plug_dashboard.json){:target="_blank" download="tuya_smart_plug_dashboard.json"} file.
138+
- Go to the **Dashboards**.
139+
- Click the **+** (**plus**) icon at the upper right corner of the table, and select **Import dashboard**.
140+
- Drag the previously downloaded JSON file into the window.
141+
- Tap **Import**.
163142

164-
- Download the [**tuya_smart_plug_dashboard.json**](/docs/user-guide/resources/tuya_smart_plug_dashboard.json) file
165-
- Go to the **Dashboard groups** tab. Create dashboard group - **Smart Plug** and go to it.
166-
- To import this JSON file, click the `import` button at the upper right corner of the dashboard group page and drag the previously downloaded file into the window. Tap **Import**.
167-
168-
{% if docsPrefix == "pe/" %}
169-
![image](/images/user-guide/integrations/tuya/tuya-dashboard-1-pe.png)
170-
{% endif %}
171-
{% if docsPrefix contains "paas/" %}
172143
![image](/images/user-guide/integrations/tuya/tuya-dashboard-1-pe.png)
173-
{% endif %}
174144

175-
- Open the **Tuya Smart Plug** dashboard
176-
- **Enter edit mode**, click **Entity aliases** button and add your device to **smartPlug** alias
145+
- Open the **Tuya Smart Plug** dashboard.
146+
- Enter **Edit mode**, and update **Entity aliases** - specify your device as target device in the **smartPlug** alias
177147

178-
{% if docsPrefix == "pe/" %}
179148
![image](/images/user-guide/integrations/tuya/tuya-alias-1-pe.png)
180-
{% endif %}
181-
{% if docsPrefix contains "paas/" %}
182-
![image](/images/user-guide/integrations/tuya/tuya-alias-1-pe.png)
183-
{% endif %}
184149

185150
{% capture difference %}
186-
**NOTE:**
187-
<br>
188-
time series data keys of your device may differ from those presented. If necessary, you will need to replace them, according to the documentation for your device (for each widget).
151+
**NOTE:** Time series data keys of your device may differ from those presented. If necessary, you will need to replace them, according to the documentation for your device (for each widget).
189152
{% endcapture %}
190153
{% include templates/info-banner.md content=difference %}
191154

192-
{% if docsPrefix == "pe/" %}
193-
![image](/images/user-guide/integrations/tuya/tuya-edit-timeseries-data-keys-pe.png)
194-
{% endif %}
195-
{% if docsPrefix contains "paas/" %}
196155
![image](/images/user-guide/integrations/tuya/tuya-edit-timeseries-data-keys-pe.png)
197-
{% endif %}
198156

199157
<br>
200158
If you have everything configured correctly, you will see Smart Plug status light (on/off) and telemetry for the last hour: voltage, power, and current.
201159

202-
{% if docsPrefix == "pe/" %}
203160
![image](/images/user-guide/integrations/tuya/tuya-dashboard-2-pe.png)
204-
{% endif %}
205-
{% if docsPrefix contains "paas/" %}
206-
![image](/images/user-guide/integrations/tuya/tuya-dashboard-2-pe.png)
207-
{% endif %}
208161

209162
Smart Plug status light is green. Try to switch off the Smart Plug by clicking on the **On/Off Round switch**
210163

211-
{% if docsPrefix == "pe/" %}
212-
![image](/images/user-guide/integrations/tuya/tuya-dashboard-3-pe.png)
213-
{% endif %}
214-
{% if docsPrefix contains "paas/" %}
215164
![image](/images/user-guide/integrations/tuya/tuya-dashboard-3-pe.png)
216-
{% endif %}
217165

218166
The Smart Plug status indicator turns grey. Power consumption stops.
219167

220-
{% if docsPrefix == "pe/" %}
221-
![image](/images/user-guide/integrations/tuya/tuya-dashboard-4-pe.png)
222-
{% endif %}
223-
{% if docsPrefix contains "paas/" %}
224168
![image](/images/user-guide/integrations/tuya/tuya-dashboard-4-pe.png)
225-
{% endif %}
226169

227170
## Next steps
228171

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,20 @@
1-
You can use our example of Downlink Converter, or write your own according to your configuration:
2-
31
```javascript
42
/** Encoder **/
53

6-
var command = {};
7-
command["code"] = msg.method;
8-
if (msg.params == "false" || msg.params == "true") {
9-
command["value"] = Boolean.valueOf(msg.params);
10-
} else {
11-
command["value"] = msg.params;
12-
}
13-
14-
var result = {
15-
16-
// downlink data content type: JSON, TEXT or BINARY (base64 format)
4+
const command = {
5+
code: msg.method,
6+
value:
7+
msg.params === "false" || msg.params === "true"
8+
? msg.params === "true"
9+
: msg.params
10+
};
11+
const result = {
1712
contentType: "JSON",
18-
19-
// downlink data
2013
data: JSON.stringify(command),
21-
22-
// Optional metadata object presented in key/value format
2314
metadata: {
24-
deviceId: metadata.deviceName
15+
deviceId: metadata.deviceName
2516
}
26-
2717
};
28-
2918
return result;
3019
```
31-
{: .copy-code}
32-
33-
{% if docsPrefix == "pe/" %}
34-
![image](/images/user-guide/integrations/tuya/tuya-create-downlink-converter-java-pe.png)
35-
{% endif %}
36-
{% if docsPrefix contains "paas/" %}
37-
![image](/images/user-guide/integrations/tuya/tuya-create-downlink-converter-java-pe.png)
38-
{% endif %}
20+
{: .copy-code}

_includes/templates/integration/tuya/tuya-downlink-tbel.md

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
You can use our example of Downlink Converter, or write your own according to your configuration:
2-
31
```javascript
42
/** Encoder **/
53

@@ -21,18 +19,11 @@ var result = {
2119

2220
// Optional metadata object presented in key/value format
2321
metadata: {
24-
deviceId: metadata.deviceName
22+
deviceId: metadata.deviceName
2523
}
2624

2725
};
2826

2927
return result;
3028
```
31-
{: .copy-code}
32-
33-
{% if docsPrefix == "pe/" %}
34-
![image](/images/user-guide/integrations/tuya/tuya-create-downlink-converter-tbel-pe.png)
35-
{% endif %}
36-
{% if docsPrefix contains "paas/" %}
37-
![image](/images/user-guide/integrations/tuya/tuya-create-downlink-converter-tbel-pe.png)
38-
{% endif %}
29+
{: .copy-code}

0 commit comments

Comments
 (0)