TP-Link smart plug HS100/HS110

Others (MiLight, Hue, Toon etc...)

Moderator: leecollings

ikek
Posts: 2
Joined: Sunday 05 February 2017 17:15
Target OS: Windows
Domoticz version:
Contact:

Re: TP-Link smart plug HS100/HS110

Post by ikek »

MikeF wrote:I'm really at a loss to explain this. I just downloaded the python script again (on my Apple Mac), saved it with a different filename (HS110.py), and copied it across to my Raspberry Pi that runs Domoticz. I changed the 'on' and 'off' commands to pick up the new filename, clicked on the dummy switch icon in Domoticz, and turned it off and back on again (monitoring it via the Kasa app).

The permissions I have on the Pi are:

Code: Select all

-rwxr-xr-x 1 pi pi 3062 Oct  6 22:21 HS110.py
The Domoticz command I have is:

Code: Select all

script:///home/pi/devices/HS110.py -t 192.168.0.84 -c on
SOLVED!
I had the same problem. Rewrote the script for python 3.4 and created Domoticz environment on Windows 10 PC. That worked fine. So Raspberry interpreter has a bug. Therefore I won't use python scripts anymore in the switch On Action and Off Action. Instead I wrote a shell command On.sh and Off.sh with the corresponding python command in it. Put these in the scripts directory and made these executable with chmod. With the On.sh and Off.sh scripts (including full path) in the switch commands On Action and Off Action everything works fine!
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: TP-Link smart plug HS100/HS110

Post by MikeF »

ikek wrote:So Raspberry interpreter has a bug. Therefore I won't use python scripts anymore in the switch On Action and Off Action.
This - and other python (2.7) - on / off script/s work fine for me on my RPi, so not clear what the 'Raspberry interpreter bug' is... :?
Hesmink
Posts: 168
Joined: Monday 22 June 2015 10:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

Re: TP-Link smart plug HS100/HS110

Post by Hesmink »

Thanks for the info, I got a HS110 to see if I can get a notification when my laundry is done.

I had to change the error handling at the end of the HS110.py script to:

Code: Select all

# Send data to Domoticz
try:
    url = baseURL + "&idx=%s&svalue=%s;%s" % (HSIdx, power, total)
    domoticzrequest(url)
except urllib2.URLError, e:
    if hasattr(e, 'reason'): # <--
        print 'We failed to reach the Domoticz server.'
        print 'Reason: ', e.reason
    elif hasattr(e, 'code'): # <--
        print 'The Domoticz server couldn\'t fulfill the request.'
        print 'Error code: ', e.code
Apparently you can get both variants of this error, and only one of them has a value.
I got a trace dump that 'code' was unknown when I forgot to add the port to the Domoticz ip.
Hesmink
Posts: 168
Joined: Monday 22 June 2015 10:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

Re: TP-Link smart plug HS100/HS110

Post by Hesmink »

I noticed the python script sometimes doesn't exit (I had 89 of them still running).
Anyone noticed this and has a fix available?
wube
Posts: 10
Joined: Wednesday 18 October 2017 12:56
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: TP-Link smart plug HS100/HS110

Post by wube »

Hi Everyone,
I have following error while using .sh script. Anyone can help me out and tell me why?

OK, i got this over with, now i get this message:
"The nc programme for sending data over the network isn't in the path, communication with the plug will fail"

Anyone?
DannyElfman
Posts: 16
Joined: Monday 13 October 2014 14:05
Target OS: Linux
Domoticz version: 3.8136
Location: France
Contact:

Re: TP-Link smart plug HS100/HS110

Post by DannyElfman »

You do not give enough info.
OS ?
Domoticz version ?
How to you send the order to the plug ? What is your command line ?
Debian Jessie VM on EsxI on a Gen 8 Proliant + RFXCom + AeonLabs Zwave+ + Fibaro eye + Fibaro Smoke + Fibaro FGS211 Philips Hue + Harmony Hub + Somfy RTS + Oregon Temp sensors + Xiaomi Gateway + Xiaomi sensors + Sonoff basic
Mace
Posts: 65
Joined: Monday 21 August 2017 19:52
Target OS: Windows
Domoticz version: 3.8153
Location: Rhoon
Contact:

Re: TP-Link smart plug HS110 - send energy data to Domoticz

Post by Mace »

MikeF wrote: Monday 03 October 2016 11:58 I have now created a python script to send energy data from the TP-Link HS110 Smart Plug to Domoticz:
Spoiler: show

Code: Select all

#!/usr/bin/env python
# 
# TP-Link Wi-Fi Smart Plug Protocol Client
# For use with TP-Link HS110: energy monitor
# 
# Gets current power (W) and cumulative energy (kWh)
# and sends to Domoticz

import socket
import argparse
import json
import urllib
import urllib2

# ip, port, and command for HS110
ip = '<IP of HS110>'
port = 9999
cmd = '{"emeter":{"get_realtime":{}}}'

# Domoticz command stub and IDx of HS110
baseURL = 'http://<Domoticz IP:port>/json.htm?type=command&param=udevice&nvalue=0'
HSIdx = <Domoticz IDx of HS110>

