Skip to content

Commit

Permalink
fix(api): fixes for tesla 20.49 API changes
Browse files Browse the repository at this point in the history
With the 20.49 firmware update by Tesla, the endpoints that previously
were open are no longer without authentication. Unfortuantely, the
mechanism used for authentication is not HTTP basic, rather it's cookie
based. This makes it so a process runs in the background to ensure that
you always stay logged in and then executes the queries against the
endpoints with the login cookie.

However, for some reason cURL was just ignoring the cookie file on my
machine, so I had to do some extra hacks to make it pass the cookies in
on the command line. Also, this requires cron, which means by that point
I might as well build a new docker container for this process.

Fixes mihailescu2m#14

DCO 1.1 Signed-off-by: Patrick Wagstrom <[email protected]>
  • Loading branch information
pridkett committed Feb 22, 2021
1 parent baa44cc commit 6fa08fa
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 83 deletions.
142 changes: 71 additions & 71 deletions dashboard.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion powerwall.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ services:
mode: host

telegraf:
image: telegraf:1.12
image: telegraf
build: telegraf
container_name: telegraf
hostname: telegraf
restart: always
Expand All @@ -33,6 +34,8 @@ services:
- "powerwall: 192.168.91.1"
depends_on:
- influxdb
env_file:
- .env.telegraf

grafana:
image: grafana/grafana:6.5.1-ubuntu
Expand Down
11 changes: 7 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Monitoring the Tesla Powerwall with the TICK framework

## Installation
* edit `powerwall.yml` and replace `192.168.91.1` with your powerwall IP
* create a file called `.env.telegraf` to define the `POWERWALL_PASSWORD` which
is used for authentication. This should be a single line that looks like this:
POWERWALL_PASSWORD=YourPowerwallPassword
* start the docker containers: `docker-compose -f powerwall.yml up -d`
* connect to the Influx database shell: `docker exec -it influxdb influx`
* at the database prompt, enter the following commands:
Expand All @@ -19,10 +22,10 @@ Monitoring the Tesla Powerwall with the TICK framework
CREATE RETENTION POLICY kwh ON powerwall duration INF replication 1
CREATE RETENTION POLICY daily ON powerwall duration INF replication 1
CREATE RETENTION POLICY monthly ON powerwall duration INF replication 1
CREATE CONTINUOUS QUERY cq_autogen ON powerwall BEGIN SELECT mean(home) AS home, mean(solar) AS solar, mean(from_pw) AS from_pw, mean(to_pw) AS to_pw, mean(from_grid) AS from_grid, mean(to_grid) AS to_grid, last(percentage) AS percentage INTO powerwall.autogen.:MEASUREMENT FROM (SELECT load_instant_power AS home, solar_instant_power AS solar, abs((1+battery_instant_power/abs(battery_instant_power))*battery_instant_power/2) AS from_pw, abs((1-battery_instant_power/abs(battery_instant_power))*battery_instant_power/2) AS to_pw, abs((1+site_instant_power/abs(site_instant_power))*site_instant_power/2) AS from_grid, abs((1-site_instant_power/abs(site_instant_power))*site_instant_power/2) AS to_grid, percentage FROM raw.http) GROUP BY time(1m), month, year fill(linear) END
CREATE CONTINUOUS QUERY cq_kwh ON powerwall RESAMPLE EVERY 1m BEGIN SELECT integral(home)/1000/3600 AS home, integral(solar)/1000/3600 AS solar, integral(from_pw)/1000/3600 AS from_pw, integral(to_pw)/1000/3600 AS to_pw, integral(from_grid)/1000/3600 AS from_grid, integral(to_grid)/1000/3600 AS to_grid INTO powerwall.kwh.:MEASUREMENT FROM autogen.http GROUP BY time(1h), month, year tz('Australia/Adelaide') END
CREATE CONTINUOUS QUERY cq_daily ON powerwall RESAMPLE EVERY 1h BEGIN SELECT sum(home) AS home, sum(solar) AS solar, sum(from_pw) AS from_pw, sum(to_pw) AS to_pw, sum(from_grid) AS from_grid, sum(to_grid) AS to_grid INTO powerwall.daily.:MEASUREMENT FROM powerwall.kwh.http GROUP BY time(1d), month, year tz('Australia/Adelaide') END
CREATE CONTINUOUS QUERY cq_monthly ON powerwall RESAMPLE EVERY 1h BEGIN SELECT sum(home) AS home, sum(solar) AS solar, sum(from_pw) AS from_pw, sum(to_pw) AS to_pw, sum(from_grid) AS from_grid, sum(to_grid) AS to_grid INTO powerwall.monthly.:MEASUREMENT FROM powerwall.daily.http GROUP BY time(365d), month, year END
CREATE CONTINUOUS QUERY cq_autogen ON powerwall BEGIN SELECT mean(home) AS home, mean(solar) AS solar, mean(from_pw) AS from_pw, mean(to_pw) AS to_pw, mean(from_grid) AS from_grid, mean(to_grid) AS to_grid, last(percentage) AS percentage INTO powerwall.autogen.:MEASUREMENT FROM (SELECT load_instant_power AS home, solar_instant_power AS solar, abs((1+battery_instant_power/abs(battery_instant_power))*battery_instant_power/2) AS from_pw, abs((1-battery_instant_power/abs(battery_instant_power))*battery_instant_power/2) AS to_pw, abs((1+site_instant_power/abs(site_instant_power))*site_instant_power/2) AS from_grid, abs((1-site_instant_power/abs(site_instant_power))*site_instant_power/2) AS to_grid, percentage FROM raw.exec) GROUP BY time(1m), month, year fill(linear) END
CREATE CONTINUOUS QUERY cq_kwh ON powerwall RESAMPLE EVERY 1m BEGIN SELECT integral(home)/1000/3600 AS home, integral(solar)/1000/3600 AS solar, integral(from_pw)/1000/3600 AS from_pw, integral(to_pw)/1000/3600 AS to_pw, integral(from_grid)/1000/3600 AS from_grid, integral(to_grid)/1000/3600 AS to_grid INTO powerwall.kwh.:MEASUREMENT FROM autogen.exec GROUP BY time(1h), month, year tz('Australia/Adelaide') END
CREATE CONTINUOUS QUERY cq_daily ON powerwall RESAMPLE EVERY 1h BEGIN SELECT sum(home) AS home, sum(solar) AS solar, sum(from_pw) AS from_pw, sum(to_pw) AS to_pw, sum(from_grid) AS from_grid, sum(to_grid) AS to_grid INTO powerwall.daily.:MEASUREMENT FROM powerwall.kwh.exec GROUP BY time(1d), month, year tz('Australia/Adelaide') END
CREATE CONTINUOUS QUERY cq_monthly ON powerwall RESAMPLE EVERY 1h BEGIN SELECT sum(home) AS home, sum(solar) AS solar, sum(from_pw) AS from_pw, sum(to_pw) AS to_pw, sum(from_grid) AS from_grid, sum(to_grid) AS to_grid INTO powerwall.monthly.:MEASUREMENT FROM powerwall.daily.exec GROUP BY time(365d), month, year END
```
* open up Grafana in the browser at `http://<server ip>:9000` and login with `admin/admin`
* from `Configuration\Data Sources`, add `InfluxDB` database with:
Expand Down
24 changes: 17 additions & 7 deletions telegraf.conf
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,26 @@
skip_database_creation = false
retention_policy = "raw"

