Welcome to Tesla Motors Club
Discuss Tesla's Model S, Model 3, Model X, Model Y, Cybertruck, Roadster and More.
Register

TesSense - Control your Tesla charging from your Sense Energy Monitor

This site may earn commission on affiliate links.

israndy

Supercharger Hunter
Mar 31, 2016
6,588
8,291
Alameda, CA
I have a Sense Energy Monitor that shows me how much solar power I am generating and how much energy my home is using every microsecond. It is nice for logging and realizing I have left the oven on but I would REALLY love if I could have it calculate any free solar and instead of sending it back to the grid, dump it into my Tesla instead.

I don't know how to program much, but after a few years of following both Sense and Tesla there have been unofficial APIs emerged and now support for them are in almost any language. So I checked out TeslaPy and the Sense_API on GitHub and using a couple of quick cut and pastes from their examples I came up with TeslaSense.

Check it out at GitHub:


Charge your Tesla with surplus solar generation as detected by your Sense Energy Monitor - First Release

TeslaSense logs into your Sense Energy Monitor and your Tesla account and tracks the amount of surplus energy your solar system is generating and asks your Tesla to start or stop charging and adjusts the amps used for charging, based on the amount of free solar and updates every minute.

If you have this app running and plug your Tesla in you can charge using ANY model of EVSE and the Python3 script will get information from your car about what capabilities your connection has and then will track the energy usage in your location, allocating any spare power to charging, up to the limits of your wall connector.

It plays nicely with the Tesla App allowing you to see the changes as they happen and gives feedback about what is happening with the app on the standard output.

You will need to edit this app, placing your Username and Password for your Sense Energy Monitor account, and your login for your Tesla account in the appropriate locations. The first time this python script is run you will be prompted for your password by Tesla and you will receive a confirmation code, paste that back into the app and you will be logged in securely and you can watch as the app detects your surplus solar and uses that to set the charging rate on your car. Before sunrise and after the sun starts going down or anytime there are too many power draws in your location the app will ask your Tesla to turn off charging. When the surplus solar returns, the charging will start again.
I haven't gotten a chance to do a TON of edge cases, but I'll update any fixes at the GitHub site. Would love everyone's feedback and I am sure it could easily be modified to talk to other energy monitors, perhaps even Tesla's for those of you with Tesla Solar too
 
I also have a (small, 3.8kW) solar install and a Sense monitor, so will definitely give this a go - thanks for writing it.

We have net metering so I guess there’s no financial saving, but I do like the idea of charging the car using home-grown power. There’s not much excess generation at this time of year, given the short days, but OTOH we’re not using the a/c either.
 
I have NO trouble charging the car in the summer, so I don't think about it, the reason I wrote this app is because I have great difficulty charging the car in winter (and I put up my solar too late to get Net Metering, although I have heard some horror stories, so probably just as happy). With this app I don't have any time of the day that I am not max charging my car with the energy I have, so nothing is going back to the grid (for the paltry return they give me) until I have used as much as I can.

Screen Shot 2022-02-03 at 1.46.56 PM.png

This is before, when I did the charging by hand, I would not notice for minutes or hours that it was over or under charging

Screen Shot 2022-02-03 at 1.48.24 PM.png

This is after writing the app, you can see how often it changes and hugs the generation. I did take the car while the hot tub was heating before noon, so you can see when I got back and you can see I have times when I get more power than the charger can take.
 
OK, updates available to my python script that controls your Tesla and only uses extra solar generation to charge your car.
Lots of bugs fixed and functionality that wasn't really there on the first release, I crossed my fingers that you guys wouldn't look at it initially, so check it out now.

Also, thinking how Tesla doesn't like their name in apps I changed the repository to be called TesSense, incase anyone ever wants to make this a real thing it has a name no one else is apparently using that doesn't have the word Tesla in it.


Much more in code documentation too, so hopefully it's easy to figure out
 
I’m new to this and also do not have net metering. I do not want to send electricity back unnecessarily . I’m looking at enphase w/ batteries vs Tesla panels w/ powerwalls. I have two teslas, and we just have the mobile connectors. Could this be an option for me or does it require the wall connector(s)?
 
Works with any EVSE, it keeps you from charging your car when there isn't any sun but it does require owning a Sense Energy Monitor.

Tesla Panels and PowerWalls are the best, and priced that way. If you get either solution they will charge from the sun during the day and you can use that power to charge your car and run your home at night, but that will wear down the batteries. Since I already have non-Tesla panels I cannot get PWs here so I put my spare solar into my cars and motorcycle, but I am home during the sunny part of the day, many have to work then.

If you need to charge *right now* so you can travel you can always just quit the app and set the charging amps to max in the Tesla app, or hit up a Supercharger, off-peak it's cheaper to charge at those here than it is to charge at home (barely).

edit: I continue to update the code as I find bugs so look for updates on GitHub. I have posted three versions and I found a fatal bug just now in the code there, so there is bound to be more updates
 
  • Like
Reactions: Stampee
Works with any EVSE, it keeps you from charging your car when there isn't any sun but it does require owning a Sense Energy Monitor.

