Skip to content
mehdirahbePublic

About

Python/django website using tesla api to connect to your marvellous Tesla car

Resources

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

matesla

Python/Django website using the Tesla API to connect to your marvellous Tesla car.

Useful files:

  1. Procedures calling the Tesla API (extend here if something is missing):
    matesla/TeslaConnect.py
  2. Views that return the HTML shown in the browser:
    matesla/views.py
  3. URL routes (path, view, and reverse name used from templates):
    matesla/urls.py
  4. Car status page template:
    matesla/templates/matesla/carstatus.html
  5. Base layout and shared CSS hooks:
    templates/base.html

How to:

  1. To display extra live fields on the status page, adapt _carstatus_body.html (and PreparestatusDictionary if the value is computed).
  2. Change look: adapt CSS under static/ / templates.

Vehicle commands (lock, climate, charge start, …) were removed: modern cars require Tesla’s Vehicle Command Protocol; the official app covers remote control. matesla focuses on status, history, maps, and stats.

Todo:

  1. Improve look of AddTeslaAccount form
  2. Allow to set EPA range
  3. Allow to change password / recover features if password is lost
  4. Add more languages. If you are a native speaker, please don't hesitate to add a new language. No need to be a programmer to do that — ask for a prepared text file and translate it.

Install on Linux (Ubuntu / Debian-like)

One script: venv, dependencies, database, static files, systemd service at boot, capture cron (history every minute), and a menu/desktop shortcut that opens the browser.

Prerequisites (once per machine):

sudo apt update
sudo apt install -y python3 python3-venv python3-pip curl
# optional (weekly DB backups): sudo apt install -y zstd

Install (from the matesla folder — clone or unzip):

cd /path/to/matesla
./scripts/install-linux.sh

Then open http://127.0.0.1:8001 (or use the MaTesla menu / desktop icon).

What the install enables for history:

Piece Role
systemd / gunicorn App always listening on 127.0.0.1:8001
user crontab (every minute) curl → /matesla/internal/capture (adaptive Fleet spacing; after a Supercharge also pulls Tesla charging-history invoices)
user crontab (20:00) manage.py FetchEliaDayAhead (Belgian day-ahead cache for home dynamic €)
log /tmp/matesla-capture.log, /tmp/matesla-elia.log

Without the capture cron, status works but graphs stay empty.

Options:

Flag Effect
--no-service Skip systemd (venv + migrate only; start manually)
--no-cron Skip history capture crontab (not recommended)
--with-backup Also install the weekly SQLite backup timer
--no-desktop Do not create menu / desktop launcher

Useful commands after install:

systemctl status matesla-gunicorn.service
sudo systemctl stop matesla-gunicorn.service     # free port for runserver
sudo systemctl start matesla-gunicorn.service
journalctl -u matesla-gunicorn.service -f
crontab -l
tail -f /tmp/matesla-capture.log
tail -f /tmp/matesla-elia.log
./scripts/install_capture_cron.sh                # re-add capture cron alone
./scripts/uninstall_capture_cron.sh
./scripts/install_elia_cron.sh                   # evening Elia day-ahead fetch
./scripts/uninstall_elia_cron.sh
./config/uninstall_gunicorn_service.sh           # remove service completely

The systemd unit is generated for your user and install path. Re-run ./config/install_gunicorn_service.sh after moving the project directory.

Tesla Fleet credentials (.env or in-app setup) still require a Tesla Developer account — matesla cannot skip that step.

Reverse geocoding (addresses on day map / drives) — optional Geoapify

By default, matesla reverse-geocodes lat/lon via public Nominatim (~1 request/s, self-imposed daily cap). That works with no API key.

For faster label fill-in and a quicker address backlog, you can use Geoapify (free tier: 3 000 credits/day, ~5 req/s, no credit card for the free plan). Reverse geocode = 1 credit.

Setup (optional)

  1. Create a free account at geoapify.com → API key.

  2. Put the key in a local .env at the project root (gitignored; never commit):

    # .env  (same file as Tesla credentials is fine)
    GEOAPIFY_API_KEY=your_key_here

    Alias GEOAPIFY_KEY is also accepted.

  3. Restart the app so gunicorn reloads env:

    sudo systemctl restart matesla-gunicorn.service
    # or restart runserver
  4. Check:

    .venv/bin/python -c "
    import django, os
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
    django.setup()
    from matesla.models.AddressFromLatLong import active_geocoder
    print(active_geocoder())  # expect: geoapify
    "

Without a key, behaviour falls back to Nominatim automatically.

On the free plan, the site footer shows Powered by Geoapify and © OpenStreetMap when Geoapify is active (provider ToS).

Backup the key outside the project tree

