Tuya Switch Control (Jinvoo, Smart Life)

hi jcf6288…can you plz publish your phyton-scripts…mini_plug1_ON.py and mini_plug1_OFF.py???

import pytuya

d = pytuya.OutletDevice('01200701dc4f2200682e', '192.168.1.XX', '428c1dcbeee12345')
data = d.status()  # NOTE this does NOT require a valid key
# Toggle switch state
switch_state = data['dps']['1']
data = d.set_status(True)  # This requires a valid key
d = pytuya.OutletDevice('01200701dc4f2200682e', '192.168.1.XX', '428c1dcbeee12345')
data = d.status()  # NOTE this does NOT require a valid key
# Toggle switch state
switch_state = data['dps']['1']
data = d.set_status(True)  # This requires a valid key

ok…but how to install that “pyTuya”- module???

cripts/mini_plug1_ON.py
Traceback (most recent call last):
  File "/etc/openhab2/scripts/mini_plug1_ON.py", line 1, in <module>
    import pytuya
ImportError: No module named pytuya

clone the git into the same folder then run it. The python script will be in the root with a folder from the github pytuya

hi jcf6288…Im a little bit dumb installing these pytuya…extracting in same folder “O:\openhab2-conf\scripts\tuyapi-master” does not work…can you please explain step by step how to import that module?..running the .py-script “mini_plug1_ON.py” is no issue! Thx 4 your help…sry

The “pytuya” folder should be in scripts

So, walk me through this. Given that practically everything I have is a SmartLife / Tuya device, what do I need to do to get them configured?

It seems like anything without a binding built already is a complete pain from what I just read. These Tuya switches are EVERYWHERE.

I would agree, a step by step approach for a beginner would be much appreciated.

2 Likes

Hi Kendrik. Yes, me too !!! But not here…only bla bla…i use IFTTT Webhooks to send trigger from Openhab to TUYA…works fine!

What part are you stuck on? You mentioned previously that installing the njsTuya scripts didn’t work?

Could you provide some terminal outputs? That would help.

I was able to find everything I needed via:

The hardest part for me was definitely getting the kid and key as it took a few tries to get the sequence of steps correct.

I’ve edited the original post to make it more clear for people. It’s quite a detailed step by step guide. People need to be a bit more clear where exactly they are having issues.

INSTALL INSTRUCTIONS are in the readme. Click the link and go down to INSTRUCTIONS

Hey,

is it possible to read out the actual power consumption of a device connected to a smart socket that support this? I want to switch of the socket when the device that is connected to it draws less then X watts.

Thank you @unparagoned,
I will try this weekend with my light bulb, have you tested with lights?

The plugs I have don’t have the ability to show power consumption.

@fmrenan I haven’t tried lights, but the unerlying script does. Mine is pretty much a wrapper so should be fairly easy to get working. https://github.com/codetheweb/tuyapi

@jcf6288 - following your example I have tried the following:
Switch TEST “TEST” [“Switchable”] {exec=">[ON: cd /etc/openhab2/scripts/python/ && node njstuya -ip MY_IP -id MY_ID -key MY_KEY ON] >[OFF: cd /etc/openhab2/scripts/python/ && node njstuya -ip MY_IP -id MY_ID -key MY_KEY OFF]" }
It does not work - I guess my syntax is not correct.

Also tried (unsuccessful):
Switch TEST “TEST” [“Switchable”] {exec=">[ON: node /etc/openhab2/scripts/python/njstuya.js -ip MY_IP -id MY_ID -key MY_KEY ON] >[OFF: node /etc/openhab2/scripts/python/njstuya.js -ip MY_IP -id MY_ID -key MY_KEY OFF]" }

From command line:
$ cd /etc/openhab2
$ cd /etc/openhab2/scripts/python/ && node njstuya -ip 192.168.0.16 -id
4200050dc4f221dfe75 -key b5d11e42ad045b62 ON
The above works.

Any idea?

Does

node /etc/openhab2/scripts/python/njstuya.js -ip 192.168.0.16 -id
4200050dc4f221dfe75 -key b5d11e42ad045b62 ON

work from the command line?

Yes it does.

Switch Test "Test [%s]"  {exec=">[ON: node /etc/openhab2/scripts/python/njstuya.js -ip 192.168.0.16 -id
4200050dc4f221dfe75 -key b5d11e42ad045b62 ON] >[OFF: node /etc/openhab2/scripts/python/njstuya.js -ip 192.168.0.16 -id
4200050dc4f221dfe75 -key b5d11e42ad045b62 OFF]" }

