TP-Link smart plug HS100/HS110

Others (MiLight, Hue, Toon etc...)
ollebull
Posts: 12
Joined: Saturday 10 October 2015 10:06
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: TP-Link smart plug HS100/HS110

Post by ollebull » Thursday 06 September 2018 9:39

Hi, i'm running Domoticz on a Windows machine can i still run this script so that i can control and read from HS110.
I saw for instance this line in your py-script " domain="http://rpi3:8080/" ", since i'm not a programmer i'm not sure how to replace this
for a windows machine, and if there more places that could be affected.
regards Ollebull

ajay100
Posts: 22
Joined: Monday 07 August 2017 15:01
Target OS: Raspberry Pi
Domoticz version: 3.9560
Location: Victoria, Australia
Contact:

Re: TP-Link smart plug HS100/HS110

Post by ajay100 » Thursday 06 September 2018 13:41

Hi ollebull,

If you are running Domoticz on your Windows machine, have a look at your browser address bar to see how you are accessing Domoticz. It might be something like http://127.0.0.1:8080 or http://192.168.0.1:8080. Whatever is there, that's what you use for the domain= variable (with the trailing slash (/)) in the python script.

You need to have Python installed on your Windows PC, as Domoticz doesn't do that for you. This HS1xx script uses Python version 2, not version 3 (they are a little incompatible).