.env is not in git. Treat the key like Tesla secrets:

  • Keep the live .env on the machine that runs matesla.
  • Copy a backup off-site (USB, password manager, or next to your DB archives in Dropbox/cloud — not inside the git working tree, and not committed). Example: same Dropbox folder as matesla-db-*.sqlite3.zst backups, e.g. matesla-secrets.env.
  • Do not put secrets only inside db-backups/ if that folder is ever shared or published; prefer a dedicated private backup location.

Do not backup .venv (reinstall with pip install -r requirements.txt).

For developers (manual run, no install script)

Python 3.12+, Django 5.2 LTS:

  1. python3 -m venv .venv && source .venv/bin/activate
  2. pip install -U pip && pip install -r requirements.txt
  3. Optional: Tesla Fleet + Geoapify keys in a .env file (see above and settings.py) — backup .env off-site, never commit it
  4. python manage.py migrate # SQLite by default (db.sqlite3)
  5. python manage.py createsuperuser # optional
  6. python manage.py test
  7. python manage.py collectstatic --noinput # needed when DEBUG is off
  8. python manage.py runserver 127.0.0.1:8001
    or systemd only: ./config/install_gunicorn_service.sh

DEBUG is off by default (no Django debug toolbar). To re-enable:

export DJANGO_DEBUG=1
python manage.py runserver 127.0.0.1:8001

Access MaTesla on your phone via Tailscale (HTTPS)

Goal: phone and laptop on the same Tailscale tailnet, with MaTesla reachable over HTTPS without opening the home router.

Default ports used in the examples below:

HTTPS (Tailscale Serve) Local backend App
8443 http://127.0.0.1:8001 MaTesla

You can use port 443 instead if nothing else on the machine already uses Tailscale Serve on 443.

1. Run MaTesla locally

Prefer ./scripts/install-linux.sh (systemd on 127.0.0.1:8001).
Restart=no: if you stop or kill the service to free the port for runserver, it stays down until the next boot (or systemctl start).

# already installed via install-linux.sh, or:
./config/install_gunicorn_service.sh
systemctl status matesla-gunicorn.service

Manual dev server:

cd /path/to/matesla
source .venv/bin/activate
python manage.py collectstatic --noinput
# sudo systemctl stop matesla-gunicorn.service   # if daemon holds the port
python manage.py runserver 127.0.0.1:8001

Database backups (full history, compressed)

Minute-level history (TeslaFi + capture) is irreplaceable — backups are full SQLite snapshots, not thinned exports. Archives live next to the live DB in db-backups/ (gitignored). MaTesla never uploads backups; copy those files yourself to USB / cloud storage. Also back up your .env (Tesla + optional Geoapify keys) to a private off-site place — see reverse-geocoding section above.

Format sqlite3.backup then zstd -3 (typically ~8× smaller)
Cadence weekly (timer) or manual; skip if last archive < 6 days (unless FORCE=1)
Retention 4 newest matesla-db-*.sqlite3.zst only
# one-shot
./scripts/backup_db.sh
FORCE=1 ./scripts/backup_db.sh   # ignore 6-day gap

# automatic (recommended): user crontab — e.g. Sunday 03:30
# 30 3 * * 0 /path/to/matesla/scripts/backup_db.sh >> /tmp/matesla-db-backup.log 2>&1
crontab -l | grep backup_db

# alternative: systemd timer (optional, needs sudo + zstd)
# ./config/install_db_backup_timer.sh
# or: ./scripts/install-linux.sh --with-backup

Restore example:

zstd -d db-backups/matesla-db-YYYY-MM-DD.sqlite3.zst -o /tmp/matesla-restore.sqlite3
# stop web first, then replace live DB carefully
sudo systemctl stop matesla-gunicorn.service
cp db.sqlite3 "db.sqlite3.before-restore-$(date +%F)"
cp /tmp/matesla-restore.sqlite3 db.sqlite3
sudo systemctl start matesla-gunicorn.service

2. Configure Tailscale Serve

Prefer making your user the Tailscale operator once:

sudo tailscale set --operator=$USER

Then either run the helper:

./scripts/tailscale-serve-matesla.sh

Or manually:

# MaTesla on HTTPS 8443 → local 8001
tailscale serve --bg --yes --https=8443 http://127.0.0.1:8001

tailscale serve status

Open on the phone (Tailscale app connected, Use Tailscale DNS / MagicDNS on):

https://<your-machine>.<tailnet>.ts.net:8443/

Use a language-prefixed path if you prefer (e.g. /fr/, /en/, …).

Read-only on Tailscale (HTTPS remote Host)

