Existing TeslaMate Integration
If you already have TeslaMate deployed, you only need to add the TeslaMateAPI service to use Mytesla.
- Users already running TeslaMate
- Users familiar with Docker
- Users who want to keep existing configurations
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
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!
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
Option 1: Docker Compose Integration (Recommended)
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
volumes:
teslamateapi-data:
driver: local
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:
Variable | Type | Default | Description |
---|---|---|---|
DATABASE_USER | string | teslamate | Database username |
DATABASE_PASS | string | secret | Database password |
DATABASE_NAME | string | teslamate | Database name |
DATABASE_HOST | string | database | Database host address |
ENCRYPTION_KEY | string | ⚠️ Must match TeslaMate's encryption key exactly | |
MQTT_HOST | string | mosquitto | MQTT broker host |
TZ | string | Europe/Berlin | Timezone setting (see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) |
Optional Environment Variables
Variable | Type | Default | Description |
---|---|---|---|
TESLAMATE_SSL | boolean | false | Use SSL for TeslaMate connection |
TESLAMATE_HOST | string | teslamate | TeslaMate host address |
TESLAMATE_PORT | string | 4000 | TeslaMate port |
API_TOKEN | string | 🔐 API token for authentication (Strongly recommended) | |
API_TOKEN_DISABLE | string | false | Disable API token requirement (Not recommended for production) |
DATABASE_PORT | integer | 5432 | Database port |
DATABASE_TIMEOUT | integer | 60000 | Database connection timeout (ms) |
DATABASE_SSL | boolean | true | Use SSL for database connection |
DEBUG_MODE | boolean | false | Enable debug logging |
DISABLE_MQTT | boolean | false | Disable MQTT functionality |
MQTT_TLS | boolean | false | Use TLS for MQTT connection |
MQTT_PORT | integer | 1883 (8883 if TLS) | MQTT broker port |
MQTT_USERNAME | string | MQTT username | |
MQTT_PASSWORD | string | MQTT password | |
MQTT_NAMESPACE | string | MQTT topic namespace | |
MQTT_CLIENTID | string | random 4 chars | MQTT 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.
- Configure Network Exposure to support remote access
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
- Visit Mytesla.cc
- Go to settings page
- Add your API endpoint address
- Test connection status