Tuya Switch Control (Jinvoo, Smart Life)

Probably dumb question. But how to install tuyapi on Openhab running on windows? npm command is not recognized. I´m able to use Smart Life plug via IFTTT with openhab integration, but it is pretty slow, unusable in fact.
Many thanks,
Michael

1 Like

Sorry for that. I can finally send commands to power sockets, but the issue is that smart life power sockets which I bought have LED light in it (around that socket) and that commands node njstuya.js just control that LED light (switch it on and off), but not power socket itself. Is there something in njstuya.js what I should change to control power and not LED light?
I try to extract ID and key twice, but the ID of each power sockets is still same.

I’ve just added support for any set command. Get the latest version and play about with the new set command, try using a dps of 1 or so. The set command takes an argument in quote marks. I don’t have any devices like that so everyone is counting on yo testing it.

'{ "dps": 1, "set": true }'

You may need to play around with the dps value

node njstuya.js -ip 10.0.0.2 -id 213klj349sdfjl324po32 -key 342kljerw98 -set '{ "dps":1, "set": true }'

edit: @Ringnutz try this see if it works for your multi device

I just tested this on one of those multi-plug devices

The command below turns plug 1 ON
/usr/bin/node /etc/openhab2/scripts/njstuya.js -ip X.X.X.X -id 07135cc50e3cba7a6 -key 338fbf2726db -set ‘{ “dps”:1, “set”: true }’

Setting the dps value to 2 turns the second one ON
/usr/bin/node /etc/openhab2/scripts/njstuya.js -ip X.X.X.X -id 07135cc50e3cba7a6 -key 338fbf2726db -set ‘{ “dps”:2, “set”: true }’

Changing the set value to false turns the plug specified with the dps value OFF
/usr/bin/node /etc/openhab2/scripts/njstuya.js -ip X.X.X.X -id 07135cc50e3cba7a6 -key 338fbf2726db -set ‘{ “dps”:1, “set”: false }’

So I would say that is win! Thank you so much!

My last question would be, does this script use the cloud to turn these devices on and off, or does it just send the command locally. Basically I want to block these devices from accessing the internet after setup if possible.

Thanks again for this!

Cool, nice to know it works. And this script works completely off-line, which is kind of why it’s a bit of a pain to get the keys.

1 Like

I tested this on a plug from Newegg called “Smart Plug” Model No. ZLD-12USA-W. This plug has an American style 110v plug and outlet as well as 2 USB charging ports.

The SET command uses dps as follows:
Turn 110v outlet ON -set ‘{ “dps”:1, “set”: true }’
Turn 110v outet OFF -set ‘{ “dps”:1, “set”: false }’
Turn USB charging ports ON -set ‘{ “dps”:7, “set”: true }’
Turn USB charging ports OFF -set ‘{ “dps”:7, “set”: false }’

I am delighted with this as I do not want my Rasberry Pi3 to rely on anything in the cloud to control my house.

Hi Michael, if I understand you have managed to configure tuyapi on openhab on windows. well! can you explain the procedure to me?

Hi, well I did, but the smart wifi power plug didn´t work anyway, it just turn on and off only light in it but not power plug itself, even when someone create that request with DPS.

But I manage to work it via IFTTT, the only problem is that it is very very slow. Just create a switch in openhab, then create in IFTTT connection to myopenhab and connection to Smart Life and create task in IFTTT (when openhab switch changed from OFF to ON then turn On Smart Life device), it works but as I wrote very slow (it takes around 2-10 minutes to turn that device ON/OFF).

M.

