pagraham.co.uk

Home energy · Node-RED

My Tesla Powerwall flow, and getting Tesla sign-in to work from a home server

A small industrial computer in the house runs Node-RED. It reads my Powerwall and solar through the Tesla Fleet API, works out what the day cost on my Octopus tariff, messages my phone on Telegram, and keeps itself signed in to Tesla, even after a power cut. Alongside it, an Octopus Energy flow decides when electricity is free or should be avoided, and both the Powerwall and my Zappi car charger follow it.

Runs on
Siemens IOT2050, Debian, Node-RED
Talks to
Tesla Fleet API, energy endpoints
Alerts
Telegram bot
Public address
energy.pagraham.co.uk, via Cloudflare Tunnel

Want to build it yourself? Step-by-step guide →

How the Powerwall flow fits together The Powerwall reports to Tesla's cloud. Node-RED on a Siemens IOT2050 at home polls the Fleet API, saves its state to disk and sends Telegram messages. Tesla and my browser reach Node-RED only through a Cloudflare Tunnel on energy.pagraham.co.uk, which lets through three paths. Powerwall + solar at home Tesla cloud Fleet API (EU region) Node-RED Siemens IOT2050 Telegram alerts to phone poll data energy.pagraham.co.uk Cloudflare Tunnel 3 paths only, no open ports /data/tesla config + saved state survives power cuts key check, sign-in redirect saves
Solid lines run all the time. Dashed lines are only used when Tesla checks the key or when I sign in.

What it does

  • Live status every 10 minutes. Solar, home load, battery level and grid flow. That's often enough for a house, and keeps API use well inside Tesla's limits.
  • Solar alerts. A message when the panels start producing in the morning and another when they finish, each once a day, with the date and time.
  • Daily summaries. At 12:00 and 21:00 a "so far today", then a final summary at 23:55: solar made and where it went, battery charged and discharged, home use, grid import and export, and the day's net cost.
  • Real costs. Import and export are priced on my Octopus rates (Intelligent Octopus Go and Outgoing), plus the standing charge. The rates are copied from Octopus automatically, so a tariff change needs no edits.
  • Billing period totals. Each final day is stored, so the flow can total up the bill from the 22nd of the month, and fill in any days it missed.
  • Export control. Buttons to switch the Powerwall between exporting solar only, or battery and solar.
  • Night schedule. On Intelligent Octopus Go the battery fills to 100 % in the cheap 23:30 to 05:30 hours, runs the house through the day, and in the evening exports what's left down to 20 %, in a window sized from the battery level, so it starts the night empty and refills cheaply. It only exports when that pays.
  • Charging with the car. When Octopus schedules the car to charge outside the night hours, the whole house gets the cheap rate, so the Powerwall charges too while the car is drawing power.
  • Octopus control. When Octopus electricity is free, the Powerwall charges from the grid. During a Saving Session it runs the house from the battery. It remembers its previous settings, even across a restart, puts them back afterwards, and each part can be paused with its own switch.
  • Back after a power cut. When the box restarts it restores everything and says so on Telegram.

Octopus decides, the Powerwall and the Zappi follow

The Powerwall flow is one of three on the same box. An Octopus Energy flow works out every minute whether electricity is free, expensive or should be avoided. The Powerwall and the myenergi Zappi car charger react to that, and all three report to Telegram.

Octopus saysWhenPowerwallZappi
FreeFree Electricity session, booked Weekend Happy Hour, power-up, or a zero or negative priceCharges from the grid to 100 %Fast charge, even if I'm away
Power-downA Saving Session the flow has joined for meRuns the house from the batteryLeft to its schedule
CheapThe 23:30 to 05:30 off-peak, or a slot when Octopus is charging the carCharges from the grid to 100 %Charges on Octopus's schedule
NormalAny other timeRuns the house; just before the off-peak, exports down to 20 %Left to its schedule

When a free period, session or cheap slot ends, the Powerwall's previous settings are put back. The same flows handle Flux (with its 16:00 to 19:00 peak) without changes, and stand aside on Intelligent Flux, where Octopus runs the battery itself.

Octopus Energy flow

  • Tariff and rates. Reads my account and active tariffs every six hours, and works out from the prices themselves which periods are off-peak or peak, rather than hard-coding times.
  • Smart charging. Every five minutes it reads the charging schedule Octopus has planned for the car, so the Powerwall can charge in the same cheap slots.
  • Free Electricity. Checks for new Free Electricity sessions every 30 minutes and tells me once about each one. At 20:00 it sends an outlook: free and negative-price windows, the cheapest and peak times, and what's coming up in the next week.
  • Saving Sessions. Finds Octopus power-down events and joins them automatically, then lets me know.
  • Live demand. Reads my Octopus Home Mini every minute for how much the house is importing or exporting right now.
  • Siemens Insights Hub. Every evening it prices yesterday's half-hourly import, export and gas at the rates that applied at the time, and writes them to Insights Hub alongside the industrial IoT data I work with.

