Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

[MOVED] TP-Link Smart Plug

If you have a question or need help, this is the place to be.
Post Reply
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: TP-Link Smart Plug

Post by kgschlosser »

Did you install the last plugin update? I don't believe you did because I removed the total seconds bit.
If you like the work I have been doing then feel free to Image
Flyingsubs
Experienced User
Posts: 127
Joined: Sat Dec 29, 2012 11:26 pm

Re: TP-Link Smart Plug

Post by Flyingsubs »

kgschlosser wrote:Did you install the last plugin update? I don't believe you did because I removed the total seconds bit.

You are correct sir! I'm not sure what happened but i cleared everything and everything is awesome!! Thank you! My next goal at some point is to see if I can recreate what you did for the wemo switch. Curently I was able to get a python script to work and eventghost just runs it nicely. It would be awesome if I could modify it into a plugin.


This is the current code. Not sure on the internet if there are more datapoints to collect! Also hoping I can learn by modifying scripts like you did to learn Python!

Code: Select all

#!/usr/bin/python

import re
import urllib2

# Configuration:
# Enter the local IP address of your WeMo in the parentheses of the ip variable below. 
# You may have to check your router to see what local IP is assigned to the WeMo.
# It is recommended that you assign a static local IP to the WeMo to ensure the WeMo is always at that address.
# Uncomment one of the triggers at the end of this script.

ip = '10.0.0.201'


class wemo:
	OFF_STATE = '0'
	ON_STATES = ['1', '8']
	ip = None
	ports = [49153, 49152, 49154, 49151, 49155]

	def __init__(self, switch_ip):
		self.ip = switch_ip      
   
	def toggle(self):
		status = self.status()
		if status in self.ON_STATES:
			result = self.off()
			result = 'WeMo is now off.'
		elif status == self.OFF_STATE:
			result = self.on()
			result = 'WeMo is now on.'
		else:
			raise Exception("UnexpectedStatusResponse")
		return result    

	def on(self):
		return self._send('Set', 'BinaryState', 1)

	def off(self):
		return self._send('Set', 'BinaryState', 0)

	def status(self):
		return self._send('Get', 'BinaryState')

	def name(self):
		return self._send('Get', 'FriendlyName')

	def signal(self):
		return self._send('Get', 'SignalStrength')
  
	def _get_header_xml(self, method, obj):
		method = method + obj
		return '"urn:Belkin:service:basicevent:1#%s"' % method
   
	def _get_body_xml(self, method, obj, value=0):
		method = method + obj
		return '<u:%s xmlns:u="urn:Belkin:service:basicevent:1"><%s>%s</%s></u:%s>' % (method, obj, value, obj, method)
	
	def _send(self, method, obj, value=None):
		body_xml = self._get_body_xml(method, obj, value)
		header_xml = self._get_header_xml(method, obj)
		for port in self.ports:
			result = self._try_send(self.ip, port, body_xml, header_xml, obj) 
			if result is not None:
				self.ports = [port]
			return result
		raise Exception("TimeoutOnAllPorts")

	def _try_send(self, ip, port, body, header, data):
		try:
			request = urllib2.Request('http://%s:%s/upnp/control/basicevent1' % (ip, port))
			request.add_header('Content-type', 'text/xml; charset="utf-8"')
			request.add_header('SOAPACTION', header)
			request_body = '<?xml version="1.0" encoding="utf-8"?>'
			request_body += '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
			request_body += '<s:Body>%s</s:Body></s:Envelope>' % body
			request.add_data(request_body)
			result = urllib2.urlopen(request, timeout=3)
			return self._extract(result.read(), data)
		except Exception as e:
			print str(e)
			return None

	def _extract(self, response, name):
		exp = '<%s>(.*?)<\/%s>' % (name, name)
		g = re.search(exp, response)
		if g:
			return g.group(1)
		return response

def output(message):
	print message

switch = wemo(ip)

# Configuration:
# Uncomment only one of the lines below to make the script work.

output(switch.on())
#output(switch.off())
#output(switch.toggle())
#output(switch.status())
Flyingsubs
Experienced User
Posts: 127
Joined: Sat Dec 29, 2012 11:26 pm