# Encryption and Decryption of TP-Link Smart Home Protocol
# XOR Autokey Cipher with starting key = 171
def encrypt(string):
	key = 171
	result = "\0\0\0\0"
	for i in string: 
		a = key ^ ord(i)
		key = a
		result += chr(a)
	return result

def decrypt(string):
	key = 171 
	result = ""
	for i in string: 
		a = key ^ ord(i)
		key = ord(i) 
		result += chr(a)
	return result

def domoticzrequest (url):
   request = urllib2.Request(url)
#   request.add_header("Authorization", "Basic %s" % base64string)
   response = urllib2.urlopen(request)
   return None;

# Send command and receive reply 
try:
	sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sock_tcp.connect((ip, port))
	sock_tcp.send(encrypt(cmd))
	data = sock_tcp.recv(2048)
	sock_tcp.close()
	
#	print "Sent:     ", cmd
	result = decrypt(data[4:])
	jsonData = json.loads(result)
#	print "Received: "
#	print json.dumps(jsonData, indent=4, sort_keys=True)
	power = jsonData['emeter']['get_realtime']['power']
	total = jsonData['emeter']['get_realtime']['total'] * 1000
#	print power, total
except socket.error:
	quit("Cound not connect to host " + ip + ":" + str(port))

# Send data to Domoticz
try:
    url = baseURL + "&idx=%s&svalue=%s;%s" % (HSIdx, power, total)
    domoticzrequest(url)
except urllib2.URLError, e:
	print e.code
Create a virtual sensor with sensor type 'Electric (Instant+Counter)', and insert its IDx and the IP addresses of your HS110 and Domoticz server in the script. Then set up a crontab entry to run the script every 5 or 10 minutes, as you prefer.

It will appear in the Utilities page - here's an example:
Image
Can't get it to work. When I run ./tplink-smartplug-energy.py, I get the following back:
Traceback (most recent call last):
File "./tplink-smartplug-energy.py", line 72, in <module>
domoticzrequest(url)
File "./tplink-smartplug-energy.py", line 47, in domoticzrequest
response = urllib2.urlopen(request)
File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 429, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 447, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1228, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1167, in do_open
h = http_class(host, timeout=req.timeout, **http_conn_args)
File "/usr/lib/python2.7/httplib.py", line 736, in __init__
(self.host, self.port) = self._get_hostport(host, port)
File "/usr/lib/python2.7/httplib.py", line 777, in _get_hostport
raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
httplib.InvalidURL: nonnumeric port: 'port>'

Any help would help
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: TP-Link smart plug HS100/HS110

Post by MikeF »

Can you check your editing, where you substituted your values for text in <angle brackets> - the last line of the error message

Code: Select all

httplib.InvalidURL: nonnumeric port: 'port>'
suggests there is a spurious '>' somewhere.
Mace
Posts: 65
Joined: Monday 21 August 2017 19:52
Target OS: Windows
Domoticz version: 3.8153
Location: Rhoon
Contact:

Re: TP-Link smart plug HS100/HS110

Post by Mace »

Thanks Mike! I didn't spot the following:
baseURL = 'http://<Domoticz IP:port>/json.htm?type=command&param=udevice&nvalue=0'

As soon as I changed the values to match my installation, it started working like a charm!
alicetharvey
Posts: 2
Joined: Thursday 28 December 2017 5:39
Target OS: -
Domoticz version:
Contact:

Re: TP-Link smart plug HS100/HS110

Post by alicetharvey »

I am using ELF Smart Plug, bought last month from equeshome. It controls plugged-in lights, devices and appliances from anywhere, through your home's WiFi via its free app. Are smart plugs available with HS100/HS110? How they works?
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: TP-Link smart plug HS100/HS110

Post by MikeF »

Hello Alice,

The Eques Elf is a different smart plug to the TP-Link HS100 / HS110 smart plugs. The script I developed was based on a website (see post near the beginning of this thread) where someone had worked out the specific commands which these smart plugs respond to. As such, I would expect that the Eques Elf uses different commands, and someone would need to work out how to link these switches to Domoticz.

I don't have any knowledge or experience of the Eques Elf, I'm afraid.
alicetharvey
Posts: 2
Joined: Thursday 28 December 2017 5:39
Target OS: -
Domoticz version:
Contact:

Re: TP-Link smart plug HS100/HS110

Post by alicetharvey »

Hi Mike

Thanks for your revert.

I am just looking to collect more information about home automation systems.

Thanks for your help :)
matzeb74
Posts: 49
Joined: Sunday 07 January 2018 19:23
Target OS: Raspberry Pi / ODroid
Domoticz version: newest
Location: Stuttgart
Contact:

Re: TP-Link smart plug HS100/HS110

Post by matzeb74 »

Hello,

I got an Error trying your meter Script.:

Code: Select all

domoticz@fipbox:~/domoticz/scripts $ ./tplink-smartplug-energy.py
/usr/bin/python: can't find '__main__' module in 'python'
Where does the error come from?

Error solved, was just a typo.