Zappi EV charger flow

  • Checks every five minutes, and straight away when Octopus changes mode, then only sends the charger a command if its mode actually needs to change.
  • Leaves normal charging to its scheduler and only steps in for: a manual override, a Telegram boost, free electricity (Fast charge, home or away), or a Telegram free hold that keeps the car waiting for the next free session.
  • Manual override. One tap holds Fast, Eco, Eco+ or Stop for three hours, then the schedule takes over again.
  • Energy reporting. Tracks how much went into the car, split between solar-only and fast charging. A daily summary covers the day, week, month and billing period, and missed days are filled in automatically.
  • Quiet by default. Telegram only hears about Octopus-driven changes and failures, not the routine daily switching.

Why sign-in is the hard part

Tesla's Fleet API is built for companies with a website and a server, not one person with a box in the garage. You need a registered developer app and a domain you control, which Tesla uses twice:

  1. It downloads a public key from a fixed address on the domain when you register.
  2. After you sign in, it sends you back to a redirect address on that domain with a one-time code.

Node-RED sits behind my home router with no public address. Rather than open ports, a Cloudflare Tunnel connects outwards from the box and gives it the name energy.pagraham.co.uk, with a proper HTTPS certificate. The tunnel only lets three addresses through: the public key, the login link and the sign-in callback. Everything else, including the Node-RED editor, stays private.

How signing in works

  1. I open a private login link. It carries a long random key, so nobody else can use it to link their own Tesla account in place of mine. Without the key it just says "Forbidden".
  2. Node-RED sends me to Tesla. It adds a one-time check value, valid for ten minutes, so a stale or forged reply is rejected.
  3. I sign in and approve access to energy data and energy controls.
  4. Tesla sends me back to Node-RED through the tunnel. Node-RED checks the value, swaps the code for tokens, saves them, and messages me that it's linked.

That's needed once. After that the flow keeps itself signed in.

Keeping it signed in

Tesla's access tokens last eight hours. Every five minutes the flow checks how long is left and refreshes once it's under half an hour. If a request is ever refused, it refreshes and tries again once. Two of Tesla's rules catch people out:

  • Refresh tokens are single use. Each refresh returns a new one and the old one stops working, so the new one is saved before anything else happens.
  • Unused tokens expire. Refreshing regularly keeps the chain alive indefinitely.

If a refresh ever fails, I get one Telegram message a day asking me to sign in again.

Surviving power cuts

Losing the tokens means signing in again, and losing the daily totals breaks the billing figures. So state isn't just held in memory:

  • It's written to a temporary file, flushed to disk, and only then swapped in, with the previous copy kept as a backup. A power cut in the middle of a save can't corrupt it.
  • It saves on every token refresh, after each daily summary and every 15 minutes.
  • On start-up it loads the saved file, falling back to the backup if needed, then carries on and tells me it's back.

Keeping secrets out of the flow

The Tesla client secret, Telegram bot token and login key live in one config file on the box, readable only by the system. The flow itself contains none of them, so I can export it, back it up to a private GitHub repo or share it without leaking anything. A small script checks for anything that looks like a key before every commit.

The Tesla calls it uses

CallUsed for
GET /api/1/energy_sites/{id}/live_statusSolar, load, battery and grid, every 10 minutes
GET /api/1/energy_sites/{id}/calendar_historyEach day's energy, for summaries and billing
POST /api/1/energy_sites/{id}/grid_import_exportExport rule, and allowing grid charging during free periods
POST /api/1/energy_sites/{id}/backupBackup reserve: 100 % in cheap or free hours, 20 % before the off-peak, low during a Saving Session
POST /api/1/energy_sites/{id}/time_of_use_settingsGive the Powerwall a copy of the Octopus tariff, with the evening export window
POST /api/1/energy_sites/{id}/operationTime-Based Control, so the Powerwall follows that tariff
GET /api/1/energy_sites/{id}/site_infoRemember the reserve before changing it
GET /api/1/productsFind the energy site (setup only)
POST /api/1/partner_accountsRegister the domain with Tesla (setup only)

All calls go to the EU address, https://fleet-api.prd.eu.vn.cloud.tesla.com.

Build your own

The home energy build guide covers the whole thing step by step: the box, the tunnel, Telegram, all three flows, start-up on boot and a test checklist, with every account detail replaced by an example.

Lessons learned

  • No "tesla" in the domain. Tesla's portal rejects any domain containing the word, so tesla.example.com won't do. I use energy..
  • Get HTTPS working first. Tesla checks the domain already has a valid certificate before it accepts it.
  • Node-RED already uses /auth/. Its own admin login lives there, so my sign-in pages use /powerwall/login and /powerwall/callback.
  • UK accounts are in the EU region. The North America address gives confusing errors.
  • Poll politely. Tesla charges for data requests and limits energy sites to one every five minutes. Every ten minutes is about 4,300 requests a month, which fits inside Tesla's monthly allowance for personal use.
  • Times are UK time. Summaries and peak pricing follow the clocks going back and forward, whatever time zone the box itself is set to.