I am trying to set up an automation to charge my Tesla with excess power from my solar panels. Every 30 seconds (in the daytime, and if the car is plugged in) the export or excess power from the house electricity meter (pow_k) is measured. The idea is to keep the export power close to 0, by adjusting the charging amps of the car. If it is above 688w, the Tesla charging amps will be increased by 1. If it is below, they will be decreased by 1.
HA gives the error message Message malformed: expected float for dictionary value @ data[āvalueā]. Any assistance to this novice would be greatly appreciated.
alias: Charging EV (T-Ray) with excess solar power
description: ""
trigger:
- platform: time_pattern
seconds: "30"
condition:
- condition: sun
before: sunset
after: sunrise
- type: is_plugged_in
condition: device
device_id: b4929c45bca6cfb24b68e22f7f0f9977
entity_id: binary_sensor.t_ray_charger
domain: binary_sensor
action:
- if:
- type: is_power
condition: device
device_id: 5ab015508a4f0ce2f17d992b8f68c3f0
entity_id: sensor.pow_k_po
domain: sensor
above: 688
then:
- device_id: b4929c45bca6cfb24b68e22f7f0f9977
domain: number
entity_id: number.t_ray_charging_amps
type: set_value
value: {{ (states.number.t_ray_charging_amps.state | int ) + 1 }}
else:
- device_id: b4929c45bca6cfb24b68e22f7f0f9977
domain: number
entity_id: number.t_ray_charging_amps
type: set_value
value: {{ (states.number.t_ray_charging_amps.state | int ) - 1 }}
mode: single
Hi! Iām looking to create an automation to do the same thing.
A couple of questions:
I think your automation will constantly adjust 1 A up or down every 30 seconds. Currently there is no option for it to leave the charging current as it is.
The Tesla app has a minimum set charging current of 5 A. Does the API allow it to go lower? Edit: it does! I thought you had to do a minimum of 5 A charging. Thatās cool!
Seeing the current to 0 (zero) amps and turning it on seems to draw around 400-500 Watts, but Iām not sure it charges the car. I think it just powers on the onboard charger. So I think the efficiency will be very low at low amps.
My electrical bill is net metering per hour (if import == export within a clock hour, my bill will be zero), so I might want to do things in bursts of 5A for low solar surplus, if that increases charging efficiency.
I will post my automation here when I get it woeking, either with or without burst charging.
True. I just had troubles to make the value statement (+/- amps) working. Now I start building the complete set of triggers, conditions etc. around the +/- amp function.
Also agree on the probably low efficiency at low amps, but want to try it and see how it works.
I will post as I progress and appreciate your thoughts.
Currently Iām charging my car with 6 amps. And āsensor.omcsbln02t_export_to_gridā is below 0.6 so it should change the amps to 5, which it actually seems like its trying, but nothing happens.
There is something in value: called ācurrent_ampsā. Iām not sure what this value/sensor is, and maybe this could be the issue. I just thought that as soon as it gives the right value, it would change number.charging_amps to this value?
I wondered why you made the long list of conditions. Is there any obvious reason that you couldnāt just calculate the number you want to adjust the charging current with?
E.g:
And another thing: Did you choose the 30 seconds interval for some specific reason?
I donāt have a better value, but I choose every 5 minutes and made the export value a 5 minutes moving average.
Just because I thought it was too often to adjust the charging current more often than this. But I have no strong considerations behind my choice.
Thanks for your input. Calculating the charging current adjustment is definitely more rational. I just got down another avenue and blindsighted on that obvious improvement to the code.
The 30 second interval is because I want to adjust close to real time, so that any change in solar production (for instance a cloud passing) and house consumption (for instance a thermostat turning on the electric heating in my garage) is (almost) immediately reflected in the charging amps. Thus minimizing the import of power from the grid.
Here is my updated code:
alias: T-Ray solar charging
description: ""
trigger:
# Time interval for triggering set
- platform: time_pattern
seconds: "30"
condition:
# Continue if T-Ray is plugged in charger (or exit)
- type: is_plugged_in
condition: device
device_id: b4929c45bca6cfb24b68e22f7f0f9977
entity_id: binary_sensor.t_ray_charger
domain: binary_sensor
# Continue if T-Ray SOC is below 1% of charge limit (or exit)
- condition: template
value_template: >-
{{(states('number.t_ray_charge_limit') | int -
states('sensor.t_ray_battery') | int) > 1}}
action:
# Continue if more than 688w EXPORT power is available (else)
- if:
- type: is_power
condition: device
device_id: 5ab015508a4f0ce2f17d992b8f68c3f0
entity_id: sensor.pow_k_po
domain: sensor
above: 688
then:
# Turn ON charger if it is OFF and continue (or just continue)
- if:
- condition: device
type: is_off
device_id: b4929c45bca6cfb24b68e22f7f0f9977
entity_id: switch.t_ray_charger
domain: switch
then:
- type: turn_on
device_id: b4929c45bca6cfb24b68e22f7f0f9977
entity_id: switch.t_ray_charger
domain: switch
# Calculate number of charging amps to add relative to current EXPORT power (Note: If current EXPORT power is below 688w -> no change)
- service: number.set_value
data_template:
entity_id: number.t_ray_charging_amps
value: >-
{% set current_amps = states('number.t_ray_charging_amps') | int %}
{% set add_amps = (states('sensor.pow_k_po') | int / 688) | round(0,'floor') %}
{{ current_amps + add_amps }}
else:
# Check if power is being imported
- if:
- type: is_power
condition: device
device_id: 5ab015508a4f0ce2f17d992b8f68c3f0
entity_id: sensor.pow_k_p
domain: sensor
above: 0
then:
# Turn OFF charger if power is being imported, but current charging power is below 550w and exit
- if:
- type: is_power
condition: device
device_id: 3efa4551c778fefe170f4c0c7b4dad6f
entity_id: sensor.go_echarger_110504_nrg_12
domain: sensor
below: 550
then:
- type: turn_off
device_id: b4929c45bca6cfb24b68e22f7f0f9977
entity_id: switch.t_ray_charger
domain: switch
else:
# Calculate number of charging amps to subtract relative to current IMPORT power (Note: If current IMPORT power is below 688w -> no change)
- service: number.set_value
data_template:
entity_id: number.t_ray_charging_amps
value: >-
{% set current_amps = states('number.t_ray_charging_amps') | int %}
{% set sub_amps = (states('sensor.pow_k_p') | int / 688) | round(0,'floor') %}
{{ current_amps - sub_amps }}
mode: single
Hi there,
Many thanks for all your work. I am a full novice to coding
Got enphase solar, a shelly that shows the net total returned to the grid (positive value) or negative value if I import.
Also go 2 tesla home.
Got an issue: amps rapidly rocket to the max (32A) and then stop, but it seem to not adapt.
Here is the code, if you could help
OP, is 688 the standard power draw for your house? I keep getting errors saying 17 is out of the range, which it is. 16 is the max ampage for my house.
Iām trying to work out where the math is going wrong.
Hello everyone Iām new here, Iām trying to figure things out ! (Sorry for my bad English, Iām french !).
I just bought 6 solar panels for 2.88kw (plug and play), and of course, I would like to charge my tesla with the excess power instead of putting it into the grid. Home Assistant seems to be awesome and I canāt wait to get it (just bought a raspberryPi). You code is awesome, but how you car is connected, I mean with an EvStation (Mine is 7kw/h) ? Through a connected plug (max 3kw/h) ? Thank you !
@Finnray I could flipping kiss you right now, man!
Your last post showing the updated YAML was just the info I needed to finally rewrite my horrible, clunky set of 24 automations down to 1 automation that calculated the max available charging amps that I can shove into the Model S, without importing anything.
YOU ARE MAGNIFICENT!!!
For years I have struggled, knowing there was a nice way to calculate the amps from the available watts and voltage, but my YAML skills are far too crap to put the maths into practice. Your code is bang on and my car is now merrily lapping up my solar, with the amps being adjusted every 30 seconds.
I made a few little tweaks, but here is the code that works with my Tesla (using @alandtseās nice integration) and my SolarEdge inverter (using @WillCodeForCatsās brilliant Modbus multi integration).
alias: Tesla Solar Charging Automation
description: ""
trigger:
- platform: time_pattern
seconds: /30
condition:
- condition: and
conditions:
- condition: sun
before: sunset
- condition: sun
after: sunrise
- condition: state
entity_id: device_tracker.nikita_location_tracker
state: home
- condition: state
entity_id: binary_sensor.nikita_charger
state: "on"
- condition: template
value_template: >-
{{ (states('number.nikita_charge_limit') | int -
states('sensor.nikita_battery') | int) > 0.1 }}
action:
- if:
- condition: numeric_state
entity_id: sensor.solaredge_m1_ac_power
above: 250
then:
- if:
- condition: state
entity_id: switch.nikita_charger
state: "off"
then:
- entity_id: switch.nikita_charger
action: switch.turn_on
- data_template:
entity_id: number.nikita_charging_amps
value: >-
{% set current_amps = states('number.nikita_charging_amps') | int %}
{% set add_amps = (states('sensor.solaredge_m1_ac_power') | int /
240) | round(0,'floor') %} {{ [current_amps + add_amps, 8] | min }}
action: number.set_value
else:
- if:
- condition: numeric_state
entity_id: sensor.solaredge_m1_ac_power
below: 0
then:
- entity_id: switch.nikita_charger
action: switch.turn_off
else:
- data_template:
entity_id: number.nikita_charging_amps
value: >-
{% set current_amps = states('number.nikita_charging_amps') |
int %} {% set sub_amps = (states('sensor.solaredge_m1_ac_power')
| int / 240) | round(0,'floor') %} {{ current_amps - sub_amps }}
action: number.set_value
mode: single
I added some conditions to ensure the automation only runs during daylight hours, and when the car is at home (to limit messing with the car when charging elsewhere), as well as converting to single phase power and capping the amps at 8A (I am running on the UMC plugged into a sketchy garage socket at the moment after having moved house). I will bump those back up to 400V and 32A once my schpanking new 3 phase cabling has gotten the HPWC online. I also trimmed out the device IDs as they were throwing a lot of errors (is that a bad thing to remove them?). I also changed the time pattern from ā30ā to /30 as I find that to be a more reliable trigger.
My next improvement will be to take the voltage sensor of the SolarEdge meter/inverter and input that into the amperage calculation so the maths improves as the voltage fluctuates throughout the day, which it does a surprising amount.
For those with knowledge of this with 3 phase SolarEdge inverters, is āsensor.solaredge_m1_ac_voltage_lnā the best sensor to use, or do I need to do yet more maths to average out the 3 phase voltages?
Ah wait, I celebrated too soon.
When the sun started to go down, instead of gradually reducing the amps, and then stopping when there is zero solar, the automation kept first stopping the charge, and then restarting with too maps amps, and then stopping ad infinitum.
I suspect the āactionā part of the automation is in the wrong order. Can anyone assist me in rewriting it to first ramp down the amps until there is no solar juice, then to stop the charge?
Charging the car with excess solar power is a really great idea. Your approach has been very inspiring . I used a slightly different approach which I like to share here.
Let me know what you think.
My main concern is that with an incremental approach, there is a theoretical risk get into an unrealistic number (e.g. negative or unrealistically high). I am not saying that it is bad; it is just a risk. Therefore I decided to directly estimate the expected power draw of the house and use an input_number that is limited to my electrical setupās amperage (between 5 and 16).
Since the minimal amount of charge current for my Tesla Model Y is 5 Amps (~1100 W), and sometimes the total power would not be enough, I am OK to charge with a mixture of 50% solar and 50% grid energy. I wanted to make sure that I also can accommodate that.
Further, for me it is OK that the routine still draws from or returns to the grid a bit of current. Therefore I decided to update once per 5 minutes.
I therefore splitted up the protocol into four parts/automations:
Calculate the Tesla Charge current every 5 minutes (see below)
Update the Tesla Charge current into the Tesla
Interrupt the charging in case of too little power (less than 1kW)
Resume the charging in case of sufficient power (more than 1.2kW)
The last three are quite obvious to implement.
I have several Shelly power meters, so those can be added up + a rest power that I cannot measure and therefore estimated at 350 W. Then I subtract this āhouse powerā from the āsolar powerā which can go into the Tesla.
I use an input_number helper to store the amount of current. Whenever this value changes and the car is charging at home (+ several other conditions), I update the Teslaās charging current value. This routine is always running every 5 minutes (I could change that if that really has significant impact).
Further, have an input_boolean to turn on āGreen Chargingā so I can also use conventional night charging.