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
Tue Aug 13, 2019 2:02 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: TP-Link WiFi Switches

Also... The #!/bin/sh MUST be the first line in the shell script

Posted on
Tue Aug 13, 2019 2:15 am
ChopOMatic offline
Posts: 110
Joined: Sep 12, 2014

Re: TP-Link WiFi Switches

Success on pulling the state and putting in a variable!

My only remaining thing to figure out now is why the ON and OFF scripts aren't working from within Indigo. They work perfectly from the command line, turning the switches on and off as instructed. They generate no errors when run from Indigo, but they're not turning the switches on or off. (I've set the permissions for both.)

Posted on
Tue Aug 13, 2019 2:29 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: TP-Link WiFi Switches

ChopOMatic wrote:
...My only remaining thing to figure out now is why the ON and OFF scripts aren't working from within Indigo. They work perfectly from the command line, turning the switches on and off as instructed. They generate no errors when run from Indigo, but they're not turning the switches on or off. (I've set the permissions for both.)
Post both scripts here along with a description of how you run them, and we'll have a look.

Posted on
Tue Aug 13, 2019 2:35 am
ChopOMatic offline
Posts: 110
Joined: Sep 12, 2014

Re: TP-Link WiFi Switches

Here you go...
Attachments
hs100-3.png
hs100-3.png (177.39 KiB) Viewed 6305 times

Posted on
Tue Aug 13, 2019 3:46 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: TP-Link WiFi Switches

ChopOMatic wrote:
Here you go...
OK!
First, let's make this as simple as possible and combine all three functions into one. Save this as /usr/local/bin/gkmain.sh
Code: Select all
#!/bin/sh
 
FUNC=$1
if [[ $FUNC = "on" ]] || [[ $FUNC = "off" ]];then
        /usr/local./bin/tplink_smartplug.py -t 10.0.0.26 -c${FUNC}
        /usr/local./bin/tplink_smartplug.py -t 10.0.0.26 -c info | json_pp | grep relay_state | cut -c26 | tr -d "\n"
elif [[ $FUNC = "status" ]];then
        /usr/local./bin/tplink_smartplug.py -t 10.0.0.26 -c info | json_pp | grep relay_state | cut -c26 | tr -d "\n"
else
        echo Error: on, off or status required
        exit 1
fi

exit 0
Now, in the Run Shell Script dialog of an action just call this script with one of the arguments: on, off or status. For example:
Code: Select all
/usr/local/bin/gkmain.sh on
Here is how it works. The command argument (on, off or status) is passed to the shell script as "$1" and for good practice we assign that value to a variable FUNC (called as $FUNC or ${FUNC}). Then, we check if $FUNC is on or off, and if it is, execute the tplink script with $FUNC as the value for the -c argument. And, we also get the status back from the device just to be sure it is on or off. If the command is status we just execute the status command you already have working. Now, all you have to do is to create three action groups: for on, off and status. That's it. At this point, you have everything needed to creat an Indigo virtual device

Note, it would be quite easy to also add a toggle function to this script. The idea being, get the status, and then issue a command to set the opposite state.
Oh, and, make sure gkmain.sh is set to 755 (executable by all)
Code: Select all
$ ls -l gkmain.sh
-rwxr-xr-x  2   root wheel  584 Aug 13 08:57 gkmain.sh
and that the first line in gkmain.sh is the #!/bin/sh

Posted on
Tue Aug 13, 2019 4:11 am
ChopOMatic offline
Posts: 110
Joined: Sep 12, 2014

Re: TP-Link WiFi Switches

Sweet. ON and OFF working perfectly from Indigo, controlling the switch. STATUS is not. All generate an error from the command line.
Attachments
hs100-4.png
hs100-4.png (54.22 KiB) Viewed 6325 times

Posted on
Tue Aug 13, 2019 4:31 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: TP-Link WiFi Switches

ChopOMatic wrote:
...STATUS is not. All generate an error from the command line.
First, I see there is output from the on and off command, so let's get rid of that. Then, are you using the unmodified (original) version of tplink_smartplug.py? If you are, you need the alternate version of the command line I posted earlier. Here is the script with changes for both issues and a typo:
Code: Select all
#!/bin/sh
 
FUNC=$1
if [[ $FUNC = "on" ]] || [[ $FUNC = "off" ]];then
        /usr/local/bin/tplink_smartplug.py -t 10.0.0.26 -c${FUNC} >/dev/null 2>&1
       /usr/local/bin/tplink_smartplug.py -t 10.0.0.26 -c info | sed -e "s/Received..//" | json_pp | grep relay_state | cut -c26 | tr -d "\n"
elif [[ $FUNC = "status" ]];then
       /usr/local/bin/tplink_smartplug.py -t 10.0.0.26 -c info | sed -e "s/Received..//" | json_pp | grep relay_state | cut -c26 | tr -d "\n"
else
        echo Error: on, off or status required
        exit 1
fi
exit 0
If this doesn't fix it, run this and post the output. Use cut and paste from the terminal window.
Code: Select all
/usr/local/bin/tplink_smartplug.py -t 10.0.0.26 -c info

Posted on
Tue Aug 13, 2019 8:23 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: TP-Link WiFi Switches

BTW... this will be resolved within 24 hours, I have ordered an HS110 - with consumption measurement,

Posted on
Tue Aug 13, 2019 9:30 pm
ChopOMatic offline
Posts: 110
Joined: Sep 12, 2014

Re: TP-Link WiFi Switches

All working PERFECTLY.

Thank you!

Posted on
Wed Aug 14, 2019 2:22 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: TP-Link WiFi Switches

ChopOMatic wrote:
All working PERFECTLY.
Good news!

Hopefully (I.e. if the french post cooperates) I will have a SmartPlug here this afternoon and can see if there are any ways to improve things. I will also look at @FlyingDiver's code snippet and see if it might offer any advantages over a shell script.

Finally, back to the beginning of this thread where
jay (support) wrote:
No need for a plugin really - just execute a run shell script action that points to that script.
There is one possible advantage to a plugin. Given the tplink_smartplug.py code, it would possible to fully configure the SmartPlug without using the Kasa app. The only advantage of doing this in a plugin would be the ability to set some options that are not available in Kasa. For example, setting the cloud service url to some phantom address to keep the plug from phoning home. However, it is not at all clear that small advantage would be with the effort, especially since it can already be done from the command line anyway.

Posted on
Wed Aug 14, 2019 12:48 pm
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: TP-Link WiFi Switches

My HS110 has arrived and is installed and , as promised, I have tweaked the control script a bit. Here it is
Code: Select all
#!/bin/zsh
 
TPLINK=/usr/local/bin/tplink_smartplug.py
PLUGADDR=192.168.5.113

#### Do not change anything below this line ####
FUNC=$1

alias tpOn='$TPLINK -t $PLUGADDR -c on >/dev/null 2>&1'
alias tpOff='$TPLINK -t $PLUGADDR -c off >/dev/null 2>&1'
alias tpStatus='$TPLINK -t $PLUGADDR -c info | grep Received | sed -e "s/Received..//" | json_pp | grep relay_state | cut -c26 | tr -d "\n"'

case $FUNC in
   on)
      tpOn
      tpStatus
      ;;
   off)
      tpOff
      tpStatus
      ;;
   toggle)
      STATE=`tpStatus`
      case $STATE in
         0)
            tpOn
            tpStatus
            ;;
         1)
            tpOff
            tpStatus
            ;;
         *)
            echo Unknown state $STATE received from plug
                    exit 1
            ;;
      esac
      ;;
   status)
      tpStatus
      ;;
   *)
           echo Error: on, off or status required
           exit 1
      ;;
esac

exit 0
All you need to do is specify the path to your copy of tplink_smartplug.py and the IP Address of your plug. The rest just works. Note, toggle is now supported. I have observed the Kasa app can track the state of the relay and am trying to figure out how it does it. So far, packet traces aren't showing anything. But, if or when I figure this out, I should be able to add support for automatically tracking the plugs on/off state and, maybe, resource usage.

BTW, so far I am reasonably happy with the plug. But, I sure wish it would work on an open Wi-Fi network - maybe there is a hack for that.

Posted on
Wed Aug 14, 2019 1:44 pm
ChopOMatic offline
Posts: 110
Joined: Sep 12, 2014

Re: TP-Link WiFi Switches

Excellent. Many thanks. I look forward to trying out the new script later today.

Posted on
Thu Aug 15, 2019 10:26 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: TP-Link WiFi Switches

More progress. I now have my HS110 configured and running without having to use the kasa app. No phoning home and I can use my open Wi-Fi network.
After I figured it out, I stumbled on this page. He wrote it up better than I could.

Posted on
Sat Aug 17, 2019 1:27 am
ChopOMatic offline
Posts: 110
Joined: Sep 12, 2014

Re: TP-Link WiFi Switches

Working like a charm. Thanks again for this!

Posted on
Tue Aug 20, 2019 3:19 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: TP-Link WiFi Switches

Even more progress. I have a background script (run as a startup trigger) that continuously polls the HS110 to determine its state and get the current energy values (watts, volts & amps), which are then loaded into variables. The script is not resource intensive so leaving it running is not an issue. I am still working on trapping a few errors, for example, to keep the the script from crashing when the HS110 returns an empty or no response. I am also thinking of having two polling frequencies. Like every 10 seconds when the plug is off, but every 2 seconds when it is on. In any case, I would be happy to share the script if there is interest.

On the same front. The HS100/110 "phones home" on every state change. Though, unfortunately, those packets are encrypted and essentially unreadable. But, if I were able to fool the plug into talking to a local server instead of TPLink's, it would be possible to use the receipt of a packet, even if it were unreadable, to initiate a poll only when changes occurred. Stay tuned.

Who is online

Users browsing this forum: No registered users and 13 guests