[[inputs.http]]
urls = [
"https://powerwall/api/meters/aggregates",
"https://powerwall/api/system_status/soe"

[[inputs.exec]]
commands = [
"/get_aggregates.sh",
"/get_soe.sh"
]
method = "GET"
insecure_skip_verify = true
timeout = "4s"
## Timeout for each command to complete.
timeout = "5s"
data_format = "json"

# [[inputs.http]]
# urls = [
# "https://powerwall/api/meters/aggregates",
# "https://powerwall/api/system_status/soe"
# ]
# method = "GET"
# insecure_skip_verify = true
# timeout = "4s"
# data_format = "json"

[[processors.date]]
tag_key = "month"
date_format = "Jan"
Expand Down
6 changes: 6 additions & 0 deletions telegraf/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM telegraf:1.17

RUN apt-get update
RUN apt-get install -y cron

COPY entrypoint.sh get_aggregates.sh get_soe.sh /
21 changes: 21 additions & 0 deletions telegraf/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

# this file supersedes the original entrypoint.sh for telegraf containers
# mainly because we need to fire off a cron job that runs every 20 minutes

# see: https://github.com/mihailescu2m/powerwall_monitor/issues/14#issuecomment-778478572

mkdir -p /tmp/cookies
CURL_CMD="curl -s -k -i -c /tmp/cookies/pw.txt -X POST -H 'Content-Type: application/json' -d '{\"username\":\"customer\",\"password\":\"$POWERWALL_PASSWORD\",\"force_sm_off\":false}' https://powerwall/api/login/Basic"
CRON_CMD="*/20 * * * * $CURL_CMD"
eval $CURL_CMD

( crontab -l 2>/dev/null | grep -Fv powerwall ; printf -- "$CRON_CMD\n" ) | crontab

set -e

if [ "${1:0:1}" = '-' ]; then
set -- telegraf "$@"
fi

exec "$@"
2 changes: 2 additions & 0 deletions telegraf/get_aggregates.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
curl -k --cookie $(cat /tmp/cookies/pw.txt | tail -n 2 | cut -d $'\t' -f 6,7 | sed -e 's/\t/=/g' | tr '\n' ';') 'https://powerwall/api/meters/aggregates'
2 changes: 2 additions & 0 deletions telegraf/get_soe.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
curl -k --cookie $(cat /tmp/cookies/pw.txt | tail -n 2 | cut -d $'\t' -f 6,7 | sed -e 's/\t/=/g' | tr '\n' ';') 'https://powerwall/api/system_status/soe'

0 comments on commit 6fa08fa

Please sign in to comment.