Step-by-Step guide for adding Tuya-bulbs, Wi-Fi smart LED (Smart Life app) to OH2 using tuya-mqtt.js by AgentK

Now I’am totally confused…

It’s not working for me. I handled it the same way. But my smart plugs won’t switch reliable if I use OH2.4.
I tried it with the old 1.x-Binding and Mosquitto and everthing is working fine. On is on, Off is off, absolutly reliable and without any delay.

I tried many different settings with OH2.4 and MQTT. In one setup I was able to switch the plugs, but there was ab big delay and it was not reliable. In this setup I connected the things driectly to the embedded broker of OH2.4.

In the other settings I often received an error from the tuya-mqtt.js. Sounds like something is preventing the communication to the device:

"at TCP.onStreamRead (internal/stream_base_commons.js:111:27)
(node:22334) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing i
nside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (reje
ction id: 5) "

Another stupid thing is. I have two smart plugs and I can switch the one more reliable than the other… Dont know why. The Id and the key of both are correct, they work with 1.x and Mosquitto.

After frustrating hours of trying I went back to my 1.x setup with Mosquitto… But with this setup I’m not able to observe the dps output of the devices… Or?

Maby somebody has an idea? I’m grateful for any help!

I still don’t know what node.js ( a prerequisite) is and how it fits in the above diagram. I have lots of learning to do.

To put it simple, node.js is required for running Java Script.

First, check if node is present on your system:

which node

Output should be something like

/usr/sbin/node

If not, install it by:

sudo apt-get install nodejs

Hope I could help.

1 Like

Thanks Holgi. I just checked and node was already installed on my RPi :smiley:.

Currently, I’m having the "“Permission denied (publickey)” problem when trying to download AgentK’s Tuya-MQTT-Client. I saw your #5 post above on how to fix that but have not yet implemented it.

Thanks to this Project.
I was now able to handle my GOSUND SP111 Socket with openHAB.

For those who still trying to get the localkey and do not have root access on android.
Check the man in the middle method, using Burp Suite as Proxy with your Smartphone to capture the traffic and find the localKey.

In openHAB I needed to extend the Things Settings to work with the GOSUND:

Maybe this helps somone.

Keep up the great work

Need more help here.
I got pass the “Permission denied (publickey)" and was able to download the AgentK’s project. However, when I tried to install the project

npm install //downloads codetheweb/tuyapi

I got the following error messages:

npm ERR! code ENOLOCAL
npm ERR! Could not install from “…/…/…/…/downloads” as it does not contain a package.json file.

Any idea?

@AgentK

Is it possible to Add a dimmer type and support dimming function?
As I understand it a couple Dimmer sold in North America such as the Charging Essentials Brand sold by Costco uses DPS 3 with values from 1-255.
DPS 1 for the on off.

Let me thank the OP author and add that this guide allowed me to connect to miniaturized smart plug by “Houzetec” (Similar to Aisirer). Dps 6 was voltage and dps 5 power.
I noticed, though, that the plug needs to connect to its cloud server, otherwise it cannot be operated, nor it returns any value.
In the end, i flashed with tasmota thanks to the tuya-convert set of scripts. This solved an annoying bug of the original firmware that made the plug disconnect the mains every few minutes.

This made it working for me. Now I’m on OH2.4 wirh MQTT. :slight_smile: Thanks a lot for this hint.

leave off the //downloads

just use the command

npm install codetheweb/tuyapi

Ok, here is what I had to do to get this working on a Windows 10 machine running openhab 2.5.0 build 1556

first you need to get this installed on your machine
https://github.com/TheAgentK/tuya-mqtt

  1. Download this project (TheAgentK/tuya-mqtt) to your openhab2-script-folder “/etc/openhab2/scripts” on raspberian but for Windows its at “c:\openhab2\C:\openhab2\userdata\etc\scripts”

To do the next step you need to have node.js installed on Windows 10, i have node.js version 10.15.3 installed (npm comes with node.js)

  1. open an Admin power shell window, and do the following:
cd c:\openhab2\userdata\etc\scripts
npm install unparagoned/njsTuya
npm install TheAgentK/tuya-mqtt