Tesla Panels and PowerWalls are the best, and priced that way. If you get either solution they will charge from the sun during the day and you can use that power to charge your car and run your home at night, but that will wear down the batteries. Since I already have non-Tesla panels I cannot get PWs here so I put my spare solar into my cars and motorcycle, but I am home during the sunny part of the day, many have to work then.

If you need to charge *right now* so you can travel you can always just quit the app and set the charging amps to max in the Tesla app, or hit up a Supercharger, off-peak it's cheaper to charge at those here than it is to charge at home (barely).

edit: I continue to update the code as I find bugs so look for updates on GitHub. I have posted three versions and I found a fatal bug just now in the code there, so there is bound to be more updates

What motorcycle do you have? I’ve see a couple of news articles but haven’t been following then too closely. How far does it go on a charge?
 
I have a couple of 2007 Vectrix Maxiscooters. They come with NiMH batteries out of the box. Similar tech to gel lead acid batts, but opposite chemistry. The guy who designed them also designed the Li Ion batt for the Leaf so he made them compatible sizes. You can drop 18-20 Leaf cells in a Vectrix and you have converted it to Lithium. Just did one last summer. Doubled the range to maybe 80-100 miles city, I see less than 60 highway. But it's a nice big bike with room for me and my wife, full fairing looks just like a Honda Silverwing. So I can go anywhere in the Bay Area now, I could probably hit Sacramento, but I would have to charge to get back. I also added a J1772 port, but it still only charges at 2kW or so.

Cleaning up the code for TesSense, so I'll have another release this weekend. No longer exits on full batt or driving or disconnected, so you can come home and plug in and it will regain control. Now I need to fix it if I am traveling and plug into an L2 charger away from home.
 
Aloha Randy,
Sorry if my questions are stupid. I think this might be the answer to charging our soon to be delivered model 3. We are off grid solar in Hawaii, 6.1kw(max) possible 24amps(max) . Concern is the Tesla stopping charge when refrigerator or some appliance kicks on. if this solutions allocates the surplus power being fed to the batteries that would be great. I presently have an outback Mate3 energy monitor that uses Optics RE to tell me on the web what my production/output is. Does that take the place of "Sense energy monitor" if not, which one would I need.
Mahalo Nui Loa
Jim Ward
 
I can answer questions about the program but I no nothing about your hardware. The app reads from sense and every two minutes changes the charge rate based on conditions like solar generation and home usage. If you can get that info and your off grid setup can handle the car continuing to charge for two minutes before changing. Might work.

It could be modified to do all sorts of things. But not by me.
 
I can answer questions about the program but I no nothing about your hardware. The app reads from sense and every two minutes changes the charge rate based on conditions like solar generation and home usage. If you can get that info and your off grid setup can handle the car continuing to charge for two minutes before changing. Might work.

It could be modified to do all sorts of things. But not by me.
Mahalo!
 
I have a Sense Energy Monitor that shows me how much solar power I am generating and how much energy my home is using every microsecond. It is nice for logging and realizing I have left the oven on but I would REALLY love if I could have it calculate any free solar and instead of sending it back to the grid, dump it into my Tesla instead.

I don't know how to program much, but after a few years of following both Sense and Tesla there have been unofficial APIs emerged and now support for them are in almost any language. So I checked out TeslaPy and the Sense_API on GitHub and using a couple of quick cut and pastes from their examples I came up with TeslaSense.

Check it out at GitHub:



I haven't gotten a chance to do a TON of edge cases, but I'll update any fixes at the GitHub site. Would love everyone's feedback and I am sure it could easily be modified to talk to other energy monitors, perhaps even Tesla's for those of you with Tesla Solar too
How does one install this? I have an iPhone and Mac.
 
This is a command line application so launch the Terminal app.

You need to have Python 3 (not the default 2.7) installed on the Mac you use and you need to have the support packages installed (as documented in the source) and you need to modify the source with your passwords and preferences. I just installed Xcode, it's a great source editor. Then you simply issue the command from the terminal: python3 tessense.py

It will connect to your Sense monitor if you put in the correct password. It will connect to your car (if you have more than one you need to edit the source code to specify which car). Then it will check for free power and if it finds some it will start your car charging. If you plug in after the app is going the car starts charging on it's own and the app will limit it to use just free solar. Since it only checks every two minutes it will allow a bit of grid power thru as things change, so it's not super specific at the edge. It may let 250 watts from the grid charge your car but if it goes over that it'll slow the charging. It can only tell your car to change in one amp increments and:
1 amp * 240ish volts = 240ish watts.

Glad to help with any questions
 
  • Like
Reactions: SageBrush
I have a Sense Energy Monitor that shows me how much solar power I am generating and how much energy my home is using every microsecond. It is nice for logging and realizing I have left the oven on but I would REALLY love if I could have it calculate any free solar and instead of sending it back to the grid, dump it into my Tesla instead.

I don't know how to program much, but after a few years of following both Sense and Tesla there have been unofficial APIs emerged and now support for them are in almost any language. So I checked out TeslaPy and the Sense_API on GitHub and using a couple of quick cut and pastes from their examples I came up with TeslaSense.

Check it out at GitHub:



I haven't gotten a chance to do a TON of edge cases, but I'll update any fixes at the GitHub site. Would love everyone's feedback and I am sure it could easily be modified to talk to other energy monitors, perhaps even Tesla's for those of you with Tesla Solar too
This is exactly what I was looking for. Are you able to help me get this setup?
 
Sure, it's getting pretty reliable. The latest release on GIThub is just a week or so old. Just download, edit the source to input your username and passwords and install the documented libraries and it should start working. Copy the location of your vehicle from the first run and paste it into the code as your home location so it won't interact with your car if it's not there.

I recently added the ability for it to also support tiered tracking of other devices to turn off when power is low and back on when there is extra, I use it to turn off the office heater while the hot tub is warming, only takes about 12 minutes a cycle, so more solar is available to cover that event. You need to copy the name of the TPLink devices from the opening screen and paste them into the code that you want tracked. If none are listed it won't run that code.
 
I just got this working for me. Thank you so much!!!! I've been wanting to have our car soak up the extra juice for years. I have very poor skills with code, but I was able to get it running after a couple attempts. I haven't seen it report charge usage data to Sense yet though. Not sure if it will take time for Sense to "see" it. Thanks again. You are indeed the MAN.
 
Yeah, I am always trying to make it better, I need to start focusing on making it work on other systems than mine. Be sure that you have TPLink support turned on in your Sense App Settings:

A4ABDCB8-0F6A-4712-B189-7A85A5406AF0_1_201_a.jpeg

If you don't have any other TPLink devices that's the problem. SenseLink pretends to be just another TPLink smart plug w/ energy monitoring, so it shows up in Sense and Sense retrieves the energy measurements from it.

If you are having issues with the Tesla not showing up in Sense you can eliminate CheckTPLink(), that just controls TPLink devices you configure.

It could be that you are not signed into SenseLink, if you change the logging line from logging.WARNING to logging.INFO it will give a bunch more info that may make sense about what's not working. On mine I see:

INFO:root:Spoofed Device ID: 2002562a7d32773e876ee6cb2d525a94a4200731
INFO:root:Starting SenseLink controller
and
INFO:root:Starting UDP server


The issue can be the lines that set the value for variable mutable_plug - located once after discovering it's not charging, once after it starts charging, and once at the bottom of each loop of TesSense(). The value SHOULD be sent with the UDP server and be picked up as a TPLink device on your Sense app.

Otherwise there may be a problem with it working on your system. I mostly just put a print statement in to see if code is working. I'll quickly run down the sections and if you continue to have problems I'm still around to help

Top comment: Where I add changes I've made and version #'s
Variables: Where you put your passwords and things specific to your install
Dependancies: Other libraries required to get the code to work including comments on how to install each
Functions for TesSense: Code called by the TesSense function, makes the main code easier to read
TesSense: The code for communicating between Sense and the Tesla, it's just a function too
CheckTPLink: The code for communicating with your TPLink devices, ignored if you have no 'controllist'
main(): This is the multitasking heart, probably not worth touching as it only spawns the three parallel tasks
 
  • Informative
Reactions: scottf200
I think the issue was I didn't have the TP-Link integration enabled. It is now so we'll see if Sense receives charging data this afternoon.

In San Diego we have non-reimbursable charges as part of NEM, so any "pull" from the grid results in a small charge ($0.03 to $0.04/kwh) that can't be offset with excess solar generation. To provide a bit of a buffer and keep consumption under total solar production more often, I added a "- 2" on line 192:
newrate = min(rate + int(power_diff/volts) - 2, chargedata['charge_current_request_max'])
It was a shot in the dark but it seems to provide the intended ~500 watt buffer. Probably only makes sense if you have a good bit of excess generation.

When I noticed the car didn't automatically start charging this morning I saw an error in terminal. It looks like something failed during the 8am wake up. The script stopped, but I started it again and has been working ok so far today. Here is the output from the first error:
Mon 07:00 AM Nighttime, Sleeping one hour...
ERROR:asyncio:unhandled exception during asyncio.run() shutdown
task: <Task finished name='Task-3' coro=<SenseLink.server_start() done, defined at /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/senselink/senselink.py:274> exception=AttributeError("'NoneType' object has no attribute 'close'")>
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/urllib3/connectionpool.py", line 703, in urlopen
httplib_response = self._make_request(
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/urllib3/connectionpool.py", line 449, in _make_request
six.raise_from(e, None)
File "<string>", line 3, in raise_from
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/urllib3/connectionpool.py", line 444, in _make_request
httplib_response = conn.getresponse()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/http/client.py", line 1374, in getresponse
response.begin()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/http/client.py", line 318, in begin
version, status, reason = self._read_status()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/http/client.py", line 287, in _read_status
raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response
I don't have any other TP-Devices yet, so it sounds like I might need to comment out CheckTPLink() to avoid future errors. Or maybe add a TP-Link device. We'll see if it happens again tomorrow morning.