Page 5 of 13

Re: TP-Link smart plug HS100/HS110

Posted: Wednesday 18 July 2018 14:23
by Hofland
Thanks,

it's working with the suggested changes .

Re: TP-Link smart plug HS100/HS110

Posted: Monday 23 July 2018 13:24
by ajay100
Hello all, I have added a version here: https://github.com/ajay10000/TP-Link-HS110 (edited to correct link 2020-09-26)

This version supports sending data to Domoticz at set intervals and also direct commands. It also supports text logging.

Thanks for all the input in this forum, which has helped me to achieve what I wanted with the HS110.

Cheers - Andrew

Re: TP-Link smart plug HS100/HS110

Posted: Sunday 05 August 2018 21:44
by Mace
I'm a bit stuck here....Hope you can help me out. I had a script running for my HS110 perfectly for months. Had an issue with a memory card going bust and the back-ups weren't actually back-ups.... :oops:

Now I'm puzzling everything back together but hit a wall here. I know I had a script running with which I could toggle the switch on/off (and I think that I also saw the state change when doing the switching on the HS110 itself) and I got the report back from usage.

Now I see at least three or four different solutions and getting stuck. I have the switching on/off working now with the code from https://github.com/softScheck/tplink-sm ... artplug.py

Works fine as a virtual switch, using "script://tplink_smartplug.py -t <HS110 IP address> -c on"
I can also get current energy with the "./tplink_smartplug.py -t <HS110 IP> -j '{"emeter":{"get_realtime":{}}}' command, but how on earth am I going to get this into Domoticz Virtual Switch?

Also looked at the code from ajay100, but there I need to fill in 3 IDx numbers....But have only 1 for energy.

Re: TP-Link smart plug HS100/HS110

Posted: Monday 06 August 2018 13:49
by ajay100
@Mace, with my modified code you can send a command directly to the HS110. For example, with the script uploaded to ~/domoticz/scripts/python and the correct permissions set, you can then try:

Code: Select all

cd ~/domoticz/scripts/python
python tplink_hs110.py -c off
Response:
Sent: {"system":{"set_relay_state":{"state":0}}}
Received: {"system":{"set_relay_state":{"err_code":0}}}

Code: Select all

python tplink_hs110.py -c on
Response:
Sent: {"system":{"set_relay_state":{"state":1}}}
Received: {"system":{"set_relay_state":{"err_code":0}}}

I added the HS110 hardware in Domoticz as 'Dummy (Does nothing, use for virtual switches)'
Then I added three virtual sensors (Create Virtual Sensors):
1. Voltage
2. Ampere (1 phase)
3. Electric (Instant + Counter)

In the code block

Code: Select all

# Begin user editable variables
that is the order for the array

Code: Select all

monitor_list = ["voltage","current","kwhr"]
The following line of code sets the array for the indexes retrieved from the Domoticz devices, (in the same order):

Code: Select all

domoticz_idx = [90,91,93]
(use your own indexes here). You can change the order or leave any out that you wish, provided your indexes from Domoticz match the corresponding virtual sensor.

Following your question, I then added a fourth virtual sensor, 'Switch'. I edited the 'on' action to:

Code: Select all

script:///home/pi/domoticz/scripts/python/tplink_hs110.py -c on
and the 'off' action to:

Code: Select all

script:///home/pi/domoticz/scripts/python/tplink_hs110.py -c off
[EDIT] Note that I didn't change the code at all for the switch to work, I just added the switch device to Domoticz. i.e. It doesn't require the index of the switch in the array.

I hope this helps, but let me know if you have any further questions.

Cheers - Andrew

[EDIT] PS: If you have more than one HS110 you could duplicate the script to a new name (i.e. tplink_hs110-2.py) and modify the logger_name, domoticz_idx and hs110_ip variables. You would then add another switch and change (for example) the 'on' action to

Code: Select all

script:///home/pi/domoticz/scripts/python/tplink_hs110-2.py -c on

Re: TP-Link smart plug HS100/HS110

Posted: Monday 06 August 2018 14:22
by Mace
Thanks Andrew,

Seems to work fine now! Now just find out if you can get the state (on or off) reported, as now there can be a mismatch between domoticz and the actual status of the device if turned on or off manually....

Re: TP-Link smart plug HS100/HS110

Posted: Monday 06 August 2018 15:29
by ajay100
@Mace, apologies for not including the missing link. I am using the systemctl daemon on my Raspberry Pi for all scripts that I create. It starts the scripts at boot time and can be used to control them (i.e. start, stop, restart, status). This is how I set it up:
Create the service:

Code: Select all

sudo nano /lib/systemd/system/tplink_hs110.service
Paste in the following (modify path/name as required)

Code: Select all

[Unit]
Description=Python script monitors TP-Link HS110 smart switch
After=multi-user.target

[Service]
Type=idle
ExecStart=/usr/bin/python /home/pi/domoticz/scripts/python/tplink_hs110.py -c energy

