TP-Link WiFi Switches

Forum rules

Questions about hardware that can be controlled by Indigo (but not through the interfaces and plugins listed). If Indigo doesn't support some bit of hardware you're interested in, and you don't find a 3rd Party Plugin for it, add it to this forum. Be sure to include links to as much information as you can find about it.

Note: adding it here does not mean we're going to add it - in fact it's possible one of our 3rd party developers may decide to write a plugin for it. We add hardware/features based on a lot of different factors beyond just having a request for it.

Posted on
Fri Aug 09, 2019 3:21 am
ChopOMatic offline
Posts: 110
Joined: Sep 12, 2014

Re: TP-Link WiFi Switches

I save this as a Python script and call it from Indigo, right?

Posted on
Fri Aug 09, 2019 3:22 am
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: TP-Link WiFi Switches

I would put it in an action group as an embedded script.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Fri Aug 09, 2019 3:27 am
ChopOMatic offline
Posts: 110
Joined: Sep 12, 2014

Re: TP-Link WiFi Switches

Getting this error:

Code: Select all
   Trigger                         hs100 test
   Script Error                    embedded script: global name 'requests' is not defined
   Script Error                    Exception Traceback (most recent call shown last):

     embedded script, line 4, at top level
NameError: global name 'requests' is not defined

Posted on
Fri Aug 09, 2019 3:29 am
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: TP-Link WiFi Switches

My bad. It's 5am here and I'm only up because I have insomnia. I really need to go back to bed.

Code: Select all
import json
import requests

# do the http request
reply = requests.get('python tplink_smartplug.py -t 10.0.0.26 -c info')

# convert the text result to Python dictionary
data = json.loads(result_json)

# extract the state
state = data['system']['get_sysinfo']['relay_state']

# update the variable (put in the correct variable ID)
indigo.variable.updateValue(1234567, value=state)

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Fri Aug 09, 2019 3:37 am
ChopOMatic offline
Posts: 110
Joined: Sep 12, 2014

Re: TP-Link WiFi Switches

No worries, Joe. Getting heavy-eyed here, too. Haven't been to bed yet. I appreciate the help a lot and we'll tackle it tomorrow if you're up for it. Again, thanks!

Posted on
Fri Aug 09, 2019 5:35 am
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: TP-Link WiFi Switches

That last wasn't quite right either.

Code: Select all
import requests

# do the http request
r = requests.get('python tplink_smartplug.py -t 10.0.0.26 -c info')

# get the results
data =  r.json()

# extract the state
state = data['system']['get_sysinfo']['relay_state']

# update the variable (put in the correct variable ID)
indigo.variable.updateValue(1234567, value=state)

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Fri Aug 09, 2019 5:38 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: TP-Link WiFi Switches

ChopOMatic wrote:
No worries, Joe. Getting heavy-eyed here, too. Haven't been to bed yet. I appreciate the help a lot and we'll tackle it tomorrow if you're up for it. Again, thanks!

Once you have this working (device state in a variable) it is pretty easy to cobble together a virtual device. You really just need two action groups: one to turn your device on, and another to turn it off. Those action groups should either directly update the state variable, or better, execute the bit of Python code to get the state and update the variable. If your device will be operated by some means outside of Indigo, you may want to run the Python script on a regular schedule, say once a minute, to keep your virtual device's state current. That is, unless the device is able to broadcast changes in the relay state - which does not seem to be the case, but you never know.

Posted on
Sat Aug 10, 2019 3:40 pm
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: TP-Link WiFi Switches

As a slight aside (not seen this thread before), will the GhostXML plugin help?

I think it has a script input option, which the plugin would then populate into device states?


Sent from my iPhone using Tapatalk Pro

Posted on
Sun Aug 11, 2019 11:00 am
ChopOMatic offline
Posts: 110
Joined: Sep 12, 2014

Re: TP-Link WiFi Switches

Joe, sorry to vanish on you after asking for help. I continue to get errors:

Code: Select all
Script Error                    embedded script: HTTPConnectionPool(host='usr', port=80): Max retries exceeded with url: /bin/python%20tplink_smartplug.py%20-t%2010.0.0.26%20-c%20info (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x10ee54fd0>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',))
   Script Error                    Exception Traceback (most recent call shown last):


As to howart's suggestion, I changed the script to write out the tplink info to a .json file and set up a GhostXML device to pull from that file. After a bit of trial and error, it all looks clean in the event log, but I never see any results and the GhostXML device also goes to ERROR in the main Indigo window as soon as it's loaded.

Posted on
Sun Aug 11, 2019 11:10 am
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: TP-Link WiFi Switches

I have no idea what I was thinking when I wrote all those messages. I claim sleep deprivation from coding the MQTT plying. You don't use requests to run a python script.

This might work. But if possible it would be better to modify the tplink_smartplug.py to update the variable directly and run it as an external script.

Code: Select all
import subprocess
import json

# do the http request
r =subprocess.call(['python', 'tplink_smartplug.py', '-t', '10.0.0.26', '-c', 'info'])

# get the results
data =  json.loads(r)

# extract the state
state = data['system']['get_sysinfo']['relay_state']

# update the variable (put in the correct variable ID)
indigo.variable.updateValue(1234567, value=state)

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Aug 11, 2019 2:28 pm
ChopOMatic offline
Posts: 110
Joined: Sep 12, 2014

Re: TP-Link WiFi Switches

That doesn't work, either.

What would I add to the Python script and where in order to update the variable?

Posted on
Sun Aug 11, 2019 2:29 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: TP-Link WiFi Switches

ChopOMatic wrote:
That doesn't work, either.

What would I add to the Python script and where in order to update the variable?


Could not say without looking at the script. Was the original posted somewhere?

Oh, this one? https://github.com/softScheck/tplink-smartplug

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Aug 11, 2019 2:55 pm
ChopOMatic offline
Posts: 110
Joined: Sep 12, 2014

Re: TP-Link WiFi Switches

Yup, that's the one.

Posted on
Sun Aug 11, 2019 3:04 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: TP-Link WiFi Switches

Short answer is, you need "import indigo" at the beginning, then

Code: Select all
state = data['system']['get_sysinfo']['relay_state']
indigo.variable.updateValue(1234567, value=state)


At the end. But I'm not sure how hard it's going to be to execute that as an embedded script and pass it the command line arguments.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Aug 11, 2019 3:09 pm
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: TP-Link WiFi Switches

You might just want to try a short shell script and save the output to a variable. There may be some issues with Indigo not interpreting the saved 0 or 1 as a boolean (on or off). But, if you at least get a value into a variable, we can work on resolving how it is interpreted. Here's how to do it...
  1. Create a file called tplink.sh in your home directory and paste the following 2 lines into that file.
    Code: Select all
    #!/bin/sh
    python /Users/<you>/tplink_smartplug.py -t 10.0.0.26 -c info | grep relay_state | cut -c25
    This assumes tplink_smartplug.py is in your home directory. If not, adjust the path.
  2. then, from the terminal, go (cd) to your home directory and enter this command to make the script executable
    Code: Select all
    chmod 755 tplink.sh
  3. Now, in Indigo create an action: Server Actions -> Script and File Actions -> Run Shell Script
    and enter the full path to the script. Like /Users/<you>/tplink.sh
  4. Select Store result in avariable
Execute the action and you should see a 0 or 1 as your variable value.


*

Who is online

Users browsing this forum: No registered users and 8 guests