Optimizing use of unused Solar Power to charge a Tesla

In this code snippet, what is sensor.psupply_2 ? Is that the output of your inverter solar in watts?

Yes indeed

I believe your output is in kilowatts, because if I don’t add a /1000 I get ridiculous values (thousands of amps :wink: ). Anyway I’m still not having much luck with this.

Here’s my code snippet from configuration.yaml to setup an entity that tracks unused amps:

  fronius_excess_solar_amps:
    value_template: "{{ ((states('sensor.power_ac_fronius_inverter_1_10_13_32_12'))| float /1000*1.4)| int }}"
    unit_of_measurement: A

Unfortunately, it doesn’t seem to be working properly. I’m in a 230V country.

According to the screenshot below, I’m generating 2232w, consumption 633w. So that’s 1599w left over. 1599 watts divided by 230 volts = 6.9 amps, but my result is 3 A.

What am I doing wrong?

  1. you are not calculating your ‘amps excess’ with your calculation, as you are only using the output from your inverter, ie you are just calculating how much amps you can put on your car from the total solar power you are generating. if you want to have excess solar you should deduct your power usage but it is definitely not that simple (i have noticed) as you can easily go into circular iterations if the readings are not updated frequently . I would propose you first get it up and running based on total solar power first, ignore the power usage (or as some others have done deduct a fixed value, e.g. 500w)
  2. my calcs are for a three phase system. I believe you have a one phase system. So that would then be (for total solar, not excess solar):
    Inverter power / 230
    so for your 2232, this would be: 2232W / 230V = 9.7A

chose 230v / 235v / 240v, whatever fits best

Been following this and i have simmilar wish or even very like some of you already done. Here is what i have :

  • I have a Victron solar system so i have all data from inverters, power usage in the house and battery level.
  • I have a Tesla Power Charger Gen 3 that i have integrated in HASS
  • I have Tesla Model Y

What i’d like to do is following :

    • Coming home plug in the car
    • If there is surplus Solar power (i am exporting to grid, meaning all househols usage is already covered and the battery is full), if no surplus then the car should not charge.
    • Calculate current that i can use to charge car (use rounding for this so sometime i will end-up returning a bit to grid or sometimes i will eat a bit from battery)
    • Keep an eye on the usage from grid and the battery and if the levels get to high lower amperage until level is so low that we stop charging because sun is gone.

I’d would also want to override this automation by just going manual so i control this when automation is not sufficient but that’s just a toggle.

Anyone have something like above for a Victron system that you could share ?

All the best
/donnib

(post deleted by author)

1 Like

Great. I did almost exactly the same thing.
Relevant example:

Mine throws in an error sometimes. Pretty sure this is because my Tesla polling is still 11 minutes. Bit I’m fine with it.

I have added a code above to increase the polling frequency if you want to use it.

I personally did not like it and by including the Shelly 3em it works more like I want it to (very responsive). For example, if someone in the house starts to use a hair dryer it will adjust the charging immediately

It is now just a matter of fine-tuning but all in all I am happy with it

Exactly. The error I get is (probably) when I unplug my car and it tries to run the script afterwards (so HA thinks it’s still plugged in). Could happen even when the polling is 1 minute. And the error doesn’t mess stuff up, so I’ll leave it at that, for now.

I finally have this working properly (I think), but more testing is needed of course. Doesn’t appear to have any issues with circular references.

I first created an entity to work out my total house load, another entity that tells the incoming power from the panels. Then work out the “leftover” in another entity. If the value is positive, it means I have excess solar. If negative, it means I’m pulling grid power. All values in amps. From there the logic is very simple. If there are excess amps, increase charger current by 1A. If not, decrease charger amps by 1A.

It still needs some polishing to set minimums / maximums (so it doesn’t try to reduce charge amps below say 6, and doesn’t try to increase them above 16 which is the max possible in mine - still using the UMC plugged into a 16A 230V outlet).

Just have to run this script every x minutes when the car is charging, and it will adjust the current up/down.

Dependencies are TeslaMate (integration via MQTT) and the Tesla integration (via HACS)


alias: Tesla_Script_SetCarChargeAmps-DynamicBasedOnLoad
sequence:
  - if:
      - alias: Check if there is excess power available
        condition: numeric_state
        entity_id: sensor.fronius_leftover_amps
        above: 0
    then:
      - alias: If there is, increase current
        service: tesla_custom.api
        data:
          email: <enter your email>
          command: CHARGING_AMPS
          parameters:
            path_vars:
              vehicle_id: '{{ state_attr(''binary_sensor.carname_online_sensor'', ''id'') }}'
            charging_amps: >-
              {{ (states('sensor.tesla_charger_actual_current') | int) + ( 1 |
              int) }}
    else:
      - alias: If there isnt, decrease current
        service: tesla_custom.api
        data:
          email: <enter your email>
          command: CHARGING_AMPS
          parameters:
            path_vars:
              vehicle_id: '{{ state_attr(''binary_sensor.carname_online_sensor'', ''id'') }}'
            charging_amps: >-
              {{ (states('sensor.tesla_charger_actual_current') | int) - ( 1 |
              int) }}
mode: single
icon: mdi:ev-plug-type2

If someone could provide some guidance on how if the value template for charging_amps could return a max or min value that’d be great!

IE this piece

charging_amps: >-
              {{ (states('sensor.tesla_charger_actual_current') | int) + ( 1 |
              int) }}

