WEMO Control based on Sense Data (Python)

Requires pywemo and sense_energy from pip

So im still working on this, but here are the basics… I had to hardcode the IP of the Wemo as the discovery via python often resulted in not finding the correct plug.

Basically this checks that we are generating solar power by getting the solar generation from sense, If we are not generating solar or not generating sufficient solar based on usage and the plug is on, turn the plug off.

If we are generating solar and have excess compared to usage, and the plug is off, based on the average watt usage of the plug do we have that amount of solar generation above usage then turn the plug on, if not turn it off.

import pywemo, re
from sense_energy import Senseable

username = 'Sense_UserName'
password = 'Sense_Pass'
sense = Senseable(wss_timeout=30,api_timeout=30)
sense.authenticate(username, password)
active_power = str(sense.active_power).split('.')[0]
active_solar_power = str(sense.active_solar_power).split('.')[0]

plug_name = "GolfCart"
asp = int(active_solar_power)
ap = int(active_power)
power_diff = asp-ap
wemo_ip_address = ""
wemoport = pywemo.ouimeaux_device.probe_wemo(wemo_ip_address)
wemourl = 'http://%s:%i/setup.xml' % (wemo_ip_address, wemoport)
wemodevice = pywemo.discovery.device_from_description(wemourl, None)
plug = wemodevice
plug_string = str(plug)
plug_power = str(plug.get_state())
plug_total_time = plug.insight_params['ontotal']
plug_total_watt = plug.insight_params['totalmw']
plug_avg_power =  int((plug_total_watt / 1000) / (plug_total_time / 60))

if plug_power == '1':
	if power_diff < 1:
    	print ("Not eough solar power", power_diff)
plugsplit = str(plug).split(',')[0]
plugstrip = str(plugsplit).strip('[ ] < >')
plugreplace1 = str(plugstrip).replace('Insight', '')
plugreplace2 = str(plugreplace1).replace('WeMo', '')
plugreplace3 = str(plugreplace2).replace(" ", "")
plugreplace4 = str(plugreplace3).replace('"', '' )
plugpwrreplace =  str(plug.current_power).rstrip('3')
plugpwr = (round(plug.current_power/1000))
if plug_name in plug_string:
	if power_diff > plug_avg_power:
		print ("Power Above Threshold at", power_diff, "Plug Average Power", plug_avg_power)
		print ("Power Below Threshold at", power_diff, "Plug Average Power", plug_avg_power)
	print (plug_name, "Not Found")

Thanks you so much for posting this script. I am looking to do the same thing with a TP-LINK plug to turn on an off my dehumidifier this summer.

Eventually, I would like to hook this up with a level 2 charger from www.openevse.com and adjust the output of the charger based on solar output and current consumption so I can charge my car with solar.

This has already been done using the open energy monitor and openevse, https://guide.openenergymonitor.org/integrations/ev-charging/. So instead of using the open energy monitor to capture consumption and solar data I want to use my sense.


this can be done with sonoff and tplink. I just used it for wemos, but both have python pip software and can be worked.