Optimizing use of unused Solar Power to charge a Tesla

Thanks a lot for helping a newbie out @matfroh

Well, I use the “tesla_current” input_number to be able to

  • be able to adjust the amps manually when my other helper “Solar Charging” is disabled.
  • simply view what’s the current amps

PS: I stopped using the P1 meter as the value, I’ll use the solaredge current power as the value to play with. I simply didn’t get there with the code, and I want to avoid “advanced coding” (python e.g.). Still, ideally the calculation is based on the P1 meter value, when f.e. activating the laundry machine; based on the solar edge meter it won’t dynamically adjust the tesla charging.

If you read my code above, you will notice that I have a solaredge modbus energy meter, this enables me to effectively as well use only the solar excess power.
So if any other device is activated, the value changes accordingly.

Quick update here on my optimisation. In the meantime, I am now gathering the energy to the grid from the Solaredge TCP Modbus integration, in order to have live data without the slow 15 minutes interval over the solaredge API.
My worry is that now the delay comes from the Tesla API, which updates the charge rate only every 10 minutes.
2 solutions for me:

  • trigger a more frequent update of the charge rate from the API when the car is charging at home (unsure of the side effects of this)
  • get the exact rate from the charger (either by reverse engineering it’s modbus or through another cheap meter)

Nevertheless, I now run the script every 10 minutes (I could also systematically round down to avoid any step over the excess) and the picture looks nice:

Thanks for sharing @matfroh .

I’ll do mine - I’m sure it’s useful for the not-so-advanced HA-user. Although, like you, I’m not fully there yet.

I’m using the (cloud based and thus 15 min delayed) Solar Edge values to calculate my Tesla charging amps (can’t do the modbus thing, according to SolarEdge documentation).
Start charging based on P1 value, stop charging based on (almost) no more sun. Still a work in progress, but it’s working quite well already (for my requirements and skills).
All this is based on what’s been said in this thread, so many thanks for that, everyone!

Next up: using the P1 value for calculation, but I’m having a bit of trouble combining the code with the math :smiley: (plus time to look for it a bit more).
To be clear; I keep KISS high & mighty (I don’t want to copy paste large chunks of python code I don’t understand fully). These are all automations and 1 simple script (mentioned above).

Start Charging based on P1 excess

alias: Tesla - Start laden bij zon over en onder 90
description: ''
trigger:
  - platform: numeric_state
    below: '-300'
    for:
      hours: 0
      minutes: 3
      seconds: 0
    entity_id: sensor.p1_meter_3c39e724c5a2_active_power
condition:
  - condition: state
    entity_id: device_tracker.dikke_baby_location_tracker
    state: home
  - condition: state
    entity_id: binary_sensor.dikke_baby_charger_sensor
    state: 'on'
  - condition: state
    entity_id: switch.dikke_baby_charger_switch
    state: 'off'
  - condition: numeric_state
    entity_id: sensor.dikke_baby_battery_sensor
    below: '90'
action:
  - service: notify.notify
    data:
      message: Tesla begint te laden (veel zon over)
      title: Tesla automatisch laden!
  - service: switch.turn_on
    target:
      entity_id: switch.dikke_baby_charger_switch
    data: {}
  - service: script.tesla_laadsnelheid_test
    data: {}
mode: single

Calculate input_number based on SolardEdge generated power, every 15 minutes

alias: Tesla - Update Tesla Charge Amperage (input number)
description: ''
trigger:
  - platform: time_pattern
    minutes: /15
condition:
  - type: is_on
    condition: device
    device_id: d8b42c4c8cf5d20b8f9571f7dd28cbe1
    entity_id: binary_sensor.dikke_baby_charger_sensor
    domain: binary_sensor
  - condition: state
    state: home
    entity_id: device_tracker.dikke_baby_location_tracker
  - condition: state
    entity_id: switch.dikke_baby_charger_switch
    state: 'on'
  - condition: state
    entity_id: input_boolean.tesladynamiccharging
    state: 'on'
