Trying to use rest command to send tracking data to local traccar server

I don’t know if it’s possible but I’m trying to forward my cat’s location (using Tractive integration) to the Traccar server using the rest_command.

I won’t pretend that I understand the API but it’s here. Specifically this part.

This is what I have at the moment, I’m not using any templates as I’m just trying to send the information to the traccar server.

    url: http://trigkey.local:8080/api/devices/3
    method: POST
    headers:
      username: traccar login email
      password: traccar login password
      accept: "application/json, text/html"
      user-agent: "Mozilla/5.0 {{ useragent }}"
      verify_ssl: false
    payload: '{"id": 3,"name": "Alice","uniqueId": "729160","status": "online","disabled": false,"lastUpdate": "2019-08-24T14:15:22Z","positionId": 0,"groupId": 0, "phone": "string","model": "string","contact": "string","category": "string","attributes": {}}'
    content_type: "application/json; text/html"

and here is the result I get from Services.

  servlet: org.glassfish.jersey.servlet.ServletContainer-41f582ee
  message: Bad Request
  url: /api/devices
  status: "400"
status: 400

If I go to this location
http://trigkey.local:8080/api/devices/3

I get a page with this
{"id":3,"attributes":{},"groupId":0,"calendarId":0,"name":"Jim","uniqueId":"729160","status":"offline","lastUpdate":null,"positionId":0,"phone":null,"model":null,"contact":null,"category":null,"disabled":false,"expirationTime":null}

And because I have it, here’s a section of my traccar log.

2024-02-19 22:13:03  INFO: [T664b87ca: gnx < 172.18.0.1] 2a310d0a24340d0a696e666f0d0a
2024-02-19 22:13:06  INFO: [T664b87ca] disconnected
2024-02-19 22:13:18  INFO: [T2c58bcb0] connected
2024-02-19 22:13:21  INFO: [T2c58bcb0: gnx < 172.18.0.1] 00000031ff534d4272000000000000000000000000000000000000000000000000000100000e00024e54204c4d20302e3132000200
2024-02-19 22:13:24  INFO: [T2c58bcb0] disconnected
2024-02-19 22:13:24  INFO: [Td470386b] connected
2024-02-19 22:13:24  INFO: [Td470386b: gnx < 172.18.0.1] 0000006efe534d4240000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024000400010000000000000000000000000000000000000000000000000000000000000002021002000302031103
2024-02-19 22:13:27  INFO: [Td470386b] disconnected
2024-02-19 22:13:39  INFO: [T1552c554] connected
2024-02-19 22:13:42  INFO: [T1552c554: gnx < 172.18.0.1] 160301010a010001060303aa4a7097161c1337aa0145910449006e72a9a609433ba94e9c011130e1c1576c20f6d4e79bdb415dc0311c3466745d7b4e25760dc285df1c460eeca24679bd3729003cc02bc02fc02cc030cca9cca8c009c013c00ac014009c009d002f0035c008c012000ac024c028c023c027003c003dc007c0110005000413011302130301000081000500050100000000000a000a0008001d001700180019000b00020100000d001a0018080404030807080508060401050106010503060302010203ff010001000017000000120000002b000b0a03040303030203010300003300260024001d00203fd7df69fe2cdad15b644b6d5f3eb9d4f66d63bcff092feb918cc36fe7917f73
2024-02-19 22:13:48  INFO: [T1552c554] disconnected

I’ve been at it all day :slight_smile: and getting nowhere.

Thanks

No problems, I’ve got it sorted, took me 2 days and the end result is visually dissappointing, but it works. I can forward any device_tracker to Traccar now :slight_smile:

Put the following in configuration.yaml

  update_traccar: # Forwards Device Tracker to Traccar
    url: http://192.168.0.114:5055/
    method: POST
    headers:
      accept: "application/x-www-form-urlencoded"
      user-agent: "Mozilla/5.0 {{ useragent }}"
    payload: '{{input}}'
    content_type: "application/x-www-form-urlencoded"

and this goes into your automations.yaml

