Optimizing use of unused Solar Power to charge a Tesla

I also use sundown as a trigger

and what is psupply2? solar generated power?

yes, reading from solar generated power

Maybe i just cannot find the information, or its not there.
Can you tell me the following:
sensor.p1_meter_3c39e7240372_active_power - What is this monitoring? Whole house consumption?
sensor.template_ev_consumption_home - What is this monitoring - Car charger power consumption?
sensor.psupply_2 - Solar production (output from inverter to house)?

Do you maybe have telegram/discord or something else, and wanna help me a bit?

sensor.p1_meter_3c39e7240372_active_power = homewizard p1 meter, which gives the net consumption of the whole house *= total consumption - solar power)

sensor.template_ev_consumption_home = consumption from charging electric vehicle

sensor.psupply_2 = generated solar power (from inverter)

This looks pretty interesting. I was planning to use TeslaMate to get this working with teslasolarcharge, but hooking that up to HA turned out to be rather challenging. Since it appears the Custom Tesla Integration also allows me to set charging amps (though I still have to figure out how) I will attempt to follow the hints on here and hope to get it working!
Thanks for the excellent work!

First order of business is going to be figuring out how to adapt this for use with my PowerFox Poweropti…I have that running in HA, so it should be possible.

Edit:
So…I built a basic test setup but it doesn’t seem to work. I made two automations, one of them starts the charging as soon as there is 100W of extra power for a Minute and the second one turns the amps to “0” when there hasn’t been any excess for 5 Minutes. Sadly both stop with an error (without telling me what that error might be) right after I trigger them.
My guess is the vehicle_id is wrong. I used the “name” my Tesla has but I assume I need some kind of internal Id which I unfortunately cannot find anywhere.

Here is my crude little test automation:

alias: Tesla_Solarcharge_Off
description: “”
trigger:

  • platform: numeric_state
    entity_id: sensor.poweropti_aktuell
    for:
    hours: 0
    minutes: 5
    seconds: 0
    above: 0
    condition: []
    action:
  • service: tesla_custom.api
    data:
    command: CHARGING_AMPS
    parameters:
    path_vars:
    vehicle_id: tai’daishar
    charging_amps: “0”
    mode: single

Help would be much appreciated! I have the HACS Custom Tesla Integration up and running and it can access my Tesla.

After reading thru the thread I don’t have much useful to add as I am not a HA user. I was linked to this thread from the Sense Energy Monitor forum as I had written a terminal python script that every two minutes will adjust the rate the Tesla charges based on free solar, so same destination as here. It’s called TesSense and is available on GitHub if you wanna check it out, I am up to version 9

I have learned a few things in writing it, so I thought I would comment. Charging Amps can be set below the 5 amp limit by issuing the API command a second time. You can set it to 4 or 3, if you issue the command to set it to 2 amps, it will set it to 3 amps. If you issue the command it set it to less it will set it to 2 amps. So you may THINK you are no longer charging, but if you get in the car or look on the phone app you will see it’s still charging at 2 amps.

I don’t have the app check for sundown as it will turn off charging long before that based on going below two amps, if there are not two amps available I stop the car charging. There are some stormy days where this causes it to toggle back and forth, but not so bad that I have written code to fix it. I do however have the code sleep from 8pm to 8am just so I am not bugging the Tesla servers all night.

I also emulate a wall plug energy monitor so that I can display the power going into the Tesla on my Sense monitor app, sure beats all that power just being bundled with the rest of the unknown power usage. Probably you guys have a way to do that with your HA

Good luck with it, hope you guys enjoy charging your Teslas as much as I do mine. Tracking solar is gonna be REAL important now that the sun is disappearing earlier and earlier. Most of the time since I wrote this app it’s been going the other way, so this is exciting to me.

1 Like

Since I could not get the easy test setup to work I copied and modified the automations provided in this thread. They don’t throw any errors (although I could not duplicate everything since I only have one sensor input, not 3) but I could not test it yet since the sun had…gone away…:joy:

To be honest I feel very much out of my depth here…I was hoping for a simple solution I could build myself, essentially linking a certain excess energy to a certain number of amps in a set of automations.
The approaches listed here are far superior of course, but since I don’t really understand them very well I have a hard time adapting and modifying them.
Well, I guess I will either have to learn to read them or find a way to build my simple implementation myself after all…:man_shrugging:

