Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Powerwall 3 Local Access via TEDAPI #97

Open
ttl74ls100 opened this issue Jun 8, 2024 · 25 comments
Open

Powerwall 3 Local Access via TEDAPI #97

ttl74ls100 opened this issue Jun 8, 2024 · 25 comments

Comments

@ttl74ls100
Copy link

Ubuntu, python3, Powerwall 3 x 2, using static route to PW

command line
python3 tedapi.py
Enter password

Result:

Tesla Powerwall Gateway TEDAPI Reader

Connecting to Powerwall Gateway 192.168.91.1
Failed to connect to Powerwall Gateway
Traceback (most recent call last):
  File "/home/jc/pypowerwall/tools/tedapi/tedapi.py", line 305, in <module>
    ted = TEDAPI(gw_pwd)
          ^^^^^^^^^^^^^^
  File "/home/jc/pypowerwall/tools/tedapi/tedapi.py", line 65, in __init__
    raise ValueError("Failed to connect to Powerwall Gateway")
ValueError: Failed to connect to Powerwall Gateway

Request returns 404, which is expected.

Added line with comment # no exception, so the request was successful and script ran successfully.

        log.debug(f"Testing Connection to Powerwall Gateway: {GW_IP}")
        url = f'https://{GW_IP}'
        try:
            r = requests.get(url, verify=False, timeout=5)
            r = True # no exception, so the request was successful
        except requests.exceptions.RequestException as e:
            r = False
            log.error("ERROR: Powerwall not Found",
                      f"Try: sudo route add -host <Powerwall_IP> {GW_IP}")
jasonacox added a commit that referenced this issue Jun 8, 2024
@jasonacox
Copy link
Owner

Thanks @ttl74ls100 ! I made the update.

Starting with v0.10.0, you don't need to use this tool and can talk to tedapi via the library. I'll create an example and post it as well.

@jasonacox
Copy link
Owner

Command Line Tool

With pyPowerwall v0.10.0, you can now run the TEDAPI tool via the command line (CLI):

# First, upgrade if you haven't
pip install -U pypowerwall

# Run the tool
python3 -m pypowerwall.tedapi

It will prompt you for the Powerwall Gateway password (usually printed on the QR code on the gateway), and will then query the Powerwall for config and curren site data. It will display some and write entire payloads to config.json and status.json in the current directory. Example:

Tesla Powerwall Gateway TEDAPI Reader

Enter Powerwall Gateway Password: **********

Connecting to Powerwall Gateway 192.168.91.1

 - Configuration:
   - Site Name: Energy Gateway
   - Battery Commission Date: 2021-09-25T16:05:08-07:00
   - VIN:  123***-**-**-*--TG************
   - Number of Powerwalls: 2

 - Power Data:
   - Battery Charge: 100.0% (25772Wh of 25772Wh)
   - Battery: -10W
   - Site: -337W
   - Load: 1108W
   - Solar: 1460W
   - Solar_Rgm: 1447W
   - Generator: 0W
   - Conductor: 0W

 - Configuration and Status saved to config.json and status.json

Using the Library

The following python script will connect to the gateway and display power data:

# Import Class
from pypowerwall.tedapi import TEDAPI

gw_pwd = "THEGWPASS"

# Connect to Gateway
gw = TEDAPI(gw_pwd)

# Grab the Config and Live Status
config = gw.get_config()
status = gw.get_status()

# Print
site_info = config.get('site_info', {})
site_name = site_info.get('site_name', 'Unknown')
print(f"My Site: {site_name}")
meterAggregates = status.get('control', {}).get('meterAggregates', [])
for meter in meterAggregates:
    location = meter.get('location', 'Unknown').title()
    realPowerW = int(meter.get('realPowerW', 0))
    print(f"   - {location}: {realPowerW}W")

@ttl74ls100
Copy link
Author

Using a python:3.10-alpine container, I installed pypowerwall and ran python3 -m pypowerwall.tedapi with the following result:

Connecting to Powerwall Gateway 192.168.91.1
Failed to connect to Powerwall Gateway
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/local/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/app/jctest-env/lib/python3.10/site-packages/pypowerwall/tedapi/__main__.py", line 27, in <module>
    ted = TEDAPI(gw_pwd)
  File "/app/jctest-env/lib/python3.10/site-packages/pypowerwall/tedapi/__init__.py", line 86, in __init__
    raise ValueError("Failed to connect to Powerwall Gateway")
ValueError: Failed to connect to Powerwall Gateway

Here are the version numbers from the install:

(jctest-env) /app/jctest-env # pip install -U pypowerwall
Requirement already satisfied: pypowerwall in ./lib/python3.10/site-packages (0.10.2)
Requirement already satisfied: requests in ./lib/python3.10/site-packages (from pypowerwall) (2.32.3)
Requirement already satisfied: protobuf in ./lib/python3.10/site-packages (from pypowerwall) (5.27.1)
Requirement already satisfied: teslapy in ./lib/python3.10/site-packages (from pypowerwall) (2.9.0)
Requirement already satisfied: charset-normalizer<4,>=2 in ./lib/python3.10/site-packages (from requests->pypowerwall) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in ./lib/python3.10/site-packages (from requests->pypowerwall) (3.7)
Requirement already satisfied: urllib3<3,>=1.21.1 in ./lib/python3.10/site-packages (from requests->pypowerwall) (2.2.1)
Requirement already satisfied: certifi>=2017.4.17 in ./lib/python3.10/site-packages (from requests->pypowerwall) (2024.6.2)
Requirement already satisfied: requests-oauthlib in ./lib/python3.10/site-packages (from teslapy->pypowerwall) (2.0.0)
Requirement already satisfied: websocket-client>=0.59.0 in ./lib/python3.10/site-packages (from teslapy->pypowerwall) (1.8.0)
Requirement already satisfied: oauthlib>=3.0.0 in ./lib/python3.10/site-packages (from requests-oauthlib->teslapy->pypowerwall) (3.2.2)

@jasonacox
Copy link
Owner

Thanks @ttl74ls100 - I can only replicate that if there is no route to the Gateway. I see where I need to adjust the code to help provide a clear message, but can you verify that you are able to ping / curl the Gateway (192.168.91.1) from that container host?

@ttl74ls100
Copy link
Author

I believe the gateway is working. For example, after the patch to tedapi.py in the tools folder, I am able get configuration a power data.

(jctest-env) /app/jctest-env # curl -k https://192.168.91.1
404 page not found

Could the issue be related to the 404 response?

@ttl74ls100
Copy link
Author

I should also highlight I am connecting to a Powerwall 3

jasonacox added a commit that referenced this issue Jun 9, 2024
@jasonacox
Copy link
Owner

I should also highlight I am connecting to a Powerwall 3

@ttl74ls100 , I didn't expect this to work on the PW3! The fact that you got the first to work is a positive step. Thanks for helping test! 🙏

I updated the logic to give a bit more details to help diagnose:

# First install the dev version
pip install pypowerwall==0.10.3.dev0

# Run again
python3 -m pypowerwall.tedapi

# Run with debug
python3 -m pypowerwall.tedapi --debug

@ttl74ls100
Copy link
Author

It worked.

(jctest-env) /app/jctest-env # python3 -m pypowerwall.tedapi
pyPowerwall - Powerwall Gateway TEDAPI Reader
 - Connecting to https://192.168.91.1... SUCCESS

Enter Powerwall Gateway Password: <>

Connecting to Powerwall Gateway 192.168.91.1

 - Configuration:
   - Site Name: <>
   - Battery Commission Date: 2024-01-31T15:36:12-05:00
   - VIN: <>
   - Number of Powerwalls: 2

 - Power Data:
   - Battery Charge: 49.32% (14210Wh of 28810Wh)
   - Battery: 2058W
   - Site: -5W
   - Load: 3375W
   - Solar: 1332W
   - Solar_Rgm: 0W
   - Generator: 0W
   - Conductor: 0W

 - Configuration and Status saved to config.json and status.json