OK so I have tried another path now with this as my rule:
rule “TEST1 rule”
when
Item TEST1 changed
then
//executeCommandLine(“node /etc/openhab2/scripts/python/njstuya.js -ip 192.168.0.16 -id 4200050dc4f221dfe75 -key b5d11e42ad045b62 OFF”)
var tuyaScript = “node /etc/openhab2/scripts/python/njstuya.js -ip 192.168.0.16 -id 4200050dc4f221dfe75 -key b5d11e42ad045b62”
var String execResult
logInfo(“home.rules”, “TEST Lamp” + TEST1.state)

if ( TEST1.state == ON) {
	execResult = executeCommandLine(tuyaScript + " OFF", 50000)
} else {
	execResult = executeCommandLine(tuyaScript + " OFF", 50000)
}

logInfo("home.rules", execResult)

end

Here is my log with the errors I get:
2018-06-10 00:58:34.136 [INFO ] [se.smarthome.model.script.home.rules] - TEST LampON
2018-06-10 00:58:38.083 [INFO ] [se.smarthome.model.script.home.rules] - TEST LampOFF
2018-06-10 00:58:38.928 [INFO ] [se.smarthome.model.script.home.rules] - events.js:165
throw er; // Unhandled ‘error’ event
^

Error: bind EADDRINUSE 0.0.0.0:6666
at _handle.lookup (dgram.js:266:18)
at process._tickCallback (internal/process/next_tick.js:114:19)
at Function.Module.runMain (module.js:692:11)
at startup (bootstrap_node.js:194:16)
at bootstrap_node.js:666:3
Emitted ‘error’ event at:
at _handle.lookup (dgram.js:267:14)
at process._tickCallback (internal/process/next_tick.js:114:19)
[… lines matching original stack trace …]
at bootstrap_node.js:666:3
2018-06-10 00:58:44.924 [INFO ] [se.smarthome.model.script.home.rules] - (node:691) UnhandledPromiseRejectionWarning: Error: resolveIds() timed out. Is the device ID correct and is the device powered on?
at timeout (/etc/openhab2/scripts/node_modules/tuyapi/index.js:143:11)
at Timeout.setTimeout [as _onTimeout] (/etc/openhab2/scripts/node_modules/p-timeout/index.js:19:13)
at ontimeout (timers.js:466:11)
at tryOnTimeout (timers.js:304:5)
at Timer.listOnTimeout (timers.js:267:5)
(node:691) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:691) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

What can I do here?

If command line works then try getting a basic rule working.

rule “TEST1 rule”
when
Item TEST1 changed
then

var String execResult
logInfo(“home.rules”, “TEST Lamp” + TEST1.state)

if ( TEST1.state == ON) {
	execResult = executeCommandLine(“/usr/bin/node /etc/openhab2/scripts/njstuya.js -ip 192.168.0.16 -id 4200050dc4f221dfe75 -key b5d11e42ad045b62 ON”) + " "
} else {
	execResult = executeCommandLine(“/usr/bin/node /etc/openhab2/scripts/njstuya.js -ip 192.168.0.16 -id 4200050dc4f221dfe75 -key b5d11e42ad045b62 OFF”) + " "
}

logInfo("home.rules", execResult)
end

Or even

rule “TEST2 rule”
when
Item TEST2 changed
then

executeCommandLine(“/usr/bin/node /etc/openhab2/scripts/njstuya.js -ip 192.168.0.16 -id 4200050dc4f221dfe75 -key b5d11e42ad045b62 ON”) 
Thread::sleep(5000)
execResult = executeCommandLine(“/usr/bin/node /etc/openhab2/scripts/njstuya.js -ip 192.168.0.16 -id 4200050dc4f221dfe75 -key b5d11e42ad045b62 OFF”) 
Thread::sleep(5000)
executeCommandLine(“/usr/bin/node /etc/openhab2/scripts/njstuya.js -ip 192.168.0.16 -id 4200050dc4f221dfe75 -key b5d11e42ad045b62 ON”) 
}

end

Or if all fails

Or even

rule “TEST3 rule”
when
Item TEST3 changed
then

executeCommandLine(“/usr/bin/node /etc/openhab2/scripts/njstuya.js -ip 192.168.0.16 -id 4200050dc4f221dfe75 -key b5d11e42ad045b62 ON”) 

end

Also when posting code, use code fences for formatting.