Skip to main content

Existing TeslaMate Integration

If you already have TeslaMate deployed, you only need to add the TeslaMateAPI service to use Mytesla.

Target Audience
  • Users already running TeslaMate
  • Users familiar with Docker
  • Users who want to keep existing configurations
warning

Strongly recommend backing up data before operation Backup and Restore

Tested with TeslaMate 2.0. Lower versions not tested.

Integration Overview​

Adding to existing TeslaMate system:

Pre-check​

Before starting, please confirm your existing system:

Required Conditions​

  • ✅ TeslaMate is running normally
  • ✅ PostgreSQL database is accessible
  • ✅ Know database connection information
  • ✅ ENCRYPTION_KEY is the same as TeslaMate

Get Existing Configuration Information​

First, find your TeslaMate container name:

# Find TeslaMate container name
docker ps | grep teslamate

# Example output shows container names like: teslamate, teslamate_teslamate_1, etc.

Then, get the configuration information using the correct container name:

# View existing TeslaMate container configuration (replace 'teslamate' with your actual container name)
docker inspect teslamate | grep -A 20 "Env"

# Find important environment variables using exec
docker exec teslamate env | grep -E "(DATABASE|ENCRYPTION|MQTT)"

# Alternative: View all environment variables and filter manually
docker exec teslamate printenv
Finding Container Name

If docker ps | grep teslamate shows multiple containers or uses different naming (like teslamate_teslamate_1), use the exact container name shown in the output for the subsequent docker inspect and docker exec commands.

You need to record the following information:

  • Database username (DATABASE_USER)
  • Database password (DATABASE_PASS)
  • Database name (DATABASE_NAME)
  • Database host (DATABASE_HOST)
  • Encryption key (ENCRYPTION_KEY) ⚠️ Must be the same!
Security Recommendation

It's strongly recommended to set up an API_TOKEN for TeslaMateAPI to prevent unauthorized access. Add it to your .env file:

# Generate a secure token
API_TOKEN=$(openssl rand -hex 32)
echo "API_TOKEN=$API_TOKEN" >> .env

Step 1: Backup Existing Configuration​

# Backup existing docker-compose.yml
cp docker-compose.yml docker-compose.yml.backup

# Backup environment variables file
cp .env .env.backup

Step 2: Add TeslaMateAPI Service​

Add the following service to your docker-compose.yml file:

services:
# ... Your existing service configurations ...

# Add TeslaMateAPI service
teslamateapi:
image: mytesla/teslamateapi:latest
container_name: teslamateapi
restart: unless-stopped
environment:
- DATABASE_USER=${TM_DB_USER} # Same as TeslaMate
- DATABASE_PASS=${TM_DB_PASS} # Same as TeslaMate
- DATABASE_NAME=${TM_DB_NAME} # Same as TeslaMate
- DATABASE_HOST=database # Your database service name
- ENCRYPTION_KEY=${TM_ENCRYPTION_KEY} # ⚠️ Must be exactly the same as TeslaMate!
- MQTT_HOST=mosquitto # If you use MQTT
- TZ=${TZ:-America/New_York} # 📍 Replace with your timezone from https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
- API_TOKEN=${API_TOKEN} # 🔐 Strongly recommended for security
ports:
- "8080:8080" # Can be changed to other port
volumes:
- teslamateapi-data:/opt/app/data
depends_on:
- database # Ensure it matches your database service name
- teslamate
- mosquitto

volumes:
teslamateapi-data:
driver: local
Important Note

ENCRYPTION_KEY must be exactly the same as the encryption key used by TeslaMate, otherwise it cannot read encrypted data!

Step 3: Start TeslaMateAPI​

# Start the new TeslaMateAPI service
docker-compose up -d teslamateapi

# View startup logs
docker-compose logs -f teslamateapi

# Check service status
docker-compose ps

Environment Variables Reference​

Required Environment Variables​

All required environment variables must be set even if default values are available:

VariableTypeDefaultDescription
DATABASE_USERstringteslamateDatabase username
DATABASE_PASSstringsecretDatabase password
DATABASE_NAMEstringteslamateDatabase name
DATABASE_HOSTstringdatabaseDatabase host address
ENCRYPTION_KEYstring⚠️ Must match TeslaMate's encryption key exactly
MQTT_HOSTstringmosquittoMQTT broker host
TZstringEurope/BerlinTimezone setting (see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)

Optional Environment Variables​

VariableTypeDefaultDescription
TESLAMATE_SSLbooleanfalseUse SSL for TeslaMate connection
TESLAMATE_HOSTstringteslamateTeslaMate host address
TESLAMATE_PORTstring4000TeslaMate port
API_TOKENstring🔐 API token for authentication (Strongly recommended)
API_TOKEN_DISABLEstringfalseDisable API token requirement (Not recommended for production)
DATABASE_PORTinteger5432Database port
DATABASE_TIMEOUTinteger60000Database connection timeout (ms)
DATABASE_SSLbooleantrueUse SSL for database connection
DEBUG_MODEbooleanfalseEnable debug logging
DISABLE_MQTTbooleanfalseDisable MQTT functionality
MQTT_TLSbooleanfalseUse TLS for MQTT connection
MQTT_PORTinteger1883 (8883 if TLS)MQTT broker port
MQTT_USERNAMEstringMQTT username
MQTT_PASSWORDstringMQTT password
MQTT_NAMESPACEstringMQTT topic namespace
MQTT_CLIENTIDstringrandom 4 charsMQTT client ID

Option 2: Standalone Container Deployment​

If you don't use Docker Compose, you can run TeslaMateAPI container independently:

Create Data Volume​

First, create the data volume that the container will use:

# Create named volume for persistent data
docker volume create teslamateapi-data

Create Standalone Container​

# Get existing network name
NETWORK_NAME=$(docker inspect teslamate --format='{{range .NetworkSettings.Networks}}{{.NetworkID}}{{end}}')

# Run TeslaMateAPI container
docker run -d \
--name teslamateapi \
--network $NETWORK_NAME \
--restart unless-stopped \
-p 8080:8080 \
-e DATABASE_USER=teslamate \
-e DATABASE_PASS=your_db_password \
-e DATABASE_NAME=teslamate \
-e DATABASE_HOST=your_db_host \
-e API_TOKEN=your_secure_api_token \
-e ENCRYPTION_KEY=your_encryption_key \
-e MQTT_HOST=your_mqtt_host \
-e TZ=Asia/Shanghai \ # 📍 Replace with your timezone from https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
-v teslamateapi-data:/opt/app/data \
mytesla/teslamateapi:latest

Add Network Exposure​

For Mytesla to access your TeslaMateAPI, you need to configure network exposure.

Verify Integration​

Check Service Status​

# View all container status
docker-compose ps

# Check TeslaMateAPI logs
docker-compose logs teslamateapi

# Verify API health status
curl -f http://localhost:8080/health

Mytesla Frontend Integration​

Get API Endpoint Address​

Based on your chosen network exposure solution:

  • Tailscale: https://api.mytesla.ts.net
  • Cloudflare: https://your-domain.com
  • Local Access: http://YourIP:8080

Configure in Mytesla.cc​

  1. Visit Mytesla.cc
  2. Go to settings page
  3. Add your API endpoint address
  4. Test connection status