Tesla Powerwall use cases and automation sharing

Of course! Only waiting for a few minutes of free time to clean up the yaml code a bit for pasting here.

OK, I started to paste the scripts and quickly realised that this medium is not going to work. So I started a github to share the information to:

Don’t go running because I’ve only just begun documenting and pasting content. It will take a couple days but once it’s up it will be much easier to use and update.

  • Scripts are uploaded now
  • Some documentation is available on the README file and wiki

Very welcome to see your scripts.

I have some similar scripts that happy to collaborate with you on.

Regarding force charge, I see you have listed two rates (1.7 kW - Raise BackupReserve & 5 kW OffPeakTBC), I have also been able to utilise a third charging rate which is 3.3 kW, by switching to the undocumented backup mode.

https://developer.tesla.com/docs/fleet-api#operation
Screenshot 2024-05-16 10.14.23

1 Like

uhh, interesting! the option is on the cloud integration, but I never tried it. do you know how it behaves? (apart from charging at 3.3kW)

In terms of forced charging options I find:

  1. Raising backup reserve > existing SOC takes effect immediately and starts charging at approx 1.7 kW per battery.
  2. Switching to backup mode takes effect immediately and starts charging at a minimum of 3.3 kW per battery, but will increase upto the value of solar production.
  3. Switching to TBC mode, AllowGridCharging = True and UtilityPlan is already in OffPeak can take up to 5 minutes (time pattern /5 ?) to start charging at 5 kW per battery.
  4. Switching AllowGridCharging to True from False, if already in TBC mode and UtilityPlan OffPeak also can take up to 5 minutes to starts charging.
  5. Changing UtilityPlan values/ times to OffPeak can take up to 15 minutes before it starts charging (time pattern /15 ??)

1 Like

Thanks! I have a similar experience but never worked out the actual timings.

Have you found a way to operate the utility rate plan via homeassistant? Or via the customer API (has to be possible, or else the app wouldn’t be able to… needs inspecting the traffic)?

Also, how do you calculate your forecast? I use a simple linear projection with a static incline (power value) that I guestimate and keep in a helper number. This gives me a projection at any time of the day. But it’s always a guess dependent on that fixed value.

I have some python scripts which utilize the teslapy library to update the utility plan, these can be called directly from home assistant.

There is a site tariff endpoint with the FleetAPI that allows you to inject the utility plan.

For forecasting I use the excellent EMHASS add-on: An energy management optimization add-on for Home Assistant OS and supervised

It gives me very detailed forecasts and control signals for my battery and other household loads. Here is my plan for the next 24 hours.

2 Likes

Would you be so kind as to post an example script to get and set the rates?

This script will fetch the current tariffs using teslapy library.

#!/usr/bin/env python3
import teslapy
with teslapy.Tesla("[email protected]") as tesla:
        tesla.fetch_token()
        battery = tesla.battery_list()[0]
#        tariff = battery.api('SITE_TARIFF')
#        battery.set_backup_reserve_percent(100)
#        battery.set_operation(backup)

#        print(battery) # should print battery status once successfully authenticated
#        print('----')
        print(battery.api('SITE_TARIFF'))

This script will set the utility rate tariff:

import datetime
import teslapy