So…I tried the provided automations and scripts, but could not get them to work properly for my setup…

Problems:

  1. The “location” sensor always sticks on “away” although the car is parked in the Carport and Tessie app has it located as “home”. I deactivated the “home” requirement to allow the automations to run at all.

  2. The calculations in the script don’t work for me because I have a little reader sensor (Powerfox Poweropti) that gives me realtime (every 5 seconds) readings on excess energy. I don’t have any way of calculating anything else since I lack additional sensors outside of what the car offers.

What I am trying to do is this:

  1. Check if the far is plugged in (works)
  2. Check if the car is home (doesn’t work)
    2a) Stop charging from Mains if SolarCharge is active (optional)
  3. read the current value from my Poweropti (works)
  4. start charging at the lowest possible Amps (1A)
  5. check the readout from the Poweropti every XX Seconds. If there is still excess, then raise amps by 1, if there isn’t, then lower amps until they reach 0 at which point the charge should disengage
  6. Create a dashboard switch to turn all corresponding Automations and/or scripts on or off so I can still charge normally off the grid if there is no sun and/or if I need to charge the car quickly.

I feel like it should be quite simple to modify the automations and scripts given in this thread to do what I need, but since I am a complete noob in terms of HA I am struggling.

Edit:
I managed to get the location thingy fixed at least. Turns out my home location was not correct in HA…

Edit 2:
There are two lines I struggle with:
charging_amps: “{{ ((states(‘sensor.poweropti_aktuell’))| float /230)| int }}” this is unclear to me. I chose my current available energy (essentially the excess read from the main power meter) and followed some of the instructions above, hoping this would give me the correct amps. originally this was calculated from a different sensor in the examples so I don’t know whether this is correct. help?
Another line I struggle with:
alias: TeslaStopCharging
description: “”
trigger:

  • platform: numeric_state
    entity_id: sensor.poweropti_aktuell
    for:
    hours: 0
    minutes: 5
    seconds: 0
    above: “300”
    The original file gave “above 5000” and I don’t really know what this means. Can someone break it down for me? I would have thought the goal is to prevent consumption going above “0”…or is this something else?

Update3: here is another approach, sadly it is based on a system with home-battery so not quite what I need, but it has some potentially interesting variables.

Edit 4:
The approach linked above does seem interesting, but it throws countless syntax errors and I am not having much luck getting it fixed…I simply don’t know enough yaml Syntax to fix it.

So…I got this working, but not really. It runs, but stubbornly sets the amps to 0 and leaves them there. I tried the calculation for 3 Phase and 1 Phase and both behaved the same.
At the same time, if I use the Tessie app and set Amps to 1 it won’t stick, but 2 amps appears to work fine although it fluctuates between 2 and 3 for some reason. It does work though, so I guess I made some mistake while adapting the automations and/or Script. More research required I guess.
This is all research right jow anyway since I only have a tiny solar array right now, the proper one is supposed to arrive early next year. But I am hoping to work out the solar charging before then.

Edit: Sorry about all the monologue here…
Current state of affairs: it starts OK, then sits at 0 amps for a few Minutes. After that it quickly goes all the way up to 13 Amps (why?) and then of course stops because it ran out of excess energy long ago (somewhere around 2 Amps actually). Currently working with the “TotalSolar” automations and scripts, maybe I should try a different approach…

Edit2:
Some success at last! Manually setting the charge amps to 1 or 2 does work. I put the number in the script and on the first run it doesn’t stick, but on the second run it does. That was posted above already, seems like a bug or something.
So I have something that works for now and will now try to figure out how to make it work with dynamic adjustment as well.

Edit3: I seem to be stuck…

As long as I choose a static value for the amps and set it in the script then it works.
As soon as I use the provided formula @Test123 provided above then the amps get stuck at 0 and/or keep rising.

I am using my PowerFox Poweropti which provides me with a value in “Watt” and updates every 5 Seconds. If I have excess energy the value will turn negative. It always shows my total excess energy and that is what I am working with. I use the formula /230*1.4 to calculate amps since I am currently running a 1 Phase test setup.
Where am I going wrong? Is it something to do with the (-) reading?

Edit4: combining the script
service: tesla_custom.api
data:
command: CHARGING_AMPS
parameters:
path_vars:
vehicle_id: “{{ state_attr(‘binary_sensor.tai_daishar_online_sensor’, ‘id’) }}”
charging_amps: >-
{{ (( states(‘sensor.tai_daishar_charging_rate_sensor’) | float /4.3) -
(states(‘sensor.poweropti_aktuell’)| float /230 *1.4)) | round() }}

With the “Totalsolarcharging” automations above seems to be giving me slightly more joy. Will test this some more and see…

I deleted my old post but added it below again and updated some of the elements
Note that:

sensor.p1_meter_3c39e7240372_active_power = homewizard p1 meter, which gives the net consumption of the whole house *= total consumption - solar power)

sensor.template_ev_consumption_home = consumption from charging electric vehicle

sensor.psupply_2 = generated solar power (from inverter)

Below you will find a Script. Make sure you get that one up and running first before you start with the rest

Automations

alias: TeslaStartCharging@ExcessSolar
description: ''
trigger:
  - platform: numeric_state
    entity_id: sensor.p1_meter_3c39e7240372_active_power
    for:
      hours: 0
      minutes: 0
      seconds: 20
    below: '-500'
condition:
  - condition: state
    entity_id: binary_sensor.tesla_model_3_charger_sensor
    state: 'on'
  - condition: state
    entity_id: device_tracker.tesla_model_3_location_tracker
    state: home
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.tesla_model_3_charger_switch
  - service: automation.turn_on
    data: {}
    target:
      entity_id:
        - automation.triggerexcesssolarchargingeveryminute
mode: single
alias: OnPluginCheckExcessSolar
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.tesla_model_3_charger_sensor
    to: 'on'
condition:
  - condition: numeric_state
    entity_id: sensor.p1_meter_3c39e7240372_active_power
    below: '-500'
  - condition: state
    entity_id: device_tracker.tesla_model_3_location_tracker
    state: home
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.tesla_model_3_charger_switch
  - service: automation.turn_on
    data: {}
    target:
      entity_id:
        - automation.triggerexcesssolarchargingeveryminute
mode: single
alias: TriggerExcessSolarChargingEveryMinute
description: ''
trigger:
  - platform: time_pattern
    seconds: /15
condition:
  - condition: time
    after: '06:00:00'
    before: '21:00:00'
  - condition: state
    entity_id: device_tracker.tesla_model_3_location_tracker
    state: home
action:
  - service: script.teslachargingsetampslinktoexcesssolar
    data: {}
mode: single
alias: TeslaStopCharging
description: ''
trigger:
  - platform: numeric_state
    entity_id: sensor.p1_meter_3c39e7240372_active_power
    for:
      hours: 0
      minutes: 1
      seconds: 0
    above: '2000'
  - platform: sun
    event: sunset
    offset: '0'
  - platform: numeric_state
    entity_id: sensor.tesla_model_3_range_sensor
    above: '415'
  - platform: state
    entity_id: switch.tesla_model_3_charger_switch
    to: 'Off'
  - platform: numeric_state
    entity_id: sensor.template_ev_consumption_home
    for:
      hours: 0
      minutes: 3
      seconds: 0
    below: '500'
condition:
  - condition: state
    entity_id: device_tracker.tesla_model_3_location_tracker
    state: home
action:
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.tesla_model_3_charger_switch
  - service: automation.turn_off
    data: {}
    target:
      entity_id:
        - automation.triggerexcesssolarchargingeveryminute
mode: single
alias: OnPlugoutStop60secTrigger
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.tesla_model_3_charger_sensor
    to: 'off'
condition:
  - condition: state
    entity_id: device_tracker.tesla_model_3_location_tracker
    state: home
action:
  - service: automation.turn_off
    data: {}
    target:
      entity_id:
        - automation.triggerexcesssolarchargingeveryminute
mode: single

Scripts

This script is key and probably one of the first things you should try to get up and running.

As a first step simply try to change (through the script) the AMP to a fixed number as per the code below

alias: TeslaChargingSetAmpsToTry
sequence:
  - service: tesla_custom.api
    data:
      email: XXX
      command: CHARGING_AMPS
      parameters:
        path_vars:
          vehicle_id: "{{ state_attr('binary_sensor.tesla_model_3_online_sensor', 'id') }}"
        charging_amps: "4"
mode: single

Once the above works you can take the code below which will calculate the best level of AMPS

Your calculation may be different in case you have a one phase system. The calculation below is for a three phase system

alias: TeslaChargingSetAmpsLinkToExcessSolar
sequence:
  - service: tesla_custom.api
    data:
      email: XXX
      command: CHARGING_AMPS
      parameters:
        path_vars:
          vehicle_id: '{{ state_attr(''binary_sensor.tesla_model_3_online_sensor'', ''id'') }}'
        charging_amps: >-
          {{ ((states('sensor.template_totalexcesssolarforcharging'))| float
          /1000*1.4) | int }}
mode: single

Templates

- platform: template
    scan_interval: 10
    sensors:
      template_totalexcesssolarforcharging:
        friendly_name: "Total Excess Solar for Charging"
        value_template: >- ### This is to not to go negative ###
          {% if (states('sensor.p1_meter_3c39e7240372_active_power') | float *-1 + states('sensor.template_ev_consumption_home') | float ) >= 0 %}
            {% if (states('sensor.p1_meter_3c39e7240372_active_power') | float *-1 + states('sensor.template_ev_consumption_home') | int ) <=  ( states('sensor.psupply_2') | int ) %}
            {{ (states('sensor.p1_meter_3c39e7240372_active_power') | float *-1 + states('sensor.template_ev_consumption_home') | float ) | round(2) }}
            {% else %}
            {{ states('sensor.psupply_2') | int }}
            {% endif %}
          {% else %}
            {{ 0 | int }}
          {% endif %}
        unit_of_measurement: "W"
        device_class: "power"
- platform: template
    scan_interval: 5
    sensors:
      template_ev_consumption_home:
        friendly_name: "EV Consumption Home"
        value_template: >-
          {{(states('sensor.shellyem3_c45bbe6bed2f_channel_a_power') | float + states('sensor.shellyem3_c45bbe6bed2f_channel_b_power') | float + states('sensor.shellyem3_c45bbe6bed2f_channel_c_power') | float ) }}
        unit_of_measurement: W
        device_class: power

This indeed a long and confusing monologue but you are trying so you will get there.

The above calculation I don’t get. I would propose, if you are trying to use what I posted, to stay as close as possible to what i have done

states(‘sensor.tai_daishar_charging_rate_sensor’) | float /4.3) —> I don’t know what this is supposed to do ? I would propose you take it out here and work in a template as I have done

(states(‘sensor.poweropti_aktuell’)| float /230 *1.4)) | round() }} → I understood you get a value in WATT of how much your charging is consuming —> if that is the case and you wish to gr from watt to KW you need to divide by 1000, not 230. If you want to go from kw to Amps I think (not sure) you then have to multiple by 4.2 or 4.3. So it should be: /1000 * 4.3

Thank you for replying. I have managed to get it working well with a static 2 Amps for now. The dynamic charging with excess energy will have to wait till next year when my rooftop solar generator will be installed. Currently my Solar is limited to 0.6KW due to regulations here in Germany. Anything over 600W requires installation by an authorized electrician.

Once I have 8KW instead of 0.6KW I might have more luck adapting your solution. My biggest challenge will be working around the fact that I do not have the “psupply 2” Sensor or anything to replace it with. Thus the template won’t work for me.
My attempt with the tesla charging speed sensor from the car failed, I think because that particular sensor in the Tesla Custom Integration updates very slowly.

The psupply 2 is simply the production of your Solar. You should be able to get this through
A) your inverter. There are a lot of integrations for the different inverter brands
B) you can add an energy meter that measures your Solar production. You can do this with a shelly EM or shelly EM3

Irt the tesla charging speed sensor. Yes this does not work properly. Either you
A) get data in from your charging station if it can be connected to HA
B) measure the power used by your charging station with e shelly EM or shelly EM3

Unless of course German Solar set ups work differently

My current inverter is a Growatt. It integrates with HA but the rate it updates at is too slow to be useful. I do have a little measurement plug that shows the currently generated solar which I can try. It is a bit slow (updating maybe every 30-60 Seconds) but it might work.
I will see whether I can get this to work tomorrow…if the sun decides to shine that is… :wink:

My Wallbox is unfortunately not “smart” in any way, so I can’t integrate that in HA directly. Having a Shelly 3EM installed is on my list, but electricians are pretty much booked out for the forseeable future, so that’s more of a long term goal.

@Test123
It seems I am stuck with static charging at 2 Amps for the forseeable future. No big Deal since the sun isn’t going to be around much in the next few months and by January, when my proper Solar setup is supposed to be installed I should have figured out what I need to do to make it work for me.

Getting a reading for the power consumption on my Wallbox is not going to be easy. The “best” way I can think of right now is calculating it. Should be simple if I can read the “current amps” that was set by the script and multiply it by 240 for a 1 Phase system or 3x240 for a 3 Phase. At least that’s the basic idea. How I would turn that into a script or Template is still way beyond me though.

@test123

I think I almost have it now, but I still need a little help unfortunately…

What I have now:

  1. A Fritz Dect plug that measures currently draw by the car. Currently my Model 3 is connected to a standard 240V/16 Amp socket. Updates about every 30-60 Seconds. In Watt
  2. currently solar production from my Inverter. Updates roughly every 60 Seconds. In Watt.
  3. Current overall powerconsumption/production from the Poweropti that reads my digital power meter. This goes to “-” if there is excess and otherwise shows positive values in Watt. Updates every 5 Seconds.

I plugged all the values into your Automations and Scripts, trying to replace your sensors correctly, put the Template Sensors in the Configuration.yaml and everything…and tried to adjust the math to 1 Phase instead of 3. Unfortunately something must still be wrong because if it runs, then it quickly climbs all the way up to 13 Amps and then of course stops because it runs out of excess.

So…either I made a mistake while replacing one of your sensors with mine, or the conversion from 3 to 1 Phase is faulty.
How would you write the formula for 1 Phase 240V?
I replaced /1000 *1.4 with /333 *1.4 but maybe that is wrong?
My reasoning…if I take the actual reading at 2 Amps, then it charges at about 480W. The reverse would mean that excess solar @480W , divided by 333 and multiplied by 1.4 gives me almost exactly 2 Amps as a result…or am I completely off track here?

Try the above

I am trying to go from Watt to Amps, not from KW…
I may have found an issue…in the logs there was a message that complained about there being no default value for the “float” in the excess charging set amps script…I corrected it by giving it float (0). Why it didn’t have any value though and whether that was actually the problem…I kinda doubt.Trying to figure out the issue from the logs hasn’t been a success so far…sun is gone for today, so no more tests, maybe tomorrow, but we are supposed to get rainy weather now, so…maybe not.

I just discovered the “Template Tester” in the dev options and ran the Templates you provided in there. It through the same “default value” warning as the script.
I added a default of (0) to every float and now it claims to be fine…but the real test will be what happens once the automation triggers.

This is my current config:

 - platform: template
    scan_interval: 10
    sensors:
      template_totalexcesssolarforcharging:
        friendly_name: "Total Excess Solar for Charging"
        value_template: >- ### This is to not to go negative ###
          {% if (states('sensor.poweropti_aktuell') | float *-1 + states('sensor.template_ev_consumption_home') | float (0) ) >= 0 %}
            {% if (states('sensor.poweropti_aktuell') | float *-1 + states('sensor.template_ev_consumption_home') | int ) <=  ( states('sensor.frj3bk309s_output_power') | int ) %}
            {{ (states('sensor.poweropti_aktuell') | float *-1 + states('sensor.template_ev_consumption_home') | float (0) ) | round(2) }}
            {% else %}
            {{ states('sensor.frj3bk309s_output_power') | int }}
            {% endif %}
          {% else %}
            {{ 0 | int }}
          {% endif %}
        unit_of_measurement: "W"
        device_class: "power"
        
  - platform: template
    scan_interval: 5
    sensors:
      template_ev_consumption_home:
        friendly_name: "EV Consumption Home"
        value_template: >-
          {{(states('sensor.fritz_dect_210_1_power_consumption') | float (0) ) }}
        unit_of_measurement: W
        device_class: power 

This is from watts to Amps. Just try the calcs

I am trying the calcs, but sadly they don’t seem to be working for me. The templates work now (with the default 0, but still sometimes complaining that there is no default…:man_shrugging:) but the end result of the first template is always 0 for me. The second one is showing correctly though, so that’s something at least.

I will leave it at a static 2 Amps now and see if I can figure it out, and if not then I will try again next year when my 7.7KWp array has been installed.

Thanks for your help and for figuring it out so noobs like me have a starting point!