- action:
  - service: rest_command.update_traccar
    data:
  input: >-
    {% set id = 729160 %}
    {% set timestump = now() | as_timestamp | int %}
    {% set lat = state_attr('device_tracker.alice_tracker', 'latitude') %}
    {% set lon = state_attr('device_tracker.alice_tracker', 'longitude') %}
    {% set accuracy = state_attr('device_tracker.alice_tracker', 'gps_accuracy')|float %}
    {% set batt = state_attr('device_tracker.alice_tracker', 'battery_level')|float %}
    {% set charge = states('binary_sensor.alice_tracker_battery_charging') %}
    id={{id}}&valid=1&timestamp={{timestump}}&lat={{lat}}&lon={{lon}}&accuracy={{accuracy}}&batt={{batt}}&charge={{charge}}
  alias: Traccar forward Alice
  condition: []
  id: '2768845976145'
  trigger:
  - platform: state
    entity_id: device_tracker.alice_tracker

That seems to do the trick, I was stuck on basically not knowing what I was doing :slight_smile:

Edited: changed the input, it’s basically the same but I was having occasional errors.

1 Like

Hey @RoadkillUK - I am awake (in Oz) and looking into this now but I guess you are sleeping (UK).

Just checking that the first part of this solution needs to be configured in configuration.yaml based around the following?


rest_command:
  jago_traccar:
    url: http://rpi5.lan:8082/
    method: POST
    headers:
      accept: "application/x-www-form-urlencoded"
      user-agent: "Mozilla/5.0 {{ useragent }}"
    payload: '{{input}}'
    content_type: "application/x-www-form-urlencoded"

Yeah, that’s right, I’ll go edit my first post for clarity and remove the reference to the cat. :slight_smile:

I’ve always been a night owl, so I’ll still be up for a while … it’s the weekend after all.

Getting very close I think but getting an error when I run the automation - no response from the server.

Have checked all the deets and they look good. Same error running from the developer / services consol. I’m thinking the request might be getting blocked by my adblocker so I will check that next.

Of course not adblocker as this is all lan side of my network. So a bit stuck

Just tried to reach this page and it wanted username/pwd for traccar the first time I tried.

http://rpi5.lan:8082/api/devices/3

Maybe there is a setting in traccar to allow all device requests. Can you share your traccar config that might help me?

The other thing is my HA server is using ssl and traccar is not so maybe something with this mixed setup.

it has just started working. So I think me doing the initial authentication has solved it.

Very strange though. I will keep an eye on it

1 Like

I’m glad you got this sorted, as much of what you have said is way over my head :slight_smile:

still having issues. some updates work as expected and others get the server response issue.

I will keep working on it and post back if I make progress to get consistent results. My gut feeling is it could be related to allowing duplicated requests in traccar

hey @RoadkillUK - i’m making good progress and trying to add altitude to the position/device update.

I can’t understand where you found how and what to include in the data/input. I have been looking at the api reference for traccar but I am a bit lost. Can you let me know how you worked this out so I can build on it?

- action:
  - service: rest_command.update_traccar
    data:
      input: "id=729160&valid=1&timestamp={{ now() | as_timestamp | int}}&lat={{ state_attr('device_tracker.alice_tracker', 'latitude') }}&lon={{ state_attr('device_tracker.alice_tracker', 'longitude') }}&accuracy={{ state_attr('device_tracker.alice_tracker', 'gps_accuracy') | float }}&batt={{ state_attr('device_tracker.alice_tracker', 'battery_level')|float}}"
  alias: Traccar forward Alice
  condition: []
  id: '2768845976145'
  trigger:
  - platform: state
    entity_id: device_tracker.alice_tracker

Using the above, copy the input: line to Developer Tools / Template, the output window will show you what the format is for sending information (once you have put in your own sensors)

My output looks like this

"id=729160&valid=1&timestamp=1710019320&lat=53.77988&lon=-1.740813&accuracy=13.0&batt=77.0"

Using this link, it shows you what variables are available.

Mine starts with the id and each variable is separated by an &, as long as you stick to that format, everything should be fine.

Good Luck :slight_smile:

I got lost in that too, I don’t understand it so I left it alone … I ended up using wireshark to find out what it was actually sending, then copied that.

Thanks for the link above. That is helpful.

All working - no more errors - they were being caused by something else stopping the traccar server responding in time.

I’m getting very close to having this all working with location based speed limit data now in the mix.

I’ve just done this. Based off the companion app. Have a text template sensor where the lat long are updated and that calls traccar using the osmd protocol.

I’m now trying to figure out if I can pull in my garmin watch latest actovoty (either via garmin or Strava) and push all those waypoints into traccar as well. Figure if I’m tracking a few things. Should track all the things

Hello @psyciknz , could you please share some detail of your template sensor implementation for the companion app. I am trying to implement a similar solution, but I am having problems with the automation. I keep getting errors “extra keys not allowed”. Many thanks in advance!