Add support for Tesla Powerwall

Thanks for this defiantly need to do some automation of my PW charging new to this only had the PW since Sept, its on time-based control and done pretty well so far on using solar and charging overnight when needed. However last night it didn’t bother charging and today no chance the solar and battery are going to see the day out, tesla algo. not that great then :slight_smile:

Install Solcast and then you will have Fairly accurate forecast of the super available in your area. You have to create a Solarcast account and get the API from the website to use in the integration. The sensors in HA will all be created as default names and same as used in Bruces automatons.

On my 5 choice automation above that one is triggered at 21:00 and looks at “sensor.solcast_tomorrow_forecast” and sets the PW charge level accordingly. You may need to adjust the solar predictions against required charge % based on your home loads and consumption. My standing loads runs 24/7 at around 450wh and my consumption is high when there is solar as other automatons switch on additional electrical loads. But like today’s forecast is less that 8kWh so all non essential electrical loads are left off all day to preserve the PW SOC as long as possible.

1 Like

Your use case sounds like a great candidate for using the EMHASS: Energy Management for Home Assistant add on.

EMHASS uses a linear programming model to optimise energy usage by continuously calculating when to switch deferrable loads throughout your house. I use it to schedule my home battery and EV charging, various other systems (hot water, air conditioning, pool pumps and heating). EMHASS considers forecasts for your household load, solar production as well as the buy/ sell price for electricity. If I have a low solar production day forecast it will defer loads to the next 24 hours.

Here is my household optimisation sensors created for the next 24 hours, HA reruns the optimisation every minute as my variable pricing changes that quickly. I use these sensors in automations for my devices to switch them at the appropriate timing.

You can see in the dark blue when my battery is scheduled to charge or export to the grid - after 5pm you can see a big export block as that is when my export tariff is highest and EMHASS has calculated exactly how much I can export, but keep the house running overnight.

You can see on the dark green when it is best to export to the grid, for today’s plan there is no grid import required. You can see the light green the charging profile for my EV for the day. You can see my pool heater isn’t scheduled as it’s run hours are set to only run when the value of my solar exports are extremely_low (< 5¢/ kWh) and I want to maximise self consumption. You can see my standing load in light purple which is based off the average standing load for the last couple of days.

There is some complexity in setting up the add-on, but the Author is very responsive and claims an 8-10% improvement in efficiency. I’m probably seeing higher efficiency as optimising quickly for my changes in my variable pricing gives me a high operational benefits (financial, environmental, sustainable).

1 Like

Hi Mark,

I tried to set up before and again tonight but I can’t get past the web ui fails to open. I set up a port forwarding rule to use 5005 external port and direct to my HA ip address:5000 but no joy.

Also in the configuration as soon as I try to change the monetary values from the default the entry forms go red and it doesn’t accept my values. I’m trying to enter then looks this:

Peak rate: 0.3100
Off peak: 0.0750
Sell: 0.0410
1 Like

Happy to work with you to get this working.

I presume you are using the add-on?

Do you need the port forwarding, can you just setup EMHASS to use port 5005? Can you access your HomeAssistant via you LAN?

I don’t actually use the dialogue for cost entry, but just tried and see what you mean, so there is a bug - @davidusb . A workaround is to switch configuration to yaml where you can edit, save and restart add-on.

It might also make sense to switch this discussion over to EMHASS add-on: An energy management optimization add-on for Home Assistant OS and supervised

The Tesla Powerwall integration appears to be broken, and it seems to be because Tesla made a change on the gateway, where it’s asking for a username and password now.

Amazing work here guys.

I have this all set up and its working great, however i’m having issues around the automations?

Its a pretty simple automation i need - set to 100% reserve at 11:30pm (which will force charge the battery) then set it back to 10% at 5:30am - so the house then runs from the battery.

Ive tried using the sensor and the number entities, but it just doesn’t appear to set it. (It works fine if i use the slider manually)

Could someone maybe help with the automation scripts?

Many thanks

Rob

On my battery automation I just change the modes not change the values, if I get some time tomorrow I’ll have a look at changing values in automations.

You could do the same, switch to Backup mode to charge and switch to Self-Powered mode for normal operation.

alias: p_batt automation
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.p_batt_forecast
    below: -3000
  - platform: numeric_state
    entity_id: sensor.p_batt_forecast
    above: -3000
    below: 4000
  - platform: numeric_state
    entity_id: sensor.p_batt_forecast
    above: 4000