Thanks for your help, Let me know if you need any PW3 testing.

@jasonacox
Copy link
Owner

Excellent! Thank you, @ttl74ls100 !

It seems that the PW3 responds with 404 for the / URI which means we could use that to detect a PW3 and accommodate with some internal logic. Can you check to see if any of these work or the type of responses you get?

import pypowerwall
pypowerwall.set_debug()

# Set these
PW_HOST="192.168.91.1"
PW_GW_PWD="xxxxxxxx" # Your Gateway Password

pw = pypowerwall.Powerwall(PW_HOST, PW_GW_PWD ,gw_pwd=PW_GW_PWD)

# Get vitals - should work
pw.vitals()

# Get current power, level - likely will not work
pw.power()
pw.level()

# Get alerts - may partially work
pw.alerts()

@ttl74ls100
Copy link
Author

pw.vitals()

DEBUG:Get Status from Powerwall
DEBUG:Starting new HTTPS connection (1): 192.168.91.1:443
DEBUG:https://192.168.91.1:443 "POST /tedapi/v1 HTTP/1.1" 200 1700
DEBUG:Response Code: 200
DEBUG:Status: {'control': {'alerts': {'active': ...
DEBUG:Get Configuration from Powerwall
DEBUG:Starting new HTTPS connection (1): 192.168.91.1:443
DEBUG:https://192.168.91.1:443 "POST /tedapi/v1 HTTP/1.1" 200 1310
DEBUG:Response Code: 200
DEBUG:Configuration: {'vin': '<>', 'meters': [{'location': 'site', 'type': 'synchrometerX', 'cts': [True, True, False, False]  ...
{'VITALS': {'text': 'Device vitals generated from Tesla Powerwall Gateway TEDAPI', 'timestamp': 1717978179.1119115, 'gateway': '192.168.91.1', 'pyPowerwall': '0.10.3'}, 
...

pw.power()

DEBUG: -- local: Request Powerwall for /api/meters/aggregates
DEBUG:Starting new HTTPS connection (1): 192.168.91.1:443
DEBUG:https://192.168.91.1:443 "GET /api/meters/aggregates HTTP/1.1" 404 19
ERROR:404 Powerwall API not found at https://192.168.91.1/api/meters/aggregates
DEBUG:ERROR unable to parse payload 'None': 'NoneType' object is not subscriptable
{'site': 0.0, 'solar': 0.0, 'battery': 0.0, 'load': 0.0}

pw.level()

DEBUG: -- local: Request Powerwall for /api/system_status/soe
DEBUG:https://192.168.91.1:443 "GET /api/system_status/soe HTTP/1.1" 404 19
ERROR:404 Powerwall API not found at https://192.168.91.1/api/system_status/soe

pw.alerts()

DEBUG:Get Status from Powerwall
DEBUG:Starting new HTTPS connection (1): 192.168.91.1:443
DEBUG:https://192.168.91.1:443 "POST /tedapi/v1 HTTP/1.1" 200 1716
DEBUG:Response Code: 200
DEBUG:Status: {'control': {'alerts': {'active': ...
DEBUG:Using Cached Payload
DEBUG: -- local: Request Powerwall for /api/system_status/grid_status
DEBUG:https://192.168.91.1:443 "GET /api/system_status/grid_status HTTP/1.1" 404 19
ERROR:404 Powerwall API not found at https://192.168.91.1/api/system_status/grid_status
['SelfConsumptionReservedLimit', 'SystemConnectedToGrid', 'FWUpdateSucceeded']

@jasonacox
Copy link
Owner

Thanks, @ttl74ls100 ! This is very helpful. I suspected we would see 404 for those calls but needed to confirm. I can build mappings from the tedapi to most of those calls. I'll work on something for you to try soon.

jasonacox added a commit that referenced this issue Jun 10, 2024
@jasonacox
Copy link
Owner

Hi @ttl74ls100 - something for you to try:

# First install the dev version
pip install pypowerwall==0.10.4.dev0

Simple Python test:

import pypowerwall

PW_GW_PWD="GWPASSWD"
pw = pypowerwall.Powerwall("192.168.91.1", gw_pwd=PW_GW_PWD)

pw.power()
pw.level()
pw.battery_blocks()

You can also try the pypowerwall proxy:

docker run \
    -p 8675:8675 \
    -e PW_PORT='8675' \
    -e PW_HOST='192.168.91.1' \
    -e PW_GW_PWD="GWPASSWD" \
    --name pypowerwall \
    --restart unless-stopped \
   jasonacox/pypowerwall:0.10.4t61-beta

Try these URLs:

@ttl74ls100
Copy link
Author

All the calls executed without error. I can post results if you want, I am leaving them out for brevity in case the actual values are not relevant.

image

🎉

@jasonacox
Copy link
Owner

jasonacox commented Jun 11, 2024

Fantastic!!! Thanks for testing! 🙏

  • Are you running the Powerwall-Dashboard? If so, you can use this container to get the extended local metrics (e.g. alerts/strings). My plan is to roll this out to that project and provide setup instructions for PW3 users.
  • How are you getting access to the Gateway IP? Are you connecting via WiFi or did you find it on your LAN and add a route?
  • You will notice that the Firmware data is wrong. The TEDAPI payloads for config or status do not contain the firmware number, at least from what I can tell. If anyone finds a way to pull this, let me know. There are a few other missing metrics but nothing too serious. The vitals data is rich with goodness.

PS - if you want to test this with Powerwall-Dashboard, the manual steps:

  1. Edit powerwall.yml and replace the pypowerwall image with jasonacox/pypowerwall:0.10.4t61-beta
  2. Edit pypowerwall.env and ensure it has this:
#PW_EMAIL=
#PW_PASSWORD=
PW_HOST=192.168.91.1
PW_GW_PWD=GWPASSWD
  1. Restart with ./compose-dash.sh up -d

@jasonacox
Copy link
Owner

Updated: the Powerwall-Dashboard will now provide an option to install for Powerwall 3 using the local gateway option. If you install and run setup.sh it will walk you through the process (hopefully) :) .

@ttl74ls100
Copy link
Author

I have a static route setup in the gateway/router (UniFi UDM) to the primary PW. The production Powerwall-Dashboard instance is running on a Proxmox VM and currently the curl -k https://192.168.91.1 test is timing out... Need to dig into the networking configs to see what is up there.

I am using WSL on a Windows machine for pypowerwall to PW3 testing.

I'll stand-up another instance of Powerwall-Dashboard using local access and let you know how it goes.

@ttl74ls100
Copy link
Author

Powerwall Dashboard is working with local PW3 w00t! Powerwall Capacity panel in Powerwall Vitals has no data. Energy Usage, Grid Status, Frequency, Voltages and Alerts working.

image

image

@jasonacox
Copy link
Owner

Looks great @ttl74ls100 !!

The Powerwall Capacity graph is puzzling. Can you show the results of http://localhost:8675/api/system_status ? It should have the capacity data. If it does check: http://localhost:8675/pod which is what the Dashboard is polling. It should list the different PW data points like PW2_POD_nom_full_pack_energy.

@jasonacox jasonacox changed the title Failed to Connect message for working connection - TEDAPI tool Powerwall 3 Local Access via TEDAPI Jun 12, 2024
@ttl74ls100
Copy link
Author

/api/system_status

{
  "command_source": "Configuration",
  "battery_target_power": 0,
  "battery_target_reactive_power": 0,
  "nominal_full_pack_energy": 28840,
  "nominal_energy_remaining": 10570,
  "max_power_energy_remaining": 0,
  "max_power_energy_to_be_charged": 0,
  "max_charge_power": null,
  "max_discharge_power": null,
  "max_apparent_power": null,
  "instantaneous_max_discharge_power": 0,
  "instantaneous_max_charge_power": 0,
  "instantaneous_max_apparent_power": 0,
  "hardware_capability_charge_power": 0,
  "hardware_capability_discharge_power": 0,
  "grid_services_power": null,
  "system_island_state": "SystemGridConnected",
  "available_blocks": 0,
  "available_charger_blocks": 0,
  "battery_blocks": [],
  "ffr_power_availability_high": 0,
  "ffr_power_availability_low": 0,
  "load_charge_constraint": 0,
  "max_sustained_ramp_rate": 0,
  "grid_faults": [],
  "can_reboot": "Yes",
  "smart_inv_delta_p": 0,
  "smart_inv_delta_q": 0,
  "last_toggle_timestamp": "2023-10-13T04:08:05.957195-07:00",
  "solar_real_power_limit": null,
  "score": 10000,
  "blocks_controlled": 0,
  "primary": true,
  "auxiliary_load": 0,
  "all_enable_lines_high": true,
  "inverter_nominal_usable_power": 0,
  "expected_energy_remaining": 0
}

/pod

{
  "time_remaining_hours": 11.0975932293044,
  "backup_reserve_percent": 33.5
}

@jasonacox
Copy link
Owner

Thanks @ttl74ls100 !

"battery_blocks": [],

Ugh, there is the problem. For previous Powerwalls, we have an entry there for each Powerwall. Can you look at http://localhost:8675/tedapi/status and see if you have entries for 'esCan' -> 'bus' -> 'THC'? That is what builds the battery_block data. I wonder if it is missing data or a code problem.

Also, the latest beta release I'm testing adds api locking which should help with overloading the /teadpi:

jasonacox/pypowerwall:0.10.5t61-beta8

It may also be good to see what you have in your logs:

docker logs pypowerwall

@ttl74ls100
Copy link
Author

Status has a batteryBlocks array:

 "batteryBlocks": [
      {
        "din": "1707000-25-G--TG1232151234VR",
        "disableReasons": null
      },
      {
        "din": "1707000-25-G--TG1232151234RL",
        "disableReasons": null
      }
    ]

Here is esCan.bus.THC:

        "THC": [
          {
            "THC_InfoMsg": {
              "THC_appGitHash": [0, 0, 0, 0, 0, 0, 0],
              "isComplete": false,
              "isMIA": true
            },
            "THC_Logging": {
              "THC_LOG_PW_2_0_EnableLineState": "ENABLE_LINE_LOW"
            },
            "packagePartNumber": "",
            "packageSerialNumber": ""
          },
          {
            "THC_InfoMsg": {
              "THC_appGitHash": [0, 0, 0, 0, 0, 0, 0],
              "isComplete": false,
              "isMIA": true
            },
            "THC_Logging": {
              "THC_LOG_PW_2_0_EnableLineState": "ENABLE_LINE_LOW"
            },
            "packagePartNumber": "",
            "packageSerialNumber": ""
          },
          {
            "THC_InfoMsg": {
              "THC_appGitHash": [0, 0, 0, 0, 0, 0, 0],
              "isComplete": false,
              "isMIA": true
            },
            "THC_Logging": {
              "THC_LOG_PW_2_0_EnableLineState": "ENABLE_LINE_LOW"
            },
            "packagePartNumber": "",
            "packageSerialNumber": ""
          },
          {
            "THC_InfoMsg": {
              "THC_appGitHash": [0, 0, 0, 0, 0, 0, 0],
              "isComplete": false,
              "isMIA": true
            },
            "THC_Logging": {
              "THC_LOG_PW_2_0_EnableLineState": "ENABLE_LINE_LOW"
            },
            "packagePartNumber": "",
            "packageSerialNumber": ""
          },
          {
            "THC_InfoMsg": {
              "THC_appGitHash": [0, 0, 0, 0, 0, 0, 0],
              "isComplete": false,
              "isMIA": true
            },
            "THC_Logging": {
              "THC_LOG_PW_2_0_EnableLineState": "ENABLE_LINE_LOW"
            },
            "packagePartNumber": "",
            "packageSerialNumber": ""
          },
          {
            "THC_InfoMsg": {
              "THC_appGitHash": [0, 0, 0, 0, 0, 0, 0],
              "isComplete": false,
              "isMIA": true
            },
            "THC_Logging": {
              "THC_LOG_PW_2_0_EnableLineState": "ENABLE_LINE_LOW"
            },
            "packagePartNumber": "",
            "packageSerialNumber": ""
          },
          {
            "THC_InfoMsg": {
              "THC_appGitHash": [0, 0, 0, 0, 0, 0, 0],
              "isComplete": false,
              "isMIA": true
            },
            "THC_Logging": {
              "THC_LOG_PW_2_0_EnableLineState": "ENABLE_LINE_LOW"
            },
            "packagePartNumber": "",
            "packageSerialNumber": ""
          },
          {
            "THC_InfoMsg": {
              "THC_appGitHash": [0, 0, 0, 0, 0, 0, 0],
              "isComplete": false,
              "isMIA": true
            },
            "THC_Logging": {
              "THC_LOG_PW_2_0_EnableLineState": "ENABLE_LINE_LOW"
            },
            "packagePartNumber": "",
            "packageSerialNumber": ""
          },
          {
            "THC_InfoMsg": {
              "THC_appGitHash": [0, 0, 0, 0, 0, 0, 0],
              "isComplete": false,
              "isMIA": true
            },
            "THC_Logging": {
              "THC_LOG_PW_2_0_EnableLineState": "ENABLE_LINE_LOW"
            },
            "packagePartNumber": "",
            "packageSerialNumber": ""
          },
          {
            "THC_InfoMsg": {
              "THC_appGitHash": [0, 0, 0, 0, 0, 0, 0],
              "isComplete": false,
              "isMIA": true
            },
            "THC_Logging": {
              "THC_LOG_PW_2_0_EnableLineState": "ENABLE_LINE_LOW"
            },
            "packagePartNumber": "",
            "packageSerialNumber": ""
          }
        ]

docker logs pypowerwall

06/13/2024 06:54:04 PM [proxy] [INFO] pyPowerwall [0.10.5] Proxy Server [t61] - HTTP Port 8675
06/13/2024 06:54:04 PM [proxy] [INFO] pyPowerwall Proxy Started
06/13/2024 06:54:04 PM [proxy] [INFO] pyPowerwall Proxy Server - Local Mode
06/13/2024 06:54:04 PM [proxy] [INFO] Connected to Energy Gateway 192.168.91.1 (JC)
06/13/2024 06:54:04 PM [proxy] [INFO] TEDAPI Mode Enabled for Device Vitals (full)

@jasonacox
Copy link
Owner

Thank you @ttl74ls100 - Sadly that is what I feared. The energy levels are not showing for Powerwall 3s. However, I can pull the aggregates and include them in the /pod API. Update to:

jasonacox/pypowerwall:0.10.5t62-beta

Edit the "Powerwall Capacity" graph you can add the total battery capacity and remaining metrics, you can create these queries:

image

If you have a chance to try it, let me know how it work. Thanks again for all your help!

@jasonacox
Copy link
Owner

Hi @ttl74ls100 - One thing we are missing for PW3 is the Firmware Version. It is not included in the TEDAPI payloads. Could you run this test for us? I suspect it is still a 404, but I would like to verify.

curl -ki https://192.168.91.1/api/status

If anyone else finds an API to get the firmware version of a PW3, let us us know.

@ttl74ls100
Copy link
Author

404

@jasonacox
Copy link
Owner

404

Thanks @ttl74ls100 ! Bummer, but as I expected. I'll keep hunting... :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants