Here is a simple script, items and rules to control Tuya switches. Quite a few Chinese switches are simply re-branded Tuya devices.
Update
Now with Cloud support. You can simply use your email/pass for the Tuya/Smart Life app used to register your devices.
Local ON/OFF commands should now be pretty much instant.
I’ve used an existing nodejs script and just added a wrapper so you can more easily use it from openhab. The existing example is a Temperature control setup. When it’s too cold the switch turns on, etc.
It’s all relatively easy except for the configuration part where you need to perform a MITM attack to get the key. (And that was way easier than I expected).
Install Instructions:
If nodejs and npm are installed, install the package from npm (otherwise obtain nodejs and npm first):
openhab=/etc/openhab2
cd $openhab/scripts
sudo npm install unparagoned/njsTuya
Test Install
Run the following which should find all devices on your network and return their state.
node node_modules/njstuya
Configuration
To use create a rule which sends a command to the script
var resp = executeCommandLine("node /etc/openhab2/scripts/node_modules/njstuya" + command, 50000)
logInfo("Tuya", "Run Command: [{}] Result {}", command, resp)
To use the following items and rule as an example copy the files to Openhab configuration directories:
cp node_modules/njstuya/items/* $openhab/items/
cp node_modules/njstuya/rules/* $openhab/rules/
Cloud setup
You can pass the cloud email and arguments through the cli, or complete the key.json.rename and rename it to key.json
It is reccomended to put the details into key.json in the folder ./scripts/node_modules/njstuya/
{
"userName" : "d@yahoo.com",
"password": "yourpassword",
"countryCode": "44",
"bizType": "smart_life",
"region": "EU"
}
bizType can be ‘tuya’, ‘smart_life’
Then you just need to run
node njstuya.js -mode cloud -id DEVICEID COMMAND
Otherwise to pass all arugments without config enter the email/pass/international phone code/region
node njstuya.js -mode cloud -user email -pass password -biz smart_life -code 44 -region eu -id DEVICEID COMMAND
TO USE
To use just have a rule run
node njstuya.js -ip DEVICEIP -id DEVICEID -key DEVICEKEY COMMAND
Commands are ON, OFF, TOGGLE, STATE, -get, -set
e.g.
node njstuya.js -ip 10.0.0.2 -id 213klj349sdfjl324po32 -key 342kljerw98 ON
All commands return the state of the switch.
Now with full dps support as below with set arg
node njstuya.js -ip 10.0.0.2 -id 213klj349sdfjl324po32 -key 342kljerw98 -set "{ \"dps\":1, \"set\": true }"
Alternatives
The tuyapi/cli has borowed most of the commands from nsjtuya, so kind of makes my script redundant, since you can just use tuyapi and it’s cli. I had problems getting it working but if you can get it working it’s probably a more streamlined method and it shouldn’t take much effort converting the rules/items to work with tuyapi
Here is a very detailed step by step mqtt setup with pictures if that’s your thing [MQTT] (Step-by-Step guide for adding Tuya-bulbs, Wi-Fi smart LED (Smart Life app) to OH2 using tuya-mqtt.js by AgentK)
Here is a new node rest server that’s fast tuyasvr