with teslapy.Tesla('[email protected]') as tesla:
    batteries = tesla.battery_list()
    battery = batteries[0]

    # Define load cost and production price forecasts

    load_cost_forecast = [0.36, 0.24, 0.2, 0.23, 0.19, 0.21, 0.2, 0.2, 0.18, 0.18, 0.18, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.18, 0.21, 0.26, 0.27, 0.39, 0.18, 0.12, 0.07, 0.06, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.33, 0.38, 0.54, 0.63, 0.5, 0.38, 0.38, 0.37, 0.39, 0.38, 0.22, 0.2, 0.24, 0.22, 0.21, 0.2, 0.21, 0.18, 0
.18, 0.17, 0.17, 0.15, 0.15, 0.15]
    prod_price_forecast = [0.13, 0.14, 0.11, 0.13, 0.1, 0.12, 0.11, 0.1, 0.09, 0.09, 0.09, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.09, 0.12, 0.16, 0.17, 0.28, 0.09, 0.04, -0.01, -0.01, -0.02, -0.02, -0.02, -0.02, -0.02, -0.02, -0.02, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.1, 0.15, 0.3, 0.38, 0.25, 0.15, 0.15, 0.14, 0.15, 0.15, 0.13, 0.11, 0.14, 0.12, 0.12, 0.11, 0
.11, 0.09, 0.09, 0.08, 0.08, 0.06, 0.06, 0.06]

    # Define time periods for each tariff
    periods = []


    current_time = datetime.datetime.now().time()

    # Find the index of the current time in 30-minute intervals
    start_index = current_time.hour * 2 + current_time.minute // 30

    start_time = datetime.time(current_time.hour, current_time.minute // 30)

    
    current_time = datetime.datetime.now()
    start_time = datetime.time(current_time.hour, 30 * (current_time.minute // 30))

    print (start_time)
    exit
    skip_next = False  # Flag to skip the next period
    for i in range(48):
        if skip_next:
            skip_next = False
            start_time = (datetime.datetime.combine(datetime.date.today(), start_time) + datetime.timedelta(minutes=30)).time()
            continue
        
        end_time = (datetime.datetime.combine(datetime.date.today(), start_time) + datetime.timedelta(minutes=30)).time()
        if end_time == datetime.time(0, 0):
            skip_next = True
            start_time = end_time
            continue
        
        load_cost = load_cost_forecast[i]
        prod_price = prod_price_forecast[i]
        period = teslapy.BatteryTariffPeriodCost(buy=load_cost, sell=prod_price, name=f'p{i}')
        periods.append(teslapy.BatteryTariffPeriod(period, start_time, end_time))
        print(period, start_time, end_time)
        start_time = end_time

    # Create the tariff
    new_tariff = teslapy.Battery.create_tariff(teslapy.BatteryTariffPeriodCost(buy=0.16, sell=0.05, name='DEFAULT'), periods, provider='Amber', plan='Custom via API')

    # Debugging: Print the original and updated tariffs
#    print("Original Tariff:", battery.get_tariff())
#    print("Updated Tariff:", new_tariff)

    # Set the new tariff
    battery.set_tariff(new_tariff)
1 Like

I am a complete newbie on Hone Assistant and relatively new on Tesla Powerwall so hope you don’t mind me asking perhaps simple questions. I am in the UK and use Octopus as an energy supplier. The tariff I am on is called Agile and I get half hourly pricing. Anyway my requirement is simple really on that what I want to do is to say If the hourly price is X or below Then charge the battery assuming that the battery level is below 100%. The issue I have is that there doesn’t seem to be any command which says charge the battery now. When I read your post you talk about moving the “Reserve” level to 100%. Is this the command that you use to force the battery to charge? Hope it’s not a stupid question. Thanks in advance oh one thing I am using the “Tesla Integration” that can be found Devices & Services + Integration” I see there is another Tesla integration under HACS however that seems to suggest you have to generate tokens etc which I have no clue about.
Graham

You set the reserve to 100% and there’s a second setting that enables/disables charging from the grid entirely. When disabled the batteries will only ever charge from solar.

Changing both the reserve and enabling charge from grid accomplish what you’re wanting.

1 Like

Thanks @nugget
That’s great however when looking at the documentation for the Tesla Powerwall integration these are the only sensors that I see (below) so I assume that Powerwall Backup Reserve - Reserve energy for grid outages in % is the one I set to 100% but I don’t see anything for the other setting you mentioned. Oh and I am confused why it talks about “backup” as I only have one battery.
Sorry if I am being a bit stupid, I did say I was a newbie.
Thanks in advance
Graham

SENSOR

The following sensors are added for each Backup Gateway aggregated across all Powerwalls:

  • Powerwall Backup Reserve - Reserve energy for grid outages in %
  • Powerwall Battery Now - Power in kW (negative for charging)
  • Powerwall Charge - Percent charge remaining in %
  • Powerwall Generator Now - Power in kW (if applicable)
  • Powerwall Load Now - Power in kW
  • Powerwall Solar Now - Power in kW (if applicable)
  • Powerwall Site Now - Power in kW (negative for grid export)
  • Powerwall Backup Reserve - Percentage of battery which will be reserved for a grid outage
  • Frequency/ Average Current/ Average Voltage Now - in Hertz, Amps and Volts

Tesla removed those two settings from the local gateway API which is how the native Home Assistant integration functions. Those settings are now available only in the Tesla cloud API which is not supported by the native HA integration.

If you supplementally install the 3rd party integration manually or via HACS, you’ll get those settings as well. It’s no problem running both integrations alongside one another, they won’t conflict.

The Teslemetry integration has been designed from the ground up to provide full access to the official Tesla FleetAPI, including a fully supported core Home Assistant integration, which I am using for Powerwall controls.

It is a subscription service.

I’m not sure how Teslemetry could assist in this situation, but as a general suggestion I’m glad to know it exists.

Generally I prefer to keep as much local as possible and try to avoid any cloud APIs unless there’s no other choice, so I think it’s unlikely I’d move to it.

I agree with the preference towards localAPI, but Tesla doesn’t allow local control.

Using the localAPI, the only command available is switching off-grid mode.

To change backup reserve and operating mode (backup, autonomous and self-powered) I have only found cloud API commands available.

Another way to say this is that all of the Tesla data is available locally and only two commands require the cloud which is available from Tesla for free.

Why would I pay 25/year just to access those two commands when I can get them for free? The teslemetry product doesn’t provide any benefit that I can see over the native Tesla api.

Perhaps it makes more sense for people who own one of the cars.

Am I right in assuming that the second setting is actually the “Operational Mode” in the app, so between “Self Powered” and “Time Based Control”. The reason I ask is that I always use the “Time based control” anyway. So I assume that if I then set my battery backup reserve then the battery should charge if my condition has been met in the automation. Or am I wrong?

I just had my Tesla solar and 4x Powerwall system installed a few weeks ago. Until PTO, I am running in self-consumption mode without exports to the grid. Though my system is 23.49kW PV DC and 20.9kW AC, after solar generation reaches > 18kw, the system will begin dumping all excess not used by the house to the grid. I’m told by Tesla this will resolve once I have PTO. To prevent this, I have been switching the system to Off-Grid Mode a bit before peak production each day if its sunny and getting up there. I decided to try to automate this and I think I got it! As of now, here is the automation:

Another simple automation turns it off at 2pm when I’m well past the point of peak generation and certainly below 18kW generation. Just set this up this morning. Excited to see how it works.

Note: As I wrote this I realized I probably could just tell it to turn off the grid once production got to around 17kW and then enable the grid once production was below 15kW for over 30 minutes or something like that. I’ll try it this way first.

Created another automation to stop pulling from the grid by setting off-grid to on if the battery is at least 30% charged and home is not pulling more than 16kW.