Write is only allowed when the HTTP Host is local (127.0.0.1 / localhost). Access via the MagicDNS name (*.ts.net) is read-only and does not require a MaTesla login:

  • Status, personal stats, day map, vehicle switcher: yes (anonymous viewers use the household owner that holds the Tesla token)
  • Tesla account / OAuth, signup, admin: no (UI hidden; direct URLs return 404)
  • Vehicle remote commands are not supported (use the official Tesla app; Fleet Vehicle Command Protocol is out of scope for matesla)
  • Local http://127.0.0.1:8001 still needs login for setup / full access

Override if needed:

export MATESLA_WRITABLE_HOSTS=127.0.0.1,localhost
# Optional if several Django users exist:
export MATESLA_OWNER_USERNAME=you@example.com

3. ACL: allow port 8443

If the site works on the laptop via MagicDNS but not on the phone, check Tailscale Access controls. A rule that only allows port 443 (e.g. "ip": ["443"]) will block 8443 on other devices. Allow both ports if you also use 443, for example:

{
  "src": ["*"],
  "dst": ["*"],
  "ip": ["443", "8443"]
}

(Adapt to your ACL format / node names if you use stricter dst.)

Ping between phone and laptop can still work when only 443 is allowed (ICMP); HTTPS on 8443 will not.

4. Django hosts / CSRF

mysite/settings.py may need your MagicDNS host for HTTPS. If Django rejects the Host header or CSRF fails, set:

export DJANGO_ALLOWED_HOSTS=your-host.tailnet.ts.net
export DJANGO_CSRF_TRUSTED_ORIGINS=https://your-host.tailnet.ts.net:8443

5. Reset / stop Serve

# Remove only MaTesla HTTPS binding on 8443
tailscale serve --https=8443 off

# Or wipe all Serve config on this node
# tailscale serve reset

TeslaFi-style history (online cars only, never wakes)

When a vehicle is already online, capture full vehicle_data into TeslaCarDataSnapshot (graphs / personal stats). Offline/asleep cars are skipped. The app never sends Fleet API wake commands (use the official Tesla app if you need to wake a car).

Adaptive spacing (cron can stay every minute)

Cron may hit the endpoint every minute; Fleet is only called when due. Spacing uses Europe/Brussels wall clock + last known activity (TeslaVehicle.state + latest snapshot: speed / shift / charging):

Context Interval
Driving, final approach (ETA ≤ 3 min) or Supercharger dest ≤ ~12 min 1 min
Driving (near / no ETA / crawl) 2 min
Driving mid-trip / long ETA (nav) 5 / 10 min
DC / Supercharger (day or night) 1 min
Night 22:00–06:00, not drive/DC 30 min (incl. AC wall charge)
Day, user present / dog / camp / climate on 2 min
Day, Sentry only 5 min
Day, online but no cabin/sentry signal 5 min
Day, AC charging 15 min
Day, asleep / offline 5 min

Tesla AC wall / mobile connectors report fast_charger_type MCSingleWireCAN or ACSingleWireCAN. Those are not DC — they use the AC 15 min day / 30 min night spacing. Combo / Supercharger / fast_charger_present keep the 1 min DC cadence.

If no vehicle is due for a user, no /vehicles list call is made. Each vehicle stores last_polled_at after a real poll attempt. JSON stats include skipped_wait when the policy defers the car.

The latest snapshot only chooses the next wait (AC → 15 min day, etc.). Flags from a snapshot older than 20 minutes are ignored, and list asleep wins over an old “Charging” flag. List offline still triggers vehicle_data (Fleet offline is unreliable); only explicit asleep skips it.

Adaptive poll spacing (Fleet cost)

matesla/capture.py still uses a reactive baseline (drive 2 min by default, or 5–10 min mid-trip when Fleet reports a long navigation ETA; DC 1 min, AC 15 min day, night idle 30 min, day idle 5 min). Final approach (ETA ≤ 3 min) or a Supercharger-like nav destination with ETA ≤ ~12 min densifies to 1 min; near arrival (ETA ≤ ~12 min), crawl (speed < 20 mph), or missing ETA uses 2 min. Trip-seal after a drive sample reuses the same ETA-aware interval.

On top of that, matesla/poll_habits.py may set idle/asleep spacing for the current weekday+hour to:

Habit class Idle interval Effect vs baseline
busy 5 min denser than night 30 (e.g. night driver)
moderate 15 min residual default when trusted (day); night keeps 30
quiet 30 min strict only — sparser than day 5

When the model is trusted, every weekday+hour is one of these three (no idle “baseline holes”). Busy and quiet stay selective; everything else is moderate. A habit interval replaces the idle baseline (not max()). Live drive/charge/cabin never use habits.

Conditions: last ~12 weeks only, ≥2 reference weeks, no regime break (school ↔ holidays ↔ trips).
Diagnose: python manage.py ShowPollHabits --force, or the personal-stats page Polling details (link next to the Fleet poll cost graph) — same logic via matesla/poll_diagnostics.py.

