Traccar devices not showing

Hi,

I’m relatively new to home assistant, and trying to add traccar to track locations. I’ve had a traccar server running previously, which is on the same machine (a pi 4) as home assistant but otherwise totally separate.

Home assistant is run as a docker image under docker-compose using plain home assistant (no hassio). I’ve added the following to my configuration.yaml:

device_tracker:
  - platform: traccar
    host: !secret traccar_host
    username: !secret traccar_user
    password: !secret traccar_password
    event: ["all_events"]
    new_device_defaults:
      track_new_devices: true
      hide_if_away: false

traccar_host is set to my internal IP address (192.168.1.xxx).

On startup, the only relevant line I see in the logs is this:
2019-12-31 15:08:05 INFO (MainThread) [homeassistant.components.traccar.device_tracker] C onnection to Traccar restored

I’m not sure what to expect next - I don’t see any new devices or entities added, and if I listen for events I don’t see any traccar-based events generated. The devices show up and update fine in Traccar itself, so I know that part is ok. Where do I go from here?

Many thanks,
Toby

Looks like you already enabled debug messages for the HA traccar component. You can also try enabling debug for the pytraccar package that it uses:

logger:
  default: info
  logs:
    homeassistant.components.traccar: debug
    pytraccar: debug

That might provide more clues in the log.

1 Like

Many thanks for that tip. I hadn’t enabled debug logging yet, the above was just the info output. I’ve now done that; the logging output isn’t particularly useful but does suggest that it’s not finding any devices:

2019-12-31 17:28:48 INFO (SyncWorker_6) [homeassistant.loader] Loaded traccar from homeassistant.components.traccar
2019-12-31 17:28:49 INFO (MainThread) [homeassistant.setup] Setting up traccar
2019-12-31 17:28:49 INFO (MainThread) [homeassistant.setup] Setup of domain traccar took 0.0 seconds.
2019-12-31 17:28:50 DEBUG (MainThread) [homeassistant.components.traccar.device_tracker] Testing connection to Traccar
2019-12-31 17:28:50 INFO (MainThread) [homeassistant.components.traccar.device_tracker] Connection to Traccar restored
2019-12-31 17:28:50 DEBUG (MainThread) [homeassistant.components.traccar.device_tracker] Updating device data
2019-12-31 17:28:50 DEBUG (MainThread) [pytraccar.api] {}
2019-12-31 17:28:50 DEBUG (MainThread) [pytraccar.api] []
2019-12-31 17:28:50 DEBUG (MainThread) [pytraccar.api] []
2019-12-31 17:28:50 DEBUG (MainThread) [pytraccar.api] {}

The updating repeats at roughly 30 second intervals.

I verified the traccar data using the web api directly to fetch a list of devices (GET /api/devices). This returns the following, which looks reasonable. Oliver and Peter aren’t setup yet so not expecting data for those two.

[
  {
    "id": 1,
    "attributes": {},
    "groupId": 0,
    "name": "Toby",
    "uniqueId": "TobyTraccar",
    "status": "online",
    "lastUpdate": "2019-12-31T17:31:08.987+0000",
    "positionId": 450,
    "geofenceIds": [],
    "phone": "",
    "model": "",
    "contact": "",
    "category": "person",
    "disabled": false
  },
  {
    "id": 2,
    "attributes": {},
    "groupId": 0,
    "name": "Sarah",
    "uniqueId": "Sarah",
    "status": "online",
    "lastUpdate": "2019-12-31T17:29:48.771+0000",
    "positionId": 449,
    "geofenceIds": [],
    "phone": "",
    "model": "",
    "contact": "",
    "category": null,
    "disabled": false
  },
  {
    "id": 3,
    "attributes": {},
    "groupId": 0,
    "name": "Oliver",
    "uniqueId": "Oliver",
    "status": "offline",
    "lastUpdate": null,
    "positionId": 0,
    "geofenceIds": [],
    "phone": "",
    "model": "",
    "contact": "",
    "category": null,
    "disabled": false
  },
  {
    "id": 4,
    "attributes": {},
    "groupId": 0,
    "name": "Peter",
    "uniqueId": "Peter",
    "status": "offline",
    "lastUpdate": null,
    "positionId": 0,
    "geofenceIds": [],
    "phone": "",
    "model": "",
    "contact": "",
    "category": null,
    "disabled": false
  }
]

So looking at the code, and your log output, it looks like an authentication error.

Unfortunately the code will only tell you that if, at startup, when it first tests the connection, the connection works AND it gets back an error 401. If, however, the connection does not work that first time, then when it tries to connect again, if it works (and you see the message “Connection to Traccar restored”), it does not check whether or not there was an authentication problem (error 401) anymore, so it just goes on merrily getting nothing from the server. To me this is a bug in the HA integration.

I see you haven’t configured port, ssl or verify_ssl. Do you need to change them from the defaults? Or maybe your host, username or password are wrong???

Hmm. I’m using defaults for port, and no ssl at the moment, so I think that should be ok.
The logs above are from initial startup.

I tried changing the password to be invalid, restarted, and did then see
2019-12-31 18:08:54 ERROR (MainThread) [homeassistant.components.traccar.device_tracker] Authentication for Traccar failed

So I’m assuming my credentials are ok, but maybe something else is causing issues?

Hmm, ok, that’s interesting.

Hmm, maybe another “bug” in the code? I.e., when it tests the connection the first time, and if it works, it doesn’t remember the connection worked, so when it updates (which it does immediately after that, and then periodically) it will test the connection again (which is where you’re seeing the restored message), and then remembers whether or not the connection worked (but ignores any authentication errors.) And even more unfortunate is that the pytraccar package doesn’t bother to write any warnings, etc. to the log when it receives an error 401. (It leaves that to the user, which in this case is the HA integration, which mostly ignores it.) Furthermore, it ignores any status response codes besides 200 & 401.

If you ask me, both the package and the HA integration are poorly written, at least regarding proper error handling. Unless you modify the code to add some additional debugging output, it might be very difficult to figure out where it’s going wrong.

So when you tried doing the GET manually, did you use http: or https:? If the latter, you probably need to change the ssl config option to true. And what port did you use, because the default port that the HA integration uses is 8082. Is that the port your server is listening on? (Now that I ask, I’m guessing it must be because otherwise your test with bad credentials probably wouldn’t have worked.)

Hi Phil,

Thanks for your perseverance! I used http for the get, on port 8082. As you say, it’s responding to authentication so the initial connection must be working.

What’s the easiest way for me to modify the code locally? I can see the home-assistant code is at https://github.com/home-assistant/home-assistant/tree/dev/homeassistant/components/traccar and the pytraccar module at https://github.com/ludeeus/pytraccar but don’t know how I’d go about using either or both of these with the “raw” code. Could I mimic it with some sort of custom component setup or is there an easier way to do this?

There’s several ways to attack it, but if you haven’t gotten into HA coding, probably the easiest is to modify the pytraccar code “in place”, wherever HA uses it from. (Then restart HA.) Do you know where that is on your system, and do you have the ability to modify the files? (It all depends on your HA installation method.)

The file I’d recommend changing is pytraccar/api.py. At the very end of the api method (in the API class), right before the final return data statement I’d add:

_LOGGER.debug('url: %s -> connected=%s, authenticated=%s, data: %s',
              url, self.connected, self.authenticated, data)

If you’d like to share the results with me to help you interpret them, feel free to PM me.

EDIT: Actually, I’d add one more debug statement. Right before the line (further up in the same method) that contains:

if response.status == 200:

I’d add:

_LOGGER.debug('response.status=%s', response.status)

Interestingly, I found this PR that seems to be a half-a…d work around for communication errors.

I’m using a docker-compose setup - first time I’ve used docker and it seems great, but maybe not so good for this! I can’t find any reference to traccar files and assume they’re all packaged up in some way.

I’ll probably try setting up a separate dev build instance and see where I get with that, but doesn’t look like a quick job, I’m slowly falling further down the rabbit hole it seems.

Yeah, I’ve steered clear of dockers, hassio’s, etc., and just use a simple Python venv install. Everything is very easy. :slight_smile:

FWIW, it would probably be in a Python site-packages directory somewhere inside the docker container. Or maybe inside /config/deps???

Yeah thanks I tried that but no joy. I’ll try a plain python install and test it in isolation when I have some time for it.

One possibility that might be simpler, so it might be worth a try. I believe that HA will look for packages in /config first, and if found there would take precedence over the “standard” one. So if you could copy all the pytraccar files (from https://github.com/ludeeus/pytraccar/tree/master/pytraccar, although I don’t think that cli.py is needed) into /config/pytraccar/, that might make it easier for you to make temporary changes (and get rid of them later.)

Also, since this is ludeeus’s baby, and he’s often on discord, you could try and ask him for help there (or by opening an Issue on the pytraccar github site.)

Thanks, I will look into both the above in a day or two and report back

So I finally figured out the problem - it was my misunderstanding of the user accounts. I’d setup the devices on traccar under the main admin account and thus my direct http GET requests were using the same, but I was logging in using ha/pytraccar using a personal account. I hadn’t added the devices to traccar under the personal account, hence they weren’t showing.

Thanks for all the help - I only figured it out by using the traccar-api python code directly and using example.py from there to test with various permutations. Adding the debugging was also useful to confirm that the account was authenticating fine (just didn’t have any devices under it)

Edit: For anyone else who’s as much an idiot as I am - you need to create a group and add everyone to that group if you want everyone to see each others’ devices

3 Likes

I just had this exact issue.
I realised that HA was authenticating using the admin account, and the device was setup using my account.

Changing HA to use my account and everything is working as expected.

I know you typed this answer so many years ago so hopefully you, or someone who knows, will reply. Which user accounts did you have to fix? The ones in the traccar server or for HA?

There wasn’t anything wrong with the accounts themselves, it was just a case of settings the traccar account credentials in HA to use my personal traccar account details (that are associated with my phone), rather than using the traccar admin account.

If you then want to see additional users, create a group in traccar and add all users to that same group.

Interesting. The traccar client app on android doesn’t have a place to enter account details.