[BETA] Tesla Vehicle via TeslaMate MQTT

RE: Using node-red to really take advantage of TeslaMate

Moved it here as to not derail OPs topic and hard work.

Yes, node-red looks interesting and is probably worth some attention in the near future. But for right now, I mostly wanted to be able to reference a couple of Tesla vehicle properties in a few of my existing rules.

@mbishop thanks for putting this together. I just got TeslaMate up and running and would like to get this data into Hubitat. Can you help clarify for me what I would put for the broker port, username, and password? I don't remember setting up any credentials - only providing TeslaMate with the token and refresh token.

Sure! I don't use authentication on mine, since the MQTT server is only accessible within the network, so username/password are optional. I assume you set up TeslaMate using docker-compose. In the default version of that file, the ports for mosquitto are commented out; you'd want to uncomment them (so that you can reach them from outside the local Docker network). Then the broker is the machine running the Docker containers and the port is the one exposed by mosquitto (1883 by default).

Awesome! That worked like a charm. It'll be fun to use some of this data now within Hubitat. Out of curiosity, are you using this information on any dashboards? If so, which template are you using?

I have a Garage dashboard that shows the state of the garage door, garage lights, and the presence/power state of each car. (Using this driver for the Tesla, and this Zigbee device for my wife's ICE.) Presence is the presence template, obviously; the Attribute template can be used to display the powerSource.

It's kind of ironic that my rules have to have inverted logic for each car -- the Tesla switches to battery when it's getting ready to leave, but when the ICE car stops being on battery, it's preparing to leave. :wink:

1 Like

I stumbled across this in HPM today and figured I'd give it a try. I was already pulling in some info about my cars from Teslamate using an MQTT driver and virtual presence sensors, but your driver may be a cleaner way to consolidate everything from Teslamate. My current solution creates one MQTT child device for each parameter, which can clutter up the devices page.

After installing, I started getting some errors from the parent device:

java.lang.NullPointerException: Cannot invoke method xor() on null object on line 332 (method parse)

This is line 332:

if( settings?.temperatureIndoor ^ property == "outside_temp" ) {

I noticed that neither car was showing the inside temp or outside temp values.

While playing around with that line to try to eliminate the error, I caused this error:

groovy.lang.MissingMethodException: No signature of method: java.math.BigDecimal.round() is applicable for argument types: (java.lang.Integer) values: [1] Possible solutions: round(java.math.MathContext), find(), pow(int), power(java.lang.Integer), and(java.lang.Number), find(groovy.lang.Closure) on line 331 (method parse)

which refers to line 331, which is:

value = value.round(1)

I commented out the original lines and inserted modified versions of those 2 lines, so my driver now looks like this:

                //value = value.round(1)
                value = value.doubleValue().round(1)
                //if( settings?.temperatureIndoor ^ property == "outside_temp" ) {
                if( settings?.temperatureIndoor == "outside_temp" ) {

Converting the value to double fixed the one error, and removing the XOR fixed the other error. I don't know if this is something unique to my setup or if others might have the same issue. I'm also not sure exactly what I did by removing that XOR, but the inside/outside temp values are now showing for both cars.

If it matters, this is my config in the parent device:

1 Like

What you're discovering are the groups of attributes that I coded for completeness, but don't use myself. :wink:

Yes, the second one results from Hubitat's celsiusToFahrenheit() method returning a BigDecimal, which doesn't have the round() method. I think I'd rather change it to the preferred type within the if block, but your change amounts to the same thing.

The first one is more broken than your fix accounts for, but it's still an easy fix. There's a setting that indicates whether the stock temperature attribute reflects indoor or outdoor temp, but both will be present as custom attributes. Your fix will never be true, so it just doesn't publish the temperature attribute.

I'll get a fixed version on HPM shortly; I'll turn on the climate attributes on my local hub first and re-test that section.

Edit: Published. Let me know if you encounter any others, please!

Thanks for getting this going, I'm trying to use it as a presence detector for one of my homes. I followed this video here TeslaMate Overview and Install | Self-Hosted Tesla Data Logger | Tesla Dashboards - YouTube and setup my own domain etc. Can you help with the values? I read the previous response about commenting out the port, but can you show an example of exactly what modifications would be needed on the yml they are using and this video setup? I can't seem to get a connection, they do have a htpasswd file in use that I'd like to keep there too.

The .htpasswd doesn't matter as much for this, because that's for the web interface. This driver points at the MQTT server, which is the mosquitto container. The example you pointed to has it running in the cloud; I haven't tried setting up TLS on mosquitto myself, but Hubitat's MQTT client can do a secure connection.

Looks like it needs specific calls, though; I'll try to take a look this weekend.

1 Like

Thank's so much, lmk if there's a link I can buy you coffee or something. I appreciate this :+1:

On your side, you'll need to expose the MQTT port. Since you're running in the cloud, you'd presumably want TLS via Traefik and authentication in mosquitto. There are some articles out there, but I haven't seen a Teslamate-specific one.

1 Like

Trying to set this up for the first time. TeslaMate is up and running. I have entered my local server address and port 1883, which is the default in my .yml file for mosquito, and I have un-commented out those lines there. Getting the following errors:

Looks like I'm not connecting, and that is what's causing the line 260 error, but I don't know what would be the issue, since I'm point to the address where TeslaMate is running and using the mosquito port in the .yml file. I did put a password in the .yml file, but it looks like it's for the database and not mosquito. I also am not sure if mosquito is actually running as it should be, because I can't find a way to access it directly (although it does show running in docker).

Any help would be greatly appreciated!

This is line 260:

At a quick glance, I have a bug in the reconnect logic if the connection has never succeeded; I'll get a fix for that ASAP. Edit: Fixed in main, but not bumping the version number because it won't affect anyone who already has it installed and running. Running a repair will grab the new code.

The inciting problem, it looks like it's failing to connect to the MQTT server -- have you verified with another MQTT client (e.g. MQTT Explorer) that the server is reachable? The default Teslamate config doesn't expose the port publicly (since the other containers can reach it anyway); does docker ps show that the container has a public port?

Thanks! Below is what docker ps shows. I tried using MQTT Explorer, and I can't seem to connect using that either, so I'm not sure what's wrong.

Okay, Docker says it's exposing the port, so that's a good sign. Next thing I'd check is if there's a firewall blocking access. When you tried MQTT Explorer, was that on the same machine or a different one?

I am running MQTT Explorer on the same machine as Docker. I don't have the built-in firewall turned on. I use Eero Wi-Fi with Eero Plus turned on, and tried opening the port with TCP & UDP, but that produced no better results. I also have Homebridge and the TeslaSpeaks servers running on this machine, and I'm able to use them without an issue. Thanks for your help with this, I know it's a bit out of scope.

I think I see the problem. In the screenshot above, it shows 0.0.0.0:4000->4000/tcp for the (public, one presumes) Teslamate port, but doesn't show the 0.0.0.0 for mosquitto. Check in your docker-compose.yml file that you're actually exposing this port to the outside world?

I use the .yml file from the TeslaMate docker install instructions. I had to un-comment-out the mosquito ports, but it looks the same format as the TeslaMate ports. This is my full .yml file (with passwords redacted). Thanks again.

Summary
version: "3"

services:
  teslamate:
    image: teslamate/teslamate:latest
    restart: always
    environment:
      - ENCRYPTION_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      - DATABASE_USER=teslamate
      - DATABASE_PASS=XXXXXXXXXXXXXXXXX
      - DATABASE_NAME=teslamate
      - DATABASE_HOST=database
      - MQTT_HOST=mosquitto
    ports:
      - 4000:4000
    volumes:
      - ./import:/opt/app/import
    cap_drop:
      - all

  database:
    image: postgres:15
    restart: always
    environment:
      - POSTGRES_USER=teslamate
      - POSTGRES_PASSWORD=XXXXXXXXXXXXXXXXX
      - POSTGRES_DB=teslamate
    volumes:
      - teslamate-db:/var/lib/postgresql/data

  grafana:
    image: teslamate/grafana:latest
    restart: always
    environment:
      - DATABASE_USER=teslamate
      - DATABASE_PASS=XXXXXXXXXXXXXXXXX
      - DATABASE_NAME=teslamate
      - DATABASE_HOST=database
    ports:
      - 3000:3000
    volumes:
      - teslamate-grafana-data:/var/lib/grafana

  mosquitto:
    image: eclipse-mosquitto:2
    restart: always
    command: mosquitto -c /mosquitto-no-auth.conf
    ports:
      - 1883:1883
    volumes:
      - mosquitto-conf:/mosquitto/config
      - mosquitto-data:/mosquitto/data

volumes:
  teslamate-db:
  teslamate-grafana-data:
  mosquitto-conf:
  mosquitto-data:

Okay, and it looks like /mosquitto-no-auth.conf should contain the appropriate commands to allow incoming connections. What do you see if you do a docker logs teslamate-mosquitto-1 (adjusting for whatever your actual container name is)? I see something like...

1692327204: mosquitto version 2.0.16 starting
1692327204: Config loaded from /mosquitto/config/mosquitto.conf.
1692327204: Opening ipv4 listen socket on port 1883.
1692327204: Opening ipv6 listen socket on port 1883.
1692327204: mosquitto version 2.0.16 running
1692327204: New connection from 172.17.0.4:55992 on port 1883.
1692327204: New client connected from 172.17.0.4:55992 as teslamateapi-4kOT (p2, c1, k2).
1692327204: New connection from 172.17.0.1:37114 on port 1883.
1692327204: New client connected from 172.17.0.1:37114 as TWCTeslaMate (p2, c1, k30).
1692327204: New connection from 172.17.0.1:37116 on port 1883.
1692327204: New client connected from 172.17.0.1:37116 as mqttjs_1846d026 (p2, c1, k60).
1692327205: New connection from 172.17.0.1:37118 on port 1883.
1692327205: New connection from 172.17.0.1:37120 on port 1883.
1692327205: New connection from 172.17.0.5:38532 on port 1883.
1692327205: New client connected from 172.17.0.5:38532 as TESLAMATE_302E3936383 (p2, c0, k60).
1692327205: New client connected from 172.17.0.1:37118 as Arduino-01 (p2, c1, k60).
1692327205: New client connected from 172.17.0.1:37120 as teslamate/cars/ (p2, c1, k60).