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)
sense.update_realtime()
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 = "192.168.1.107"
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)
	plug.off()
else:
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)
		plug.on()
	else:
		print ("Power Below Threshold at", power_diff, "Plug Average Power", plug_avg_power)
		plug.off()
else:
	print (plug_name, "Not Found")
5 Likes

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, EV Charging - Guide | OpenEnergyMonitor. So instead of using the open energy monitor to capture consumption and solar data I want to use my sense.

Thanks,
Andy

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

I just tried this on my Mac, and it was not successful. Latest MacOS, version 2.7 of Python, when I run your script I get the error:

ImportError: No module named pywemo

I tried the other python script posted here and got the same result, I want the functionality, but I don’t know python.

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py
pip3 install sense_energy
pip3 install pywemo

1 Like

Thanks, this really got me started, I tied into my Tesla and can ask it to change the charger off and on or adjust the charge rate based on how much free solar I have (I don’t have NetMetering, so best to charge from the sun directly)


This is me, coming home and plugging in, after a few minutes I run my TesSense app and that causes the Tesla to track the amount of free solar and set the charge rate right along those lines. If too much load, the Tesla would stop charging, if any excess it would start charging and adjusting to match free solar.

Now I need to figure out how to control the TP-Links I have all over my house from the app so I can turn other devices off and on. Also figure out how to pretend to be a TP-Link so I can label the Tesla’s charging so it doesn’t show as “Other”, looks like someone contributed a script called Sense-Link I’ll take a look at.

4 Likes

I started looking at this then bought an Eyedro, that integrates with SmartThings - you get access to real time use data and can use it. At the moment I have a routine that says -
If time is
and
Total use is > 3500 watts
then
Set the AC temp to 85
Send me a notification

Trivial to do with Eyedro,

Trivial to do with Sense and Home Assistant, as well. But if you want to show people how to do it with Eydro and SmartThings, there are better places for you to post. Different strokes. Thanks.

1 Like

Actually no. Sense does not provide real time use data. At least not that I can find anywhere other than writing a python script to ferret it out.

@harvs, the realtime data is available. Two places to look:

  1. If you can code in Python, you can use the API below. The Usage Example actually shows how to access both realtime and trend data.
  1. You can also access real-time data inside of the Home Assistant home automation platform. I have written several automations using Sense real-time data and Node-RED. But there are numerous ways in Home Assistant to set up an automation routine as simple as the one you posted.

But it sounds like you are already committed to the SmartThings universe and are making your decisions based on that.