note: both projects (njsTuya and tuya-mqtt) should finish with 0 errors. if you do get errors, do what I did and google the error message to see what you can do to fix it. You could always try to use the

npm install -g  xxxxxxxx 
  1. in windows file explorer goto c:\openhab2\userdata\etc\scripts\tuya-mqtt and copy the config.json.sample to config.json

  2. edit your config.json file, since i plan on using the embedded MQTT system broker my file looks like this:

    {
    "host": "localhost",
    "port": 1883,
    "topic": "tuya/",
    "mqtt_user": "",
    "mqtt_pass": "",
    "qos": 2
}
  1. you need to test to see if the tuya-mqtt.js will now work. You can run the tuya-mqtt.js from the powershell :

node tuya-mqtt.js

As long as the powershell window is not exited the tuya-mqtt.js will be running as a process.

Side note: to find my device’s

<Virtual id> and <localKey>,

I used the BLUE STACKS method as described above. These are needed when you define the channel(s) for the Generic MQTT Thing:

tuya STATE String:	
tuya/socket/<Virtual id>/<localKey>/XXX.XXX.XXX.XXX/state

tuya COMMAND String:
tuya/socket/<Virtual id>/<localKey>/XXX.XXX.XXX.XXX/command

*****please remember to set "ON" to 1 and "OFF" to 0

I do not own any android devices so this was the best method for me. I own about ten of these GOSUND / TanTan / Teckin (SP25) smart plugs. They all use the “Smart Life” App.

  1. I use MQTTlens and /or MQTT.fx as mqtt clients to see if I can subscribe to testTopic and publish to testTopic and see the message appear in the subscribe window. To do the test you first need to install MQTT binding in the paper UI of openhab2 or change your openhab2 addons.cfg file to include it in your bindings list.

  2. now that you have the MQTT binding installed in openhab2, you need to use the paper UI (or in a .thing file define the MQTT broker) to add a MQTT broker. Since I want the embedded broker I chose to goto (Addons → Misc). and choose the “Embedded-MQTT-Broker”. Here is how I did it in the mqtt.things file:

THINGS FILE:

Thing mqtt:topic:systemBroker:embedded-mqtt-broker:FR_Tuya_MQTT_Thing "Family Room MQTT Thing" (mqtt:systemBroker:embedded-mqtt-broker) @ "Family Room" {
    Channels:
        Type    switch:FamilyRoom_MQTT_TuyaChannel_01         "Family Room MQTT Tuya Channel 1"              [ stateTopic="tuya/socket/1576xxxxxxxxxxxxxxxx/919yyyyyyyyyyyyy/192.168.X.XX/state", commandTopic="tuya/socket/1576xxxxxxxxxxxxxxxx/919yyyyyyyyyyyyy/192.168.X.XX/command", on="1",off="0" ]
    }

Here is my mqtt.items file:

ITEMS FILE

// Mute the Family Room Echo Plus
Switch                          Family_Room_EchoPlus_Tuya_Plug           "Family Room Echo Plus Smart Plug"                             <switch>                                    (gMuteFamilyRoom,gMuteFrontOfHouse)                                            {channel="mqtt:topic:systemBroker:embedded-mqtt-broker:FR_Tuya_MQTT_Thing:FamilyRoom_MQTT_TuyaChannel_01"} 

Sorry I got side tracked. We were testing the tuya-mqtt.js script. Once your “Embedded Broker” is installed just run the tuya-mqtt.js script in a powershell window :

cd c:/openhab2/userdata/etc/scripts/tuya-mqtt
node tuya-mqtt.js
  1. open two MQTT.fx windows, make sure each instance uses a different clientID (MQTT-Client1 and MQTT-Client2). If you do not use seperate ClientID that openhab2 will get a ton of ERROR messages in the LOG file. Connect each instance to localhost port 1883. Now subscribe to a topic in one window, in the other window publish to the topic to see if your message shows up in the subscribe window. If it does, than openhab2 embedded broker is working. Exit out of both instances of MQTT.fx.

  2. now you need to install the tuya-mqtt.js as a service in windows. I used
    qckwinsvc. Since you have npm now installed on your machine do the following:

npm install qckwinsvc

