Tuya devices to Openhab via MQTT - a working solution (without flashing)!

Dear OH-community,

I found several postings about adding tuya devices to OH. Thanks to @tsightler finally there is a working solution now.

Tom rewrote his whole code with some modifications on the underlaying TuyAPI. One of the highlights here is the easy naming (device names instead id’s) and the OH-friendly path (for example: if you have a power strip with multiple outlets, you can access the mqtt-topic easily by tuya/powerstrip/dps/2/state).

Please note that the underlaying TuyAPI doesn’t support protocol version 3.2 yet. On the other hand, I have a lot of tuya devices here and none are using the protocol version 3.2. Everytime I ran into a problem with a device not working, I just switched the pv from 3.1 to 3.3 or vice versa).

So here I want so share a short How-To:

  1. Follow the instructions here and add all your devices within the (tuya-/smartlife)-app.
    See section: Listing Tuya devices from the Tuya Smart or Smart Life apps (highly recommended)
  1. In order to get tuya-mqtt running, you will need a running mqtt server. A step-by-step guide can be found here (section: Update for MQTT-Binding 2.4):
  1. Installation of tuya-mqtt (I installed it in the OH-scripts-directory):

    cd /etc/openhab2/scripts
    git clone GitHub - TheAgentK/tuya-mqtt: Nodejs-Script to combine tuyaapi and openhab via mqtt
    cd tuya-mqtt
    npm install

    cp config.json.sample config.json

Edit config.json and adjust to your environment - for example:

{
    "host": "localhost",
    "port": 1883,
    "topic": "tuya/",
    "mqtt_user": "",
    "mqtt_pass": "",
    "qos": 2
}

Now it’s time to configure the devices. You can use “tuya-cli wizard” to get a basic devices.conf. As this can be complex (especially if you run your devices in different VLANs) please read this first:

For example, my devices.conf looks like:

[
  {
    name: 'TV Living',
    id: '363062476001********',
    key: '0354913f********',
    ip: '192.168.103.19',
    version: '3.1',
    type: 'SimpleSwitch'
  },
  {
    name: 'Monitor',
    id: 'bf2358890b1bfd********',
    key: '26db678b********',
    ip: '192.168.103.16',
    version: '3.3',
    type: 'SimpleSwitch'
  },
  {
    name: 'LightBulb',
    id: '56100002c44f********',
    key: '848c69c9********',
    ip: '192.168.103.23',
    version: '3.3',
    type: 'RGBTWLight'
  }
]
  1. You should be able to start tuya-mqtt now:

    node tuya-mqtt.js

  2. Next you have to define your MQTT-channels - I simply did that via PaperUI. Use a “Generic MQTT Thing” (for example “MQTT Tuya”) and add your channels here according to your devices.conf:

  1. It’s time to configure your items - here an example ( /etc/openhab2/items/tuya.items ):

    // Adding a switch which is linked to a mqtt-topic
    Switch Office_Monitor “Monitor” {channel=“mqtt:topic:mqtt_tuya:Office_Monitor”}

  2. With the items generated in step 5 you can proceed as usual (add the items to sitemaps, use it in rules, etc.)

  3. To run tuya-mqtt as a systemd-service, you can create a file:

     sudo vi /lib/systemd/system/tuya-mqtt.service
    

    and add the folling lines:

     [Unit]
     Description=tuya-mqtt
     After=mosquitto.service
    
     [Service]
     ExecStart=/usr/bin/node /etc/openhab2/scripts/tuya-mqtt/tuya-mqtt.js
     Restart=always
     User=openhabian
     Group=openhabian
     Environment=PATH=/usr/bin/
     Environment=NODE_ENV=production
     WorkingDirectory=/etc/openhab2/scripts/tuya-mqtt/
    
     [Install]
     WantedBy=multi-user.target
    

    After saving the file, you have to run:

     sudo systemctl daemon-reload
    

    Now you can start the service:

     sudo systemctl start tuya-mqtt.service
    

    Check if the service is runnning properly:

     sudo systemctl status tuya-mqtt.service
    

    If you see any error, please run

     journalctl -u tuya-mqtt.service -n 100
    

    and post the output here.

    The last step is to make the service starting automatically on reboot. This is done by:

     sudo systemctl enable tuya-mqtt.service
    

I am using tuya-mqtt now for 2 weeks in my production environment and it’s absolutely stable!

If you have any further questions/proplems/ideas, please feel free to reply here…

What else I can say… Thank you @tsightler !

11 Likes

Great tutorial!
I flashed mine with tasmota to have matt binding.
Good to know we can control directly tuya device without soldering and flashing

I had the problem that many of my devices are not flashable. But as you are now able to create a free account on iot.tuya.com, it’s pretty easy to get your neccessary device_id’s and keys. Still some effort but you will be able to get almost every tuya-device in without need to flash it first.

Especially the handling of the light bulbs is fantastic now with tuya-mqtt 3.0 - it even respects the white mode of some newer bulbs…

Is it just me, with some wrong configs, or is node.js really hungry for resources and therefore a basic RPi 3B+ isn’t enought for the job?

I mean, I’ve invested literally days on this subject and I could make it work! But in order to make it work I had to first run “node tuya-mqtt.js” and let terminal open with that, or I had to use “forever start tuya-mqtt.js”.

In either cases my CPU was up to 30/45% all the time and that caused OH to become very slow responding commands (sometimes not even responding at all).

With this script not running, my system is just very, very fast! I mean, I’ve been optimizing this for a long time now and although I have around 50/60 equipments (some 10 via Z-Wave and others via MQTT), the system simply works like a charm!

This Tuya integration was like a new challenge and I did made it… to a certain point I mean! I’ve just unninstalled nodejs and tuya-mqtt because I can’t just have this slow and unstable system.

As far as I could read on my research, Java is (very) hungry for memory due to the way this language operates. So, I mean, does JS also work that way? Is NodeJS just too much to handle for this small computing capacity?

I don’t know, I though I could just share my experience. This was just to control a single blind, so I don’t mind having to tell Alexa commands or to operate it with the remote it has, it’s just that I like to have everything integrated into OH. :slight_smile:

@pedrolima I am running my test-environment on a Raspberry 4 and the memory/cpu usage was extremely low (couldn’t even find it on the first page of the top-output). I also created a Grafana-graph which showed me the resource usage during all my tests. In the beginning I had such a cpu-usage problem but then I updated node and after it worked fine. Can you please share your node and java version?

And yes, you are right, my bad - I forgot to add a section “How to run as a systemd service” in my how-to - sorry for that! I will write it in the next couple of minutes…

*** Update: Added step 7 to the how-to ***

I’ve unnistalled node and will not install this again. But I really appreciate your feedback and your work.

I believe this might have been something I did wrong, but I just don’t have the time to play with this.

Having my businesses to run leaves me with not much time to spare, but it’s an awesome hobby! :smiley:

Node should, in general, be very light on memory and tuya-mqtt should not need much memory overall, although I guess everything is relative. In my environment, tuya-mqtt running on NodeJS v12.18.4 uses about 65-70MB of memory, which I think is pretty light. If you’re seeing high memory/CPU usage from tuya-mqtt, that would be unexpected for sure as I’ve worked pretty hard to make sure there are no memory leaks or other issues. CPU wise, usage for tuya-mqtt should be next to nothing, it’s extremely lightweight in this regard.

The only thing I could think of is if you device is perhaps already right at the edge of available memory, so that running a process that needs another 75MB of RAM is just too much and pushes it into swap, making everything run slower.

1 Like

Hello,

I was looking but cannot find anywhere - does it work with Tuya Moes Zigbee Thermostat for Radiator?

Thanks for answer!

1 Like

Is your device connected to a Tuya Zigbee gateway? If so, it might not work that way… I handle all my Zigbee devices via a Conbee USB-stick…

1 Like

I havent bought it yet :slight_smile: Just wondering whether it will be working when connected with Zigbee Tuya gate. If not, than I will stick with zigbee2mqtt :slight_smile:

I get warnings and errors with the command ‘sudo npm i @tuyapi/cli -g’ saying node must be > 10 and mine is 8.10
What should I do?

I updated to the latest with sudo npm i -g npm@latest.
Still get a lot of warnings ‘notsup’

Can you please check your node version:

node -v

its 8.10

So your node version hasn’t been updated. Depending on your operating system you could try this:

Finally installed into the ubuntu VM without errors.
My OH is a docker container inside the ubuntu VM. Will this installation work, or do I also need to make another kind of installation inside the container?

Works perfectly, after making sure that I didn’t missed any steps.

Thanks!

Hi there. I´ve got a problem. I can´t run node because no ´devices.conf´will be created.
If I try to create it manually, an error occours ´devices file not found´.
Any solution?

Sorry for the late reply - pretty busy at the moment… Did you run „tuya-cli wizard“ (as described in step 3) before you started tuya-mqtt?

I´ve tried it a several times in different variations. Still no ´devices.conf´.
Just to clarify: the devices.conf should be in the ´tuya-mqtt´-directory?

Yes, the devices.conf should be in the same path of tuya-mqtt.js…

Ok, let’s get it sorted:
Can you please navigate to your directory where you installed tuya-mqtt (in my example: /etc/openhab2/scripts/tuya-mqtt) and from there please run the following commands:

cd node_modules/.bin
./tuya-cli wizard

This script should ask for your api-credentials and a virtual ID - all those values you should have from Step 1 in the tutorial… Please check if the script runs through and returns your tuya-items.