DIY smart grid: EV (Tesla) charging on excess solar power production

Hi @dikkertjedap love your work and looking to implement this. I’ve copied across all your code and automations and then stumbled across this comment and extra code for 3 phase which i have. Where do i insert the 3 phase code or what code do i need to modify?

Excellent work @dikkertjedap
I am going to implement this tomorrow as it seems to go way beyond what I had in mind.

I’m also on 3-phase and I think the most simplest way to make it work is to set the voltage as 720v instead of 240v. That should make all your calculations work without modification.

I saw you mention a few times about Tesla ramping up and down quickly at superchargers. I felt I had to point out that it is completely different to home charging. A super charger pumps DC directly to the battery. The actual charger is at the SC site. Home charging uses the cars built in charger to convert AC to DC.

I have just installed a Shelly EM3 which updates the house load every second, so I think it best I add the slight delay, maybe 15 seconds, as it’s highly likely the API and car will start charging within 1 second.

Thanks again.

Hi all! Love the automation scheme. I set it up yesterday and have been tailoring it to my needs. Two issues im having right now.

The Charge on Solar automation is triggered every 1 minute and its always updating the charging current. I’m assuming this wakes up the Telsa everytime. Even if charging is off. Looking through the logs, it was continually setting the charging amps to 5 (my minimum, as per my settings) all night. The car stopped charging yesterday at 74% and I woke up to it being at 72% which I’m thinking the drain was from the automation constantly polling the car waking it up? I’m thinking I can just add another condition for Charge on Solar to not trigger if the sun isn’t up. Any other suggestions?

The other issue I am having is the car never starting charging again when solar started producing. The amperage was continually getting set higher, as it should, but the charging never restarted… Car was set at 80% charge limit, car was only at 72%. Logs show the charging was waiting on car.

Hi Tim.

I setup my system with the Sense integration. I had to build another sensor like @dikkertjedap did at the top of this thread.

Also, I’m using the entities from Tessie. All the entities used in the automation should be based off Tessie and shouldn’t need any information from the Wall Charger or Mobile charger.

Hi Adam,

it’s so nice to hear people taking this up and adapting!

As for the automation triggering all night: have you implemented the Smart Charging boolean function? As you can see, my smart charging setting defaults to ‘off’ so the car always starts charging to the desired max setting. When the charger disconnects, the smart charging also resets to ‘off’. As the boolean is implemented as a condition, it seems to me that it never triggers runs any part of it when the boolean is set to off.

Any polling done to the car by any app or third party platform (e.g. Tessie or any other automation from Home Assistant polling the car) keeps the car awake, just as Sentry Mode does. Have you turned all these off? I would certainly recommend you skip the Tessie integration and use the Tesla Custom Integration from HACS, as this is a direct implementation of the Tesla API instead of a ‘via’.

As for the charger not restarting: I’m not sure how you are pausing or stopping the charge session as the grid power production drops <0, so I can’t really say what’s going on there! Could it be that you set your car to specific charge times through the app settings? Or might this be a Tessie function (I’m not familiar with the platform)?

1 Like

Hi there! Happy to hear this might help you out in some way.

Thanks for detailing the discussion on the down- and upramping of voltage. You are of course absolutely right concerning the difference in functionality of the AC and DC charging. Still, in my opinion it remains safe to assume that although the physical connection differs (DC charging bypasses the internal power converter), as long as the changing of amps happens well outside the range of 50 to 60 times per second, you won’t see any negative effects.

I’m curious on how you’ll integrate the EM3 and what your experience will be, I’ve never seen or used it but I have developed a keen interest ever since Enphase decided that my local data is their cloud data. Keep us updated!

So I managed to get this working yesterday.
I have eventually modified the script a lot, as I like things to be a bit more verbose, so i’ve split it up into more conditions:

  • Car is not currently charging and we have enough spare amps
  • Car is charging, we have spare energy and we aren’t at the maximum charge amps yet
  • Car is charging and we don’t even have enough juice to charge at minimum amps
  • Car is charging and we are using too much energy
  • default: Nothing to do

The shelly works great, as you get a trigger every time the grid usage changes. I use your delay code to only run the automation after 5s has passed. This does cause the amps to go slightly too high for 10-15s after first starting the charge, because the car hasn’t even ramped up the charge before it’s recalculating based on the amps set. Shame we can’t use the actual charge amps instead of just what is set.
It settles down pretty quickly.

Setting the voltage to 720 does in fact correct all the calculations for 3-phase, so no other changes required there.

I did notice an issue when setting the amps to zero, which may be the same for @Hermi88 issue:

When setting amps to zero the Tesla app sends me a notification that charging has stopped. When increasing the amps again the charge does not start. Manually starting the charge, even through the Tesla app results in an error. The only way I can start charging again is to unplug and replug the car.
My car is a 2017 Model S, still with the old MCU 1 computer, so it is likely an issue with it not having all the latest updates.
Using the script to delay switching off seems to be the best I can do when there is less than 1A.
I started implementing your delayed turn off script, but it seemed to freeze the automation, so today I will be implementing a Timer to set the charger off after a delay which can be cancelled if the sun comes back out.

The cars state isn’t always up to date if you adjust something through the app. How do you handle polling the car?
Do you increase polling frequency when charging?

Thanks for the quick reply. I appreciate the suggestion on using the HACS Telsa Integration but it’s infuriating on how hard it is to setup. I’m struggling setting all the addons with creating a domain through duckdns, nginx and then the HTTP tesla proxy. For whatever reason I can’t get the duckdns domain to be secure for tesla to accept my application. Not sure what I’m not seeing… Did you have issues configuring?

I’m really curious to find out how you got yourself into this, as none of this concerns the Tesla custom integration as far as I’ve experienced. I’m talking about this one: alandtse/tesla: Tesla custom integration for Home Assistant. This requires a refresh token be generated by third-party apps to login. (github.com)