action:
  - service: input_number.set_value
    data:
      value: >
        {{ (states('sensor.solaredge_current_power') | float / 600) | round(0)
        }}
    target:
      entity_id: input_number.tesla_current
  - service: notify.notify
    data:
      message: Input number update "{{ states('input_number.tesla_current') }}"
      title: Tesla
  - service: script.tesla_laadsnelheid_test
    data: {}
mode: single

Stop Charging based on “no more sun”

alias: Tesla - Stop laden bij geen zon meer
description: ''
trigger:
  - platform: numeric_state
    entity_id: sensor.solaredge_current_power
    below: '500'
    for:
      hours: 0
      minutes: 5
      seconds: 0
condition:
  - condition: state
    entity_id: binary_sensor.dikke_baby_charger_sensor
    state: 'on'
  - condition: state
    entity_id: device_tracker.dikke_baby_location_tracker
    state: home
  - condition: state
    entity_id: switch.dikke_baby_charger_switch
    state: 'on'
  - condition: state
    entity_id: input_boolean.teslaoverruleautostop
    state: 'off'
  - condition: numeric_state
    entity_id: sensor.dikke_baby_battery_sensor
    above: input_number.tesla_min_charge
action:
  - service: notify.notify
    data:
      message: Tesla gestopt met laden (zon weg)!
      title: Tesla
  - service: switch.turn_off
    data: {}
    target:
      device_id: d8b42c4c8cf5d20b8f9571f7dd28cbe1
  - type: turn_off
    device_id: d8b42c4c8cf5d20b8f9571f7dd28cbe1
    entity_id: switch.dikke_baby_charger_switch
    domain: switch
mode: single

Script

alias: Tesla Adust Charging Amps
sequence:
  - service: tesla_custom.api
    data:
      command: CHARGING_AMPS
      parameters:
        path_vars:
          vehicle_id: '{{ state_attr(''binary_sensor.dikke_baby_online_sensor'', ''id'') }}'
        charging_amps: '{{ states(''input_number.tesla_current'')|int }}'
mode: single
icon: mdi:battery-charging-20

What I’m working on to replace the solaredge calculated Amp value, but renders minus values, for now

alias: Tesla - TEST - Update Tesla Min Charge (P1 calc)
description: ''
trigger:
  - platform: time_pattern
    hours: /1
condition:
  - type: is_on
    condition: device
    device_id: d8b42c4c8cf5d20b8f9571f7dd28cbe1
    entity_id: binary_sensor.dikke_baby_charger_sensor
    domain: binary_sensor
  - condition: state
    state: home
    entity_id: device_tracker.dikke_baby_location_tracker
  - condition: state
    entity_id: switch.dikke_baby_charger_switch
    state: 'on'
  - condition: state
    entity_id: input_boolean.tesladynamiccharging
    state: 'on'
action:
  - service: input_number.set_value
    data:
      value: >-
        {{ (states('input_number.tesla_current_test') | float) -
        ((states('sensor.p1_meter_3c39e724c5a2_active_power') | float / 500) |
        round(0))}}
    target:
      entity_id: input_number.tesla_current_test
  - service: notify.notify
    data:
      message: >-
        Tesla - TEST P1-calculated "{{ states('input_number.tesla_current_test')
        }}"
      title: Tesla
mode: single

I’m pretty sure surrounding the code above by an “IF” ( calculation < 0, then GO, ELSE Charging Amps = 0) is a dirty solution, and that’s what I’m going for, for now.

I’m using a few helpers

And I also have a few other automations which I won’t share because of the fact they’re not relevant here (stop charging when plugged in, stop charging when no sun and < minimum charge,…)

I have SolarEdge MODBUS monitoring as well, had to switch from wifi to Ethernet. Which allows me to update every 5 seconds.

I have the Tesla API updating every 30 seconds and update the charging Amps request at twice that rate, IE every minute I set a new charging Amps.

You can make a service call from home assistant to change the Tesla API update rate, whilst charging for example.

1 Like

Modbus is definitely worth a try! All solaredge have it, although sometimes tricky to activate. You need to use the app + the mobile phone browser:

Sun is up, time to finally optimize the excess solar usage. Thanks for sharing the automation ideas, I have taken and customized a bit more.

I have a Huawei Solar inverter + Shelly EM3 energy monitor + 1-phase Tesla mobile charger.