Re: TP-Link Smart Plug

Post by Flyingsubs »

kgschlosser wrote:and Oh one other thing. if you turn on the plug with the app on your phone. does it trigger an event in EG?
So i tested this as well! It does work on a slight delay, my guess is its linked to when you first set the pluggin for the polling feature. When I use the app it does trigger separate events. Very cool, this switch is now way smarter than my WEMO. This would be especially cool if you had a light switch and wanted to know the state or trigger other smart switches based on that event.
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: TP-Link Smart Plug

Post by kgschlosser »

well here is what you want to do then.. start searching for wemo API. just in case you do not know what an API is.. it's a "standard" and i use that term loosely that the developers of the wemo have come up with that they are able to use for external influence/data collection across all of their models. it is made so that you will have basic functions and data across all models. and as the models differ then the "extras" are added. so it will usually always be the same in terms of how to communicate and how to identify what model. and once you have that you are able to specify the extras you want to control.

I do the same thing with this plugin.. I identify the model number and because the TP-Link model numbers are as follows.

LB100 LB105 LB110 kind of thing for a "L"ight "B"lub

and they use HS for the switches and plugs. because the switches are the same as a plug in functionality they use the same model number designation. but they only have one switch which is the HS200 it is easy to identify.


But this should also point you in the direction to start searches for

http://developers.belkin.com/wemo/sdk
If you like the work I have been doing then feel free to Image
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: TP-Link Smart Plug

Post by kgschlosser »

Flyingsubs wrote: So i tested this as well! It does work on a slight delay, my guess is its linked to when you first set the pluggin for the polling feature. When I use the app it does trigger separate events. Very cool, this switch is now way smarter than my WEMO. This would be especially cool if you had a light switch and wanted to know the state or trigger other smart switches based on that event.

OK so the event does get generated... I have to change something out on the polling i messed something up. it was supposed to be in tenths of a second and not one second increments. so let me make that change
If you like the work I have been doing then feel free to Image
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: TP-Link Smart Plug

Post by kgschlosser »

