Tesla controlled charge rate based on power sensor

Github Gist: Tesla controlled charge rate based on power sensor · GitHub

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

Purpose

Dynamically adjusts Tesla EV AC charging load to keep the overall power load within the capacity of the home power infrastructure. :oncoming_automobile: :electric_plug:
This functionality is also known as load curtailment, load management, load shedding, and dynamic load management.
Similar functionality to Smart EV chargers with load curtailment features, but by using an existing power sensor (CT claim sensor) in your home and the Telsa cars charge rate controls.

IMPORTANT => Use at your own risk

Smart EV chargers with load curtailment features (i.e. using their own power/CT clamp sensor) are superior to what this automation can provide because they are dedicated to that task, respond quicker to load changes and have higher availability.

Requirements

  1. Tesla HACS Integration
    alandtse/tesla: Tesla custom integration for Home Assistant. (github.com)
    a. Controls > Safety > Allow Mobile Access must be enabled in car

  2. CT clamp sensor to measure grid power
    Integrating your electricity grid - Home Assistant
    Must provide a “power” device class and a unit of measurement of W or kW (take note of this) via an entity in Home Assistant.
    e.g.

state_class: measurement
unit_of_measurement: W
device_class: power
friendly_name: Main Power Meter power

Optional Integrations

  1. EV charger controlled charge rate (instead of Tesla vehicles native one), currently only Lektrico is supported
    Lektrico/ha_lektrico: Manage your Lektrico EV Charger (github.com)

