Sign inSign up

q84fh/leafspy2mqtt

By q84fh

•Updated over 1 year ago

This is simple LeafSpy server that bridges LeafSpy to MQTT broker

Image
Message queues
Internet of things
Monitoring & observability
0

2.7K

q84fh/leafspy2mqtt repository overview

⁠leafspy2mqtt⁠

Docker Pulls Docker Stars Docker Image Size Github stars Github forks Github issues Github last-commit Built with Devbox

This is simple server that bridges LeafSpy⁠ to MQTT broker.

⁠Environment variables

Configuration is done via environment variables:

  • MQTT_PORT - port of your MQTT server
  • MQTT_USERNAME - username in your MQTT server
  • MQTT_PASSWORD - password to your MQTT server
  • MQTT_TOPIC - topic to which messages should be sent
  • MQTT_HOST - address of your MQTT server
  • PASSWORD_HASH - hashed password entered in LeafSpy⁠, can be generated with: python -c 'import hashlib; print(hashlib.sha3_512(b"Nobody inspects the spammish repetition").hexdigest())'
  • USERNAME=q84fh - username entered in LeafSpy⁠

⁠Example docker-compose

You need MQTT broker. You can install one yourself or for deploy one built into Home Assistant⁠.

version: '3'
services:
 2mqtt:
   image: q84fh/leafspy2mqtt:latest
   ports:
     -  8888:8888
   restart: unless-stopped
   environment:
     - MQTT_PORT=1883
     - MQTT_USERNAME=leaf
     - MQTT_PASSWORD=secretpassword
     - MQTT_TOPIC=leafspy
     - MQTT_HOST=yourmqttbroker.local
     - PASSWORD_HASH=51b771f538117(...)
     - USERNAME=q84fh
   logging:
     options:
       max-size: "100M"
       max-file: "5"

⁠LeafSpy configuration

You need to expose port 8888 so LeafSpy⁠ would be able to reach it. You can do it via VPN to your mobile, or by just exposing it to the public Internet.

Then:

  1. Go to the settings using ≡ menu
  2. Find Server section
  3. Check Enable
  4. Select desired Send Interval (ex. 5s)
  5. Check A: Enable
  6. Enter username in ID field
  7. Enter password in PW field
  8. Check if your leafspy2mqtt is reachable over HTTP or HTTPS
  9. Enter URL (without http:// or https:// prefix)

https://github.com/user-attachments/assets/38f6c019-b40b-4f0c-8840-9eabd31f0c31⁠

You can configure your LeafSpy⁠ to send data to up to 4 different servers (A, B, C, D).

⁠Data format

LeafSpy⁠ sends its data with HTTP GET request with data placed in URL query string.

It goes like this:

GET /?user=q84fh&pass=Nobodyinspectsthespammishrepetition&DevBat=100&Gids=219&Lat=-11.12421&Long=-71.75095&Elv=35&Seq=143&Trip=86&Odo=37774&SOC=46.2757&AHr=107.3014&BatTemp=8.7&Amb=7.0&Wpr=0&PlugState=2&ChrgMode=2&ChrgPwr=1300&VIN=3VWSB81H8WM210368&PwrSw=1&Tunits=C&RPM=0&SOH=92.95&Hx=102.51&Speed=0.0&BatVolts=352.56&BatAmps=-1.006

LeafSpy⁠ expects response HTTP/200 with payload:

"status":"0"
FieldTypeUnitExampleComment
userstr-q84fhUsername
passstr-NobodyinspectsthespammishrepetitionPassword
DevBatint%100Phone battery level
Gidsint?219RAW battery SOC reported by BMS
Latfloat°-11.12421Lattitute from GPS
Longfloat°-71.75095Longitute from GPS
Elvintm35Elevation from GPS
Seqint-117LeafSpy⁠ request number (resets on restart)
Tripintkm86Current trip distance (resets on restart)
Odointkm37774Total car milage
SOCfloat%46.2157SOC calculated by LeafSpy⁠
AHrfloatAH107.3014Maximum capacity of battery
BatTempfloat°TunitsAvarage battery temperature (check Tunits for used unit)
Ambfloat°Tunits7.5Ambient temperature (check Tunits for used unit)
Wprint-0Front wiper status
PlugStateint-2I don't know, PR welcome
ChrgModeint-2I don't know, PR welcome
ChrgPwrintW1300Negotiated charging power
VINstr-3VWSB81H8WM210368VIN of the car
PwrSwint-1Power switch state 0=off, 1=on
Tunitsstr-CUnit used to report temperatures (C/F)
RPMintRPM0Number of revolution per minute of motor
SOHfloat%92.95State of health reported by BMS
Hxfloat%102.51Percent of nominal battery conductivity
Speedfloatkm/h0.0Vehicle speed
BatVoltsfloatV352.55Voltage of battery
BatAmpsfloatA-1.189Current flowing into (negative) or out of battery (positive)

This is converted to JSON payload and sent towards MQTT broker. Message looks like this:

{
  "user": "q84fh",
  "pass": "Nobodyinspectsthespammishrepetition",
  "DevBat": 100,
  "Gids": 219,
  "Lat": -11.12421,
  "Long": -71.75095,
  "Elv": 35,
  "Seq": 117,
  "Trip": 86,
  "Odo": 37774,
  "SOC": 46.2157,
  "AHr": 107.3014,
  "BatTemp": 8.7,
  "Amb": 7.5,
  "Wpr": 0,
  "PlugState": 2,
  "ChrgMode": 2,
  "ChrgPwr": 1300,
  "VIN": "3VWSB81H8WM210368",
  "PwrSw": 1,
  "Tunits": "C",
  "RPM": 0,
  "SOH": 92.95,
  "Hx": 102.51,
  "Speed": 0.0,
  "BatVolts": 352.55,
  "BatAmps": -1.189
}

⁠How to use it

You can use it for example to feed this data to your Home Assistant⁠ with its MQTT integration⁠ and do some cool automation or just data visualisation with it. If you fill find some cool application let me know, I would love to include some examples here.

Example configuration:

mqtt:
  sensor:
    - name: leafspy_Gids
      state_topic: "leafspy"
      value_template: "{{ value_json.Gids }}"
      
    - name: leafspy_ODO
      state_topic: "leafspy"
      device_class: distance
      unit_of_measurement: km
      state_class: "total_increasing"
      value_template: "{{ value_json.Odo }}"

    - name: leafspy_SOC
      state_topic: "leafspy"
      unit_of_measurement: "%"
      device_class: battery      
      value_template: "{{ value_json.SOC }}"
      
    - name: leafspy_AHr
      state_topic: "leafspy"
      unit_of_measurement: AHr
      device_class: battery
      value_template: "{{ value_json.AHr }}"
      
    - name: leafspy_BatTemp
      state_topic: "leafspy"
      unit_of_measurement: °C
      device_class: temperature
      value_template: "{{ value_json.BatTemp }}"
      
    - name: leafspy_Amb
      state_topic: "leafspy"
      unit_of_measurement: °C
      device_class: temperature
      value_template: "{{ value_json.Amb }}"
      
    - name: leafspy_RPM
      state_topic: "leafspy"
      unit_of_measurement: RPM
      value_template: "{{ value_json.RPM }}"
 
    - name: leafspy_SOH
      state_topic: "leafspy"
      unit_of_measurement: "%"
      value_template: "{{ value_json.SOH }}"
     
    - name: leafspy_BatVolts
      state_topic: "leafspy"
      unit_of_measurement: V
      device_class: voltage
      value_template: "{{ value_json.BatVolts }}"
      
    - name: leafspy_AvgCellVolts
      state_topic: "leafspy"
      unit_of_measurement: V
      device_class: voltage
      value_template: "{{ value_json.BatVolts | float / 96 }}"
      
    - name: leafspy_BatAmps
      state_topic: "leafspy"
      unit_of_measurement: A
      device_class: current
      value_template: "{{ value_json.BatAmps }}"
      
    - name: leafspy_BatteryEnergy
      state_topic: "leafspy"
      unit_of_measurement: kWh
      device_class: energy
      value_template: "{{ (((value_json.BatVolts | float * value_json.AHr | float) / 100000) * value_json.SOC | float) | round(2) }}"

Tag summary

Content type

Image

Digest

sha256:4f9c0be37…

Size

19.8 MB

Last updated

over 1 year ago

docker pull q84fh/leafspy2mqtt