[Install]
WantedBy=multi-user.target
Save the new service (In nano, the keys are Ctrl-X, Y, Enter)

Make it executable:

Code: Select all

sudo chmod 644 /lib/systemd/system/tplink_hs110.service
Reload systemctl:

Code: Select all

sudo systemctl daemon-reload
Enable the new service:

Code: Select all

sudo systemctl enable tplink_hs110.service
To check if the service is running:

Code: Select all

sudo systemctl status -l tplink_hs110
If you change the code, restart the service:

Code: Select all

sudo systemctl restart tplink_hs110
I have used this now for all of my python scripts and it is helpful for me that I can use consistent commands.

Andrew

[EDIT] PS: It seems that you edited your question as I was responding, so this post doesn't answer your question!

Re: TP-Link smart plug HS100/HS110

Posted: Monday 06 August 2018 15:43
by ajay100
Mace wrote: Monday 06 August 2018 14:22 Thanks Andrew,

Seems to work fine now! Now just find out if you can get the state (on or off) reported, as now there can be a mismatch between domoticz and the actual status of the device if turned on or off manually....
Yes, I was thinking along the same lines and will need to put some further thought into that. I will possibly modify the script, or create a new interrogating script.

Re: TP-Link smart plug HS100/HS110

Posted: Monday 06 August 2018 16:14
by Mace
You are the man!!!! ;) That would be awesome....

Re: TP-Link smart plug HS100/HS110

Posted: Monday 06 August 2018 18:22
by Mace
ajay100 wrote: Monday 06 August 2018 15:29 @Mace, apologies for not including the missing link. I am using the systemctl daemon on my Raspberry Pi for all scripts that I create. It starts the scripts at boot time and can be used to control them (i.e. start, stop, restart, status). This is how I set it up:


[EDIT] PS: It seems that you edited your question as I was responding, so this post doesn't answer your question!
Thanks again, I tried it with a crappy cronjob, all I know....This helped me a lot and learned me a better way to do it.

With cronjob's you can set intervals on when it runs, how does this work?

Re: TP-Link smart plug HS100/HS110

Posted: Wednesday 08 August 2018 3:37
by ajay100
cronjobs work well for scheduling applications. In my case I want the app running all the time, capturing data at the time intervals set in the code

Code: Select all

delay_time = 15 #update time in seconds
15 seconds is probably too frequent but sometimes you need to capture peaks and the shorter times make that more likely. If you would like hourly readings, use 3600, etc.

I am trying to find the command to get the relay status of the HS110. Does anyone know? I'm guessing something like

Code: Select all

{"system":{"get_relay_state":{}}}
but that didn't work.

[EDIT] Or

Code: Select all

{"system":{"get_sysinfo":{"relay_state"}}}
That didn't work either, but I found the correct command (see my next posts).

Re: TP-Link smart plug HS100/HS110

Posted: Wednesday 08 August 2018 6:26
by punter9
I've got a related question

Version: 4.9700
Build Hash: a3a45906
Compile Date: 2018-06-23 07:24:51
dzVents Version: 2.4.6
Python Version: 3.4.2 (default, Oct 19 2014, 14:03:53) [GCC 4.9.1]

I currently have several tplink smart plugs going with domoticz using the standard tplink_smartplug.py script
https://github.com/softScheck/tplink-sm ... artplug.py

Works great to turn on and off switches, but my switches can be run by alexa, kasa, and domoticz. So domoticz doesn't know when I use one of the other interfaces and then the status gets off (domo can say "off" when it is actually "on").

What I would like to do is update the switches with relay state (another virtual switch, or other means if there are better ideas). So far I can see the relay state by running
python ~./tplink_smartplug.py -t <ip> -c info
this returns a list of items, one of which if relay_state:0

Question is how do I use this to update my device status?

Re: TP-Link smart plug HS100/HS110

Posted: Thursday 09 August 2018 14:50
by ajay100
Reading back through this thread, MikeF suggested the answer way back:

Code: Select all

relay_state = jsonData['system']['get_sysinfo']['relay_state']
@punter9 and @Mace, there will probably be another virtual 'state' switch to check for the HS1XX before taking other actions.
[EDIT] No need to add another switch after all.

Cheers - Andrew

Re: TP-Link smart plug HS100/HS110

Posted: Saturday 11 August 2018 8:06
by ajay100
I have updated the code https://github.com/ajay10000/TP-Link-HS110 to provide relay (switch) state feedback to the existing HS110 switch device in Domoticz.

track_state and hs110_switch_idx user variables are added. If track_state is True, the switch with hs110_switch_idx in Domoticz is updated at approximately delay_time intervals.

This may not be ideal if you have set a long update interval. In my case, the status is updated every 15 seconds.

Andrew

Re: TP-Link smart plug HS100/HS110

Posted: Sunday 12 August 2018 3:03
by ajay100
In this version you will see four Domoticz index values as I have added another Custom Sensor device to measure power only. I'm using the HS110 to measure and log maximum and minimum power for various devices. The kWHr Domoticz device only shows power in watts for 24 hours, then for historical logging it only keeps the daily usage in kWHr.

For example, logging my new ASUS TM-AC1900 router and wireless internet antenna for 12 hours shows average power of around 10.5W with peaks just over 11W and a minimum of 10.1W. this is much more consistent than I would have expected. These are useful things to know when living off the grid.

Andrew

Re: TP-Link smart plug HS100/HS110

Posted: Monday 03 September 2018 5:44
by punter9
I'm getting so close, I have a couple questions in the user editable variables.

# Begin user editable variables

base_url = domain + "json.htm?type=command&param=udevice&nvalue=0"
-do I need to do anything here??

monitor_list = ["voltage","current","power","usage"]
domoticz_idx = [90,91,108,93]
-what do I do with these IDX? Do I create 4 switches in domoticz for each of the above "voltage","current","power","usage" and then plug the IDX into this line? If so what kind of switches?

hs110_switch_idx = 107
-is this for the on/off functionality?


# End user editable variables

Re: TP-Link smart plug HS100/HS110

Posted: Monday 03 September 2018 6:38
by punter9
I think I may have sorted out the 4 switches to make from above, not counting the on off switch. Now when i execute the script for commands that aren't on/off/info I get

File "tplink_hs110_latest.py", line 238, in <module>
hs.read_hs110()
File "tplink_hs110_latest.py", line 224, in read_hs110
self.send_json(received_data)
File "tplink_hs110_latest.py", line 155, in send_json
voltage = round(float(json_data['emeter']['get_realtime']['voltage_mv']) / 1000,2)
KeyError: 'emeter'

Re: TP-Link smart plug HS100/HS110

Posted: Monday 03 September 2018 20:38
by eriksson25
ajay100 wrote: Sunday 12 August 2018 3:03 In this version you will see four Domoticz index values as I have added another Custom Sensor device to measure power only. I'm using the HS110 to measure and log maximum and minimum power for various devices. The kWHr Domoticz device only shows power in watts for 24 hours, then for historical logging it only keeps the daily usage in kWHr.

For example, logging my new ASUS TM-AC1900 router and wireless internet antenna for 12 hours shows average power of around 10.5W with peaks just over 11W and a minimum of 10.1W. this is much more consistent than I would have expected. These are useful things to know when living off the grid.

Andrew
Hi

I am looking to get the hs100 unit to my domoticz setup. (dont have any need for energy messurment).

I have been reading and was wondering if your new script works for the hs100, or only for the 110? I would like to have the status reported back so domoticz and reality dont get "missmatched".

/KG

Re: TP-Link smart plug HS100/HS110

Posted: Tuesday 04 September 2018 10:08
by ajay100
punter9 wrote: Monday 03 September 2018 5:44 I'm getting so close, I have a couple questions in the user editable variables.

# Begin user editable variables

base_url = domain + "json.htm?type=command&param=udevice&nvalue=0"
-do I need to do anything here??

monitor_list = ["voltage","current","power","usage"]
domoticz_idx = [90,91,108,93]
-what do I do with these IDX? Do I create 4 switches in domoticz for each of the above "voltage","current","power","usage" and then plug the IDX into this line? If so what kind of switches?

hs110_switch_idx = 107
-is this for the on/off functionality?


# End user editable variables
@punter9 base_url can stay the same. I've described adding the 4 devices (these are not switches) in my post of 06 August 2018 (above). hs110_switch_idx is the index of the virtual switch in Domoticz, for the switching function of the HS1xx.

In Domoticz, the switch has these actions, also described in the above post:
On action: script:///home/pi/domoticz/scripts/python/tplink_hs110.py -c on
Off action: script:///home/pi/domoticz/scripts/python/tplink_hs110.py -c off

Andrew

Re: TP-Link smart plug HS100/HS110

Posted: Tuesday 04 September 2018 10:10
by ajay100
punter9 wrote: Monday 03 September 2018 6:38 I think I may have sorted out the 4 switches to make from above, not counting the on off switch. Now when i execute the script for commands that aren't on/off/info I get

File "tplink_hs110_latest.py", line 238, in <module>
hs.read_hs110()
File "tplink_hs110_latest.py", line 224, in read_hs110
self.send_json(received_data)
File "tplink_hs110_latest.py", line 155, in send_json
voltage = round(float(json_data['emeter']['get_realtime']['voltage_mv']) / 1000,2)
KeyError: 'emeter'
@punter9, can you post the command you are executing? This error could also be caused by older firmware. If in doubt, turn full debugging on: debug_level="DEBUG" and see what the log file shows.

Andrew

Re: TP-Link smart plug HS100/HS110

Posted: Tuesday 04 September 2018 12:20
by ajay100
@eriksson25, even though this is overkill for the HS100, it should work. If you issue the commands from Domoticz for off/on, it will not run in the loop that does the logging. However the catch is that the feedback does require the loop, so I would think that a stripped down version is required.

Andrew