As my Tesla battery is LFP, I can charge it to 100% but I charge it less to save some energy from regenerative braking. Don’t forget to turn off scheduled charging in the app.

Since I work from home, my plan is to charge up to 70% during the off-peak hours (or 60% when the predicted solar forecast is high). If I need a full battery, I’ll turn on the max range switch (or set it to 100% in the app) and these automations will not turn on.

These are the automations that should hopefully handle all of the cases (if not, I’ll update it):

- id: tesla_offpeak_charge
  alias: Tesla charge during offpeak hours
  description: During offpeak hours, turn on charging at max current to reach 70% battery limit
  trigger:
    - platform: homeassistant
      event: start
    - entity_id: binary_sensor.tesla_model_3_charger_sensor
      from: "off"
      platform: state
      to: "on"
    - entity_id: utility_meter.daily_house_energy
      platform: state
      to: "offpeak"
  condition:
    - alias: "Offpeak rate"
      condition: state
      entity_id: utility_meter.daily_house_energy
      state: "offpeak"
    - alias: "Charge Cable plugged in"
      condition: state
      entity_id: binary_sensor.tesla_model_3_charger_sensor
      state: "on"
    - alias: "Tesla at home"
      condition: state
      entity_id: device_tracker.tesla_location
      state: "home"
  action:
    - delay: 00:01:00
    - entity_id: switch.tesla_model_3_charger_switch
      service: switch.turn_on
    - service: tesla_custom.api
      alias: "Set Charge amps to max available at the charger"
      data:
        command: CHARGING_AMPS
        parameters:
          path_vars:
            vehicle_id: '{{ state_attr("binary_sensor.tesla_model_3_online_sensor", "id") }}'
          charging_amps: '{{ state_attr("sensor.tesla_model_3_charging_rate_sensor", "charge_current_request_max") }}'
    - service: tesla_custom.api
      alias: "Set charge limit to 70%"
      data:
        command: CHANGE_CHARGE_LIMIT
        parameters:
          path_vars:
            vehicle_id: '{{ state_attr("binary_sensor.tesla_model_3_online_sensor", "id") }}'
          percent: 70

- id: tesla_stop_peak_charge
  alias: Tesla Offpeak ended
  description: Stop charging when offpeak tariff ends and we don't need max range
  trigger:
    - entity_id: utility_meter.daily_house_energy
      platform: state
      from: "offpeak"
      to: "peak"
  condition:
    - alias: "Offpeak ended"
      condition: state
      entity_id: utility_meter.daily_house_energy
      state: "peak"
    - alias: "Not max range"
      condition: state
      entity_id: switch.tesla_model_3_maxrange_switch
      state: "off"
  action:
    - entity_id: switch.tesla_model_3_charger_switch
      service: switch.turn_off

- id: tesla_stop_peak_charge_early
  alias: Tesla Stop offpeak charge early
  description: If the predicted solar production is high, stop charging early and continue the charge (60% to 95%) later in the day.
  trigger:
    - entity_id: sensor.tesla_model_3_battery_sensor
      platform: numeric_state
      above: 59
      for:
        minutes: 1
  condition:
    - alias: "Offpeak rate"
      condition: state
      entity_id: utility_meter.daily_house_energy
      state: "offpeak"
    - alias: "Not max range"
      condition: state
      entity_id: switch.tesla_model_3_maxrange_switch
      state: "off"
    - alias: "Predicted high solar production today"
      condition: numeric_state
      entity_id: sensor.energy_production_today
      above: 50
  action:
    - entity_id: switch.tesla_model_3_charger_switch
      service: switch.turn_off

