Tuya Switch Control (Jinvoo, Smart Life)

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.

I am setting this up and cannot seem to get this code to execute when the switch I have created is pressed from the sitemap. The only difference I have is that my njstuya.js is in the scripts folder, not in the python subfolder. I can run the ON / OFF from command and the switch item shows up on the sitemap and shows its pressed on the log. Any troubleshooting tips?

First: A big thanks to unparagoned for this code!

I was a bit confused looking at the implementation options i saw in this thread. If I’m not mistaken is the item configuration with the exec string in the items file typical for exec binding v1, the string will not execute with exec binding v2.

The Exec binding v2 configuration is a bit complex for what I need: An as-simple-as possible generic approach, where the configuration resides in item files not in scripts. New items can be added by just listing them in the items file.

The item definitions for the tuya device consists of two items:
A switch item and a string item for configuration. The group membership and naming (itemname and itemname_config) is critical.

Items:
//Tuya items
Switch tuyasw001 "Tuya 01" (gTuya)
String tuyasw001_config "-ip 192.168.15.228 -id 06200971dc4f2238b6ad -key 46a3b8475bdb5423" (gTuya)
Switch tuyasw002 "Tuya 02" (gTuya)
String tuyasw002_config "-ip 192.168.15.234 -id 0720006884f3eb84a9da -key e64e284602eae1dd" (gTuya)

The sitemap file simply lists the switch items:

sitemap default label="Default" {
	Frame label="BG" {
		//Switch item=ruleTrigger
		Switch item=tuyasw001
		Switch item=tuyasw002
               }
       }

The rule:

import java.util.String
var tuyaScript = "node /etc/openhab2/scripts/njstuya.js "

rule "tuyaDeviceCrtl"
when
	Member of gTuya changed
then
    val configItem = gTuya.members.findFirst[ t | t.name == triggeringItem.name+"_config" ] //get the config item
	val configStr = configItem.label + " "

	logInfo("rules", "tuyaDeviceCrtl - Member " + triggeringItem.name + " to " + triggeringItem.state)
	//logInfo("rules", "tuyaDeviceCrtl - Config:" + configStr + ".")
	
    var String execResult
	execResult = executeCommandLine(tuyaScript + configStr + triggeringItem.state, 50000)
	logInfo("rules", "tuyaDeviceCrtl - ExecResult:" + execResult + ".")
end

The rule triggers on a change in any member of gTuya group.
Based on the triggeringItem name, the config item is identified.
The config string is extracted and combined with triggeringItem.state is used to create a command line.

To install the required code I used git clone and copied the bits to /etc/openhab2/scripts.

I had to edit line 67 of njstuya.js:
tuya.resolveIds()… gave me a warning, changing it to tuya.resolveId() took care of that.

2 Likes

After a few weeks, I’m running into issues with tuyapi (i think). About 50% of the time, the scripts would fire correctly but nothing happens. All in all: too many moving parts to my taste.

Since MQTT is already in use for for some tasmota flashed sonoff devices, I decided to see if the Tuya devices could be flashed with Tasmota firmware and have them function over MQTT.

To my surprise, it wasn’t too difficult. I found a de-assembly instructions for the neo coolcam plug (tasmota user group) And it works correctly after flashing, switching over MQTT, only button and LED need to be configured in the Tasmota web interface.

So I’m leaving this subject and will continue to tinker with all my esp8266 device to have them run Tasmota.