Traccar via Nabu Casa webhooks

Was chatting on Discord with Ludeeus last week about amongs other things Traccar.
I am using the Traccar add-on from Frenck and the Traccar client on my iPhone is updating the Traccar add-on via a NGINX connection.

I was looking for a way to do this via Nabu Casa, as that would allow me to complety close all open ports on my router (Traccar is actually the only useful thing I have setup the NGINX server for).

Ludeeus had the idea to use the Nabu Casa webhooks to update the Traccar add-on. And more important he also had the knowledge how to achieve it and the patience to test it together with me :grinning:

This post describes the steps how to set this up. But I first wish to express my very special thanks to @ludeeus for helping me to achieve this and also for making all the great stuff for the community (HACS, the Traccar device tracker etc.). Without his help in getting the webhook to work I would still be doing trial and error changes at this moment :see_no_evil:
And of course also a big thanks to @frenck for making all these great add-ons.

What is needed

  • A local Traccar server, you can use the Traccar add-on for this
  • A Traccar client, for example the Traccar app for the iPhone
  • A Nabu Casu subscription in order to use webhooks

Setup
1. Traccar add-on

  • Make sure you set a port in the add-on. This is needed for the REST command we are going to set up. Don’t worry, the port is only going to be used on your local network.
    traccar_add-on

2. Webhook

  • Create a new automation which will be triggered by the webhook. You can check this guide on Nabu Case for more details. The automation I use is the following:
- alias: Traccar data
  initial_state: 'on'
  trigger:
    - platform: webhook
      webhook_id: traccar_info_to_addon
  action:
    - service: rest_command.traccar_addon
      data_template:
        traccar_data: "{{ trigger.query | string }}"
  • Reload the automations in HA and go to Configuration -> Cloud and scroll to the webhooks card. You will now see that the newly created webhook is available.
  • Click the toggle to enable the webhook to be accessible via the cloud. A dialog will open that will show you a unique URL that you can use to trigger your automation.
  • Copy that URL as you need this to configure the Traccar client.

3. Traccar client

  • In the Traccar client copy the URL from the step above in the field Server URL.

4. Rest command
Add the following to your configuration.yaml, where you have to change the IP address and port to match your own settings. When using the Traccar add-on, the IP address is the one of HA and the port is the one you set in the Traccar add-on step above. Make sure you use the same name (in this case traccar_addon) as you call in the action part of the automation.

rest_command:
  traccar_addon:
    url: >-
      http://192.168.1.131:8072{%set d = traccar_data.replace("<MultiDict(", "").replace(")", "").split("'") %}/?id={{d[3]}}&timestamp={{d[7]}}&lat={{d[11]}}&lon={{d[15]}}&speed={{d[19]}}&bearing={{d[23]}}&altitude={{d[27]}}&accuracy={{d[31]}}&batt={{d[35]}}
    method: POST
    username: !secret traccar_user
    password: !secret traccar_password

After this you need to restart HA in order for the rest_command to take effect.
:sparkles: Now you are using Traccar with webhooks and you can avoid to open a router port just for this. :sparkles:

6 Likes

hello, working very well, a question … you have to add something to the mariadb addon to change the traccar database, I’m not sure about the configuration
You could also explain how to change the database, I have put my data in the file traccar.xml but I am not sure if something has to be done in mariadb addon, greetings and thanks

Good to see it’s working for you.
Allthough I also have a MariaDB I don’t use that for Traccar, so can’t help you with that.
But this site it is explained how to do it -> https://www.reddit.com/r/homeassistant/comments/byeqyn/change_traccar_database/

@gerard33 Thank you very much for this information!

I was able to get this working but a couple details were missing and/or not immediately obvious to me.

I just wanted to provide some feedback of my setup experience…

  • For the Traccar Addon configuration, in addition to defining the port, I had to set "ssl": false, in the JSON configuration section to match the URL of the REST command.
  • In the REST command, for the username, I had to use the email address which was entered while configuring an account in the Traccar addon; using the actual username of that account didn’t work for me.
  • This one wasn’t necessary to get it to function but I don’t like hard coding IP addresses, for the REST URL I used localhost instead of an ip address. e.g. http://localhost:8072{%set d...

Otherwise everything worked great, and again, thanks very much for posting this info! :grinning:

1 Like

@gerard33 Is the URL in step 4 specifically use with Traccar Client or can you use the same webhook for other devices like a OBD GPS Tracker?

1 Like

That URL is specifically for use with Traccar.

Yes, I understand that. My question is can other traccar devices utilize that webhook or is it specifically for the traccar client on port 5500. So if I have an “approved” traccar device, can I use that webhook and will it process a different protocol or is it hardcoded to osmand?

I have the same question. I use a ST-90 GPS tracker in my car. It is compatible with Traccar and configuration is by sending an SMS to the device with the IP/domain settings etc. For such devices, can we use webhooks?

The url in step 4 processed the data sent by the Traccar client on iOS indeed. I don’t know if the Traccar server always expect the same data set and I don’t have other devices to test this.

But I don’t see why webhooks can’t be used in other cases as well. Good change however that you need to change the url in step 4 in order to correctly process the data.

I currently have a port open for my GPS tracker to send data to my traccar server. Is this achievable with this solution?

Have you found a solution? I doubt it’s possible to set a webhook string to one of the Chinese trackers.

EDIT: Ok, in case of GT09(ST-901) it looks like it’s possible to set the webhook path but, at least for me, my URL is probably “too long”(too many characters) and i’m getting “invalid command”. When i reduce the string a bit, it accepts that. (anyway i wonder if it even supports “https” :smiley: )
EDIT2: Yep, 152 characters is the limit for IP in case of my tracker.

@gerard33 Thank you so much for this thread! This method works like a charm and i won’t need to open any ports to outside :slight_smile: I hope i’ll make it to work also with Chinese trackers, that would be awesome.

1 Like