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:

7 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.

2 Likes

Very interesting post. Thank you.
I am still a bit lost between the Traccar Integration and the Traccar Add-On.
In the first place, I thought that the purpose of the Traccar Integration was to send information about my existing devices in HA to the Traccar add-on, but that’s the opposite.
My goal was to reuse the device trackers I get in HA (from the HA Android App or other sources) and mirror their status in the HA Addon (for long term tracking and other things like keep the intelligence of zone tracking in Traccar). I don’t want to use the Traccar client on the phones I already track with the HA app.
From what I see, there is nothing to do a such thing with the current components.
So my idea would be to reuse the logic presented here with the REST command but the automation trigger would be state update of my current device trackers in HA. The parameters of the request would be extracted from the attribute of my device trackers.

Does it make sense or do I miss something easier there?

This works flawlessly with my setup, thanks for this.

I’m considering buying a OBD GPS tracker for my car. I think it is not that likely that works with this solution as I understand there are a lot of different protocols for there GPS trackers, correct? The URL in rest_command would need to be reworked, right?

Thanks for sharing this. It works flawlessly. However, I have been thinking. If HA can receive Traccar data via webhook, why do we still need a Traccar server? Can’t we just update the device_tracker entity directly using device_tracker.see service? I have tried this. unfortunately unsuccessful.

automation:
- alias: Traccar data
  initial_state: 'on'
  trigger:
    - platform: webhook
      webhook_id: traccar_info_to_addon
  action:
    - service: device_tracker.see
      data_template:
        dev_id: >-
          {%- set query = trigger.query|string -%}
          {%- set d = query.replace("<MultiDict(", "").replace(")", "").split("'")[3] -%}
          {%- if d == 111111 -%} user1_gps
          {%- elif d == 222222 -%} user2_gps
          {%- elif d == 333333 -%} user3_gps
          {%- elif d == 444444 -%} user4_gps
          {%- endif -%}
        gps: >-
          - {%- set query = trigger.query|string -%}{{ query.replace("<MultiDict(", "").replace(")", "").split("'")[11] }}
          - {%- set query = trigger.query|string -%}{{ query.replace("<MultiDict(", "").replace(")", "").split("'")[15] }}
        gps_accuracy: >- 
          {%- set query = trigger.query|string -%}
          {{ query.replace("<MultiDict(", "").replace(")", "").split("'")[31] }}
        battery: >- 
          {%- set query = trigger.query|string -%}
          {{ query.replace("<MultiDict(", "").replace(")", "").split("'")[35] }}

Hey! How did you set URL as the server adress in ST-901?

Sending command like 804[password] [URL from Webhook] doesn’t work (no replay from tracker). I tried to shorten the address but no success at all.

After sending the command “RCONF”, it show old IP adress with old port.

I have the same question, did you get any solution?

I’m trying to shorten the url.

Nope. I don’t know how to make it work… Someone can help?

Blockquote * 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 

I am very new to HA and I cant get this
to work, can anybody Help me…
@gerard33

1 Like

I also would like to know how to make this work on a st-901.

If anybody can give me advice?