Add support for Tesla Powerwall

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:

Im having issues this morning with the integration?

Screenshot 2022-10-26 at 09.19.40

Screenshot 2022-10-26 at 09.19.50

Anyone else having similar issues?

Can you enable debug and report back what you find in your logs?

logger:
  default: warning
  logs:
    teslajsonpy: debug
    custom_components.tesla_custom: debug

Hi Mark

Below is the output (im removed my site info)

2022-10-26 11:21:38.801 DEBUG (MainThread) [teslajsonpy.connection] 408: {"response":null,"error":"{\"error\": \"timeout\"}","error_description":""}
2022-10-26 11:21:38.802 DEBUG (MainThread) [teslajsonpy.controller] Exception: vehicle_unavailable
api(('VEHICLE_DATA',) {'path_vars': {'vehicle_id': '1492931233743874'}, 'wake_if_asleep': False})
2022-10-26 11:21:38.802 DEBUG (MainThread) [teslajsonpy.controller] 94876: Changing car_online from True to False
2022-10-26 11:21:38.802 DEBUG (MainThread) [teslajsonpy.controller] Vehicle asleep - data unavailable.
2022-10-26 11:21:38.803 DEBUG (MainThread) [teslajsonpy.connection] Token expiration in 6:00:14
2022-10-26 11:21:38.803 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/energy_sites/*******************/site_info {}
2022-10-26 11:21:39.120 DEBUG (MainThread) [teslajsonpy.connection] 200: {"response":{"id":"1152100-13-J--CN322024G3J04I","site_name":"My Home","backup_reserve_percent":30,"default_real_mode":"self_consumption","installation_date":"2022-05-09T11:29:29+01:00","user_settings":{"storm_mode_enabled":true,"powerwall_onboarding_settings_set":true,"sync_grid_alert_enabled":true,"breaker_alert_enabled":false},"components":{"solar":true,"solar_type":"pv_panel","battery":true,"grid":true,"backup":true,"gateway":"teg","load_meter":true,"tou_capable":true,"storm_mode_capable":true,"flex_energy_request_capable":false,"car_charging_data_supported":false,"off_grid_vehicle_charging_reserve_supported":false,"vehicle_charging_performance_view_enabled":false,"vehicle_charging_solar_offset_view_enabled":false,"battery_solar_offset_view_enabled":true,"solar_value_enabled":true,"energy_value_header":"Energy Value","energy_value_subheader":"Estimated Value","energy_service_self_scheduling_enabled":true,"show_grid_import_battery_source_cards":true,"backup_time_remaining_enabled":true,"battery_type":"ac_powerwall","configurable":true,"grid_services_enabled":false},"battery_count":1,"tou_settings":{"optimization_strategy":"economics","schedule":[{"target":"off_peak","week_days":[1,0],"start_seconds":84600,"end_seconds":19800},{"target":"peak","week_days":[1,0],"start_seconds":19800,"end_seconds":84600}]},"installation_time_zone":"Europe/London","max_site_meter_power_ac":1000000000,"min_site_meter_power_ac":-1000000000,"<REMOVED GEO LOCATION INFO>
2022-10-26 11:21:39.121 DEBUG (MainThread) [teslajsonpy.connection] Token expiration in 6:00:13
2022-10-26 11:21:39.122 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/powerwalls/STE20220509-00082 {}
2022-10-26 11:21:39.636 DEBUG (MainThread) [teslajsonpy.connection] 408: {"response":null,"error":"https://powergate.prd.sn.tesla.services:443/api/powerwalls/1152100-13-J--CN322024G3J04I/fullstatus => Can't get recent status from the battery","error_description":""}
2022-10-26 11:21:39.636 DEBUG (MainThread) [teslajsonpy.controller] Exception: vehicle_unavailable
api(('BATTERY_DATA',) {'path_vars': {'battery_id': 'STE20220509-00082'}})
2022-10-26 11:21:39.675 ERROR (MainThread) [custom_components.tesla_custom] Unable to communicate with Tesla API: VEHICLE_UNAVAILABLE

Looks like your vehicle is asleep, does it do the same after your vehicle is awake?

@shred for your visibility