Skip to content

How to obtain Device ID and Local Key

Andre Lucca edited this page Mar 25, 2023 · 7 revisions

Attention!

This is a crucial step to make the plugin communicate with Tuya devices, I made this tutorial entirely from scratch based on gathered info across the internet, especially this Tutorial from codetheweb.
This tutorial requires a Developer Account on Tuya IoT Cloud Service, I'll guide on how to set this up and don't worry because this account is totally free.
There are another method to obtain this info, but I couldn't make it work, and its based on packet sniffing using an Android phone and a third-part app that work with Tuya. I didn't like this method because you can't use the official Tuya app or Smart Life, and if you remove the device from the app its Local Key changes. But if you want to try and manage to get it working here's the Tutorial

Before you start

Devices compatibility

I have a fair amount of Tuya devices at my home, but they are a mix of brands and specially a mix of older ones and new ones.
Before jumping in finding the required information I must say that some newer devices with specific MCU version worked better than the older ones, so to check this out you must go to your Tuya/Smart Life app, go to your device, then click on the right upper corner in the pencil logo, and go to the Device Update (images below on where to find it).
There you'll see the MCU Module version. I found that devices with version 1.1.7 or lower are not that reliable with the plugin and tend to disconnect from it when you try to interact. The same doesn't happen with the MCU version 1.3.0 or above, of course this is just my personal experience and if you manage to find some different results let the community know in the Discussions area.
Pencil

Device Update

MCU Version

Important Info About Device ID and Local Key

The Device ID is a very easy info to get, its an open info on the app, just go to the pencil logo and Device Information you'll find the Virtual ID. This ID never changes, you can remove de device and readd it and it will always be the same.
The problem comes with the Local Key, its a protected information and are only available for development purpuses. And most important, this information changes, so if you remove the device for some reason and readd it the Local Key will be a different value and you'll have to redo this tutorial steps to obtain the new one.

Let's Begin!

For this process you'll need a client made in Node.js that will connect to Tuya Cloud Services and retrieve the info needed. You may install it beforehand to make things quickier.
To install this client you'll need Node.js, it is a tool completely independent from Operating System so you may follow their official website chose the OS of your preference an install it. For this tutorial I won't guide on how to install it because it is a very simple next>next>finish, and I used a Windows machine in all steps, so if you use other OS feel free to share if there is something very specific opening an Issue and I'll be most happy to add to this Wiki.
You'll also need the device you want to use with the plugin added and working with Tuya/Smart Life app. If that's not your case you won't be able to connect the app with the Cloud Services.

Installing Tuya CLI

After installing Node.js go to the command prompt and install the tuya-cli packages by typing npm install @tuyapi/cli -g
A progress bar will appear and it should be finished quickly. If everything goes correctly you shoiuld see somethind like this in your cmd.

npmInstall

Developer Account on Tuya IoT Cloud Service

Tuya changes their plataform very often (you'll see the same warning if read the original tutorial that I linked above), so some steps below may look different for you or you may find some problem that is not mentioned here. If that happens and you haven't found how to solve it yet fell free to use the Discussions, but if you already solved fell free to open an Issue detailing how the error ocurred and how you managed to solve it and I'll update this Wiki.

  1. Go to Tuya IoT Cloud website. You'll have to create a new account, I recommend using tha same email that you used on the Tuya/Smart Life app, I found out that this made things easier. During the process select United States as the country because it will skip an Enterprise verification. (Unfortunatelly I don't have images for this process because my account was already created when I started this project)

  2. After logging in go to the left navbar hover in Cloud and click in Development.

  • If this option isn't available to you, you'll have to "purchase" a Trial Plan, this is completely free of charge and you won't be required to inform any payment methods. After purchansing your account will be limited with option but will have all we need to proceed.

    CloudDevelopment
  1. At this point you'll be at the My Cloud Projects panel, all you have to do is click on Create Cloud Project on the right corner.

    CreateCloudProject

  2. In the pane that opens give your project a name, a description and in Industry and Development Method make sure you've selected Smart Home. In the Data Center comes the first inconsistency, for some users they must input the correct data center according to the region your Tuya/Smart Life app is registered, Tuya provides a Mapping for this, so in my case the correct Data Center was Central Europe but when trying to link my app some steps below I ended up with an error saying that "0 accounts and 0 devices were added" (don't feel desperate if you've selected the wrong Data Center or the selected one gives you the same error i've got because you can edit the project later and remove/add any Data Centers). The solution I found for this problem after some research was adding only Western America. An Add Services will open after clicking OK, you may skip it. After the project is created you'll land directly on your project page on the Overview tab, there you'll be able to see some infos about it and under Authorization Key take a note of the Access ID/Client ID and Access Secret/Client Secret they will be required in step 8 when using the tuya-cli, but never share this credentials, otherwise anyone can access and control your smart devices with it.

    CreateNewProject

  3. Still in your project page find the tab Service API and click on it.

    ServiceAPI

  4. Click on the button Go to Authorize and in the opening pane you'll have to select 3 services from the dropdown options: IoT Core, Authorization Token Management and Smart Home Scene Linkage. Click OK and check if they appear listed on the Service API tab, if not redo this steps until you see them. It might be possible that in order to be able to select each of this APIs you'll have to click a Free Trial link to make one of those "purchases" that the plataform requires, each of this "purchases" may come as a popup, so check your browser if you got stuck here, and remember that any of this are free of charge

    GoToAuthorizeButton

    GoToAuthorizePanel

    AuthorizedAPIs

  5. Go to the Devices tab of your project, and under this tab go to Link Tuya App Account. Click on the button Add App Account. A panel with a QR code will pop-up, you'll have to go to your Tuya/Smart Life app, click on Me and click inan icon on the right corner that resembles a frame with a horizontal line in the middle, this will open your camera app to scan the QR code. If evereything goes correctly the QR code panel will change with some info on what app you are linking, in my case it showed some chinese word with the word "app" in the end, when I hit OK it warned me that 1 account and X devices were added and my account appeared in the Link Tuya App Account tab.

    If after you scan the QR the plataform shows you that "0 accounts and 0 devices were added" and no account is listed on the Link Tuya App Account tab, this might be due to Data Certer region problem that I mentioned above in step 4. You can return to the Overview tab click Edit and add or remove as many Data Certers as you want, in my experience leaving only Western America worked for me, but if thats is not your case you'll have to play around the Data Centers a little. You can't add the same account twice to the same Data Center, but if you remove that Data Center from your project, save and added it again all the configurations are erased and you can try to link your app once more.

    LinkTuyaAppAccount

    AddAppAccountButton

    TuyaAppMe

    TuyaAppScanQR

  6. If evereything goes correctly you'll be able to see all your devices under the All Devices tab.
    So now is the time to go back to Command Prompt and use the tuya-cli. Start simply typing tuya-cli wizard this will start a wizard that will request your cloud project credentials that you noted down on step 4. The first request is The API key from tuya.com this is the value found in the Access ID/Client ID of your project. Then it will request the The API secret from tuya.com that is the value from Access Secret/Client Secret. Then the last info Provide a 'virtual ID' of a device currently registered in the app: this is simply a Device ID from any device linked in your project, this is easily retrieved just going to the Devices tab then All Devices and copy any value from the Device ID column. If you've done everything correctly you'll see on the Command Promp a list in JSON format of all your devices containing the name of the device, its ID and its Key, this are the two infos that you'll be needed in the plugin.
    I highly advise to save this list for further uses.
    (Unfortunately I can't provide screenshots of this part of the process due to obvious security reasons but I hope the text guide is detailed enough)