Optimizing use of unused Solar Power to charge a Tesla

Your condition is on “Away”

condition:
  - condition: state
    entity_id: device_tracker.tesla_model_3_location_tracker
    state: Away

That means that the automation only kicks off when the car is not at home.
Could that be the issue? (You can also check the trace of the automation when clicking on the clock)

@Chacsam

I thought I explained that in my post but apparently I did not save that part.

The location always shows ‘away’ so I probably have to adjust some settings in the car itself

So I don’t think that is the issue at this point as it is constantly on ‘away’ in case.

Screenshot_20220205-182650_Home Assistant

I have a feeling I have just forgotten something very stupid here so that it actively checks every X seconds or minutes or so. Like a loop Function if that makes sense

OK, Slice the elephant.
As the condition is not relevant here, remove it from your automation.
The technical state for “Away” is “not_home”, so your condition should match “not_home” anyway.

A test you can do is manually activate the switch from the services tab in the development tools. Does that work?

Ok @Chacsam I sliced the elephant and took out the location condition

Yes?, I was able to run both automations in developer tools - services

How about the “Switch: Turn on” service, does it do what it should?
If yes, review your automation;
If no, then you have an issue with the Tesla integration, which might be more complex to solve.
Good luck

it seems like the problem was that the values of the P1 meter need to ‘cross’ the trigger point ie, it only triggers when the p1 meter goes from a level below my trigger point to above my trigger point (for stopping to charge) or from above to below (for starting to charge). I did not realize that this is how the trigger worked , and would have assumed it works more as a ‘if this then that’. So now it does seem to work

read it here

thanks you so much, now the next step is to figure out how to get the amperes settings to work

@Maart84 would you mind sharing your code and calculations?
3 phase 230v?

@markpurcell I thought i got it but for some reason the code that you use does not work for me. For now i have been trying just to get a fixed value inputted. I don’t get any error message, the values don’t seem to stick / be transferred to the car, but the car does react

So what i do is: i simply run the script in the scripts section, while the car is already charging
When I look in the tesla app one of two things happens:

  1. the value of the charging drops to 5A, and then within 30 seconds goes back to the value it initially had, e.g. 16A
  2. the value drops to 0A (charging 2km/hr) and moves to 5a 30 sec or a minute later or it stays at 0

So the value never goes to value i have asked for, in this case 9A. Would you or anyone else have any idea why this does not work?

service: tesla_custom.api
data:
  email: [redacted]
  command: CHARGING_AMPS
  parameters:
    path_vars:
      charging_amps: '9'
      vehicle_id: '{{ state_attr(''binary_sensor.tesla_model_3_online_sensor'', ''id'') }}'


If the command is getting across that means the service call is working correctly.

There must be s something else (another app) which is changing the setting

Do you use charge hq, teslafi or teslamate?

hi @markpurcell , no I don’t actually, have never used any of them. I only have the official tesla app itself, now HA, the tesla tokens app for the token. are there settings in the car that could impact this?

ok, edit: I have it now. the syntax on this thing is driving me insane :slight_smile:

main issue was that the ‘charging amps’ had to be on the same line as the ‘path vars’

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: '9'

thank you for your help

Maybe one more question: would anyone know what the value in the command section and in the parameter section should be in case of trying to set the charge limit? i would have assumed (command) ‘charge_limit’ in stead of ‘CHARGING_AMPS’ and in parameter section ‘percent’ in stead of ‘charging_amps’ but apparently i am not understanding the documentation well ( Charging - Tesla JSON API (Unofficial) (timdorr.com))

Wow. Thanks so much for this.
Looked for two hours yesterday why this didn’t work with me. The indentation was the answer.

Sidenote: what amazes me is that this is (more or less) the only place on Google or the internet where this topic is discussed. Which I find quite weird. And I’m pretty sure many people will try to do the same thing (in Belgium and the Netherlands, to begin with).

Sometime people just don’t know that this is possible, and could actually be done. :slight_smile:

Here’s to hope that more car manufacturers and more chargers/meters/inverter provider (and maybe even power companies who would publish real-time rate schedule) would actually open their APIs, to make things like this possible.

Yes, would be nice of Tesla had a defined open API as well, as this is all reverse engineered and not guaranteed to be supported going forward. :frowning:

Hi,
does somebody arrived to put this service call in Node Red?

Ok, so here is my version to optimize unused solar power to charge a Tesla:
prerequisite:

  • a tesla named tesla (change the car’s name in the script)
  • a solaredge inverter with a power meter
  • a tri-phase installation (single phase installation need to adapt the math in the formula in the script)

I have first created a script that will set the Amps deducting the power currently provided to the grid (converted to Amps) from the current charge rate the Tesla provides (in my case in km/h transformed to Amps)

alias: TeslaChargingSetAmps
sequence:
  - service: tesla_custom.api
    data:
      command: CHARGING_AMPS
      parameters:
        path_vars:
          vehicle_id: '{{ state_attr(''binary_sensor.tesla_online_sensor'', ''id'') }}'
        charging_amps: >-
          {{ (( states('sensor.tesla_charging_rate_sensor') | float /4.3) -
          (states('sensor.solaredge_grid_power')| float *1.4)) | round() }}
mode: queued
icon: mdi:car
max: 10

Once done, I have created an automation that checks if the car is home, charging, and every 15 minutes invokes the script that adjust the amps

alias: adjust charging car amps if charging switch is on
description: >-
  runs every 15 minutes, as SolarEdge API is invoked and is only refreshed every
  15 minutes. 

