Tuya Switch Control (Jinvoo, Smart Life)

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.

I have a tuya smart bulb, is it possible to flash with the tasmota?

Not to hijack this thread, this question is better asked here: https://groups.google.com/forum/#!forum/sonoffusers
Generally, if you can open the device, find the esp8266 chip , solder some wire, connect to your fdti or similar and get esptool.py to ID the chip, then probably yes. In all other cases: No

Look what I found:
AiLight Hackable

And this is my smart bulb deassembled:

So, Iā€™m planning to flash this ESP8266 with the tasmota firmware or ESPurna firmware, both have smart bulb support and Iā€™ll try the tasmota first and pray to it works!

1 Like

Nice! Where did you buy it?

You can find here:
Ali Express

Gearbest

This light is not much powerfull(Only 7w and 450Lumen) but is RGB and a budget option with customizable firmware flashing the esp8266.
I wanna buy a Yeelight soon, and I found that:
Yeelight white
Is a 10w and 800 lumen light and a better choice to be the main light of a room but is only white and warm light, the RGBā€™s are a little more expensive. I expect that can works with the Yeelight binding.

I hadnā€™t checked on the Tuyapi for a while, but looks like shortly after my comments here support was added for dimmers.

Hey ! everything work when I tried the command : node njstuya.js -ip 192.168.1.xx -id xxx -key xxx ON

but it super slowā€¦(it take up to 10 second to trigger) anyone else is having this issue ?

I just tried it through my habpanel which uses a GUI to activates a chain of rules, and that tool less than 2 seconds. I havenā€™t had any problems for a while.

Hello any ideas how to make this work from openhab in a docker?

So I have it working fine in a rule for a Tuya plug. However the smart plug has 2 outlets. This only turns on/off one of the 2 outlets on the plug. Is there any way to specify which outlet to turn off at this time?

    import pytuya

d = pytuya.OutletDevice('01200701dc4f2200682e', '192.168.1.XX', '428c1dcbeee12345')
data = d.status()  **# NOTE this does NOT require a valid key**

Just stumbled onto this, does this mean there is no need for a valid key? Is the device ID the ā€œVirtual IDā€ I can lookup in the Smart Life app?

If yes, the binding will work without a valid kay? I would give it a try thenā€¦

Only the status functions without a key. So anyone can spy and check the status of your plug. But only those with a key can change it.

Any idea on controlling the second plug of a multi-plug unit? Or are we stuck with just one of them working with this script at this time?

I am also in the process of integrating Tuya-Devices. Yesterday I found a nice way to read out ID and Key, and today I set up Tuyapi.

So far, I can switch light on and off in the CLI using these commands:

node /etc/openhab2/scripts/njstuya.js -ip 192.168.75.119 -id 12204702807d3a252d43 -key 0399de7c4a27915b ON
 node /etc/openhab2/scripts/njstuya.js -ip 192.168.75.119 -id 12204702807d3a252d43 -key 0399de7c4a27915b OFF

But when I tried to implement coyoteā€™s solution, I get a error in the rule:

2018-12-05 18:54:53.662 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'tuyaDeviceCrtl': The name 'gTuya' cannot be resolved to an item or type; line 8, column 22, length 5

This is my items definition:

//Tuya items
Switch tuyasw001 "Tuya 01" (gTuya)
String tuyasw001_config "-ip 192.168.75.118 -id 12204702807d3a49b2ae -key dd28170cbf5dcd08" (gTuya)
Switch tuyasw002 "Tuya 02" (gTuya)
String tuyasw002_config "-ip 192.168.75.119 -id 12204702807d3a252d43 -key 0399de7c4a27915b" (gTuya)

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

Sitemap:

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

Running on openHAB 2.3.0 Release Build

Any ideas?

ā€¦found solution by myself, had to declare group in items definition, now it works:

//Tuya items
Group gTuya
Switch Tuyasw001 "Tuya 01" <light> (gTuya)
String Tuyasw001_config "-ip 192.168.75.118 -id 12204702807d3a49b2ae -key dd28170cbf5dcd08" (gTuya)
Switch Tuyasw002 "Tuya 02" <light> (gTuya)
String Tuyasw002_config "-ip 192.168.75.119 -id 12204702807d3a252d43 -key 0399de7c4a27915b" (gTuya)

Another question, is the script able to control a group?

I can find ID and Key for Group in my XML, but what about IP adress?

Hi Folks,

Iā€™m on debian stretch an if I try to run

npm install codetheweb/tuyapi

the result is

bash: npm: Kommando nicht gefunden. (comand not found)

what is npm and why is it neccesary? or better how can I install this if its missing?
I think this should made clearer in the README because I donā€™t get any further here and maybe Iā€™m not the only one on debian stretch??

thanks

Dan