- alias: Smart Solar charging
  id: smart_solar_charge_tesla
  description: "To only use excess power from solar, limit the number of Amps Tesla charger use. We'll stop charging if it drops below 5A"
  trigger:
    - platform: time_pattern
      minutes: "/1"
      #  This will check every minute
    - platform: state
      entity_id: binary_sensor.tesla_model_3_charger_sensor
      to: "on"
      # or when we plug it in
  condition:
    - alias: "Is home"
      condition: state
      entity_id: device_tracker.tesla_location
      state: "home"
    - alias: "Is plugged in"
      condition: state
      entity_id: binary_sensor.tesla_model_3_charger_sensor
      state: "on"
    - alias: "Sun is shining"
      condition: numeric_state
      entity_id: sensor.elevation
      above: 5
    - alias: "Not fully charged"
      condition: numeric_state
      entity_id: sensor.tesla_model_3_battery_sensor
      below: 95
  variables:
    amps: '{{ [[((states("sensor.tesla_charger_actual_current")|int(0)) - ((states("sensor.shelly_channel_b_power")|float(0) + 200) / (states("sensor.shelly_channel_b_voltage")|int(0)))|int(0)),0]|max, state_attr("sensor.tesla_model_3_charging_rate_sensor", "charge_current_request_max") | int(0) ]|min }}'
    # Calculate excess amps, we should set the Tesla charger to:
    # Current charger amps - (current power usage (i.e. -2000W) + 200W overhead, converted to amps)
    # It should not be lower than 0 and not higher than charger max current.
  action:
    - choose:
        - alias: "Just in case we reached charging limit, override and increase to 90%"
          conditions: "{{ (states('sensor.tesla_charge_limit_soc') |int(0)) < 95 }}"
          # and (states('sensor.tesla_battery_level') | int(0) ) + 5 >= (states('sensor.tesla_charge_limit_soc') | int(0)) }}"
          sequence:
            - service: tesla_custom.api
              alias: "Set charge limit to 90%"
              data:
                command: CHANGE_CHARGE_LIMIT
                parameters:
                  path_vars:
                    vehicle_id: '{{ state_attr("binary_sensor.tesla_model_3_online_sensor", "id") }}'
                  percent: 90
    - choose:
        - alias: "Enough energy to charge"
          conditions: "{{ amps >= 5 }}"
          sequence:
            - choose:
                - alias: "Turn on charger if needed"
                  conditions:
                    - condition: state
                      entity_id: switch.tesla_model_3_charger_switch
                      state: "off"
                  sequence:
                    - entity_id: switch.tesla_model_3_charger_switch
                      service: switch.turn_on
            - choose:
                - alias: "Update AMPs if changed"
                  conditions:
                    - "{{ amps >= 5 and amps != (states('sensor.tesla_charger_actual_current')|int(0)) }}"
                  sequence:
                    - service: tesla_custom.api
                      alias: "Set charging amps"
                      data:
                        command: CHARGING_AMPS
                        parameters:
                          path_vars:
                            vehicle_id: '{{ state_attr("binary_sensor.tesla_model_3_online_sensor", "id") }}'
                          charging_amps: >-
                            {{ amps }}
      # ELSE (Turn off charging charging - it is ineffecive below 5A)
      default:
        - choose:
            - alias: "Turn off charger if it is ON"
              conditions:
                - condition: state
                  entity_id: switch.tesla_model_3_charger_switch
                  state: "on"
              sequence:
                - entity_id: switch.tesla_model_3_charger_switch
                  service: switch.turn_off


Also, when the car battery is full, a good idea to convert excess power to heated water, I use this automation to set the water boiler to heat up to 60C and restore the previous state when done.

- alias: Water Boiler on when excess solar power is high
  description: ""
  id: heat_water_from_solar
  trigger:
    - platform: numeric_state
      entity_id: sensor.shelly_channel_b_power
      below: -2000
      for:
        minutes: 5
  condition:
    - alias: "Sun up"
      condition: state # 'day' condition: from sunrise until sunset
      entity_id: sun.sun
      state: "above_horizon"
    - alias: "Not Vacation"
      condition: state
      entity_id: input_boolean.vacation_mode
      state: "off"
    - alias: "Low temperature"
      condition: numeric_state
      entity_id: sensor.boiler_temperature
      below: 60
    - "{{ state_attr('climate.water_boiler', 'hvac_action') != 'heating' and state_attr('climate.water_boiler', 'temperature') != 60 }}" # not already heating
    - alias: "Tesla not charging"
      condition: numeric_state
      entity_id: sensor.tesla_charger_power
      below: 1
    - "{{ states('sensor.tesla_battery_level') | int ) >= (states('sensor.tesla_charge_limit_soc') |int) or is_state('binary_sensor.tesla_model_3_charger_sensor', 'off') }}" # tesla is unplugged or reached the soc limit
  action:
    - service: scene.create
      data:
        scene_id: boiler
        snapshot_entities:
          - climate.water_boiler
    - service: climate.set_temperature
      data:
        hvac_mode: heat
        temperature: 60
      target:
        entity_id: climate.water_boiler