Why not manage.py from cron?

SQLite does not like two Django processes writing at once (web on port 8001 plus a second python manage.py …). Prefer:

  1. One web process only, bound to localhost, e.g.
    gunicorn mysite.wsgi --bind 127.0.0.1:8001 --workers 1 --threads 4
    (or runserver 8001 for dev). Threads keep the UI responsive during Fleet I/O; a process-wide lock refuses a second /internal/capture if one is already running (cron overlap).
  2. Cron that only runs curl against that process (capture runs inside the web process → same DB connection)

Manual one-shot when the site is stopped is still fine:

python manage.py TakeTeslaCarDataSnapshot

Capture HTTP endpoint

URL http://127.0.0.1:8001/matesla/internal/capture
Methods GET or POST
Auth none (intended for localhost-only; do not expose this port to the internet)
Response JSON, e.g. {"saved":1,"skipped_offline":0,"skipped_wait":2,"skipped_error":0,"token_error":0,"fleet_limit":0}

saved = snapshotted; skipped_offline = list said not online; skipped_wait = deferred by adaptive interval (no Fleet call for that car).

Quick test in a browser or shell (site must be running):

curl -fsS http://127.0.0.1:8001/matesla/internal/capture

Schedule with cron (Linux “Task Scheduler”)

./scripts/install-linux.sh installs this automatically. To add or refresh the capture and Elia lines:

./scripts/install_capture_cron.sh
# remove: ./scripts/uninstall_capture_cron.sh
./scripts/install_elia_cron.sh
# remove: ./scripts/uninstall_elia_cron.sh

Elia (Belgian day-ahead, used for home dynamic €/kWh) is fetched once in the evening (20:00), after the D+1 auction (~13:00 Brussels). That fills tonight and tomorrow so DayMap can price overnight home charging without opening Charges. Cached days are skipped; a 7-day lookback covers a missed evening. The machine must be on at 20:00.

0 20 * * * { date -Iseconds; cd /path/to/matesla && .venv/bin/python manage.py FetchEliaDayAhead; echo; } >> /tmp/matesla-elia.log 2>&1

Cron jobs run as your user, whether or not you are logged into a graphical session, as long as the machine is on and the cron service is running.

Manual equivalent (every minute + timestamped log):

* * * * * { date -Iseconds; curl -fsS http://127.0.0.1:8001/matesla/internal/capture; echo; } >> /tmp/matesla-capture.log 2>&1

Check:

crontab -l
tail -f /tmp/matesla-capture.log
tail -f /tmp/matesla-elia.log

Cron field order

minute  hour  day-of-month  month  day-of-week  command
*       *     *             *      *            run every minute
*/5     *     *             *      *            every 5 minutes
0       *     *             *      *            every hour at :00

What the log line does

Part Role
date -Iseconds timestamp before each run (curl alone has no time)
curl -fsS … call the capture endpoint
echo blank line between runs
>> /tmp/matesla-capture.log append stdout to the log file
2>&1 also append errors (e.g. site down)

Example when the site is down (dev, not started):

2026-07-25T20:15:00+02:00
curl: (7) Failed to connect to 127.0.0.1 port 8001 … Couldn't connect to server

Example when the site is up:

2026-07-25T20:16:00+02:00
{"saved":1,"skipped_offline":2,"skipped_error":0,"token_error":0}

Logging to /tmp is optional (handy; may be cleared on reboot). Without >> … 2>&1, cron still runs the job but you usually see nothing.

To remove the job later: crontab -e and delete the line.

Requirements for a successful capture

  • Machine awake; cron service active. On AC power, closing the lid or locking the screen must not suspend. Since Ubuntu 26.04 the login screen (GDM) applies its own power settings once the session locks. Install with ./config/install_server_mode_logind.sh. PicturesDjango has the same script; if one of them has already configured the machine, the other only reports that and leaves logind and GDM running. On battery, closing the lid still suspends.
  • Web app listening on 127.0.0.1:8001
  • Tesla account linked (OAuth) and partner register done for the Fleet region

TeslaFi history import (monthly CSV)

To backfill graphs from TeslaFi monthly exports:

  1. Download month CSVs: scripts/download_teslafi_exports.py
    (cookie from Chrome recommended if the TeslaFi account has 2FA)
  2. Import into TeslaCarDataSnapshot:
    python manage.py ImportTeslaFiCSV path/to/MYYYY.csv --tz Europe/Brussels

Full guide (auth, 2FA, long ranges, gaps, batch import):
docs/teslafi-import.md

About

Python/django website using tesla api to connect to your marvellous Tesla car

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages