Working TUYA LOCAL Tutorial on core-2021.2.1 TESTED ON 7/02/2021

As most of you know tuya is rolling out updates which stops Tuya convert from working correctly. however i have put together this tutorial on how to get tuya local control to work.

First of all you will need your local key and device id. Follow these steps.

  • Install Bluestacks (find a link on the Bluestacks Tweaker page)
  • Root Bluestacks: BmB8I63ea-o at YouTube
  • Install SmartLife version 3.6.1 using an APK (newer versions seem to be hiding the key and localKey data we need)
  • Install ES File Explorer
  • Click the three lines at the top and scroll down and enable “Root Explorer” then click allow
    permission from superuser
  • Navigate to the app’s data directory (in my case, /data/data/com.tuya.smartlife/shared_prefs
  • Open preferences_global_key(numbers and characters).xml
  • Highlight and copy and paste to word then use “Ctrl and f” to use the find tool to search for
    local and id key
  • Write these down along with each device’s IP

Next we need to setup the python script, this acts as a tuya server and allows us to control our tuya devices

  1. go to this GitHub repository and download it https://github.com/stast1/localtuya
  2. use the samba addon to add it to hassio, create a "custom_components"folder under the
    config directory, inside it paste the “localtuya” folder and restart hassio
    3 your done

Now add your switch to home assistant to control it
1.add this to your config file and replace dev id, localkey and IP with yours

-   platform: localtuya
    host: 192.168.0.12
    local_key: !secret tuya_plug1_local_key 
    device_id: !secret tuya_plug1_devid 
    name: Bedroom 1 Fan
    id: 1
    icon: mdi:power-socket

  1. restart hassio and your switch will be there.

Notes
if you have multiple plugs add them one at a time so finding the local key and device id is easier
local and devids change when you re add you tuya device
your dev id is longer than local key

Hopefully this tutorial Help youlocaltuya

4 Likes

Sorry just to clarify does this mean we can operate TUYA devices via HA and the APP but HA control is local and no need for an internet connection?

That is correct and i have done this and it is working

This sounds excellent Thank you!

Does not work for me:

Update for light.tuya_01 fails

Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/helpers/entity.py”, line 261, in async_update_ha_state
await self.async_device_update()
File “/usr/src/homeassistant/homeassistant/helpers/entity.py”, line 441, in async_device_update
await self.hass.async_add_executor_job(self.update)
File “/usr/local/lib/python3.7/concurrent/futures/thread.py”, line 57, in run
result = self.fn(*self.args, **self.kwargs)
File “/config/custom_components/localtuya/light.py”, line 220, in update
status = self._device.status(self._bulb_id)
File “/config/custom_components/localtuya/light.py”, line 122, in status
self._cached_status = self.__get_status(switchid)
File “/config/custom_components/localtuya/light.py”, line 92, in __get_status
status = self._device.status()[‘dps’][switchid]
File “/config/custom_components/localtuya/pytuya/init.py”, line 311, in status
result = json.loads(result)
File “/usr/local/lib/python3.7/json/init.py”, line 348, in loads
return _default_decoder.decode(s)
File “/usr/local/lib/python3.7/json/decoder.py”, line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File “/usr/local/lib/python3.7/json/decoder.py”, line 355, in raw_decode
raise JSONDecodeError(“Expecting value”, s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I’m not sure about lights but I got plugs to work for me.

Did you put

Switch: 

- platform: localtuya 
host: 192.168.0.12
local_key: !secret tuya_plug1_local_key 
device_id: !secret tuya_plug1_devid 
name: Bedroom 1 Fan
id: 1
icon: mdi:power-socket

Yes I did .

Not sure then mate mine work but I only have tested it with smart plugs.

1 Like

that’s a shame, i was intending to use this with lights like @francisp

Try “light:” instead of “switch:”

I’ve followed this guide exactly to enable me to find my devices Local & ID Keys but I cannot find the information in the preferences_global_key.xml file or any of the other files in the folder.
Is obtaining the keys still possible?

I got excited when i saw this working. however if i use switch: i get a startup error

2020-01-21 22:53:12 WARNING (MainThread) [homeassistant.loader] You are using a custom integration for localtuya which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you do experience issues with Home Assistant.
2020-01-21 22:53:14 ERROR (MainThread) [homeassistant.components.switch] Error while setting up platform localtuya
  File "/config/custom_components/localtuya/switch.py", line 68, in setup_platform
  File "/config/custom_components/localtuya/switch.py", line 129, in __init__
  File "/config/custom_components/localtuya/switch.py", line 111, in status
  File "/config/custom_components/localtuya/switch.py", line 87, in __get_status
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 311, in status

If i use light:, i get an error when trying to change its status as per @francisp

yes it is, message me, i am still using it to this day, with the same method

Thanks for replying but I ended up moving away from using Tuya local and started custom firmware flashing my plugs with Tuya-Convert as was suggested by many people on this forum. I flash them with tasmota and after I had done 1 I never looked back, I’ve done over 20 plugs, about 10 bulbs and a couple of LED controllers.
Thanks anyway

I would, if the 2 lights I have had a esp inside. Unfortunately, it is a realtek, so can’t be flashed. And local tuya still does not work :frowning:

Take a look at https://github.com/TradeFace/tuyamqtt/ That might solve your problem. I be releasing v1.1 in a few days. TuyaFace ( the library to connect to the devices was released yesterday v1.3.1)

1 Like

Thanks. I will check it out !

same, flashed my first bulb coulple days ago and it is working like a charm!

Quick question, I can’t seem to find the answer.

Where you have…

  host: 192.168.0.12

… is this the IP address of your HASSIO computer? Or the IP address of the bulb/socket?

Thanks!!