čt 17. 1. 2019 v 0:25 odesílatel Giancarlo bot@community.openhab.org napsal:

  1. Install Git for windows
  2. Add git to PATH. (Maybe an option in setup or you can do it manually link
  3. Install node

Then you should be able to follow the instructions as usual using command prompt.

npm installl unparagoned/njstuya 
#Then test with - You can get the status with any key
node "node_modules/njstuya/scripts/njstuya.js" -ip 192.168.1.8 -id 1231231237 -key 12312312 status

Do you know if anything changed (in the tuya firmware) in the last month?
I had this working without the need for the plugs to have internet access. In the last week I was messing about with a different app on my phone and I had to set up my plugs again, so I ended up with new local keys. Now with the new keys, the plugs will only work if they have access to the internet. If I block them at the router, I get a timeout:

Command:

node njstuya.js -ip 192.168.1.136 -id 5303504684f3eb211111 -key 746961009ead1111 -set '{"dps":1, "set": true}'

Output:

    (node:30105) UnhandledPromiseRejectionWarning: Error: resolveIds() timed out. Is the device powered on and the ID correct?
        at timeout (/home/openhabian/.node-red/node_modules/njstuya/node_modules/tuyapi/index.js:61:27)
        at Timeout.setTimeout [as _onTimeout] (/home/openhabian/.node-red/node_modules/p-timeout/index.js:19:13)
        at ontimeout (timers.js:436:11)
        at tryOnTimeout (timers.js:300:5)
        at listOnTimeout (timers.js:263:5)
        at Timer.processTimers (timers.js:223:10)
    (node:30105) 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: 2)
    (node:30105) [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.

Any ideas?

Can you get the status update with the internet blocked or is it just actions that are blocked?
If you run the following script in njstuya does it show your devices

 python njstuyamonitor.py
#or
python node_modules/njstuya/scripts/njstuyamonitor.py

Are the devices broadcasting?
There has been quite a bit of chatter about about firmware recently, but I think it’s mainly around trying to update the firmware. But there might be something there about what the devices are looking for and how to emulate it. The thread has a few hundred posts in the last couple of weeks, so you might find something there. Let me know if you find anything and there is something I can do to help get it working.

Hi
thanks for the quick response!

With Internet Access
node njstuya.js … STATE returns the actual state of the plug.
node njstuya.js … ON returns ON but does not switch the plug on
node njstuya.js … OFF returns OFF but does not switch the plug off
node njstuya.js … TOGGLE if you tried an ON command before the the TOGGLE it will return ON regardless
of the state of the switch
node njstuya.js … -set '{“dps”: 1, “set”:true} returns ON and switches the plug on
node njstuya.js … -set '{“dps”: 1, “set”:false} returns OFF and switches the plug off

Without Internet Access
node njstuya.js … STATE timeout
node njstuya.js … ON timeout
node njstuya.js … OFF timeout
node njstuya.js … TOGGLE timeout
node njstuya.js … -set '{“dps”: 1, “set”:true} timeout
node njstuya.js … -set '{“dps”: 1, “set”:false} timeout

python njstuyamonitor.py (when in folder with the file). Same result with or without internet access
returns:

Traceback (most recent call last):
  File "node_modules/njstuya/scripts/njstuyamonitor.py", line 9, in <module>
    from timeout import timeout
ImportError: No module named timeout

I’ll have to dig out wireshark to see what is going over the ‘wires’.
gary

Hi

thanks for the quick response!

With Internet Access

node njstuya.js … STATE returns the actual state of the plug.
node njstuya.js … ON returns ON but does not switch the plug on
node njstuya.js … OFF returns OFF but does not switch the plug off
node njstuya.js … TOGGLE if you tried an ON command before the the TOGGLE it will return ON regardless
of the state of the switch
node njstuya.js … -set '{“dps”: 1, “set”:true} returns ON and switches the plug on
node njstuya.js … -set '{“dps”: 1, “set”:false} returns OFF and switches the plug off

Without Internet Access
node njstuya.js … STATE timeout
node njstuya.js … ON timeout
node njstuya.js … OFF timeout
node njstuya.js … TOGGLE timeout
node njstuya.js … -set '{“dps”: 1, “set”:true} timeout
node njstuya.js … -set '{“dps”: 1, “set”:false} timeout

python njstuyamonitor.py (when in folder with the file). Same result with or without internet access
returns:
Traceback (most recent call last):
  File "node_modules/njstuya/scripts/njstuyamonitor.py", line 9, in <module>
    from timeout import timeout
ImportError: No module named timeout

I’ll have to dig out wireshark to see what is going over the ‘wires’.

gary

Try getting the latest version of njstuya.
npm install unparagoned/njstuya Then replace the scripts with the new ones.
I move the timout function into the script for windows compatibility so it shouldn’t be trying to import timeout in the last few releases.

I’ve also changed the way the script works. It used to resolve and get the state of the item before sending a command. If your device stopped sending status updates when the internet is down then it might have timed out before the command was sent. Let me know if the latest version of the script works.

@Dominic_Bonneau The latest version should fix your speed problem. Some devices don’t broadcast their status that often, in which case the previous version of the script would run as slow as those broadcasts.

hi,
thanks for the tip. I did this and it solved the problem and the response is noticeably quicker.
gary

First of all thank you for this great script.
I have a Tuya PowerSwitch with a LED indicator.
When I use the command node njstuya.js -ip x.x.x.x -id xxxxxxxxxxxxx -key xxxxxxxxxx ON
The LED goes ON so that is working fine.
And on the console i get the return message ON
Now i want to control the PowerSwitch using the dps settings
I have used all kind of numbers but nothing seems to work (always returning ON in the console)
node njstuya.js -ip x.x.x.x -id xxxxxxxxxxxxx -key xxxxxxxxxx -set ‘{ “dps”:1, “set”: false }’

Is there a log file or something to see what goes wrong? any help would be great.

Sorry, I broke the dps option in the previous update. I’ve fixed it now, so just update to the latest version and dps should work. npm install unparagoned/njstuya Then replace the scripts with the new ones.
Also if you stick debug at the end of the command you get a bit more information.
node njstuya.js -ip x.x.x.x -id xxxxxxxxxxxxx -key xxxxxxxxxx -set ‘{ “dps”:1, “set”: false }’ debug

Thank you for the quick response.

with node njstuya.js -ip x.x.x.x -id xxxxxxxxxxxxx -key xxxxxxxxxx -set ‘{“dps”:1, “set”:false}’
I can turn off the LED light now so the dps settings seems to work a lot better :slight_smile:
I still have a little issue:
dps 2 till 4 does nothing.
dps 5 and above switches the power but power is switched on automatically again. It looks like the script ties to switch it 3 times (i hear three clicks on the power switch) and then i get this error message:
node njstuya.js -ip x.x.x.x -id xxxxxxxxxxxxx -key xxxxxxxxxx -set ‘{“dps”:5, “set”:false}’
debug enabled
ip x.x.x.x id xxxxxxxxxxxxx key xxxxxxxxxx
Instant
new state:{“dps”:5,“set”:false}
Error: Timeout waiting for response - Try without IP to auto resolve IP

I have tried without IP address and get the same response. It looks like the PowerSwitch is turning itself on again and does not send a response. any idea?

How many ports/switches do you have on that device? I’ve updated the script again, so now you can get the dps options and check them individually. If it’s undefined it will say so rather than giving an ON/OFF state. Try it for 0,1…5. If you are getting timeout errors on everything then check your id and key.
The following should show you the state all the available dps and the associated numbers

node njstuya.js -ip x.x.x.x -id xxxxxxxxxxxxx -key xxxxxxxxxx -get '{ "schema": true}'
If you want the state of a single dps then you can use the following.

node njstuya.js -ip x.x.x.x -id xxxxxxxxxxxxx -key xxxxxxxxxx -get ‘{ “dps”: 1}’ debug