Tesla Powerwall use cases and automation sharing

Hi pjvenda

I have been reading this thread with great interest as I have a similar set of requirements as you and others have stated, namely:-

  • Get a level of control over the PW behaviour
  • Force PW to charge to 80%, or more if it a a heavy usage day like Sundays.
  • Discharge unused power towards the end of the normal day (@15p/kWh) ready to fill up @7p after 23:30.

I wonder if the automations you posted in Oct '23 have been improved on as I want to implement your solution in my HA?

If you have developed them would you kindly re-post them as a consolidated solution?

Thanks for all your efforts :slight_smile:

1 Like

keen to hear answer on this too - i am choosing between fronius inverter (hytbrid) and byd battereis vs Tesla solution .
i dont understand if Tesla can be automated by HA integration i.e to set the sell to grid amount if my amber solar export is x amount

thanks

Iā€™m thinking in buying just the PW for my home as I live in a flat without possibility to install solar panels, to balance the charging time of the day , as my Energy provider charges me with official hour price (changes every hour) OMIE.
As during the day , the cost is high influenciated by solar production, prices get into negative (cents or 1 euro). and at the evening , it increases a lot. My view is to charge when is cheaper, and cut the cord and use the PW when itā€™s expensive. Do you have any PW suggestion or any other advice ?
Thank you so much in advance.

Hi Peter, all your points are feasible. Naturally my scripts and automations have evolved since when I last posted them, so I will be posting an update.

1 Like

The powerwall can be automated by using the HA ā€˜cloudā€™ integration, or the non-official one. The way it is controlled, however, is indirect. By this I mean you cannot make it do what you want by asking directly, but you can setup the modes of operation and setup some values so you can get it to decide to do what you want.

I am able to:

  • Force charge at full power (5kW) or at low power (1.8kW)
  • Prevent discharging (e.g. for when the EV is charging)
  • Force exporting (*)
  • Prioritise exporting solar generation over using it to feed the house (**)

Using these Iā€™ve setup automations to manage battery operation against several factors like time of day, etc.

I have also developed a ā€œdumbā€ linear projection of battery consumption to decide whether itā€™s time to export or how much I want to export at a given time of the day.

You can absolutely control the powerwall to charge/discharge/do nothing at your criteria, in this particular case, time of day vs cost of energy.

It may take a couple of steps to get to a point where you can automate this, as you need a way to collect the information from your provider, etc, feed it into homeassistant then use that to drive your automations.

1 Like

thanks . Thatā€™s a fact that influenciate the adoption/wish to :slight_smile:
assuming the gross price forecast published for market OMIE , and already integrated in Home assistant with price luuuis/hass_omie: OMIE Home Assistant Integration (github.com) , it just bring us to the automation in Home Assistant. Iā€™m having a meeting with a solar panel store/implementer to have prices on it . question is the ROI ā€¦

Are you able share these updates?

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