here is the URL: https://www.npmjs.com/package/qckwinsvc

  1. even thou qckwinsvc says it started the service, do the following at the powershell prompt:
net start "tuyamqtt.exe"
  1. So at this point your have you tuya-mqtt running as a service, you have openhab2 up and running with MQTT and you have defined your MQTT Generic Thing. As pointed out by user agossi “Simon”., it’s critical that you use a “1” for ON and a “0” for OFF. Otherwise the commands will not work!!

  2. you can now go into openhab2 paper UI and go to the “Control” section and find your “Generic MQTT Thing” and switch it “off” and “on”. You should be able to here the smart plug turn off and back on again.

  3. or you can set up a .rules file:

RULES FILE:

var Timer Mute_Family_Room_occupancyTimer = null
val int Mute_Family_Room_timeoutMinutes = 15  

// voice activated Mute the Family Room Echo for 15 minutes

rule "Mute The Family Room Echo Plus for 15 Minutes"
when
    Member of gAlexa_EchoCmds changed to "mute the family room for fifteen minutes"
then
{
if(Mute_Family_Room_occupancyTimer === null) {
        logInfo("Mute_The_Family_Room_Echo_Plus_for_15_Minutes","-------------->Mute the Family Room Echo Plus for 15 Minutes has been triggered using Echo: "+ triggeringItem.name)
        Family_Room_EchoPlus_Tuya_Plug.sendCommand(OFF)
        Mute_Family_Room_occupancyTimer = createTimer(now.plusMinutes(Mute_Family_Room_timeoutMinutes ), [|
        Family_Room_EchoPlus_Tuya_Plug.sendCommand(ON)
        logInfo("Mute_The_Family_Room_Echo_Plus_for_15_Minutes", "----------------------------------------->Sent the tuya/socket/xxxx/xxxx/xxxx/command/1")
        Mute_Family_Room_occupancyTimer = null
        ])
    }
    else {
        Mute_Family_Room_occupancyTimer.reschedule(now.plusMinutes(Mute_Family_Room_timeoutMinutes ))    
    }
}
end

I have tried to do this WITHOUT the tuya-mqtt.js script but it will not work unless you install this light client (tuya-mqtt.js).

I hope this helps someone out.

2 Likes

Thanks again GadgetAngel !!!
After seeing your post (#69), I realized

//downloads codetheweb/tuyapi

was just a comment and NOT part of the install command. It was a newbie mistake on my part :blush:.
So the Github download and install commands without comments should look like this

cd /etc/openhab2/scripts
git clone git@github.com:TheAgentK/tuyaapi_mqtt.git 
cd tuyaapi_mqtt
npm install 

After creating my configuration file:

cp config.json.sample config.json

and without modifying it, I was able to run

node tuya-mqtt.js

If I don’t not see any error nor the command termination, does this mean my OH embedded MQTT broker is successfully communicating with the Tuya-mqtt client? If my guess is correct, is the flow in BOLD below what I have established so far?

OPENHAB <> MQTT-Binding[Embedded MQTT Broker Thing]<>MQTT-Generic-Thing<>Tuya-mqtt-Client(aka topic)<>Smart Plug

Look at my post below the one you mention to see how I tested out the Openhab embedded MQTT broker…use two instances of a mqtt client on the openhab host machine. You can use google’s MQTTlens or MQTT.fx on windows. On Linux you can use the mosquitto client routines called mosquitto_sub and mosquitto_pub. If you google it there are plenty of examples on how to test the MQTT broker

Hi Holgi

In your 2.4 instruction (post #1) , you still have a step where you create an extra MQTT broker.
image

Does this mean I could skip that step and connect my Generic MQTT thing directly to the embedded broker ?
Thank you.

yes, just skip that step and load the embedded broker and you should only have ONE MQTT Broker in your inbox that points to the embedded broker:

1 Like

Here is a step by step pictorial on how to use the embedded-mqtt-broker to create a Generic MQTT Thing.

  1. Add a Generic MQTT Thing by choosing the inbox --> + , select “MQTT Binding” to start searching but hit the “Add manually” then select “Generic MQTT Thing”

  1. Now add a channel to the Generic MQTT Thing. When you do this pick your channel_id_with_no_spaces.

NOTE: Double Check everything BEFORE you hit SAVE. Once you create the topic this way (via the UI) the only way to edit it is to delete it and recreate it.

  1. Create the ITEM to Attach to the MQTT Topic channel:

    Choose “Create new item”.

  2. Link and ITEM to the TOPIC channel like this:

  3. Hit Link. Now you are setup to actually switch the plug/light “on” or “off” or just use its ITEM in a rule. So here is the final result:

  4. To access the plug in a rule, do this

To Turn the Plug ON use this in a rule:
             Guest_Bathroom_EchoDot_Tuya_Plug.sendCommand(ON)

To Turn the Plug OFF use this in a rule:
             Guest_Bathroom_EchoDot_Tuya_Plug.sendCommand(OFF)
  1. (optional) You can also do a test by accessing Guest_Bathroom_EchoDot_Tuya_Plug (by its label or name “Guest Bathroom MQTT Thing”) through the “Control” and go to its assigned location and hit the switch!

NOTE1
“The big hurdle at the moment is getting the localkey for each device. Only developers get a secret key they can use it to set or get the private key for the device(it changes each time it’s set up)” In other words, if you have to change the setup on any tuya plug or light bulb, you will have to rediscover the “localkey” for the plug or light bulb!!

tuya/<"socket" or "lightbulb">/<Virtual-id>/<localkey>/<ip-address>/command
tuya/<"socket" or "lightbulb">/<Virtual-id>/<localkey>/<ip-address>/state

NOTE2
NOTE1 means that if you added your Generic MQTT Things by using the PAPER UI and you make any setup change to the plug or bulb (i.e. change wifi networks, assign a new ip address, or factory reset the plug or bulb)…YOU WILL HAVE TO RECREATE the Generic MQTT Thing to gain access to the MQTT topic configurations (the /command and /state definitions)

I think it is better to create the Generic MQTT Thing inside a “mqtt.things” FILE and create the channels via “mqtt.items” FILE

Here are my files at this moment:

mqtt.things

Thing mqtt:topic:systemBroker:embedded-mqtt-broker:Generic_Tuya_MQTT_Thing                  "Generic MQTT Thing"                    (mqtt:systemBroker:embedded-mqtt-broker)                  @ "Home" {
    Channels:
        Type    switch : MasterBedroom_EchoPlus_MQTT_TuyaChannel                              "Master Bedroom Echo Plus MQTT Tuya Channel"                                                    @ "Master Bedroom"                                              [ stateTopic="tuya/socket/<Virtual-id>/<localkey>/<ip-address>/state", commandTopic="tuya/socket/<Virtual-id>/<localkey>/<ip-address>/command", on="1",off="0" ]
        Type    switch : Netgear77_MQTT_Tuya_Channel                                          "NETGEAR77 MQTT Tuya Channel"                                                                   @ "Family Room"                                                 [ stateTopic="tuya/socket/<Virtual-id>/<localkey>/<ip-address>/state", commandTopic="tuya/socket/<Virtual-id>/<localkey>/<ip-address>/command", on="1",off="0" ]
        Type    switch : FamilyRoom_MQTT_Tuya_Channel                                         "Family Room Echo Plus MQTT Tuya Channel"                                                       @ "Family Room"                                                 [ stateTopic="tuya/socket/<Virtual-id>/<localkey>/<ip-address>/state", commandTopic="tuya/socket/<Virtual-id>/<localkey>/<ip-address>/command", on="1",off="0" ]
        Type    switch : Basement_Garage_MQTT_Tuya_Channel                                    "Basement Garage Ech Dot MQTT Tuya Channel"                                                     @ "Basement Garage"                                             [ stateTopic="tuya/socket/<Virtual-id>/<localkey>/<ip-address>/state", commandTopic="tuya/socket/<Virtual-id>/<localkey>/<ip-address>/command", on="1", off="0" ]
        Type    switch : Master_Bathroom_MQTT_Tuya_Channel                                    "Master Bathroom Ech Dot MQTT Tuya Channel"                                                     @ "Master Bathroom"                                             [ stateTopic="tuya/socket/<Virtual-id>/<localkey>/<ip-address>/state", commandTopic="tuya/socket/<Virtual-id>/<localkey>/<ip-address>/command", on="1", off="0" ]
        Type    switch : Guest_Bedroom_MQTT_Tuya_Channel                                      "Guest Bedroom Ech Dot MQTT Tuya Channel"                                                       @ "Guest Bedroom"                                               [ stateTopic="tuya/socket/<Virtual-id>/<localkey>/<ip-address>/state", commandTopic="tuya/socket/<Virtual-id>/<localkey>/<ip-address>/command", on="1", off="0" ]
        Type    switch : Guest_Bathroom_MQTT_Tuya_Channel                                     "Guest Bathroom Ech Dot MQTT Tuya Channel"                                                      @ "Guest Bathroom"                                              [ stateTopic="tuya/socket/<Virtual-id>/<localkey>/<ip-address>/state", commandTopic="tuya/socket/<Virtual-id>/<localkey>/<ip-address>/command", on="1", off="0" ]
        Type    switch : Netgear86_MQTT_Tuya_Channel                                          "NETGEAR86 MQTT Tuya Channel"                                                                   @ "Master Bedroom"                                              [ stateTopic="tuya/socket/<Virtual-id>/<localkey>/<ip-address>/state", commandTopic="tuya/socket/<Virtual-id>/<localkey>/<ip-address>/command", on="1",off="0" ]
        Type    switch : Netgear86Bridge_MQTT_Tuya_Channel                                    "NETGEAR86 BRIDGE MQTT Tuya Channel"                                                            @ "Family Room"                                                 [ stateTopic="tuya/socket/<Virtual-id>/<localkey>/<ip-address>/state", commandTopic="tuya/socket/<Virtual-id>/<localkey>/<ip-address>/command", on="1",off="0" ]        
        Type    switch : ARRIS_528A_MQTT_Tuya_Channel                                         "ARRIS-528A MQTT Tuya Channel"                                                                  @ "Master Bedroom"                                              [ stateTopic="tuya/socket/<Virtual-id>/<localkey>/<ip-address>/state", commandTopic="tuya/socket/<Virtual-id>/<localkey>/<ip-address>/command", on="1",off="0" ]        
}

mqtt.items

// Master Bedroom Echo Plus Plug
Switch                          Master_Bedroom_EchoPlus_Tuya_Plug           "Master Bedroom Echo Plus Smart Plug"                           <switch>                                                                                                                    {channel="mqtt:topic:systemBroker:embedded-mqtt-broker:Generic_Tuya_MQTT_Thing:MasterBedroom_EchoPlus_MQTT_TuyaChannel"}
// Turn off NETGEAR 77 Router
Switch                          NETGEAR77_Tuya_Plug                         "NETGEAR 77 Router Smart Plug"                                  <switch>                                                                                                                    {channel="mqtt:topic:systemBroker:embedded-mqtt-broker:Generic_Tuya_MQTT_Thing:Netgear77_MQTT_Tuya_Channel"} 
// Mute the Family Room Echo Plus
Switch                          Family_Room_EchoPlus_Tuya_Plug              "Family Room Echo Plus Smart Plug"                              <switch>                                    (gMuteFamilyRoom,gMuteFrontOfHouse)                                             {channel="mqtt:topic:systemBroker:embedded-mqtt-broker:Generic_Tuya_MQTT_Thing:FamilyRoom_MQTT_Tuya_Channel"} 
// Mute the Basement Garage Echo Dot
Switch                          Basement_Garage_EchoDot_Tuya_Plug           "Basement Garage Echo Dot Smart Plug"                           <switch>                                                                                                                    {channel="mqtt:topic:systemBroker:embedded-mqtt-broker:Generic_Tuya_MQTT_Thing:Basement_Garage_MQTT_Tuya_Channel"} 
// Mute the Master Bathroom Echo Dot
Switch                          Master_Bathroom_EchoDot_Tuya_Plug           "Master Bathroom Echo Dot Smart Plug"                           <switch>                                                                                                                    {channel="mqtt:topic:systemBroker:embedded-mqtt-broker:Generic_Tuya_MQTT_Thing:Master_Bathroom_MQTT_Tuya_Channel"} 
// Mute the Guest Bedroom Echo Dot
Switch                          Guest_Bedroom_EchoDot_Tuya_Plug             "Master Bathroom Echo Dot Smart Plug"                           <switch>                                                                                                                    {channel="mqtt:topic:systemBroker:embedded-mqtt-broker:Generic_Tuya_MQTT_Thing:Guest_Bedroom_MQTT_Tuya_Channel"} 
// Turn off NETGEAR86 Router
Switch                          NETGEAR86_Tuya_Plug                         "NETGEAR 86 Router Smart Plug"                                  <switch>                                                                                                                    {channel="mqtt:topic:systemBroker:embedded-mqtt-broker:Generic_Tuya_MQTT_Thing:Netgear86_MQTT_Tuya_Channel"} 
// Turn off NETGEAR86 Bridge 
Switch                          NETGEAR86_Bridge_Tuya_Plug                  "NETGEAR 86 Bridge Smart Plug"                                  <switch>                                                                                                                    {channel="mqtt:topic:systemBroker:embedded-mqtt-broker:Generic_Tuya_MQTT_Thing:Netgear86Bridge_MQTT_Tuya_Channel"} 
// Turn off ARRIS-528A Router 
Switch                          ARRIS_528A_Tuya_Plug                        "ARRIS-528A Smart Plug"                                         <switch>                                                                                                                    {channel="mqtt:topic:systemBroker:embedded-mqtt-broker:Generic_Tuya_MQTT_Thing:ARRIS_528A_MQTT_Tuya_Channel"}

Since you are using files you will also need the “addons.cfg” file:

addons.cfg

# The installation package of this openHAB instance
# Note: This is only regarded at the VERY FIRST START of openHAB
# Note: If you want to specify your add-ons yourself through entries below, set the package to "minimal"
# as otherwise your definition might be in conflict with what the installation package defines.
#
# Optional. If not set, the dashboard (https://<yourserver>:8080/) will ask you to choose a package.
#
# Valid options:
#   - minimal  : Installation only with dashboard, but no UIs or other add-ons. Use this for custom setups.
#   - simple   : Setup for using openHAB purely through UIs - you need to expect MANY constraints in functionality!
#   - standard : Default setup for normal users, best for textual setup
#   - expert   : Setup for expert users, especially for people migrating from openHAB 1.x
#   - demo     : A demo setup which includes UIs, a few bindings, config files etc.
#
# See https://www.openhab.org/docs/configuration/packages.html for a detailed explanation of these packages.
#
package = minimal

# Access Remote Add-on Repository
# Defines whether the remote openHAB add-on repository should be used for browsing and installing add-ons.
# This not only makes latest snapshots of add-ons available, it is also required for the installation of
# any legacy 1.x add-on. (default is true)
#
remote = true

# Include legacy 1.x bindings. If set to true, it also allows the installation of 1.x bindings for which there is 
# already a 2.x version available (requires remote repo access, see above). (default is false)
#
#legacy = true

# A comma-separated list of bindings to install (e.g. "binding = sonos,knx,zwave")
binding = wemo, ntp, hue, zwave, mqtt, zigbee, amazonechocontrol, wifiled

# A comma-separated list of UIs to install (e.g. "ui = basic,paper")
ui = basic, paper, habpanel

# A comma-separated list of persistence services to install (e.g. "persistence = rrd4j,jpa")
persistence = jdbc-mysql

# A comma-separated list of actions to install (e.g. "action = mail,pushover")
action = 

# A comma-separated list of transformation services to install (e.g. "transformation = map,jsonpath")
transformation = 

# A comma-separated list of voice services to install (e.g. "voice = marytts,freetts")
voice = 

# A comma-separated list of miscellaneous services to install (e.g. "misc = myopenhab")
misc = openhabcloud

I hope this helps

3 Likes

Hi GadgetAngel,
Many thanks for creating the above step by step pictorial. You and Holgi are awesome. I’ve learned so much from your posts.

Your welcome. I am sharing what I know like everyone else in this community does. I, like you, am still learning. I want to thank everyone else in this community for sharing. Also, you should know, I have only been doing this for a month and have learned the same way you have…by asking questions, reading the posts on the subject

THANK YOU CONTRIBUTORS for all your knowledge and time that you take to help us newbies!!!!

:blush:

1 Like

Hi Fraser,
Just want to say thank you. The rooted Bluestacks method in post #1 did not work for me either. I used your method in post #31 and was able to find the “localKey” for my device. Great job!

I finally got OH2.4 to control my smart plug. However, OH can only switch 1 of the 2 outlets on the plug and has zero control on the other. Using the Smart Life app, I could switch the outlets individually or both at the same time.

Any idea on how to control the 2nd outlet?

This is what I have
https://www.amazon.com/SUPERNIGHT-Upgraded-Anywhere-Required-Configuration/dp/B07K1QSFFJ
Thank you.

have you run tuya-cli to query which /dps/ topic that device uses? If you found your

tuya/<"socket" or "lightbulb">/<Virtual-id>/<localkey>/<ip-address>/command
tuya/<"socket" or "lightbulb">/<Virtual-id>/<localkey>/<ip-address>/state

with the anyproxy method then you had to install tuya-cli

This is the method of finding the keys, I was referring to:
https://github.com/codetheweb/tuyapi/blob/master/docs/SETUP.md

“oh_newbie”, I believed you used a different method. So here are the commands to install tuya-cli on a windows 10 machine (for linux use the right directory):

open a window Admin powershell at c:/openhab2/userdata/etc/scripts
>git clone git@github.com:TuyaAPI/cli
>cd cli
>npm install -g --save

Note: on linux or windows you want to install Tuya-cli GLOBALLY (-g flag on npm install line)

Here is the link to Tuya-cli setup page: https://github.com/TuyaAPI/cli

Run this tuya-cli command at any command prompt to see if it installed correctly (

Important
If tuya-cli throws an error under the following condition (#1 - somethimes, #2 always):

  1. try shutting down you OpenHAB2 server before using the tuya-cli command
  2. Also, BE-AWARE that while you are using the tuya-cli command DON’T have the Smart Life App (or which ever Tuya Smart App you use) OPEN while you are running the tuya-cli command on your server (tuya-cli will definitely through an unhanded error). If you must you the APP, access it before running the tuya-cli then make sure you exit the APP (don’t leave it running in the background on your phone)
tuya-cli help

or to find info on your plug:
tuya-cli get --id <Virtual-id> --key <localkey> --<ip-address> --all

example:
tuya-cli get --id 67833830cc50e3d4c9cf --key 559b607b2f0bac09 --ip 192.168.0.37 --all

tuya-cli produced the following output:
{ devId: '67833830cc50e3d4c9cf',
  dps: { '1': true, '7': true, '11': 0, '101': 0 } }

From that output I see I have two plugs on my unit that can be controlled.  The default, dps 1, can be controlled by using the MQTT state and command Topic below:

tuya/socket/67833830cc50e3d4c9cf/559b607b2f0bac09/192.168.0.37/state
tuya/socket/67833830cc50e3d4c9cf/559b607b2f0bac09/192.168.0.37/command, on="1", off="0"

I am presently testing this my self (I will let you know if this works) but I believe the second power port in the example can be controlled by the following MQTT state and command Topics:

tuya/socket/67833830cc50e3d4c9cf/559b607b2f0bac09/192.168.0.37/state
tuya/socket/67833830cc50e3d4c9cf/559b607b2f0bac09/192.168.0.37/dps/7, on="true", off="false"

I know this works:
tuya-cli set --id 67833830cc50e3d4c9cf --key 559b607b2f0bac09 --ip 192.168.0.37 --dps 1 --set false

result: the power turns off on dps 1 (the main socket)

tuya-cli set --id 67833830cc50e3d4c9cf --key 559b607b2f0bac09 --ip 192.168.0.37 --dps 7 --set false

result the power on the USB port turns off

This is my tuya smart plug:
https://www.amazon.com/Compatible-Function-Required-Allliances-Overcharging/dp/B07G78YSQS

So, now I just need to figure out how to translate the tuya-cli command into the MQTT topic. I am in the process of testing this out now, but I am in the middle of reinstalling Openhab2 first. I will let you know if I guessed wrong

1 Like