Tuya Switch Control (Jinvoo, Smart Life)

I’ve got it right now:

rule "TEST1r"
when
    Item TEST1 changed
then
	Echo_Living_Room_TTS.sendCommand('TEST1 has changed')
	//val results=executeCommandLine(“rmt_cmd '/usr/bin/node /etc/openhab2/scripts/python/njstuya.js -ip 192.168.0.16 -id 04200050dc4f221dfe75 -key b5d11e42ad045b62 ON'”,5000) 
	val results = executeCommandLine("sudo@@/usr/bin/node@@/etc/openhab2/scripts/python/njstuya.js@@-ip@@92.168.0.16@@-id@@04200050dc4f221dfe75@@-key@@b5d11e42ad045b62@@ON",5000)
	logInfo("execTest", results)
end

the @@ and sudo were the issues - more my lack of knowledge.

Now I need to do something nice from this.

Thank you again for putting me on the good path!

Hey, just implemented two plugs this way but if I want to change the state of both switches at the same time one of them won’t change. I suppose it’s due to the script handling one request at a time.
Is there any workaround to fix this?

Trying to add another device. I can see the state of the device but cannot changed it.
What do you think?

X@XX:~ $ node /etc/openhab2/scripts/python/njstuya.js -ip 192.168.XX -id 06200217XX -key XXX5 STATE
ON
X@XX:~ $ node /etc/openhab2/scripts/python/njstuya.js -ip 192.168.XX -id 06200217XX -key XX5 OFF
Error: Error communicating with device. Make sure nothing else is trying to control it or connected to it.

I’ll try mine when I get home.

Question - do you have multiple routers in your house? Wondering if there’s some lower level network issue / hiccup(?) just guessing.

no multiple routers - I am clueless on this one.

Hi Miles,

while installing tuyapi/cli it is giving me below error. I am not sure, why permission is denied. Please help

Blockquote
[21:44:27] root@smarthome:~/node_modules# npm i @tuyapi/cli -g
(node:7604) [DEP0022] DeprecationWarning: os.tmpDir() is deprecated. Use os.tmpdir() instead.
npm ERR! git clone git@github.com:tuyapi/cli Cloning into bare repository ‘/root/.npm/_git-remotes/git-github-com-tuyapi-cli-3083bead’…
npm ERR! git clone git@github.com:tuyapi/cli Warning: Permanently added ‘github.com,192.30.253.113’ (RSA) to the list of known hosts.
npm ERR! git clone git@github.com:tuyapi/cli Permission denied (publickey).
npm ERR! git clone git@github.com:tuyapi/cli fatal: Could not read from remote repository.
npm ERR! git clone git@github.com:tuyapi/cli
npm ERR! git clone git@github.com:tuyapi/cli Please make sure you have the correct access rights
npm ERR! git clone git@github.com:tuyapi/cli and the repository exists.
npm ERR! addLocal Could not install tuyapi/cli
npm ERR! Error: ENOENT: no such file or directory, stat ‘tuyapi/cli’
npm ERR! If you need help, you may report this entire log,
npm ERR! including the npm and node versions, at:
npm ERR! http://github.com/npm/npm/issues

npm ERR! System Linux 4.14.52-v7+
npm ERR! command “/usr/bin/node” “/usr/bin/npm” “i” “@tuyapi/cli” “-g”
npm ERR! cwd /root/node_modules
npm ERR! node -v v8.11.1
npm ERR! npm -v 1.4.21
npm ERR! path tuyapi/cli
npm ERR! syscall stat
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /root/node_modules/npm-debug.log
npm ERR! not ok code 0
[21:44:38] root@smarthome:~/node_modules#

Blockquote

I tried this tonight, and I have no issues running either of these commands:

node njstuya.js -ip 192.168.2.xx -id xxx -key xxx ON

and

 node njstuya.js -ip 192.168.2.xx -id xxx -key xxx OFF

I did play around a bit, and received the error message once, but it must have been a ‘blip’ as I couldn’t re-create it again. Seems like it has a pretty quick ‘timeout’ when attempting to communicate with the switch… sorry I can’t help more.

Not sure what the @tuyapi/cli package is (been a while since i installed this), but what I know does work is:

  1. in my openhab /scripts folder, create a folder called like 'tuyapi`
  2. in /scripts/tuyapi-master, run git clone https://github.com/unparagoned/njsTuya.git
  3. this creates another folder called njsTuya
  4. cd njsTuya
  5. If you run node njysTuya.js you should get an error about Cannot find module 'tuyapi'
  6. Now, initialize the folder as a nodejs project by running npm init and just press Enter through all of the options
  7. Now run npm install njsTuya
  8. Try running node njstuya.js, you should get an error about missing ID’s

From here, you should be able to use the the node njstuya.js etc etc etc command in OpenHAB to get it to work.

You guys might wanna have a look at my implementation of tuyapi, it supports rgb bulbs, scenes, flash patterns etc and should be easier to use, also make sure to always use latest tuyapi, there are huge performance differences.

Hi Miles,

Can you provide step by step procedures, to get it working? because it is still throwing error.