The way to check if you have python installed is to go to a command prompt (windows key-R, or Start -> run then type cmd) and then type python at the prompt. If you get a response rather than an error (such as ''python is not recognized as an internal or external command, operable program or batch file.") then you have python installed. The response will tell you which version is installed (2.x or 3.x).

If you need to install python, you can start with 'Windows x86 MSI installer' from https://www.python.org/downloads/windows/ (click the link 'Latest Python 2 Release - Python 2.7.xx'). Apparently Domoticz works best with the x86 version (not 64 bit).

Once you get to that point, your Windows PC should work as well as a linux box for python scripting. In the following post, you can see how the commands work: viewtopic.php?t=17391#p132472 but use .bat instead of .cmd.

This may be a bridge too far if you don't have a little experience with command prompts, etc. on Windows. I hope it helps.

Cheers - Andrew

Marco31
Posts: 2
Joined: Friday 07 September 2018 17:00
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: TP-Link smart plug HS100/HS110

Post by Marco31 » Sunday 09 September 2018 13:22

Hello,
I've installed Domoticz on a raspberry to record the power consumption history with a HS-110.
I've successfully added the device to the dashboard and I'm able to control it
I have 2 questions about the data recovery :
1 - I don't understand how the "Virtual sensors" are linked to the hardware IP adress. I've simply added the idx of the sensors to the editable variables section of the script, but all the sensors are in red. Perhaps that's due to the script error return code?

2 - I get an error when I run the script with the energy option :

Code: Select all

pi@raspberrypi:/home/marc/domoticz/scripts/python$ ./tplink_hs110_1.py -c energy
Traceback (most recent call last):
  File "./tplink_hs110_1.py", line 238, in <module>
    hs.read_hs110()
  File "./tplink_hs110_1.py", line 224, in read_hs110
    self.send_json(received_data)
  File "./tplink_hs110_1.py", line 155, in send_json
    voltage = round(float(json_data['emeter']['get_realtime']['voltage_mv']) / 1000,2)
KeyError: 'voltage_mv'
The info option works fine :

Code: Select all

pi@raspberrypi:/home/marc/domoticz/scripts/python$ ./tplink_hs110_1.py -c info
Sent:      {"system":{"get_sysinfo":{}}}
Received:  {"system":{"get_sysinfo":{"err_code":0,"sw_ver":"1.2.5 Build 180410 Rel.182657","hw_ver":"1.0","type":"IOT.SMARTPLUGSWITCH","model":"HS110(FR)","mac":"50:C7:BF:08:68:15","deviceId":"800643B60A0A5AA51473F47BFFD9E3FA1781ED98","hwId":"797CB3D09D9A83B1C43A623C6DD103C2","fwId":"00000000000000000000000000000000","oemId":"0D13F703F04C040DC3D27DB248C01A1A","alias":"My 1st Smart Plug","dev_name":"Wi-Fi Smart Plug With Energy Monitoring","icon_hash":"","relay_state":1,"on_time":17765,"active_mode":"schedule","feature":"TIM:ENE","updating":0,"rssi":-64,"led_off":0,"latitude":43.779649,"longitude":1.399550}}}
The generated debug shows that the values are correctly gathered :

Code: Select all

pi@raspberrypi:/home/marc/domoticz/scripts/python$ tail -4 hs110-1.log
2018-09-07 17:01:25,730:__main__:INFO:hs110-1 version 0.5 has started...
2018-09-07 17:01:25,777:__main__:DEBUG:Command: {"emeter":{"get_realtime":{}}}
2018-09-07 17:01:25,803:__main__:DEBUG:Sent:     {"emeter":{"get_realtime":{}}}
2018-09-07 17:01:25,805:__main__:DEBUG:Received: {"emeter":{"get_realtime":{"current":0.644156,"voltage":230.228466,"power":80.957878,"total":0.262000,"err_code":0}}}
pi@raspberrypi:/home/marc/domoticz/scripts/python$ more tplink_hs110_1.py 
#!/usr/bin/env python2
Also, here it is the config section of the script :

Code: Select all

...
# Begin user editable variables
version = 0.5
logger_name = "hs110-1"  #used for log file names, messages, etc
debug_level="DEBUG"  # debug options DEBUG, INFO, WARNING, ERROR, CRITICAL
delay_time = 15 #update time in seconds
domain="http://rpi3:8080/"
base_url = domain + "json.htm?type=command&param=udevice&nvalue=0"
monitor_list = ["voltage","current","power","usage"]
domoticz_idx = [1,2,3,5]
hs110_ip = "192.168.1.64"
text_logging = True
track_state = True
hs110_switch_idx = 4
datafile_columns = "Time,Voltage,Current,Power (W),Usage (kWHr)"
dailyfile_columns = "Date-Time,Usage (kWHr)"
# End user editable variables
....
Does someone have an idea please?
Thanks in advance.

ajay100
Posts: 22
Joined: Monday 07 August 2017 15:01
Target OS: Raspberry Pi
Domoticz version: 3.9560
Location: Victoria, Australia
Contact:

Re: TP-Link smart plug HS100/HS110

Post by ajay100 » Sunday 09 September 2018 14:34

Hi Marco31,

It looks like you are really close to getting it working!
1 - I don't understand how the "Virtual sensors" are linked to the hardware IP adress. I've simply added the idx of the sensors to the editable variables section of the script, but all the sensors are in red. Perhaps that's due to the script error return code?
The hardware IP address (hs110_ip variable) is used when reading or 'commanding' the HS110. There is no connection between the hardware IP and the Domoticz device indexes. What you have done in the config section looks fine, assuming your Domoticz URL is on http://rpi3:8080/. You are correct in assuming that the red devices in Domoticz are a result of the error you are seeing when running the script directly.
2 - I get an error when I run the script with the energy option :
Can you check that the tplink_hs110_1.py file has UNIX style line endings, which should be LF (Line Feed) only, not CRLF (Carriage Return Line Feed)? I am using Notepad++ and when I click the 'Show all characters' button, I can see the line endings. To convert in Notepad++, choose Edit -> EOL Conversion -> UNIX/OSX format.

The other issue I came up against was having TABS in the file instead of spaces, particularly in the

Code: Select all

commands = {
        'info'     : '{"system":{"get_sysinfo":{}}}',
        'on'       : '{"system":{"set_relay_state":{"state":1}}}',
        'off'      : '{"system":{"set_relay_state":{"state":0}}}',
        'led_on'   : '{"system":{"set_led_off":{"off":0}}}',
        'led_off'  : '{"system":{"set_led_off":{"off":1}}}',
        'state'    : '{"system":{"get_sysinfo":{}}}',
        'cloudinfo': '{"cnCloud":{"get_info":{}}}',
        'wlanscan' : '{"netif":{"get_scaninfo":{"refresh":0}}}',
        'time'     : '{"time":{"get_time":{}}}',
        'schedule' : '{"schedule":{"get_rules":{}}}',
        'countdown': '{"count_down":{"get_rules":{}}}',
        'antitheft': '{"anti_theft":{"get_rules":{}}}',
        'reboot'   : '{"system":{"reboot":{"delay":1}}}',
        'reset'    : '{"system":{"reset":{"delay":1}}}',
        'energy'   : '{"emeter":{"get_realtime":{}}}'
    }
section.

If you copied the script directly from GitHub, it will depend on how you pasted/saved the script to the RPi as to whether these formatting errors may be an issue. BTW, I do my editing using FileZilla between the RPi and Notepad++ on a Windows 10 laptop.

Cheers - Andrew

Marco31
Posts: 2
Joined: Friday 07 September 2018 17:00
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: TP-Link smart plug HS100/HS110

Post by Marco31 » Friday 14 September 2018 19:43

Hi Andrew,
Many thanks for your reply. Unfortunately, I've checked all the points you've highlighted but still no success :-(
For the Url section, I didn't modified it as I wasn't sure that it was useful. Now its set to "http://192.168.1.253:8080" which is the working Domoticz URL (tested with my laptop).
Also, I've followed the notepad++ instructions you've provided (make sense as I was in the situation where CRLF and now it's only LF.
I get exactly the same behavior than before.
Do you have anymore ideas please?
Thanks.

Cheers.

Marc

ajay100
Posts: 22
Joined: Monday 07 August 2017 15:01
Target OS: Raspberry Pi
Domoticz version: 3.9560
Location: Victoria, Australia
Contact:

Re: TP-Link smart plug HS100/HS110

Post by ajay100 » Saturday 15 September 2018 4:13

Hi Marc,

I'm not a Python expert by any means but I will have a go. There's a good tutorial on KeyError here: https://airbrake.io/blog/python-excepti ... n-keyerror
the KeyError is raised when accessing an invalid key within a dict.
The dict setup for us is the code below, so we can be fairly sure the problem is in that area.

Code: Select all

    commands = {
        'info'     : '{"system":{"get_sysinfo":{}}}',
        'on'       : '{"system":{"set_relay_state":{"state":1}}}',
        'off'      : '{"system":{"set_relay_state":{"state":0}}}',
        'led_on'   : '{"system":{"set_led_off":{"off":0}}}',
        'led_off'  : '{"system":{"set_led_off":{"off":1}}}',
        'state'    : '{"system":{"get_sysinfo":{}}}',
        'cloudinfo': '{"cnCloud":{"get_info":{}}}',
        'wlanscan' : '{"netif":{"get_scaninfo":{"refresh":0}}}',
        'time'     : '{"time":{"get_time":{}}}',
        'schedule' : '{"schedule":{"get_rules":{}}}',
        'countdown': '{"count_down":{"get_rules":{}}}',
        'antitheft': '{"anti_theft":{"get_rules":{}}}',
        'reboot'   : '{"system":{"reboot":{"delay":1}}}',
        'reset'    : '{"system":{"reset":{"delay":1}}}',
        'energy'   : '{"emeter":{"get_realtime":{}}}'
    }
TABS show as (->) and spaces as dots (.) when Show all Characters is turned on in Notepad++. I also have the Tab Settings set as 'Replace by space' in Notepad++ Preferences, so that TABs are not automatically added. If you checked and there are no TABS in this section instead of spaces, we need to debug a bit further.

Firstly I would try pasting the above dict code block directly over yours, just in case you are missing a minor formatting issue.

If that doesn't help, we can add an extra debugging line after json_data = (my line 146) to see what is coming through:

Code: Select all

    json_data = json.loads(received_data)
    logger.debug("json_data: {}".format(json_data))
I see you have debug_level="DEBUG", which is the best option for catching problems. In addition to any error that may appear in the console, we can also look at the log file, as you have done. It is saved in the same folder as the program. (in my case HS110-1.log). For one power logging circuit, mine shows the following:
2018-09-15 11:53:23,780:__main__:DEBUG:Command: {"emeter":{"get_realtime":{}}}
2018-09-15 11:53:23,885:__main__:DEBUG:Sent: {"emeter":{"get_realtime":{}}}
2018-09-15 11:53:23,886:__main__:DEBUG:Received: {"emeter":{"get_realtime":{"voltage_mv":242153,"current_ma":14,"power_mw":0,"total_wh":2,"err_code":0}}}
2018-09-15 11:53:23,887:__main__:DEBUG:json_data: {u'emeter': {u'get_realtime': {u'total_wh': 2, u'current_ma': 14, u'power_mw': 0, u'voltage_mv': 242153, u'err_code': 0}}}
2018-09-15 11:53:23,887:__main__:DEBUG:IDX: 90, Item: voltage, Value: 242.15
2018-09-15 11:53:23,888:__main__:DEBUG:URL: http://rpi3:8080/json.htm?type=command& ... lue=242.15
2018-09-15 11:53:23,895:__main__:DEBUG:IDX: 91, Item: current, Value: 0.01
2018-09-15 11:53:23,896:__main__:DEBUG:URL: http://rpi3:8080/json.htm?type=command& ... value=0.01
2018-09-15 11:53:23,903:__main__:DEBUG:IDX: 108, Item: power, Value: 0.0
2018-09-15 11:53:23,904:__main__:DEBUG:URL: http://rpi3:8080/json.htm?type=command& ... svalue=0.0
2018-09-15 11:53:23,910:__main__:DEBUG:IDX: 93, Items: Power (W), Usage (kWhr), Values: 0.0;0.002
2018-09-15 11:53:23,911:__main__:DEBUG:URL: http://rpi3:8080/json.htm?type=command& ... ue=0.0;2.0
There's nothing plugged in, so the current and power readings are very low.

Also note my software and hardware versions are different from yours, which could suggest different firmware, although I'm a bit unsure about the dates (mine looks earlier):
2018-09-15 11:53:08,757:__main__:DEBUG:Received: {"system":{"get_sysinfo":{"sw_ver":"1.2.10 Build 170828 Rel.104625","hw_ver":"2.0","type":"IOT.SMARTPLUGSWITCH","model":"HS110(AU)","mac":"xx:84:C6:3D:C0:xx","dev_name":"Smart Wi-Fi Plug With Energy Monitoring","alias":"TP-Link HS110 #1","relay_state":1,"on_time":5894,"active_mode":"none","feature":"TIM:ENE","updating":0,"icon_hash":"","rssi":-70,"led_off":0,"longitude_i":xx,"latitude_i":xx,"hwId":"A28C8BB92AFCB6CAFB83A8C00145F7E2","fwId":"00000000000000000000000000000000","deviceId":"8006B1553E3288EB95B589531A1EF88519FD5012","oemId":"6480C2101948463DC65D7009CAECDECC","err_code":0}}}
Good luck, I'm sure you're almost there! Please let us know either way.

Cheers - Andrew

ajay100
Posts: 22
Joined: Monday 07 August 2017 15:01
Target OS: Raspberry Pi
Domoticz version: 3.9560
Location: Victoria, Australia
Contact:

Python3 version of the logger

Post by ajay100 » Saturday 15 September 2018 10:46

I've just finished the Python3 version of the HS110 logger here https://github.com/ajay10000/TP-Link-HS110, with thanks to https://github.com/GadgetReactor/pyHS10 ... rotocol.py for the howto on the encryption and decryption routines.

Cheers - Andrew

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest