tobiasehlert / teslamateapi

TeslaMateApi is a RESTful API to get data collected by self-hosted data logger TeslaMate in JSON
MIT License
89 stars 20 forks source link

Empty results with /api/v1/cars/1/status #14

Closed kegelmeier closed 3 years ago

kegelmeier commented 3 years ago

Hello,

I have an issue with the teslamateapi. I do not get valid responses from the /api/v1/cars/1/status, however, the response of /api/v1/cars/1 is fine. How can this be?

The logs also look fine.

Response /api/v1/cars/1/status

{
    "data": {
        "car": {
            "car_id": 1,
            "car_name": "MJ3"
        },
        "status": {
            "display_name": "",
            "state": "",
            "state_since": "0001-01-01T00:53:28+00:53",
            "odometer": 0,
            "car_status": {
                "healthy": false,
                "locked": false,
                "sentry_mode": false,
                "windows_open": false,
                "doors_open": false,
                "trunk_open": false,
                "frunk_open": false,
                "is_user_present": false
            },
            "car_details": {
                "model": "",
                "trim_badging": ""
            },
            "car_exterior": {
                "exterior_color": "",
                "spoiler_type": "",
                "wheel_type": ""
            },
            "car_geodata": {
                "geofence": "",
                "latitude": 0,
                "longitude": 0
            },
            "car_versions": {
                "version": "",
                "update_available": false,
                "update_version": ""
            },
            "driving_details": {
                "shift_state": "",
                "speed": 0,
                "heading": 0,
                "elevation": 0
            },
            "climate_details": {
                "is_climate_on": false,
                "inside_temp": 0,
                "outside_temp": 0,
                "is_preconditioning": false
            },
            "battery_details": {
                "est_battery_range": 0,
                "rated_battery_range": 0,
                "ideal_battery_range": 0,
                "battery_level": 0,
                "usable_battery_level": 0
            },
            "charging_details": {
                "plugged_in": false,
                "charge_energy_added": 0,
                "charge_limit_soc": 0,
                "charge_port_door_open": false,
                "charger_actual_current": 0,
                "charger_phases": 0,
                "charger_power": 0,
                "charger_voltage": 0,
                "scheduled_charging_start_time": "0001-01-01T00:53:28+00:53",
                "time_to_full_charge": 0
            }
        },
        "units": {
            "unit_of_length": "km",
            "unit_of_temperature": "C"
        }
    }
}

Response /api/v1/cars

{
    "data": {
        "cars": [{
            "car_id": 1,
            "name": "MJ3",
            "car_details": {
                "eid": 19437902XXXX,
                "vid": 18249217XXXX,
                "vin": 5YJ3E7EA2MFXXXXX,
                "model": "3",
                "trim_badging": null,
                "efficiency": 0.13
            },
            "car_exterior": {
                "exterior_color": "PearlWhite",
                "spoiler_type": "None",
                "wheel_type": "PinwheelRefresh18"
            },
            "car_settings": {
                "suspend_min": 12,
                "suspend_after_idle_min": 15,
                "req_not_unlocked": false,
                "free_supercharging": false,
                "use_streaming_api": true
            },
            "teslamate_details": {
                "inserted_at": "2021-03-13T12:22:57+01:00",
                "updated_at": "2021-03-15T09:54:24+01:00"
            },
            "teslamate_stats": {
                "total_charges": X,
                "total_drives": X,
                "total_updates": X
            }
        }]
    }
}

Logs

teslamateapi_1  | 2021/03/16 10:54:11.855339 [info] TeslaMateAPICarsV1 /api/v1/cars/1 executed successful.
teslamateapi_1  | [GIN] 2021/03/16 - 10:54:11 | 200 |    3.695194ms | 192.168.178.178 | GET      "/api/v1/cars/1"
teslamateapi_1  | 2021/03/16 10:54:14.813472 [info] TeslaMateAPICarsStatusV1 /api/v1/cars/1/status executed successful.
teslamateapi_1  | [GIN] 2021/03/16 - 10:54:14 | 200 |  775.427588ms | 192.168.178.178 | GET      "/api/v1/cars/1/status"

Thank you in advance!

tobiasehlert commented 3 years ago

Hi @kegelmeier,

Interesting! Looks to me that there is something related to MQTT then.

What type of setup of TeslaMate are you running.. Basic Docker or Advanced Traefik? Or something custom made?

Does MQTT container log say something similar to an attempt to connect? Could you please share the configuration of your MQTT section in docker-compose.yml file?

It should look similar to something like this:


  mosquitto:
    image: eclipse-mosquitto:1.6
    restart: always
    ports:
      - 127.0.0.1:1883:1883
    volumes:
      - mosquitto-conf:/mosquitto/config
      - mosquitto-data:/mosquitto/data

In addition, please set DEBUG_MODE environmental variable to true so that we can get more output from the log.

Regards, Tobias

kegelmeier commented 3 years ago

Hi @tobiasehlert Thanks for the quick reply. I am using the Traefik setup. Regarding the MQTT part, I also have a Home Assistant setup running on the network that serves as the broker. Therefore the MQTT settings within the docker-compose.yml are custom as the following:

      - MQTT_HOST=192.168.178.197
      - MQTT_USERNAME=k***
      - MQTT_PASSWORD=k***

Could it be that the HA does not allow two connections from the same host? Or does the API container rely on the mosquitto instance within teslamate?

Best, Jens

kegelmeier commented 3 years ago

Here, additionally the output of the container logs in DEBUG_MODE

teslamateapi_1  | 2021/03/16 13:43:47.581394 [debug] initDBconnection - successfully completed (connected to postgres).
teslamateapi_1  | 2021/03/16 13:44:07.025670 TeslaMateAPICarsStatus successfully connected to mqtt.
teslamateapi_1  | 2021/03/16 13:44:07.135052 [debug] getTimeInTimeZone - UTC 0001-01-01T00:00:00Z time converted to Europe/Berlin is 0001-01-01T00:53:28+00:53
teslamateapi_1  | 2021/03/16 13:44:07.135207 [debug] getTimeInTimeZone - UTC 0001-01-01T00:00:00Z time converted to Europe/Berlin is 0001-01-01T00:53:28+00:53
teslamateapi_1  | 2021/03/16 13:44:07.135322 [debug] TeslaMateAPICarsStatusV1 /api/v1/cars/1/status returned data:
teslamateapi_1  | [GIN] 2021/03/16 - 13:44:07 | 200 |  783.093968ms | 192.168.178.178 | GET      "/api/v1/cars/1/status"
teslamateapi_1  | 2021/03/16 13:44:07.136668 [debug] {"data":{"car":{"car_id":1,"car_name":"MJ3"},"status":{"display_name":"","state":"","state_since":"0001-01-01T00:53:28+00:53","odometer":0,"car_status":{"healthy":false,"locked":false,"sentry_mode":false,"windows_open":false,"doors_open":false,"trunk_open":false,"frunk_open":false,"is_user_present":false},"car_details":{"model":"","trim_badging":""},"car_exterior":{"exterior_color":"","spoiler_type":"","wheel_type":""},"car_geodata":{"geofence":"","latitude":0,"longitude":0},"car_versions":{"version":"","update_available":false,"update_version":""},"driving_details":{"shift_state":"","speed":0,"heading":0,"elevation":0},"climate_details":{"is_climate_on":false,"inside_temp":0,"outside_temp":0,"is_preconditioning":false},"battery_details":{"est_battery_range":0,"rated_battery_range":0,"ideal_battery_range":0,"battery_level":0,"usable_battery_level":0},"charging_details":{"plugged_in":false,"charge_energy_added":0,"charge_limit_soc":0,"charge_port_door_open":false,"charger_actual_current":0,"charger_phases":0,"charger_power":0,"charger_voltage":0,"scheduled_charging_start_time":"0001-01-01T00:53:28+00:53","time_to_full_charge":0}},"units":{"unit_of_length":"km","unit_of_temperature":"C"}}}
teslamateapi_1  | 2021/03/16 13:44:07.136708 [info] TeslaMateAPICarsStatusV1 /api/v1/cars/1/status executed successful.
tobiasehlert commented 3 years ago

Hi @kegelmeier,

Alright, so your HA does provide the MQTT functionality in your setup? That would mean that TeslaMateApi would need to connect to HA to get the information published by TeslaMate to HA.

Is this what you are using? https://www.home-assistant.io/docs/mqtt/service/

When thinking of MQTT and multiple clients, that should be no problem. The question is more, why HA doesn't send back the last information published on a topic so that it can be consumed.. but it seems that you have not configured HA to retain the messages.

If you enable retain, the MQTT broker will stores the last retained message. Each client (e.g. TeslaMateApi) that subscribes to a topic pattern that matches the topic of the retained message receives the retained message immediately after they subscribe. The broker stores only one retained message per topic and no history.

Regards, Tobias

kegelmeier commented 3 years ago

Hi @tobiasehlert,

I am running Mosquitto on Home Assistant with this add-on (https://github.com/home-assistant/addons/blob/master/mosquitto/DOCS.md)

I checked the set-up with an MQTT Explorer and find that the messages are already retained.

111318557-76547f00-8665-11eb-8d33-e8ea572a7dc0

Any other idea? Best, Jens

LelandSindt commented 3 years ago

I have a theory/suspicion...

https://github.com/tobiasehlert/teslamateapi/blob/9cbb2fa244610fbb01197052684059f6472a8384/src/v1_TeslaMateAPICarsStatus.go#L454

Mosquitto is not returning results within 100 milliseconds.

tobiasehlert commented 3 years ago

I am running Mosquitto on Home Assistant with this add-on (https://github.com/home-assistant/addons/blob/master/mosquitto/DOCS.md)

I checked the set-up with an MQTT Explorer and find that the messages are already retained.

Alright, then we know that it's retained already.

I have a theory/suspicion...

https://github.com/tobiasehlert/teslamateapi/blob/9cbb2fa244610fbb01197052684059f6472a8384/src/v1_TeslaMateAPICarsStatus.go#L454

Mosquitto is not returning results within 100 milliseconds.

Was thinking about the same thing @LelandSindt.. let's make it customizable to see if this can solve the problem for Jens.

tobiasehlert commented 3 years ago

@kegelmeier, created a separate branch (bug-14) and you can test the image bug-14 to see, if it works.

Set the MQTT_SLEEPTIME to 500 (default is 100) to see, if that works better for you.

kegelmeier commented 3 years ago

Great, thanks @LelandSindt and @tobiasehlert! The image seems to work. The first try did not work but every request after that worked out.

tobiasehlert commented 3 years ago

Awesome @kegelmeier Then I'll go ahead and merge it with main.

Don't forget to switch image to latest to don't miss out on new stuff =D