Should return 16 if the charging_amps entity is currently at 16, and 6 if the entity is at 6 already.

1 Like

Hi,
I am trying to use the automations in this thread to charge my Tesla based on the data of my Tesla Powerwall. I have the modules for the Tesla and the Powerwall running, I am struggling how to add a yaml automation to home assistant. I tried editing the automations.yaml directly and to add the yaml in the GUI either as a whole or in chunks within the sections of the automation, so far I am ending up with error messages. How do I add the automations to Homeassistant?

Many thanks for your help.

Many regards,
Thomas.

Hi @twiederh , if you are not yet familiar with the file structure, I suggest you start with the GUI (settings… automation & scenes… ) and then go to automation. Start a new empty automation from the GUI. At the very top right, in the three dot menu, you’ll be able to copy-paste your yaml. But alternatively, feel free to try with the GUI.
Good luck!

1 Like

Hi, All!
I’ve been following this thread with interest, and I’ve been crafting my own script (based upon one that was already posted in this thread earlier) with the hopes of it being useful to others here. However, I wanted to make it useful for other EV’s, as well (not just Teslas).

So, here’s where I’m kinda stuck. The Tesla integration lets you set the charging rate as amperes, but you fetch the current charging rate as miles-of-range-added-per-hour. Because of this, I’ve got some functions which do some converting from “mph” to amps, amps to kilowatts, etc. If most other EV’s (with HA integrations) report charging rate as mph, then I can keep the code this way. If not, then I would either need to have the script be really flexible in how it can convert charge rate, or maybe convince the maintainers of the Tesla integration to add a derived entity which returns amperes based upon some stored Wh/mi rating of the vehicle.

Does anybody know what units are used for the charge rate sensor for other EV’s?

Another feature I’m trying to add is the ability to split surplus solar between the vehicle and the storage battery (if present) based upon the state of charge of each, but I’ve just been doing that by setting the charge rate of the vehicle to some fraction of the surplus (and just assuming that any residual surplus would go to the house battery). Is that how batteries (in general and Powerwalls, in particular) work, or would I also need to manually set the charge rate on the batteries?

Hey @Test123,
I’m looking at your examples, but it looks that automation.triggersolarchargingeveryminute is missing.
Could you pleas post it?
Thanks
R

It’s irrelevant. You don’t need it and can delete any reference to it

I just have made a set of automations which work on total solar (which I posted earlier) and a set which works on excess solar, as posted most recently

I’m trying to create a very simple automation to change the amps based on the amount of input solar power but the automation is throwing an error.

In the HA logs I found when running the automation:

  • Set Tesla charging to 08A between 2300-2800W: Error executing script. Unexpected error for call_service at pos 1:
  • While executing automation automation.set_tesla_charging_to_10a_between_2300_2800w
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 447, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 680, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1713, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1750, in _execute_service
    await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
  File "/config/custom_components/tesla_custom/services.py", line 56, in async_call_tesla_service
    await api(service_call)
  File "/config/custom_components/tesla_custom/services.py", line 112, in api
    return await controller.api(name=command, path_vars=path_vars, **parameters)
  File "/usr/local/lib/python3.10/site-packages/teslajsonpy/controller.py", line 183, in wake_up
    result = await wrapped(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/teslajsonpy/controller.py", line 1796, in api
    return await self.__connection.post(
  File "/usr/local/lib/python3.10/site-packages/teslajsonpy/connection.py", line 164, in post
    return await self.__open(url, method=method, headers=self.head, data=data)
  File "/usr/local/lib/python3.10/site-packages/teslajsonpy/connection.py", line 213, in __open
    raise TeslaException(resp.status_code)
teslajsonpy.exceptions.TeslaException

Automation:

id: '1659171445553'
alias: Set Tesla charging to 08A between 2300-2800W
description: ''
trigger:
  - platform: time_pattern
    minutes: /5
condition:
  - type: is_power
    condition: device
    device_id: cfd5226f8c2a575c166bda9e8af5b599
    entity_id: sensor.active_power
    domain: sensor
    above: '2300'
    below: '2800'
  - condition: device
    device_id: 02d33bf2e3cc6e02b68a9954163074b5
    domain: device_tracker
    entity_id: device_tracker.tessy_location_tracker
    type: is_home
  - condition: device
    device_id: 02d33bf2e3cc6e02b68a9954163074b5
    entity_id: binary_sensor.tessy_charger_sensor
    domain: binary_sensor
    type: is_on
action:
  - service: tesla_custom.api
    data:
      email: xxx@xxx
      command: CHARGING_AMPS
      parameters:
        path_vars:
          vehicle_id: '149xxxxxxx507'
        charging_amps: '8'
mode: single

Not sure what’s going wrong here?

Will, could you kindly explain why you have used the template for excess solar? Instead of just using a negative P1 reading?

Found the issue, there was a bug fixed in v2.3.1 and I wasn’t on this latest version.

I’ve had exactly the same issue (to disable charging when no solar any more), so glad to hear it’s solved in the 2.3.1. Will check it out tonight!
Thanks

I assume you mean the template_totalexcesssolarforcharging

Assume the following scenario:

P1 at - 2000 → automation triggers start charging at 2000w
P1 is then 0 → so far so good
More sun emerges
P1 is back at - 1000 because more sun → what now? I would want car to charge at the value it is already charging at + the additional excess solar. This becomes EV consumption + excess solar from p1 meter. I have put this in a template to avoid circular references