thx for great script!!!
Olfer
Posts: 21
Joined: Thursday 17 November 2016 10:28
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Germany
Contact:

Re: TP-Link smart plug HS100/HS110

Post by Olfer »

Tried it out! Is working ootb like a charm! Just want to leave a huge thank you! I use the HS-110 to measure loads greater than 2,5kW.
Domoticz on RPi3 with Aeontec Z-Stick gen5. Beta.
Z-Wave, Zigbee, WiFi and MQTT (mainly Tasmota), Neo Cool Cam, Devolo, Fibaro, Shelly, Hue, Osram, Blitzwolf, TP-Link, Alexa. Latest: Somfy IO integration
matzeb74
Posts: 49
Joined: Sunday 07 January 2018 19:23
Target OS: Raspberry Pi / ODroid
Domoticz version: newest
Location: Stuttgart
Contact:

Re: TP-Link smart plug HS110 - send energy data to Domoticz

Post by matzeb74 »


Then set up a crontab entry to run the script every 5 or 10 minutes, as you prefer.

Can you please explain how to do this?
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: TP-Link smart plug HS110 - send energy data to Domoticz

Post by MikeF »

matzeb74 wrote: Monday 22 January 2018 18:18

Then set up a crontab entry to run the script every 5 or 10 minutes, as you prefer.

Can you please explain how to do this?
I don't know what your hardware setup is, but here's how to do it if you're running Domoticz on a Raspberry Pi:

Using the command line (Terminal from RPi desktop, or SSH via PuTTy in Windows / Terminal in macOS), enter

Code: Select all

crontab -e
Then add the following line (for every 5 minutes):

Code: Select all

*/5 * * * * sudo python /home/pi/devices/HS110.py >/dev/null 2>&1 &
or this (for every 10 minutes):

Code: Select all

*/10 * * * * sudo python /home/pi/devices/HS110.py >/dev/null 2>&1 &
(assuming you have saved the script as /home/pi/devices/HS110.py - if not, change)
Then save and exit the text editor.
matzeb74
Posts: 49
Joined: Sunday 07 January 2018 19:23
Target OS: Raspberry Pi / ODroid
Domoticz version: newest
Location: Stuttgart
Contact:

Re: TP-Link smart plug HS110 - send energy data to Domoticz

Post by matzeb74 »

MikeF wrote:
matzeb74 wrote: Monday 22 January 2018 18:18

Then set up a crontab entry to run the script every 5 or 10 minutes, as you prefer.

Can you please explain how to do this?
I don't know what your hardware setup is, but here's how to do it if you're running Domoticz on a Raspberry Pi:

Using the command line (Terminal from RPi desktop, or SSH via PuTTy in Windows / Terminal in macOS), enter

Code: Select all

crontab -e
Then add the following line (for every 5 minutes):

Code: Select all

*/5 * * * * sudo python /home/pi/devices/HS110.py >/dev/null 2>&1 &
or this (for every 10 minutes):

Code: Select all

*/10 * * * * sudo python /home/pi/devices/HS110.py >/dev/null 2>&1 &
(assuming you have saved the script as /home/pi/devices/HS110.py - if not, change)
Then save and exit the text editor.
Many many thanks. Thats it. I'll try after work.

Gesendet von meinem SM-G935F mit Tapatalk

MnM001
Posts: 4
Joined: Wednesday 31 January 2018 3:42
Target OS: Linux
Domoticz version:
Contact:

Re: TP-Link smart plug HS100/HS110

Post by MnM001 »

I am seeing some errors with the script.
I am running ubuntu with python 2.7:

path (tried from /home/domo same result)
/home/domo/domoticz/scripts

Code: Select all

sudo ./H110.py
error

Code: Select all

Traceback (most recent call last):
  File "./H110.py", line 60, in <module>
    jsonData = json.loads(result)
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
Hope someone can help me fix this so I can get power stats out of my HS110 unit.

Thanks

Edit:

more information - when I run

Code: Select all

python tplink-smartplug.py -t 192.168.0.151 -c time
I get this reply:

Code: Select all

Sent:      {"time":{"get_time":{}}}
Received:
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: TP-Link smart plug HS100/HS110

Post by MikeF »

@MnM001,

Can you check that you've got the correct ip and port for your HS110 in lines 16 & 17 in the script (providing you've not changed it), and also that you've substituted the Domoticz parameters correctly in lines 21 & 22?
MnM001
Posts: 4
Joined: Wednesday 31 January 2018 3:42
Target OS: Linux
Domoticz version:
Contact:

Re: TP-Link smart plug HS100/HS110

Post by MnM001 »

Hi,

Thanks for looking

Below are my lines from the script:

Code: Select all

# ip, port, and command for HS110
ip = '192.168.0.151'
port = 9999
cmd = '{"emeter":{"get_realtime":{}}}'

# Domoticz command stub and IDx of HS110
baseURL = 'http://192.168.0.183:8080/json.htm?type=command&param=udevice&nvalue=0'
HSIdx = 4
IP of the plug is correct and it responds to pings.
Port - I have no idea I thought it is the default port for the HS110?

Domoticz server IP and port are correct.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest