Optimizing use of unused Solar Power to charge a Tesla

@Test123

I think I finally managed to get this working.

My main changes were to the Template for solarexcesscharging, the calculation for amps (adapted for a 230V 1Phase system). I also eliminated the second Template in the Config.yaml because it is not needed since I get my current EV Charging Rate directly from a measuring plug.

- 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(0) *-1 + states('sensor.fritz_dect_210_1_power_consumption') | float (0) ) >= 0 %}
            {% if (states('sensor.poweropti_aktuell') | float(0) *-1 + states('sensor.fritz_dect_210_1_power_consumption') | int ) <=  ( states('sensor.shellyplug_s_397ebb_power') | int ) %}
            {{ (states('sensor.poweropti_aktuell') | float(0) *-1 + states('sensor.fritz_dect_210_1_power_consumption') | float (0) ) | round(2) }}
            {% else %}
            {{ states('sensor.shellyplug_s_397ebb_power') | int }}
            {% endif %}
          {% else %}
            {{ 0 | int }}
          {% endif %}
        unit_of_measurement: "W"
        device_class: "power"
alias: TeslaChargingSetAmpsLinkToExcessSolar
sequence:
  - service: tesla_custom.api
    data:
      email: XXX
      command: CHARGING_AMPS
      parameters:
        path_vars:
          vehicle_id: "{{ state_attr('binary_sensor.tai_daishar_online_sensor', 'id') }}"
        charging_amps: >-
          {{ ((states('sensor.template_totalexcesssolarforcharging'))| float (0)
          /333*1.4) | float(0) | round(1) }}
mode: single

With this I get reliable results, at least in Theory. Today it is overcast, so I can’t really do much real world testing, but I am confident that I finally got it right.
MY Home Assistant kept complaining about errors in the Templates. I worked on that for a long time with trial and error until I got it working. the “scan_interval” had to go since it kept trowing errors and I needed to se “default” for all the “float” variables. Now it isn’t throwing any errors and if I input it in the Dev Tools Template Tester it gives me valid results.
The second change (apart from converting to 1 Phase 230V system) is that I use fractions of an amp. I managed to get that working by manually setting something like “1.5 Amps” in the Api and hope it will also work in the Script.
I will post my results if I manage to get real world results to back up the theoretical success… :wink:

Edit: turns out the fractions (1.x amps) don’t work in the automation.

Very good

You could have raken either /1000 * 4.2 or /333*1.4, same thing

Good luck !

1 Like

Thing is…with the newer versions of HA, I don’t think your Config.yaml Template sensors are actually working correctly…
The Sensor1+sensor2 >0 and so on only seems to be reading Sensor 1 and then pasing that value on. Can you recheck that? It seems HA now requires it to be something like
Sensor1
Sensor2
map | sum | round

I found this when I made a Template for the Shelly 3EM I had installed yesterday…

Edit:
I think I managed to correct the Syntax…but please double-check what I did as I am working with trial and error here…

- sensors:
      
      
      template_totalexcesssolarforcharging:
        friendly_name: "Total Excess Solar for Charging"
        value_template: >- ### This is to not to go negative ###
          {% if [ states('sensor.verbrauch_total') | float(0)*-1, 
               states('sensor.fritz_dect_210_1_power_consumption')]
               | map('float') | sum |round(0)  >= 0 %}
            {% if [ states('sensor.verbrauch_total')| float(0)*-1, 
               states('sensor.fritz_dect_210_1_power_consumption')]
               | map('float') | sum |round(0)  <=  ( states('sensor.shellyplug_s_397ebb_power') | int ) %}
            {{ [ states('sensor.verbrauch_total')| float(0)*-1, 
               states('sensor.fritz_dect_210_1_power_consumption')]
               | map('float') | sum |round(0)  | float (0)  | round(2) }}
            {% else %}
            {{ states('sensor.shellyplug_s_397ebb_power') | int }}
            {% endif %}
          {% else %}
            {{ 0 | int }}
          {% endif %}
        unit_of_measurement: "W"
        device_class: "power"

Edit:
Corrected some errors…hopefully… :smiley:

There is an additional issue now…it seems the HA Tesla Custom integration no longer wakes my Model 3 up when it sens the “charger switch” command…:roll_eyes:

I’m experiencing the same, since I use the custom integration (± 1Y). The weird thing is, not consistently. It fails to switch the charger switch in 50% of the cases (and has nothing to do with the car being “awake”).

1 Like

I am still wondering whether an easier automation should not be possible…

My idea:

  1. Wait for a certain amount of excess energy (say 600W).
  2. Set the Charging amps to 1 and create a variable “X” that now has the value 1
  3. keep checking for excess energy. As soon as there are 600W available again run “X+1” for the Charging amps. You now have Charging at 2 amps which is now your new X
  4. keep checking. If more sun appears, then keep adding “1” to the X you have. If you run into excess solar values >0 then do “X-1” until you have your excess back to <=0

I am probably wrong, but from what my tired brain tells me this should enable excess solar charing with just one Sensor, sometimg like the Powerfox Poweropti or the Shelly 3EM.

Problem is…I have no idea whether this is even possible in HA (storing current amps in a variable and working with that) and I certainly have no idea how to code it in yaml…

I’m having a similar issue. I unhooked the charger from my Tesla, yet the charger switch sensor is still in the on position, which is preventing some of my automations (e.g switch back to slow polling once charge has finished) from triggering.

2 Likes

The HACS Tesla Integration has been completely rewritten. Not sure what kind pf impact this might have on our project, but I thought I’d point it out

Edit:

The changes are helpful since they now expose the charging amps to Home Assistant directly.
Sadly I am still too stupid to do anything useful with it.
It feels like this would be soooo easy but I just don’t know how to make it work with Home Assistant automations.

Essentially…1 Amp of excess power is about 700W. So…if 700W is available as excess (my sensor says -700) then set charging amps to 1. Repeat that so that it sticks and done.
Now wait a minute, then check if there is still 700W left. If so, then do “current amps +1…” and that is what I can’t figure out. I can of course create 16 Up and 16 Down automations to cover the entire range, but there has to be a better way…right?

I am currently experimenting with the new Custom Integration and came up with this script to replace the original excess solar amps script by @Test123

Please, since I currently can’t try this out, maybe someone else can see if it works?
The Sensor “Poweropti_Einspeisung_Aktuell” simpy shows my total negative energy, turned into a positive value in a template sensor. I am sure that this needs more work, but theoretically it should work and would not require any additional sensors?

Thanks!

service: number.set_value
data:
  value: >-
    {{ (states('sensor.poweropti_einspeisung_aktuell') | float(0) * 0.0014 -
    states('number.tai_daishar_charging_amps') | float(0)) | round (0)}}
target:
  entity_id: number.tai_daishar_charging_amps

I made another iteration of the script, with some refinement and the addition of a sensor that meadures the consumption of my Wallbox instead of relying on the Tesla API data.

service: number.set_value
data:
  value: >-
    {{ [0, (states('sensor.poweropti_einspeisung_aktuell') | float(0) * 0.0014 +
    states('sensor.wallbox_power') | float(0) *0.0014 ) | round(0)] | max(16) }}
target:
  entity_id: number.tai_daishar_charging_amps

Edit:
And it’s me again. My rooftop solar has been installed yesterday, so I can now really try out these automations.

This is my current attempt and it seems to work reasonably well, but does still occasionally lag behind. This version is for 1 Phase Charging, but the changes to make it work for 3 Phases are minimal. Seems to work perfectly now, unless there are still quirks I haven’t found yet.

service: number.set_value
data:
  value: >-
    {{ ([0, states('sensor.poweropti_aktuell') | float(0) *0.0033 *-1 +
    states('number.tai_daishar_charging_amps') |float(0) , 13] | sort)[1] | round(0) }}
target:
  entity_id: number.tai_daishar_charging_amps

2 Likes

Thanks for your efforts!
Just got my P1 meter installed so I’ll play with your scripts once there is enough sun available here.

1 Like

The latest one is working like a charm here. It occasionally takes a bit too long to adapt if there is a load with strong fluctuations, like our induction stove, but other than that it appears to get the job done very well.
Make sure to update the other parts of the automations test123 posted above to work with the newest version of the Tesla Custom Integration…there have been quite a few small changes to the entities.

Despite the fact that it’s November and I have a relatively small rooftop solar installation (7.7kWp in two strings) I already put quite a few kWh into the battery with the automatic charging…started yesterday around lunchtime at about 42% and am now at 70% on my SR+ Model 3. Pretty decent all things considered…

@cpuram what id should I be using as the vehicle_id. I’m stumped.

If you have the Tesla integrated with the Custom Integration from HACS then your car should be listed in Home Assistant by it’s name

Hey guys,

Before I start, I want to say that I am a total noob in this. This is my first interaction with custom automation and home assistant. I am from Poland, where recently I have installed at my house a 9 KwH PV with an SolarEdge inverter. Currently, I own a Tesla MYP with a mobile charger and I wish to charge the car with solar energy. I have read this whole thread but to be honest I do not understand very much. I would like to make such automation but I need some help with understanding. Could I contact someone privately and try to explain this for me? Maybe @CommanderROR? Or can someone make a full guide of this? I think such guide would bevery helpful for the whole society!

1 Like

I am not sure I am best qualified to help you since I essentially learned how to do this by following this thread and doing a lot of Trial and Error…but I am certainly willing to try.

Before you get started on this though…just a few questions:

  1. Do you have a sensor (like something from you SolarEdge inverter) that allows you to get a reading on your current energy consumption in Home Assistant? Like real time Power data in Watt?

  2. Do you already have the Tesla Custom Integration in Home Assistant?

  1. No, it has a current energy production, but what I read from this thread it has a refreshment of 15 minutes
  2. Unfortunately not

Ok… that makes things a bit harder …

Concerning the Power readings…do you have an energy meter with a digital display? And does it have those little IR or optical diodes on the front?

Concerning the Tesla Integration…you can find detailed instructions on how to install HACS. Once you have that you are ready to proceed.

Quick Update:
The last script I posted above is working flawlessly. Quickly changing power, like a windy day with a lot of clouds might lead to a few tiny over or under compensations, but that is simply down to the fact that the Tesla can’t adjust the charging amps within seconds.
It requires only one Sensor reading you entire house input/output and the Tesla Custom integration.

1 Like

Just wondering, isn’t it more logical/readable to replace *0.0033 *-1 with your (negative) mains voltage reading for example /-240? In my case it’s around 235 but sadly the P1 meter doesn’t provide this value to HA.