condition: []
action:
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sensor.p_batt_forecast
            below: -3000
        sequence:
          - service: select.select_option
            data:
              option: Backup
            target:
              entity_id: select.home_energy_gateway_operation_mode
          - service: notify.mobile_app_pixel_6
            data:
              title: p_batt  alert {{states('sensor.p_batt_forecast')}} - mode:backup
              message: Price:{{states('sensor.amber_general_price')}} $/kWh
      - conditions:
          - condition: numeric_state
            entity_id: sensor.p_batt_forecast
            above: 4000
            enabled: true
        sequence:
          - service: tesla_gateway.set_operation
            data:
              real_mode: autonomous
              backup_reserve_percent: 1
            enabled: false
          - service: notify.mobile_app_pixel_6
            data:
              title: >-
                p_batt alert {{states('sensor.p_batt_forecast')}} - consider
                mode:autonomous
              message: Price:{{states('sensor.amber_general_price')}} $/kWh
    default:
      - service: select.select_option
        data:
          option: Self-Powered
        target:
          entity_id: select.home_energy_gateway_operation_mode
      - service: notify.mobile_app_pixel_6
        data:
          title: >-
            p_batt  alert {{states('sensor.p_batt_forecast')}} -
            mode:self_consumption
          message: Price:{{states('sensor.amber_general_price')}} $/kWh
mode: single

THanks Mark

I will see if i can figure it out from there.

Ideally im trying to keep the Time Based Control enabled (as when i change it to self powered or backup it reduces the charge rate from 3.6kw to 1.7kw - so the battery doesnt charge fully in the 6 hour window)

I have it working on the super over complex way at the minute which all it does is change the backup percent to 100% to charge and 10% when the house needs to run from it.

Its just making it neater and removing all the complexities around the jsons files etc etc :slight_smile:

quick question on this do you use time based or self powered mode with your automation as got it working but yesterday it picked and set 80% but it overshot to 95% ish. I assume this maybe because of thr mode?

Hi Rob,

Try this one. Create an new automation ant then switch to yaml mode and overwrite with the below (change automation name to what you want:

alias: Robs Automation
description: ""
trigger:
  - platform: time
    at: "23:30:00"
condition: []
action:
  - service: tesla_gateway.set_operation
    data:
      real_mode: autonomous
      backup_reserve_percent: 100
  - condition: time
    after: "05:30:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun
  - service: tesla_gateway.set_operation
    data:
      real_mode: self_consumption
      backup_reserve_percent: 10
mode: single

Thanks Chris - this is how its set up at the minute :slight_smile: using the long winded approach before this integration was available. Its all working at present - im just trying to make it a cleaner and more simple setup.

I am now setting the backup reserve % using the rewrite integration.

You need to call the number.set_value service.

See my automation below with the legacy tesla_gateway.set_reserve service (enabled: false) and the number.set_value service. This automation sets backup reserve to the current value of my sensor.soc_batt_forecast, which is set by the EMHASS add-on: An energy management optimization add-on for Home Assistant OS and supervised, but you should be able to hard code your values.

You can also check by manually calling number.set_value service from Developer Tools.

The advantage of using this new service call is you receive confirmation to what the value has been changed to.

alias: Battery SOC Forecast
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.soc_batt_forecast
condition: []
action:
  - service: tesla_gateway.set_reserve
    data:
      backup_reserve_percent: "{{states('sensor.soc_batt_forecast')|int(0)-5}}"
    enabled: false
  - service: number.set_value
    data:
      value: "{{states('sensor.soc_batt_forecast')|int(0)-5}}"
    target:
      entity_id: number.home_energy_gateway_backup_reserve
mode: single

Thanks Mark

Just checking on this one - should i remove the old Tesla Gateway Integration before setting the above up? (noticing the conflict in service names?)

No need, I have both loaded and can issue service calls from either.

Great - thanks Mark, the above has helped - and its now working with the new integration :slight_smile:

Heres what i have :slight_smile:

alias: PowerWall - Charge Off Peak"
description: ""
trigger:
  - platform: time
    at: "23:30:00"
condition: []
action:
  - service: number.set_value
    data:
      value: "100"
    target:
      entity_id:
        - number.my_home_backup_reserve
mode: single
2 Likes

Hi Angus,

I’ve copied your code into a new automation in YAML mode, but when trying to run it to test, I get the below error - any ideas?

Triggered manually at 25 October 2022 at 16:51:15

Tesla Gateway:

Stopped because an error was encountered at 25 October 2022 at 16:51:16 (runtime: 0.75 seconds)

403 Client Error: Forbidden for url: https://auth.tesla.com/oauth2/v3/authorize

Which integration have you installed? The new “dev” version of Tesla_custom? That error looks like it could be to do with the Tesla account authorisation issue…but with the new dev Tesla_custom you shouldn’t experience that error.

1 Like

Thanks for the reply.

I have the new dev release and got all controls in HA.

Oddly, it appears that even though that error pops up in the trace, it does still work. :man_shrugging: