Skip to content

MattBrittan/teslamateapi

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

83 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TeslaMateApi

GitHub CI GitHub go.mod version Docker version Docker size Docker layers Docker pulls

TeslaMateApi is a RESTful API to get data collected by self-hosted data logger TeslaMate in JSON.

  • Written in Golang
  • Data is collected from TeslaMate Postgres database and local MQTT Broker
  • Endpoints return data in JSON format

Table of Contents

How to use

You can either use it in a Docker container or go download the code and deploy it yourself on any server.

Docker-compose

If you run the simple Docker deployment of TeslaMate, then adding this will do the trick. You'll have TeslaMateApi exposed at port 8080 locally then.

services:
  teslamateapi:
    image: tobiasehlert/teslamateapi:latest
    restart: always
    depends_on:
      - database
    environment:
      - DATABASE_USER=teslamate
      - DATABASE_PASS=secret
      - DATABASE_NAME=teslamate
      - DATABASE_HOST=database
      - MQTT_HOST=mosquitto
      - TZ=Europe/Berlin
    ports:
      - 8080:8080

If you are using TeslaMate Traefik setup in Docker with environment variables file (.env), then you can simply add this section to the services: section of the docker-compose.yml file:

services:
  teslamateapi:
    image: tobiasehlert/teslamateapi:latest
    restart: always
    depends_on:
      - database
    environment:
      - DATABASE_USER=${TM_DB_USER}
      - DATABASE_PASS=${TM_DB_PASS}
      - DATABASE_NAME=${TM_DB_NAME}
      - DATABASE_HOST=database
      - MQTT_HOST=mosquitto
      - TZ=${TM_TZ}
    labels:
      - "traefik.enable=true"
      - "traefik.port=8080"
      - "traefik.http.middlewares.redirect.redirectscheme.scheme=https"
      - "traefik.http.middlewares.teslamateapi-auth.basicauth.realm=teslamateapi"
      - "traefik.http.middlewares.teslamateapi-auth.basicauth.usersfile=/auth/.htpasswd"
      - "traefik.http.routers.teslamateapi-insecure.rule=Host(`${FQDN_TM}`)"
      - "traefik.http.routers.teslamateapi-insecure.middlewares=redirect"
      - "traefik.http.routers.teslamateapi.rule=Path(`/api`) || PathPrefix(`/api/`)"
      - "traefik.http.routers.teslamateapi.entrypoints=websecure"
      - "traefik.http.routers.teslamateapi.middlewares=teslamateapi-auth"
      - "traefik.http.routers.teslamateapi.tls.certresolver=tmhttpchallenge"

In this case, the TeslaMateApi would be accessible at teslamate.example.com/api/

Environment variables

Basically the same environment variables for the database, mqqt and timezone need to be set for TeslaMateApi as you have for TeslaMate.

Required environment variables (even if there are some default values available)

  • DATABASE_USER string (default: teslamate)
  • DATABASE_PASS string (default: secret)
  • DATABASE_NAME string (default: teslamate)
  • DATABASE_HOST string (default: database)
  • MQTT_HOST string (default: mosquitto)
  • TZ string (default: Europe/Berlin)

Optional environment variables

  • DATABASE_PORT integer (default: 5432)
  • DATABASE_TIMEOUT integer (default: 60000)
  • DATABASE_SSL boolean (default: true)
  • DEBUG_MODE boolean (default: false)
  • DISABLE_MQTT boolean (default: false)
  • MQTT_TLS boolean (default: false)
  • MQTT_PORT integer (default: 1883 / default if TLS is true: 8883)
  • MQTT_USERNAME string (default: )
  • MQTT_PASSWORD string (default: )
  • MQTT_NAMESPACE string (default: )
  • MQTT_SLEEPTIME integer (default: 100)

API documentation

More detailed documentation of every endpoint will come..

  • GET /api
  • GET /api/v1
  • GET /api/v1/cars
  • GET /api/v1/cars/:CarID
  • GET /api/v1/cars/:CarID/charges
  • GET /api/v1/cars/:CarID/charges/:ChargeID
  • GET /api/v1/cars/:CarID/drives
  • GET /api/v1/cars/:CarID/drives/:DriveID
  • GET /api/v1/cars/:CarID/status
  • GET /api/v1/cars/:CarID/updates
  • GET /api/v1/globalsettings
  • GET /api/ping

Security information

There is no possibility to get access to your Tesla account tokens by this API and we'll keep it this way!

The data that is accessible is data like the cars, charges, drives, current status, updates and global settings.

Also, apply some authentication on your webserver in front of the container, so your data is not unprotected and too exposed. In the example above, we use the same .htpasswd file as used by TeslaMate.

Credits

About

TeslaMateApi is a RESTful API to get data collected by self-hosted data logger TeslaMate in JSON

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 99.3%
  • Dockerfile 0.7%