Optimizing use of unused Solar Power to charge a Tesla

found it :slight_smile:
The vehicle_id is the id attribute of binary_sensor.<car name>_online_sensor (not to be confused with the vehicle_id attribute).

Hi, i have been reading this thread and this is just what i need.

This is what i have

Tesla Model Y
Home Assistant
Solar panel system
Single phase 16A max charger, that is in no way “smart”, “WiFi enabled and so on”

I have sensors for “current house consumption”, “Current solar panel production” and also a sensor that subtract these to give a sensor that shows “Surplus solar”

I have some constraints, mainly that i am on a “use it or sell it” contract, where any power that i do not use at the same moment i produce it, must be sold at at low price, compared to what i would have to pay for it.

Also the inverter can do a max of 3700 Watts, if i consume more, i will be buying, whatever i use over 3700 Watts

I also have a battery pack, that the inverter can put any surplus up to 3000 Watts into, but only when the battery is not full, will i get more than 3700 Watts out of the solar panels, even though they are more powerful, so the system must be able to max out at 3700 Watt total power usage.

What am i missing, to getting this to work.

Disclaimer…I am NOT a coder, i have very basic understanding of the programming, i will at the most be able to change parameters/rename sensors, in a working script.

So…What am i missing (Besides the skills to make the scripting myself obviously :slight_smile: )

1 Like

In terms of skills to get this going, I am not a programmer either. And a lot if it you can do with the GUI. In fact, a lot of what I have done above is with the GUI. But in HA you can do things in either the GUI or CLI, and swap between and it will be translated (mostly works, but some things supported in the CLI, but not the GUI which does not break things, but they will not be visible in the GUI). Bottom line, it is not trivial to get this going, and there is a learning curve (which hopefully you are already well down if you already have HA). But it is not enormously hard either, and as long as you are happy to give it a go and use some common sense and cut and pasting coding and a bit of tweaking for your own setup, you should be able to get it going. Start with the Tesla integration and then you can move to some basic automations (if useful) and then to the basic script and automation to adjust the charging speed to use free solar.

In terms of what you need, you mostly have it. Certainly on the charger/Tesla side, you can at least start, because the smarts for all of this is in the Tesla side. So there is only 1 other mandator component to get this working and that is some basic way of measuring grid export/import in kW. So if your “Surplus solar” is a semi real time measurement in kW (or W) that tells you if you are exporting you can use that. If you “Surplus solar” is measure in kWh (or Wh), it is probably less useful and you will probably want a sensor that measures grid power (see below for what I use).

That is all you probably all you need to get something going. However depending on the detail of how accurate and granular your “Surplus Solar” is, you might want to add some addition sensors to significantly improve the granulatity and accuracy that you can track and use the “free solar”. Your “surplus solar” might be entirely suitable if it is an accurate reflection of kW you might be exporting and it is frequency updated (which in your case would I assume be entirely dependant on the frequency and accuracy of “Current solar panel production” and “current house production”). If this is all reading frequently or at least say less than every 10 seconds, you should get a pretty good result. Faster would be better (I read every 2 seconds and could probably go to every second). But if it is only updated every 15 minutes as some are, there will be lots of times when household usage changes in consumption or changes in solar output due to clouds etc won’t be accounted for. The good news if you need a better meter than you have got, there are lots of options for relatively affordable and easy to install to improve this. I use Iammeters which I am very happy with. See https://www.iammeter.com/.

Somewhat related to above, I would strongly recom you consider a meter for your Single phase charger. The issue is that to be constantly adjusting Tesla charging speed, it is handy to know in more real time what charging speed the Tesla is currently charging. You can get this from the Tesla integration in HA, but the truth is it is VERY delayed. ie when you set the charging Amps to say 2 Amps, there is a difference between the time it take to set that (which is usually quite fast) and the time that that value is ready back by HA and confirmed (which is probably more like 10 minutes by default, 1 minute if you tune it back). You can probably get around this programatically by tracking the values you set, and assuming the time to set it is short and has taken effect by the time you do your next reading. But this will work much better with an additional meter (eg Iammeter) to give me that number independant from the Tesla intergation to overcome that, and in more real time.

The logic of this whole setup is actually very simple at the high level. It is just to look at the grid export or import. If we have excess solar being exported, we can increase the Tesla charging speed by the size of that export. If you start needing to import power from the grid, then you need to slow the charger down by whatever is the size of that import. So we are always just trying to net the grid export/import to zero.

In some ways, your battery is both a help and hinderance to this. I assume your battery already some some of this and is probably really doing EXACTLY the same thing as the HA Tesla charging is doing above. ie monitors grid consumption or export and adjusts its charging and discharging rate to try and net that to zero at all times it has capacity to do this. On the plus side, that might potentially help “smooth” out any deficiencies you have in getting granulatity matching supply and demand with the Tesla charging due to deficiencies with your meters. But on the downside, the economics of using home batteries to charge the Tesla battery is often not as good as you think because of the wear this places on the battery.

The other challenge is that effectively your Tesla (via the HA automation) and your battery as likely competing for the same “free solar”. If this all can be controlled via HA, you should be able to prioritise it to suit your requirements. But if the home battery is not controlled via HA, you might have less ability to prioritise as you want. How they work together to give you something sensible with full HA control of both, might vary depending on the specifics of your implementation. Most likely they will probably work together to some extent, but you will probably have less control over if the free solar is going to the battery, or the Tesla. But at the worst case (eg there is a delay between reading grid input/output and implementing charges that can lead to large swings in power), they might be fighting against each other to give you a poor result.

A few things to consider : -

  1. How much “free solar” you actually have. With your small solar system + battery, I could believe you don’t have a lot of free solar. If not a lot of free solar, is it worth setting up. I could believe with only 3.7kW solar and a battery (depending on size), you might not have enough free solar to make it particularly worthwhile to get going.

  2. What is best for you, will depend on what electricity tariff options you have. ie if you have a very low priced off peak plan (which often have high peak tariffs to make them work), you might be better off to prioritise any free solar to charging your home battery, and using that to reduce you peak and shoulder usage to the maximum, and then us a very cheap off peak rate to do the heavy lifting to charge your car. Usually it is not a big deal when you charge your Tesla, so it make sense to use that as a load in the cheapest tariffs where as other loads like cooking, lights and TV in the evening need to be when they need to be, so makes sense to have the battery to cover these. But charging the home battery that then is discharged to the Tesla at night (or any time for that matter), is probably not good economics due to the wear on the battery PLUS efficiency losses. So you will really want to make sure you have some way to control that (or not worry and wear you battery out and get a better battery next time you can better control).

I suggest if you have not done so already, start with the Tesla integration to at least get control of you Tesla in HA. From there it is very easy to do some basic automations to at least control when they Tesla turns on and off without needing any coding (all easy via the GUI) if this is of any use to you. Even doing this gives you some benefits to manually give you better control than you have with the Tesla AP. Eg with my Tesla mobile charger, the min charging speed I can set is 5A which here is about 1.3kW. In the HA Tesla integration, I can set that down to 1A which is 230W (though it is worth noting that I suspect there is very little benefit of charging at 1A because I suspect by the time it is online it might be using that quicker than it is charging so for real charging you probably need a couple of A.

Good luck with your journey of discovery.

1 Like

Thank you for the answer.

To answer some of the concerns.

The actual surplus solar sensor is based off of data from the inverter, this is read at 1-2 second intervals by a connected “Solar Assistant” Raspberry PI, so it should be pretty accurate and “up to date”

Also the solar assistant is “in house”, before this i had the dongle that came with the system, but this dongle only communicated with the manufacturers server in China, and the integration in HA, would only update once every 5 minutes, making it utterly useless for this use case.

My system is a bit “complicated” because i have 3 phase power and a 1 phase inverter.

The way it works is, there is an inline smart meter on the 3 phases, it measures the current draw on each phase, and then it tries to match the usage on all 3 phases, and send the same amount of wattage back into the grid on one phase.

The inverter gets the data and does the “load levelling” so that i dont have to pay for the power used right now.

As for price, our prices are ofte very silly.

There are NO off peak, i pay, whatever the Nordpool stock is selling power at, at any given time. They do have it in hour intervals, but it is never cheap, especially considering that the government puts taxes on top of the price.

In H1 2023, they lowered the taxes to a fixed amount of $0,09 on top of every kilowatt bought, but before that and when we enter H2 2023, the tax will go up to $0,20 and then you have to add the cost of electricity.

And when i sell a kilowatt, i am offcourse not getting those taxes, only the price for the electricity, with a deduction because the “supplier” has to make money on that kilowatt as well.

So it will be well worth it in the long run, if i can maximize the usage.

The other benefit is, i dont have to manually turn on, and off the charging of the car, if the wife needs to run the washing machine, the clothes dryer or the dishwasher, i would be able to let her just do her thing, and then this will adjust itself.

I was kind of hoping someone had a “production ready” script i could “just copy” and adjust some figures, but as said, i am not very savvy, so i am probably going to not even try, as i know i will fail miserably.

I have been looking for some “charger side” implementation, but so far i have only found a few, and they all relied on me installing current sensing equipment, for the load balancing.

I will just have to wait until some charger manufacturer deems it profitable to make something, that is working out of the box, and i only need to feed it my one surplus solar sensor, and it will do the rest.

Thank you for your thorough and lengthy answer, i really appreciate it.

I will install the Tesla integration, it would be nice having it in Home Assistant, regardless of whether i can automate the charging.

This is very good, not only for having frequent spare solar measurement, but also potentially to give you full control over your battery inverter so you can prioritise Tesla charging or home battery charging to suit your priorities? But to get the “extra” functionality in addition to the Tesla/solar charging control, this would depending on you having control of your battery inverter/charging in Solar Assistant?? I use SolarAssistant to integrate my MPP Solar/Battery inverter to HA assistant via MQTT which in theory gives me full control over my battery inverter (I say in theory as I am working with Solar Assistant to fix a small bug with 2 of the parameters, that I am confident will be fixed very soon because of their good support, but most of it is already working). So if you can control your battery inverter with solar assistant, it is almost certain you will be able to extend that control to HomeAssitant so you can have full control over both your Tesla charging and the home batter charging. So sounds likely you have have all the components to do whatever you are likely to want to do with full Tesla Charging from solar as well as prioritising between the Tesla and Home battery.

This is pretty standard and the way it works here in Australia and assume lots of other places. And the economics probably similar in that you are better off maximising the solar you use rather than export. And I also assume there are probably slightly less bad times to be importing and using grid power. So with HA, as long as you can get all the right inputs (eg forcasts of when power is going to be cheaper or more expensive), there is a lot you can do to optimise and reduce the price of keeping the Tesla charged whether that is maximising what you can get from solar, AND picking the cheapest times to charge from the grid when you need to. And the Tesla is a great load to use for this because it is not only big, but you are probably relatively flexible in when it is charged in a lot of cases.

My scripts above should be close enough to being production ready. But they will need some adjustments to suit your purposes. If you have enough skills to get yourself going with HA and SolarAssistant, I would guess you are not a million miles off not being able to do this. Some things you be trivial (ie charging your Tesla name, name of you grid energy sensors). And some things a little harder, but not rocket science either if you can justify the time to giving it a go (eg using the value you set the Tesla to charge, rather than my seperate meter which measures the actual charging rate, and also some things to integrate your battery inverter/charger into the solution in order to be able to prioritise between Tesla and battery inverter to suit your preferences).

But again, do it 1 step at a time will make it look a lot easier. Eg start with getting the Tesla integration going and the battery inverter integration if you want to take that optional step. Then you can move to the automation and scripts if you want to.

There are a number of commercail off the shelf offerings which are targetting charging Tesla from free solar. Yes, they will all require some way of measuring the grid/free solar, and with the turn key solutions this sometimes means their own meters. But on the plus side, I assume this should make it easy to find someone to do that for you (no doubt at some cost). Some solutions I am aware of include https://chargehq.net/ and things like the Myenergi Zappi and Smappee EV chargers to name a couple. And these might have some appeal to get away from depending on a platform like HA which requires some technical expertice to a platform you can just paysome to install and get working for you. And might even have some advatanges of HA (eg MyEnergi also has a solar diverter to heat a standard resistive hot water service with incremental solar which might also have a lot of appeal if you use this type of HWS and want another load you can drive with solar). But I suspect without knowing these solutions well enough, I have my doubt whether anything will give you the infinite flexibility to integrate everything together and drive it the exact way you want rather that the often limited configurations designed to suit the majority from the turn key solutions.


Thanks for the chargehq suggestion - it works perfectly for me since I have a Tesla EV and Powerwall. I was attempting to go through this thread to set it up, but this app does it all for me by simply logging into Tesla.

If I didn’t have a powerwall, it wouldn’t have worked out since it doesn’t seem they support Enphase integration

Yes, https://chargehq.net/ is going to be a good fit for some people. If you happen to have the right hardware to support it, it can be a good way forward without some of the complextity of HA.

I would like them to support a cheap generic power meter with no ongoing monthly cost such as the Iammeter so there is a solution for everyone that decouples the need for specific solar inverter or battery support. But they are close with support of SolarAnalytics and Wattwatchers (not that I have any experiece of Wattwatchers). But both of these look like they depend on online services with some sort of ongoing fee (or up front cost for these services) which is less than ideal for me. I have suggested Iammeter support to both parties, but no trackion on this as far as I know.

The other issue in the future with ChargeHQ is they have yet to establish a commericail business model to fund it that i know about. So it would not be surprising if there is a charge coming to users at some stage, though I believe it is free for the moment.

But having said this, I suspect its advantage over HA is that it is probably much easier to setup for the non technical if they happen to have the right bit of kit that is supported by ChargeHQ. But I suspect not much will compete with HA if you want full control to do whatever you want and full integration with all sorts of other things that are more likely to be supported through HA long before they are supported in propriety bespoke platforms. This is the real power of HA to act as a broken between lots of otherwise not integrated platforms.

1 Like

I invested in solar panels and I’m now looking to Tesla a new car. I was looking on how to charge my car with solar via HA, but then I came across EVCC. It can be installed as an addon for Home Assistant and it looks really promising. Might be worth it to give it a try if you haven’t got a solution yet.

Looks promising from their website, but i was unable to find the addon, for home assistant, both in official supported, and HACS sections.

Where and how would i go about installing it as an addon for Home Assistant?

Thank you.

Tried this but i am not clever enough to even get past the first steps apparantly :slight_smile:

Did as the manual says, and it apparantly added the evcc, i can see it in the “three dots - Repository” menu with a red trashcan besides it.

But when i close the window and go back to repositories and search it does not find the EVCC one anyway.

Did try to restart, but it is still not there.

Using latest version of Home Assistant OS (HAOS) installed on a RaspberryPi 4

You need to a hard refresh to see the new addons after adding a repo. Shift+F5 should do it if you’re on the addon page

Thank you that worked.

Now comes the hard part, it seems that all setup is done inside the evcc.yaml i created.

When i start the evcc it offcourse complains about missing stuff. I found the documentation and found the section for Growatt, but it seems it expects a RS-485 connection to the inverter. I already have solar assistant connected via RS-232 .

Is it not possible to setup the system to use the sensors i have already integrated into Home Assistant ?

For the battery i have a sensor called “sensor.battery_state_of_charge”

For the hose usage i have a sensor called “Load power” (This is current combined consumption)

For the solar panels i have a sensor called “PV power” that shows how much solar the solar panels are producing right now.

With those sensor i believe the calculation on how much power the charger is allowed too draw should be possible to calculate.

These sensors are updated every 2 seconds

I also have a max usage of 3700 Watts as this is how much my solar inverter is able to deliver to the grid, that needs to be set. This is what the system should try to (just) stay under, when charging.

Another option would be to use my blueprint/script for PV excess automation. That should work for your Tesla (or wallbox) too.
See https://community.home-assistant.io/t/pv-solar-excess-optimizer-auto-control-appliances-wallbox-dish-washer-heatpump-based-on-excess-solar-power/

1 Like

You could use the HTTP component in EVCC and call the Home Assistant API to get the sensor data.
I haven’t tried this yet, but I use the HTTP component for my WiFi P1 meter

So there is no way i can just feed it a home assistant sensor and let it use that directly, i need to have it call the actual Solar Assistant Raspberry to get the data ?

No, because it’s not a Home Assistant integration. Is just a Docker container that you can load via the add-on store, but it has no direct connection with the Home Assistant sensors.

If you use modbus there is probably a 1 device limitation. To get around that you can setup the Modbus proxy addon. That addon will make the connection to the inverter and then the other services will send their requests via the addon over that same modbus connection.

Fair enough, i think i will find another solution then, i wanted something that integrated into Home Assistant, not another standalone solution.

But thanks for the tip.

this is mine, GitHub - torsteinelv/NodeRed-Excess-charging-flow: Charge EV from excess solar

I am currently using “switch on Tesla charger when Powerwalls are above 99%” which seems to work well.
However , i cant get it to switch off again when Powerwall drops to 90%. i had it working last year, so not sure whats happening differently now.