trigger:
  - minutes: /15
    platform: time_pattern
condition:
  - condition: device
    type: is_on
    device_id: xxxxxxxxxxx
    entity_id: switch.tesla_charger_switch
    domain: switch
  - condition: device
    device_id: xxxxxxxxxxxxxx
    domain: device_tracker
    entity_id: device_tracker.tesla_location_tracker
    type: is_home
action:
  - service: script.teslachargingsetamps
    data: {}

Now I still want to enable the charge at sunrise and stop at sunset. I add a 2 hours delay as the sun needs time to properly create power on my panels:

alias: Turn on car charging
description: Turn on car charging in the morning
trigger:
  - platform: sun
    event: sunrise
    offset: '02:00'
condition:
  - condition: state
    entity_id: binary_sensor.tesla_charger_sensor
    state: 'on'
action:
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - type: turn_on
    device_id: xxxxxxxxxxxxxxxxxxxx
    entity_id: switch.tesla_charger_switch
    domain: switch
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - service: script.teslachargingsetamps
    data: {}
alias: Turn off car charging
description: Turn off car charging in the evening
trigger:
  - platform: sun
    event: sunset
    offset: '-02:00'
condition:
  - condition: state
    entity_id: switch.tesla_charger_switch
    state: 'on'
action:
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: xxxxxxxxxxxxxxxxxxxxxxxxx
    entity_id: switch.tesla_charger_switch
    domain: switch

This still needs to be tested through properly over a couple of days.
This could be drastically improved if I would start to take the data from the Solaredge Modbus TCP connection, but I’ll keep that for future improvement.

2 Likes

Thanks! I was looking for the 15 min routine.

On top of that I will create an extra boolean “solar powered charging”, because of the fact I drive more than we have sunny hours (here in Belgium).

On my end, I plan to have a physical switch that enables those automations or completely disables them, so that I can either only charge with Solar excess or I can charge the car at full pace and override according to my needs.
A third switch position could be a trade-off of both situation with a target to charge to a certain level or something similar that I still need to figure out, but that’s optional.

Any recommendations for such a hardware, if possible not battery operated, that can fit on an existing plug? I had in mind to put a Shelly device in an unused light switch that already has a built -in led light. This could enable me to activate my automation and have a low cost low energy seamlessly built-in device with visual control…

Unsure of your need for a physical switch.

On my lovelace dashboard I have virtual switches to switch charging on/off, max charging (32A) and solar tracking (automation sets charging current based of excess solar)

If you wanted to physically switch these HA virtual buttons you could setup something like an NFC tag or a ZigBee physical button, which calls these automations.

Alternatively you could use something like a Shelly 1PM/ Kass/ MEROSS smart plug to switch charging on/off, but I would be worried about high current draw as you would be limited to 16-10 Amps, which would only give you around 2-3 kW of charging power. The Tesla HPWC can deliver 12 kW, and you wouldn’t have the excess solar option, unless you implement the earlier suggestions.

What sort of wall charger do you have?

Very nice cards!
I did not yet have time to work on that.
The reason behind a physical switch is that I’m not alone in my household and it’s quite nice to not depend too much on your screens for us. The switch with the led indicator to reassure my better half that the car is now charging at full power is a no-brainer. Now it just happen that I have an unused light switch at my kitchen-frontdoor which will perfectly fit this purpose.


Now let’s not worry, I will not set the load of the charger to à Shelly device. I want my Shelly device to just act as a trigger in HA. Mounted in such a wall-switch, the Shelly device will notice if it’s ON or OFF, which I should be able to connect to a Group switch in HA, at least that’s the plan.

As for my charger, it’s a whole different story: it’s a ABL EMH1 11kW (3-phase 16A) with an RS485 modbus module (DEFA HomeCLU) that reads the load and is able to “Peak-shave” to avoid to overpass the maximum household load. I had bought this 2 years ago (only just finished my PV-installation) to avoid upgrading my 20A installation (I live in a country where they charge you USD140/amps one-off for the upgrade). The code to communicate with it is not documented and I’m not in the mood to reverse engineer modbus.

Will see how that will work with the switch but will definitely as well create some cards at some point!

Update: found it. Missing space and replaced int by float.

I’m optimizing my “dynamic charging”, using a slightly different approach; my calculation is based on my P1 monitor value.
I’ll update an input number (tesla_current (in amps)) every 15 minutes, to run the script afterwards which sends the new values to my car (script is already working (manually)).
I’m messing with the syntax (the “value” is not correct), I’m sure:

Here’s my YAML code:

- id: '1648755281416'
  alias: Tesla - Update Tesla Amps (input number))
  description: ''
  trigger:
  - platform: time_pattern
    minutes: '15'
  condition: []
  action:
  - service: input_number.set_value
    data:
      value:'{{ (states(''input_number.tesla_current'') | int) - ((states(''sensor.p1_meter_3c39e724c5a2_active_power'') | int / 415) | round(0))}}'
    target:
      entity_id: input_number.tesla_current
  mode: single

Corrected code:

- id: '1648755281416'
  alias: Tesla - Update Tesla Amps (input number))
  description: ''
  trigger:
  - platform: time_pattern
    minutes: '15'
  condition: []
  action:
  - service: input_number.set_value
    data:
      value: '{{ (states(''input_number.tesla_current'') | float) - ((states(''sensor.p1_meter_3c39e724c5a2_active_power'') | float / 415) | round(0))}}'
    target:
      entity_id: input_number.tesla_current
  mode: single