Operation
Responds by decreasing the charge rate significantly when an upper or maximum W value is reached by a power sensor.
Checks every x minutes to find the optimum charging value based on current power sensor value W.
Increases charge rate by 1 amp every x minutes (+ random delay of 1-30 seconds if the power sensor is below a lower W value. Stops delay and restarts the automation if another trigger occurs.

Only operates while charging is occurring and when the Tesla is at a specified (Home/Away/Any), to prevent changing charging rates while not connected to the power sensor selected.

See the example below from Home Assistant showing it in action.

Configuration Recommendations

The default values for the blueprint at based on 60 amp 240 volt service which calculates to a technical max of 14400 watts and the Tesla EV charger is capable of 10A.

These are the default values based on power sensors which measure in W, they must adjusted to match your use case.

  • Power Sensor 1 Max value: 11000
  • Power Sensor 1 Upper value: 8000
  • Power Sensor 1 Lower value: 6000
  • Polling frequency: 5 minutes
  • Peak amps for charger: 8
  • Minimum amps for charger: 2

11000 watts is used as the max value so very little charging occurs if the load is high, headroom from uncontrolled loads is getting close to the technical maximum of 14400 watts at this level.
Between 6000 and 8000 watts is where we want the charging to occur. This provides a bit of headroom for other dynamic loads to come online before the charging is knocked back to its minimum amp value of 2 amps.
Polling is set to 5 minutes to allow a gradual but useful increase in charging amps as well as a frequent additional check the power upper value hasn’t been exceeded.

Power Sensor 2 can be used to point at either the same sensor as Power Sensor 1 but with a higher value. i.e. 12000W, or if you have an additional sensor from another high-load device like a clothes dryer or kitchen oven set to that device’s sensor and an appropriate value like 2000W. The only purpose of this is to provide an additional trigger to quickly knock back the charging rate.

If you have multiple Teslas charging at the same location, you can create an automation based on this blueprint for each Tesla. You may need to increase the range between the lower and upper-value w to prevent them from knocking each other’s charging rates back frequently.

The location (Home/Away) is based on the home location of the Tesla account configured in Home Assistant.

Future improvements
This blueprint is could be modified to work with other brand EVs or control Smart EV chargers without CT clamps. Please let me know if you have a use case like this.
It was developed using the Tesla Mobile Connector and cars native charge rate controls, but I would also be interested if controlling the charging of a Tesla Gen3 Wall Connector can be done using this automation. They don’t currently support load shedding using a CT clam.

Troubleshooting
The version of the blueprint is shown in the description of the automation, check if this is the latest.

Blueprint Code

Show
blueprint:
  name: Tesla controlled charge rate based on power sensor
  description: |
    ## Tesla controlled charge rate based on power sensor  

    Checks every x minutes to find the optimum charging value based on home power usage W.

    Decreases charge rate significantly when a upper or maximum W value is reached by a power sensor.

    Increases charge rate by 1 every x minutes if power sensor is below a lower W value.

    **IMPORTANT => Use at your own risk. Please see the discussions in the Home Assistant community**

    *[Blueprint Version: 0.04.01](https://community.home-assistant.io/t/tesla-controlled-charge-rate-based-on-power-sensor/607107)*
  domain: automation
  input:
    power_sensor_1_entity:
      name: Power Sensor 1
      description: This is the main power sensor that will be used to measure load (e.g. sensor.main_meter_power), check the state of the sensor to determine if it uses W or kW and use the same units when setting trigger values.
      selector:
        entity:
          filter:
            - domain: sensor
              device_class: power
    power_sensor_1_max_value:
      name: Power Sensor 1 Trigger Max Value (W or kW based on sensor type)
      description: This is a absolute max value which will trigger a drop in charging amps.
      default: 11000
      selector:
        number:
          min: 0
          max: 100000
          mode: box
    power_sensor_1_charge_upper_value:
      name: Power Sensor 1 Trigger Upper Value (W or kW based on sensor type)
      description: This is the upper value for the power sensor which will trigger a drop in charging amps.
      default: 8000
      selector:
        number:
          min: 0
          max: 100000
          mode: box
    power_sensor_1_charge_lower_value:
      name: Power Sensor 1 Charging Lower Value (W or kW based on sensor type)
      description: This is the value for the power sensor which allows the charger to increase amps.
      default: 6000
      selector:
        number:
          min: 0
          max: 100000
          mode: box
    polling_time:
      name: Polling frequency (Mins)
      description: Number of minutes before checking if charging amps can be increased.
      default: /5
      selector:
        select:
          options:
            - label: 1 min
              value: /1
            - label: 5 mins
              value: /5
            - label: 15 mins
              value: /15
            - label: 30 mins
              value: /30
    tesla_charging_state_entity:
      name: Select the Tesla charging state entity
      description: Select the entity which represents the Tesla charging state (e.g. binary_sensor.tesla_charging)
      selector:
        entity:
          filter:
            - domain: binary_sensor
              device_class: battery_charging
    tesla_charging_amps_number_entity:
      name: Select the Tesla charging amps entity
      description: Select the entity which represents the Tesla charging amps (e.g. number.tesla_charging_amps)
      selector:
        entity:
          filter:
            - domain: number
              integration: tesla_custom
            - domain: number
              integration: tesla
            - domain: number
              integration: lektrico_custom
    tesla_charging_amps_upper_value:
      name: Select the peak amps for charger (Amps)
      description: Select the number of amps that charging should not be increased past. Best set to the maximum charge rate possible with the charger, or a lower value. Not higher than the maximum possible with the charger.
      default: 8
      selector:
        number:
          min: 0
          max: 32
          step: 1
          mode: slider
    tesla_charging_amps_lower_value:
      name: Select the minimum amps for charger (Amps)
      description: Select the number of amps to reduce charging to when a power sensor upper/max value is exceeded
      default: 2
      selector:
        number:
          min: 0
          max: 32
          step: 1
          mode: slider
    tesla_device_tracker_entity:
      name: Select the Tesla location entity
      description: Select the entity which represents the Tesla location (e.g. device_tracker.tesla_location_tracker)
      selector:
        entity:
          filter:
            - domain: device_tracker
              integration: tesla_custom
            - domain: device_tracker
              integration: tesla
    tesla_control_location:
      name: Select the Tesla location to be controlled
      description: Select the location where the charging should be controlled (e.g. Home)
      default: home
      selector:
        select:
          options:
            - label: Home
              value: home
            - label: Away
              value: not_home
            - label: Any
              value: "null"
    power_sensor_2_entity:
      name: Power Sensor 2
      description: Additional power sensor which can trigger a drop in charging amps (e.g. 2nd high load monitor). If no additional power sensor is required, set to the same entity as Power Sensor 1 but with a higher trigger max value, it can then act as backup trigger.
      selector:
        entity:
          filter:
            - domain: sensor
              device_class: power
    power_sensor_2_max_value:
      name: Power Sensor 2 Trigger Max Value (W or kW based on sensor type)
      description: This is a absolute max value which will trigger a drop in charging amps.
      selector:
        number:
          min: 0
          max: 100000
          mode: box
  source_url: https://gist.github.com/DataBitz/bcccb37ab0678286bc8cabb23a61d4cb

variables:
  var_tesla_charging_amps_number_entity: !input tesla_charging_amps_number_entity  

trigger:
  - platform: numeric_state
    entity_id: !input power_sensor_1_entity
    above: !input power_sensor_1_max_value
    id: power_1_max_trigger
  - platform: numeric_state
    entity_id: !input power_sensor_1_entity
    above: !input power_sensor_1_charge_upper_value
    id: power_1_upper_trigger
  - platform: time_pattern
    minutes: !input polling_time
    enabled: true
    id: polling_time_trigger
  - platform: numeric_state
    entity_id: !input power_sensor_2_entity
    above: !input power_sensor_2_max_value
    id: power_2_max_trigger

condition:
  - condition: state
    entity_id: !input tesla_charging_state_entity
    state: "on"
    enabled: true
  - condition: state
    entity_id: !input tesla_device_tracker_entity
    state: !input tesla_control_location

action:
  - if:
      - condition: trigger
        id:
          - polling_time_trigger
    then:
      - delay:
          hours: 0
          minutes: 0
          seconds: "{{ range(1, 30) | random }}"
          milliseconds: 0
  - if:
      - condition: numeric_state
        entity_id: !input power_sensor_1_entity
        above: !input "power_sensor_1_charge_upper_value"
    then:
      - service: number.set_value
        data:
          value: !input "tesla_charging_amps_lower_value"
        target:
          entity_id: !input tesla_charging_amps_number_entity
        alias: Cut Tesla charging rate significantly
    else:
      - condition: numeric_state
        entity_id: !input power_sensor_1_entity
        below: !input "power_sensor_1_charge_lower_value"
        alias: Check load from main power sensor is low enough to allow increase in charging rate
      - condition: numeric_state
        entity_id: !input tesla_charging_amps_number_entity
        below: !input tesla_charging_amps_upper_value
        alias: Check charging amp rate is not already at or above the desired upper value
      - service: number.set_value
        target:
          entity_id: !input tesla_charging_amps_number_entity
        data:
          value: "{{ states(var_tesla_charging_amps_number_entity) | int + 1 }}"
        alias: Increase charging rate by 1 amp
    alias: >-
      Adjust Tesla charging amp based on Home EV Charging Max Power and Target values
mode: restart

Changelog
* 0.01.05 Initial version published to HA Blueprint Exchange
* 0.02.03 Adds random delay (1-30 seconds) to polling, so when multiple copies of the automation are being used (i.e. multiple Tesla’s), it prevents them from all increasing load at exactly the same time.
Trigger mode changed to restart, to allow an overload trigger to stop the existing automation - i.e. while waiting for delay time.
* 0.03.00 Can now control charge amps using Lektrico EV chargers (lektrico_custom) instead of Tesla. Thanks to Cquad
* 0.04.01 Updated descriptions and configuration guide to explain that if kW sensors are used as triggers, values in automation must be in kW. Added link to HA Community page.

5 Likes

Interesting!
I have been using something somewhat similar myself for a year now, but I have a problem:

HA can’t tell whether my Tesla is plugged in, because the binary sensor for that is only polled when the car is awake…so when there is excess solar power then it triggers the automation, but the car reports “not plugged in” and the process fails.

I would need to rewrite it so that it wakes the car, then checks for the binary sensor and then runs the rest of the automation. I will have to figure out how to do that…right now I usually just “arm” the automation manually…

Didn’t have that problem with this automation as it only operates while the car is charging, so the car is always awake.

1 Like

Updated version 0.02.03.
Adds random delay (1-30 seconds) to polling, so when multiple copies of the automation are being used (i.e. multiple Tesla’s), it prevents them from all increasing load at exactly the same time.
Trigger mode changed to restart, to allow an overload trigger to stop the existing automation - i.e. while waiting for delay time.

To upgrade install this over the last version and then go to Developer Tools, YAML, click Automation to trigger a reload to see the new version.

Hi.

First of all, great blueprint, it’s just what i need it.

It is possible to change minutes to seconds on the polling rate? I want the automation to check the power every 10 seconds. What do i have to change?

Thanks in advanced.

Edit: I just found it. Thanks.

1 Like

Thanks!
Power is checked primarily by the trigger. i.e. Home Assistant will trigger the automation as soon as it sees the threshold crossed. It’s always watching the sensors listed as triggers and responds in milliseconds with a call to the Tesla API asking for the charging amps to be reduced, though the Tesla API part could take seconds.
The polling every x minutes is primarily used to increase the charge rate/amps.
So reducing it to 10 seconds won’t really change how quickly the automation responds to a change in power load. But it will change how quickly the charging amps increase again after being knocked back.

Thanks a lot for this blueprint.
I forked it to allow charging also with lektrico_custom integration.

If you need this blueprint fork with lektrico, it’s here.

1 Like

Great,
I was hoping it could be used with other chargers, I’ll roll your change in.

Thanks for this. My power sensor is a Nett type and goes into negative when we have surpluss solar. For example it may read -4000 meaning it has 4kw of surplus power going to grid.

How could I use this type of popular sensor?

I also have 2 inverters that I do have sensors for. They would typically read 4000 (one) and 4000 (two). These show production so I tried them but the Automation never triggers.

Any tips before I change things around in the code?

Do the sensors show up in the blueprint for selection already?

Yes.

So all I had to do to get it working was change the validation to allow negative numbers.

I also added a Sunrise to the trigger as I sometimes use a special deal with my energy provider from midnight to 6am is crazy cheap power.

I didn’t fork your code, but here is my code that works.

HH/hh_tesla_dynamic_charge.yaml at main · KonsultInternetConsulting/HH (github.com)

Just logged in to thank you, DataBitz, for your job. Installed yesterday, and worked during the night like a charm. I needed to balance the Powerwall and the Tesla, both charging at the same time, so this made the job!

1 Like

Thanks,
Do you see a delay using it this way, as the car may be asleep and does not know about the change in charge rates triggered by the automation?
If you do you may be able to fix it by changing the Tesla polling policy to “Connected” so it stays awake while connected to a charger. Polling policy · alandtse/tesla Wiki (github.com)
The original automation assumes the charge is already charging so it is already awake and ready to receive the changes.

Tesla firmware 2023.38.8 and higher requires the HACS Tesla custom integration to be updated to v3.19.3 or higher allowing the location to be detected.
Without the update, the location will be “unknown” even when the Tesla is at Home/Away which will prevent the automation from running - depending on your automations location setting.

1 Like

Hey great project! I am using a very similar system i implemented in NodeRed, but I have one main problems at the moment.
Sometimes the car does not respond. I am now debugging the issue and installing a WiFi AP closer to the car, but i’m not sure if coverage is the real problem or it’s the API that sometimes does not work. This is not great when another load is connected and i need to quickly reduce the charge rate to avoid exceeding the maximum available power. Is it something happening also in your use case?

Thank you,
No, I haven’t seen that but my car does connect to WiFi while charging (validated by network ping) and I’m not using NodeRed.
Try setting the Uptime Kuma addon in Home Assistant, set up an IP reservation on your router for the Telsa, and then have Uptime Kuma do ping monitoring of it. Then you will get a graph of when the car is on WiFi and can tell if it has dropped off when you see it not responding to the request to change the charge rate.

Just to validate the blueprint/Tesla environment, I checked and here is my most recent charging session where the load was reduced. The main power meter increases and the “Shed power meter” which is where the car is charging drops within a few seconds as the charging amps are reduced.

Example Uptime Kuma graph (not same time as load graph due to scale)

Hi,
thanks for this one! I’ve just installed and gave it go
my home setup 3x 25A with a Tesla on 3x 16A

now I have a couple of other big users in the house: Like the oven runs close to 3kW on Phase1, a washer that peeks to 2kW on Phase2 and a backup heater (heatpump) that run 3kW on Phase3. Apart from the washer these are appliances that I cannot measure individually. They will show up on the power meter on the Phase grouped together.

A moment ago I had a good test;
set the power sensor1 to 15.000W with lower and upper values to 7kW - 14kW
set the power sensor2 to 5.500W - measuring Phase3

  • the Tesla is charging with 11kW spread across the 3 phases
  • washer using 2kW on Phase2
  • some other stuff <1000W

At this point I noticed Phase2 is exceeding the max breaker capacity of 5,7kW (actually on 6.7kW !) and total usage is 16kW > exceeding the upper threshold of 14kW. But the blueprint is not acting on it.

I manually reduced the Tesla from 16A to 12A because things are heating up. And now I can see the BP is ramping up the Tesla charging speed again. So the BP seems to be working - but only in one direction.

Where can I find out to see what numbers the BP is considering? Or maybe I’m not interpreting the working of the BP correctly?
Can we include more (power) sensors? I would like to monitor the 3 different phases individually and act on them

The BP was designed on a single-phase system so some changes will probably be needed for three-phase. I may need to create a separate version of the BP.
What is your minimum charge amps set to and can you get a graph of the charge amps to see what changes the BP made during that Dec 18 charge session?
To see what decisions the BP has made have a look at the traces in the automation, it shows the last few runs of the automation.

I’ve set the min to 2A and 1 min polling
(not even sure the car can charge that slowly, maybe I should set it to 6A or so)

Picture below includes the charge amps. It drops from 16A to 12A twice on my own action. And immediately steps up to 16A by the bp.

The traces from yesterday seem no longer available.

I’m just browsing the ‘States’ section, and maybe I should put in the kW instead of W (as indicated in the bp)

sensor.power_consumed
Power Consumed 1.233
state_class: measurement
unit_of_measurement: kW
device_class: power
friendly_name: Power Consumed

sensor.power_consumed_phase_1
Phase1 0.183
state_class: measurement
unit_of_measurement: kW
device_class:
power friendly_name: Phase1