All you need to do is create a refresh token as per the instructions, and you’re good to go!

Great to hear your customizations work to your liking! I’m sure the 720 volts ‘trick’ works just as well, I do prefer the x3 solution as this is mathematically ‘more correct’ (but I don’t want to be the frustrated math teacher here that rejects your outcome because of a ‘wrong’ calculation :wink: ).

To avoid disabling of the charging session and difficult plug-out-plug-in scenarios, it might be helpful to adjust the script to avoid setting the amps to 0, but instead temporarily stop charging altogether.

Yes this is what I have done now. I’ve set a 2A minimum startup and a 1A minimum drop when charging. It starts a 2 minute timer to turn the charger off if it drops below 1A but cancels the time if it goes back above.

Thanks again for your efforts here.

The Tesla API has been updated and many cars no longer work with the old access token method. It now requires a proxy GitHub - llamafilm/tesla-http-proxy-addon: Tesla HTTP Proxy Add-on for Home Assistant

I’m lucky as mine hasn’t dropped off the old access method yet, but i’m going to look into getting the new stuff working soon.
I believe it should be easier if you use a Cloudflare socket as this uses a domain name and valid SSL certs.

@dikkertjedap - Basically the reason why I got into such a deep dive was as mentioned above by @dazboj. I read it as I wouldn’t be able to use the normal API without using the fleet API. However, if I knew how to comprehend what I read, I would have understood that the original API still works but it has lost some functionality but for the needs of what we are discussing it still works.

I am grateful for your response as I went with the original API and I was able to use the Telsa Custom Integration. Now I can ditch the 3rd party Tessie! Thanks again. The automation you build here is a great foundation that I can adjust for my specific needs.

Thanks again!

Hey everyone! Any chance someone wants to create a repo with examples of common configs and a readme with a few FAQ’s?

This has become quite the mega thread and it’s a lot to digest for people who stumble upon it. Just an idea!

I’ve been trying for a couple days to implement the automation for my new Tesla but can’t quite work out what I’m doing wrong. My grid_power is below 0 but the charger remains charging. Any guidance? happy to share all automations I’ve put in if that helps?

This is the automation:


alias: "[Tesla] Charge on solar power"
description: ""
trigger:
  - platform: state
    entity_id: sensor.power_grid_usage
condition:
  - condition: state
    entity_id: binary_sensor.charger
    state: "on"
  - condition: state
    entity_id: device_tracker.location_tracker
    state: home
  - condition: state
    entity_id: input_boolean.smart_charge
    state: "on"
action:
  - variables:
      grid_usage: "{{ states('sensor.power_grid_usage') | float }}"
      max_charging_amps: 16
      min_charging_amps: 1
      grid_voltage: 720
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ grid_usage > 0 }}"
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.charger
            data: {}
          - service: number.set_value
            target:
              entity_id: number.charging_amps
            data:
              value: >
                {% set charger_power_draw = (states('number.charging_amps') |
                int) * grid_voltage %} {% set charging_amps = ((grid_usage +
                charger_power_draw) / grid_voltage) | round(0, 'floor') | int %}
                {{ [max_charging_amps, charging_amps] | min }}
      - conditions:
          - condition: template
            value_template: "{{ grid_usage <= 0 }}"
        sequence:
          - service: number.set_value
            target:
              entity_id: number.charging_amps
            data:
              value: >
                {% set deficit_watts = -1 * grid_usage %} {% set
                current_charging_amps = states('number.charging_amps') | int %}
                {% set required_charging_amps = ((deficit_watts / grid_voltage)
                | round(0, 'ceil')) | int %} {% set updated_charging_amps =
                current_charging_amps - required_charging_amps %} {% if
                updated_charging_amps < min_charging_amps %}
                  1
                {% else %}
                  {{ [max_charging_amps, updated_charging_amps] | min }}
                {% endif %}
          - choose:
              - conditions:
                  - condition: template
                    value_template: "{{ states('number.charging_amps') | int <= 1 }}"
                sequence:
                  - service: switch.turn_off
                    target:
                      entity_id: switch.charger
                    data: {}
mode: single

sensor.power_grid_usage has been created correctly and it showing negative when pulling from the grid and positive when there is a solar surplus but the automation is not turning the charger off when no solar available.

Have you clicked Traces at the top to view exactly what happens?

Yes I have.

So it’s failing on that last check.
Could it be that it’s lowering the amps, but obviously that doesn’t happen instantly so number.charging_amps isn’t 1 at the point it’s checked.

In my setup, I use a lot more local variables in the automation like this:

    # Setup some variables
    - variables:
        min_charging_amps: 1
        minimum_start_amps: 2
        max_charging_amps: "{{ state_attr('number.jess_charging_amps', 'max') | int }}"
        current_charging_amps: "{{ states('number.jess_charging_amps') | int }}"
        spare_energy: "{{ states('sensor.power_in_or_out_reversed') | float }}"
        grid_voltage: "{{ 240 | float * 3 }}"
        spare_amps: "{{ (spare_energy / grid_voltage) | round(0, 'floor') | int }}"
        new_charge_amps: "{{ (current_charging_amps + spare_amps) | int }}"
        charge_limit: "{{ states('number.jess_charge_limit') | int }}"
        battery_level: "{{ states('sensor.jess_battery') | int }}"

That way you know they won’t change during the automation plus you can actually view what they are within the trace.

Thanks i’ve implemented your suggestions and now when i do the trace i am getting the following error

“Error: In ‘template’ condition: UndefinedError: ‘grid_usage’ is undefined”

You need to change my variable names to match your calculation. I changed a lot in my automation so it’s very different, it’s not just a copy and paste.