ok so here is the fixed version. this one you can properly set the polling intervals. if you set it to 0.1 it will check the device 10 times a second. so as soon as the device turns on the the event should come up no later then one tenth of a second later. (depending on how long it takes the devic to also change it's internal data

Now... events are generated for... On and Off, IP address changes, Alias (name) changes, Color changes (bulb), Color Temp Changes (bulb), Dim level (Bulb)

and through the use of schedule Ghost, and other various plugins like SunTracker. you can set up some pretty nice scenes, and You basically have yourself a full fledged lighting controller.

My personal favorite is installing an application like EGI GeoZone (Android) which is a geofencing application that can trigger events in EG. you couple that with the suntracker and you can have EventGhost turn your outside lights on when you come within 100 feet of your home but only if it is dark out. you can have it turn the inside lights on as well... But you are thinking what if the wife is home there is no need to turn the lights on inside. well that's a good point. so if she has the app also installed on her phone it will know if she is home or not. and do what it needs to do. this also works in reverse when leaving. if no one is home it will turn everything off. if the wife is home it will only turn the outside lights off..

this is how i have my system set up. it works perfect.. I have mine tied into more then just the lights. it turns off my gas fireplaces. turns down the heat (if it's winter time) My system also randomly turns various lights on and off throughout the house at varying dim levels for varying amounts of time. makes a would be bugler think that someone is home.

Next project I am going to do is I am going to put in 2 thermistors into my furnace. one on the intake side and one on the output side. so I know the in and out temps. and I then know what the delta T rise is. the purpose for knowing this is.. when you furnace filter needs to be changed the delta T will be a lot higher then when it's clean. This is due to a lower amount of air going through the furnace and causing the exchanger to get hotter. So i would take a temp reading when I know the filter needs to be changed. and use that temp reading to send me an sms or display something on one of my TV's about changing it.
If you like the work I have been doing then feel free to Image
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: TP-Link Smart Plug

Post by kgschlosser »

HA forgot to attach the plugin...
If you like the work I have been doing then feel free to Image
loveleejohn
Experienced User
Posts: 133
Joined: Thu Dec 10, 2015 12:09 am

Re: TP-Link Smart Plug

Post by loveleejohn »

This looks like another exciting plugin! Good grief. I wish I could clone myself and win the lottery so I'd have enough time to try all of these cool plugins! Are these smart plugs fast? How long is the delay when you turn them on from Eventghost?
Flyingsubs
Experienced User
Posts: 127
Joined: Sat Dec 29, 2012 11:26 pm

Re: TP-Link Smart Plug

Post by Flyingsubs »

kgschlosser wrote:ok so here is the fixed version. this one you can properly set the polling intervals. if you set it to 0.1 it will check the device 10 times a second. so as soon as the device turns on the the event should come up no later then one tenth of a second later. (depending on how long it takes the devic to also change it's internal data

Now... events are generated for... On and Off, IP address changes, Alias (name) changes, Color changes (bulb), Color Temp Changes (bulb), Dim level (Bulb)

and through the use of schedule Ghost, and other various plugins like SunTracker. you can set up some pretty nice scenes, and You basically have yourself a full fledged lighting controller.

My personal favorite is installing an application like EGI GeoZone (Android) which is a geofencing application that can trigger events in EG. you couple that with the suntracker and you can have EventGhost turn your outside lights on when you come within 100 feet of your home but only if it is dark out. you can have it turn the inside lights on as well... But you are thinking what if the wife is home there is no need to turn the lights on inside. well that's a good point. so if she has the app also installed on her phone it will know if she is home or not. and do what it needs to do. this also works in reverse when leaving. if no one is home it will turn everything off. if the wife is home it will only turn the outside lights off..

this is how i have my system set up. it works perfect.. I have mine tied into more then just the lights. it turns off my gas fireplaces. turns down the heat (if it's winter time) My system also randomly turns various lights on and off throughout the house at varying dim levels for varying amounts of time. makes a would be bugler think that someone is home.

Next project I am going to do is I am going to put in 2 thermistors into my furnace. one on the intake side and one on the output side. so I know the in and out temps. and I then know what the delta T rise is. the purpose for knowing this is.. when you furnace filter needs to be changed the delta T will be a lot higher then when it's clean. This is due to a lower amount of air going through the furnace and causing the exchanger to get hotter. So i would take a temp reading when I know the filter needs to be changed. and use that temp reading to send me an sms or display something on one of my TV's about changing it.



What you are talking about and doing is so sick man!! I also use suntracker and I use tasker on Android to turn my internal lights 30 mins before sunset. I'll take a look at this GeoZone app since I have android. The fireplace idea you have sounds like a great idea. The Delta T makes so much sense. I'll try out the new pluggin. Thank you so much for all your hardwork, this will make a great addition to EG for TP Link
Flyingsubs
Experienced User
Posts: 127
Joined: Sat Dec 29, 2012 11:26 pm

Re: TP-Link Smart Plug

Post by Flyingsubs »

loveleejohn wrote:This looks like another exciting plugin! Good grief. I wish I could clone myself and win the lottery so I'd have enough time to try all of these cool plugins! Are these smart plugs fast? How long is the delay when you turn them on from Eventghost?

Definitely, this program is so awesome when you dedicate some time to it. I've been using it since 2012 and been making small changes every so often. The light switch is pretty instant. Same experience as the app on a device.
Flyingsubs
Experienced User
Posts: 127
Joined: Sat Dec 29, 2012 11:26 pm

Re: TP-Link Smart Plug

Post by Flyingsubs »

kgschlosser wrote:well here is what you want to do then.. start searching for wemo API. just in case you do not know what an API is.. it's a "standard" and i use that term loosely that the developers of the wemo have come up with that they are able to use for external influence/data collection across all of their models. it is made so that you will have basic functions and data across all models. and as the models differ then the "extras" are added. so it will usually always be the same in terms of how to communicate and how to identify what model. and once you have that you are able to specify the extras you want to control.

I do the same thing with this plugin.. I identify the model number and because the TP-Link model numbers are as follows.

LB100 LB105 LB110 kind of thing for a "L"ight "B"lub

and they use HS for the switches and plugs. because the switches are the same as a plug in functionality they use the same model number designation. but they only have one switch which is the HS200 it is easy to identify.


But this should also point you in the direction to start searches for

http://developers.belkin.com/wemo/sdk

Makes sense, I work in IT as Business Analyst and familar with these terms. I'm a pretty technical BA and trying to get to the next level of programming when I have some free time. Thanks for the link, i'm hoping I can modify what you did here and mimic it for the WEMO switch. RIght now WEMO works for my needs but it would be a cool coding project for me. I also set a static IP address for my device so the IP wouldnt switch. But you latest update sounds like a great update for it.
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: TP-Link Smart Plug

Post by kgschlosser »

well tell me what you need to know and I can get you off and running. I have been working on a little standalone program that will generate the code for a plugin. very basic kinds of things but it really simplify the very basics of the plugin structure and standard methods and ways of handling threads and having them close properly.


Attached is a little script i made. this will not run inside of EG. you would need to run this from an external python installation

make sure you click on the view button
If you like the work I have been doing then feel free to Image
Flyingsubs
Experienced User
Posts: 127
Joined: Sat Dec 29, 2012 11:26 pm

Re: TP-Link Smart Plug

Post by Flyingsubs »

kgschlosser wrote:well tell me what you need to know and I can get you off and running. I have been working on a little standalone program that will generate the code for a plugin. very basic kinds of things but it really simplify the very basics of the plugin structure and standard methods and ways of handling threads and having them close properly.


Attached is a little script i made. this will not run inside of EG. you would need to run this from an external python installation

make sure you click on the view button

Thanks for the script!! I'll take a look and start practicing, i just started with EG build you own plugin and i started looing at the WEMO script and starting to understand how it works. It doesn't have all the features as TP link because they discontinued the API.
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: TP-Link Smart Plug

Post by kgschlosser »

you have to think outside the box.. I am pretty sure with the web api that it has you can accomplish everything I have with the TP-Link script.

So the wemo bulbs have been discontinued.

so all you have are the switches and plugs which basically you can turn on and off. but I am sure in their web API you can query the switch to see if it is on or off..

so this is what you do.. when the plugin starts.. discover all of the devices.. and check all of their states.. store their states locally in EG. then set up a thread with a polling loop so that every n milliseconds you check all of the devices again and see if their state matches the one stored locally.. if it doesn't then trigger an event. that's how that bit of it is done.

there really isn't much you can do with the wemo. I really don't even know why they bothered to make them LOL
If you like the work I have been doing then feel free to Image
Flyingsubs
Experienced User
Posts: 127
Joined: Sat Dec 29, 2012 11:26 pm

Re: TP-Link Smart Plug

Post by Flyingsubs »

kgschlosser wrote:you have to think outside the box.. I am pretty sure with the web api that it has you can accomplish everything I have with the TP-Link script.

So the wemo bulbs have been discontinued.

so all you have are the switches and plugs which basically you can turn on and off. but I am sure in their web API you can query the switch to see if it is on or off..

so this is what you do.. when the plugin starts.. discover all of the devices.. and check all of their states.. store their states locally in EG. then set up a thread with a polling loop so that every n milliseconds you check all of the devices again and see if their state matches the one stored locally.. if it doesn't then trigger an event. that's how that bit of it is done.

there really isn't much you can do with the wemo. I really don't even know why they bothered to make them LOL

Yeah exactly, I definitely feel like TP link is the best compared. I'm not sure what the best is, but i look for a deal online and then buy. I got the wemo for $20 and the Tp link for $18 which are really good prices. I really want to next get a light switch one and then i can make existing switches Smart. But i'm not ready to drop $100 on making my whole house smart yet haha. Hopefully next year they will come down in price. When i have sometime, i'm going to try and recreate the polling. This solves the problem oh not having eg know when a google home or another device toggles the state. THis will be a fun project for me to learn eg while learning python.
Post Reply