Blockquote
[13:48:08] root@smarthome:/etc/openhab2/scripts/tuyapi# cd njsTuya/
[13:48:16] root@smarthome:/etc/openhab2/scripts/tuyapi/njsTuya# node njysTuya.js
module.js:549
throw err;
^

Error: Cannot find module ‘/etc/openhab2/scripts/tuyapi/njsTuya/njysTuya.js’
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
[13:48:39] root@smarthome:/etc/openhab2/scripts/tuyapi/njsTuya# npm init
(node:16765) [DEP0022] DeprecationWarning: os.tmpDir() is deprecated. Use os.tmpdir() instead.
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sane defaults.

See npm help json for definitive documentation on these fields
and exactly what they do.

Use npm install <pkg> --save afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (njsTuya)
version: (0.0.0)
description:
entry point: (index.js)
test command:
git repository: (GitHub - unparagoned/njsTuya: Openhab interface for Tuya home automation devices sold under various names)
keywords:
author:
license: (ISC)
About to write to /etc/openhab2/scripts/tuyapi/njsTuya/package.json:

{
“name”: “njsTuya”,
“version”: “0.0.0”,
“description”: “Copies of config files are in .config”,
“main”: “index.js”,
“scripts”: {
“test”: “echo "Error: no test specified" && exit 1”
},
“repository”: {
“type”: “git”,
“url”: “git+https://github.com/unparagoned/njsTuya.git”
},
“author”: “”,
“license”: “ISC”,
“bugs”: {
“url”: “Issues · unparagoned/njsTuya · GitHub
},
“homepage”: “GitHub - unparagoned/njsTuya: Openhab interface for Tuya home automation devices sold under various names
}

Is this ok? (yes) yes
[13:49:44] root@smarthome:/etc/openhab2/scripts/tuyapi/njsTuya# npm install njsTuya
(node:16799) [DEP0022] DeprecationWarning: os.tmpDir() is deprecated. Use os.tmpdir() instead.
npm WARN install Refusing to install njsTuya as a dependency of itself
[13:49:58] root@smarthome:/etc/openhab2/scripts/tuyapi/njsTuya# node njstuya.js
module.js:549
throw err;
^

Error: Cannot find module ‘/etc/openhab2/scripts/tuyapi/njsTuya/njstuya.js’
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
[13:50:17] root@smarthome:/etc/openhab2/scripts/tuyapi/njsTuya#

Blockquote

Sorry Amit; my previous post is about as step by step as you can get :grinning:

1 Like

I believe something is missing, you asked to create folder tuyapi, while in 2nd step it look like it is tuyapi-master.

also I am not sure, how the find the id… in my case, I can see virtual if in SMart life app, so trying to use that.

for key: after creating account in Tuya.com and creating app as mentioned in previous post, it will show key and secret, I beleieve secreat is to be used, please confirm.

Still one doubt, how node njysTuya.js command will turn off and on my plug, if i had not registered my plug into app or Tuya.com somewhere.

so many gaps in overall view. please help.

Thanks

You need to change the devices one at a time. It runs a separate process which might not work great or at all with multiple copies running. But you shouldn’t have to wait that long between the requests. Maybe just add a small sleep command for a few hundred ms if you have any issues.

@nicolas_bfg Make sure that all apps that control the device are completely closed. You might have got the id or key wrong. A wrong key lets you view the device but not change it’s state.

@illnesse Nice.

@Amit_Kumar If you are having issues tuyapi has just release a config wizzard which may help with the initial problems you have having. So get it working on the command line with tuyapi first then come back if you have issues getting it to work with openhab https://github.com/codetheweb/tuyapi

Hi Miles,

I am able to close all hurdles :slight_smile: now in last… when i run

Blockquote
node njstuya.js -ip 192.168.1.xx -id xxx -key xxx ON or OFF or TOGGLE command get executed but plug not respond.

I tried to use key from captured from various sources e.g.

  1. through Tyua IOT platform developer portal, buy creating an app and fetch app & secret key from there.
  2. MITM attack on smart life app using capture packet… here i can find my local key
  3. MITM attack on smart life app using capture packet… here i can find my client id
    in all 3 options, command executed without an error, but my plug not responding on that.

can you share inputs here…to do it correctly, if it is wrong

Hi Nicolas,

I am also struck-up in same state … i.e from command line i can control the plug, but through openhab as an item, it is not working. I never used rules.

Can you share rule, item and other parameter configuration in openhab to get it integrated. below commands are working well

Blockquote
node /etc/openhab2/scripts/njstuya.js -ip 192.168.1.42 -id xx -key xx OFF/ON/TOGGLE

Blockquote

There are example items and rules files. All you need to do is uncomment the top of the rules file and put in your id and key. Then activate the rule with whatever interface you have.

So I’ve taken a look at some of the solutions here

Looks like it supports switches and bulbs but what about the Dimmer?

See Tuya Switch Control (Jinvoo, Smart Life)

That doesn’t answer the question at all. I don’t see how the dimmer piece is implemented. It’s just on and off how does it handle the brightness levels?

Sorry missed that. I don’t have any support for dimming. Check it tuyapi and see if that does. If so I’ll add support for it.