- alias: Water Boiler off when excess solar power is low or reach target temperature
  description: ""
  id: stop_heat_water_from_solar
  trigger:
    - platform: numeric_state
      entity_id: sensor.shelly_channel_b_power
      above: 100
    - platform: numeric_state
      entity_id: sensor.boiler_temperature
      above: 60
  condition:
    - alias: "Sun up"
      condition: state # 'day' condition: from sunrise until sunset
      entity_id: sun.sun
      state: "above_horizon"
    - alias: "Not Vacation"
      condition: state
      entity_id: input_boolean.vacation_mode
      state: "off"
    - alias: "High temperature"
      condition: numeric_state
      entity_id: sensor.boiler_temperature
      above: 60
    - "{{ state_attr('climate.water_boiler', 'hvac_action') == 'heating' and state_attr('climate.water_boiler', 'temperature') == 60 }}" # heating
  action:
    - service: scene.turn_on # Revert the state to original state
      target:
        entity_id: scene.boiler
1 Like

I guess you are using something similar to this Dynamic Polling Frequency · alandtse/tesla Wiki · GitHub

1 Like

See below some codes which I currently use and and may be helpful to you and may advance your thinking

I have charging based on solar generating working quite well, excess solar charging I believe I am missing a clean read from the charging station (the calculation route through tesla gives me a bit of a headache) so will install a shelly 3em in the fusebox to read the power Used by the charging station. I am happy to take any tips on the excess solar charging

Increase polling on the tesla

Automation

alias: AUT_TeslaPollingIntervalTo60
description: ''
trigger:
  - platform: state
    entity_id: switch.tesla_model_3_charger_switch
    to: 'on'
condition:
  - condition: state
    entity_id: device_tracker.tesla_model_3_location_tracker
    state: home
action:
  - service: script.teslaupdateintervalto60
    data: {}
mode: single

Script

alias: TeslaPollingIntervalTo60
sequence:
  - service: tesla_custom.polling_interval
    data:
      scan_interval: 60
mode: single

Polling interval to default
Automation

alias: AUT_TeslaPollingIntervalToDefault
description: ''
trigger:
  - platform: state
    entity_id: switch.tesla_model_3_charger_switch
    to: 'off'
condition:
  - condition: state
    entity_id: device_tracker.tesla_model_3_location_tracker
    state: home
action:
  - service: script.teslapollingintervaltodefault
    data: {}
mode: single

Script

alias: TeslaPollingIntervalToDefault
sequence:
  - service: tesla_custom.polling_interval
    data:
      scan_interval: 660
mode: single

charging based on total solar
Automations

alias: TeslaStartCharging@TotalSolar
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.triggersolarchargingeveryminute
mode: single

alias: OnPluginCheckTotalSolar
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.triggersolarchargingeveryminute
mode: single

alias: TriggerTotalSolarChargingEveryMinute
description: ''
trigger:
  - platform: time_pattern
    minutes: /1
condition:
  - condition: time
    after: '04:00:00'
    before: '23:00:00'
  - condition: state
    entity_id: device_tracker.tesla_model_3_location_tracker
    state: home
action:
  - service: script.teslachargingsetampslinktosemsportal
    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: '5000'
  - 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'
condition:
  - condition: state
    entity_id: device_tracker.tesla_model_3_location_tracker
    state: home
  - condition: numeric_state
    entity_id: sensor.tesla_model_3_charging_rate_sensor
    above: '2'
action:
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.tesla_model_3_charger_switch
  - service: automation.turn_off
    data: {}
    target:
      entity_id:
        - automation.triggersolarchargingeveryminute
        - automation.triggerexcesssolarchargingeveryminute
mode: single

Scripts

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

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.