Device Tracker - Sending data to Traccar

Hello everyone :wink:

if anyone is interested I have developed this simple package to send data from any device_tracker from Home Assistant to Traccar, a popular open-source GPS tracking system, already integrated in HA but from Traccar to HA.

For example, I used it to track on Traccar phones that are already being tracked with Home Assistant’s iOS/Android app, without having to install any other software that sends data to Traccar (and without exposing Traccar outside my network) and my electric scooter (Silence) integrated in HA but not on Traccar.

I think Traccar is much better than Home Assistant for storing and showing travel history data, while Home Assistant is much better for automations and everything else.

You can find the script and a very small guide on how to configure and use it here.
https://github.com/lorenzo-deluca/homeassistant-traccar

If anyone wants to participate in the project go ahead and PR at will!

Let me know what you think and if you want to add contributions :slight_smile:

5 Likes

Thanks for sharing Lorenzo!
Works like a charm.
I would like to add a button with tekstbox in home assistant to send an annotation to traccar for when i see something while travelling i want to remember where approx. it is on the map.

Nice project! Works perfectly and indeed Traccar is much better at tracking history.

1 Like

Thanks, good :slight_smile:

Very interesting project!
Thinking about a possible use for myself…

Cannot get it working with a demo Traccar server.
It has ā€œhttps://demo3.traccar.org/ā€ address.

Tried this:

    url: "http://'xxx:yyy'@demo3.traccar.org:8082"

where ā€œxxxā€ - login, ā€œyyyā€ - password.
And in fact ā€œxxxā€ - email id, ā€œyyyā€ contains a ā€œ@ā€ character.
So, the line is like

    url: "http://'[email protected]:abc@'@demo3.traccar.org:8082"

Added a device on a Traccar server:
изображение

But still the device is not updated:
изображение

Modified the automation a bit, it sends a persistent notification:

...
    action:
      - service: notify.persistent_notification
        data:
          message: virtual_tracker_1
      - service: rest_command.update_traccar
        continue_on_error: true
        data:
...

So, the notification appears when the ā€œvirtual_tracker_1ā€ is updated, but the corr. device on Traccar - not updated…

I write it in documentation, for updates I use default protocol that runs on port 5055 :wink:
I never try on public demo Traccar server but I guarantee that on local server works :slight_smile

Let me know.

Sad, I am using a public server…

ok, so, you have to check if with default protocol (on port 5055) can be enable authentication… by default there is no authentication as I know.

Ciao Lorenzo,

I’m using your automation and it works great (thank you for sharing) but I have an issue with battery level with one of my devices. I’m tracking 2 samsung smartphones and a senseCAP T1000-A tracking device. Battery level are correct for my 2 smartphones but stays at 0% for my tracker. In the Sensecap integration, I can clearly see that battery level is reported correctly:

[custom_components.sensecap.sensor] entities:{'2cf7f1c064900884': {'SOS Event': <entity sensor.2cf7f1c064900884_sos_event=[]>, 'longitude': <entity sensor.2cf7f1c064900884_longitude=2.113286>, 'latitude': <entity sensor.2cf7f1c064900884_latitude=48.891124>, 'Air Temperature': <entity sensor.2cf7f1c064900884_air_temperature=20.5>, 'Light': <entity sensor.2cf7f1c064900884_light=0.0>, 'Battery': <entity sensor.2cf7f1c064900884_battery=60.0>}}

I’m pushing these data in a device.tracker using this automation:

battery: "{{ states('sensor.2cf7f1c064900884_battery') | float(0) | round(6) }}"

once these data are in the device.tracker it triggers your automation and the resulting payload is always:

Success. Url: http://192.168.4.8:5055. Status code: 200. Payload: b'?id=device_tracker.t1000_anais&lat=48.891124&lon=2.113286&altitude=0&batt=0&speed=0'

Can you please help me fix this issue, I tried to modify the traccar_positionning.yaml but with no success.

Thanks

Hi,

I found a workaround but I’m no dev, I don’t understand your code so, I’m quite sure my workaround is ugly:

actions:
      - action: rest_command.update_traccar
        continue_on_error: true
        data:
          id: '{{ trigger.entity_id }}'
          lat: '{{ state_attr(trigger.entity_id, ''latitude'')  | float(0) }}'
          lon: '{{ state_attr(trigger.entity_id, ''longitude'') | float(0) }}'
          alt: '{{ state_attr(trigger.entity_id, ''altitude'') | float(0) }}'
          batt: >
            {% set device_tracker = trigger.entity_id %}
              {% set battery_level = state_attr(device_tracker, 'battery_level') %}
              {% if battery_level is not none %} 
                {{ battery_level | float(0) }}
              {% else %}
                {% set sensor_entities  = states.sensor | selectattr('entity_id', 'match', '.*' ~ device_tracker.split('.')[1] ~ '_battery_level') | map(attribute='entity_id') | list %}
                {% if sensor_entities is not none and sensor_entities | length > 0 %}
                  {{ states(sensor_entities[0]) | float(0) }}
                {% else %}
                  {{ state_attr(trigger.entity_id, 'battery') | float(0) }}
                {% endif %}
              {% endif %}
          speed: '0'

Can you please advise why it’s doing this and if it would be better to modify your code to get a proper solution ?

Grazie mille

1 Like

Hello, yes, can be a solution.
This is because, on HA App devices the state attribute for battery is ā€œbattery_levelā€, but, in your case is only ā€œbatteryā€.

So, maybe I can add another check, and, for backward compatibility, check for both ā€œbatteryā€ and ā€œbattery_levelā€.

Can you try this version

My curiosity, how works senseCAP T1000-A ? do you use with your LoRa network? with Meshtastic or something?
Are you in EU or US?

Just to chime in on this - most device trackers don’t report the battery as an attribute any more (ha companion app as one example) so it’s a separate sensor that isn’t tied to the device tracker id. I actually ended up dropping battery level support as i have that info in ha and I only need the location!

@lorenzo-deluca Thanks for your hardwork! Wish I found this before I rolled my own - would’ve saved an hour! I actually just list my device trackers (wish there was some efficient way to run for all domain:device_tracker) with the automation below.

Also I noticed in your automation you have a condition for the from state being unavailable - would this not prevent updating in the event a device tracker changed fro unavailable to a location?

alias: Traccar Updater
description: ""
triggers:
  - entity_id:
      - device_tracker.pixel_8
      - device_tracker.juno_tracker
      - device_tracker.niro_ev_19_location
      - device_tracker.sm_g780g
    trigger: state
conditions:
  - condition: template
    value_template: '{{ trigger.to_state.state not in [''unknown'', ''unavailable''] }}'
actions:
  - data:
      input: >
        {% set device = trigger.entity_id %} {% set timestamp = now() |
        as_timestamp | int %} {% set lat = state_attr(device, 'latitude') %} {%
        set lon = state_attr(device, 'longitude') %} {% set accuracy =
        state_attr(device, 'gps_accuracy') | float %}
        id={{device}}&valid=1&timestamp={{timestamp}}&lat={{lat}}&lon={{lon}}&accuracy={{accuracy}}
    action: rest_command.update_traccar
1 Like

This is fantastic, thank you so much for putting it together. Is there any scope to import the historic data from Home Assistant?

1 Like

Honestly, I haven’t even thought about it because I think it’s the wrong place to keep the history… it doesn’t normally keep more than a few days’ worth of travel history on home assistant.
i had made a python script that opened the SQLite database but it was very slow and caused problems for HA…

Thanks. Yes, after doing some more looking around I realised that I didn’t have any particular history in HA to import. I managed to miss that my old google data was all deleted at the end of last year so was casting around for alternatives. Oh well. This is still great for future use.

1 Like

Great, I’m working on a Blueprint to integrate this more easily :wink:

Hi, I’m sorry I think I never replied to your questions.

The SenseCAP T1000-A is working ok…ish :
I can get GPS positionning if it’s exposed to clear sky.
It doesn’t work when in a bag for example.
I’ve never really managed to get GNSS+WIFI to work, it’s either one or the other (which may also be a user error, I’m not a pro).
There’s a limitation on how frequently you can send data (it’s a LoRa network limitation I believe, not a T1000-A one), it’s not really designed to get quick GPS positionning, but if speed is not needed, then it works pretty well.

As soon as it gets connection to a LoRa gateway it sends its data. It can take some time if gateway is slow or if it has a lot of position-history to upload.

I use it on a LoRa Gateway I just bought for the test. I tried other LoRa provider but I was not able to get it working. I’m based in EU. France to be precise.

I have a node-red instance in my HA that is getting data from the software stack provided by the gateway, performs data-processing and then updates a Traccar instance.

Hope this helps.

Arnaud

I got a SenseCAP T1000-A as well and can confirm your opinion.
Small form-factor but mediocre reception of both GPS and LoRaWAN.

I did test it with Helium and The Things Network LoRaWAN global networks and did not get persistant location tracking. TTN has not as many gateways as Helium (which is also declining) so the coverage in the wild of Germany is not optimal.
This tracker can be set to cache GPS fixes to upload when getting a LoRa connection which might help in some cases but not when helping LoRa coverage mapping (TTN mapper).

Which local gateway and software are you using? I recently got some 2nd hand Senscap M1 for cheap, testing with local Chirpstack and global TTN.

Getting geolocation into Homeassistant was not that easy (Node Red, MQTT, yaml, using HA actions to update GPS tracking device) for me. In HA the longitude and latitude seem to be updated not in sync, resulting in a shifted location.

Do you mind sharing your Node Red config?

tnx

Thanks for this.

I got it to work with a bit of a struggle. For others that might want to do this, here are my notes…

  1. I didn’t use the ā€˜packages’ thing as I already had some Rest commands in my configuration.yaml file. I just added this new one thus
rest_command:
  ... 
  other rest commands
  ...
  update_traccar:
     url: "http://<My domain name>:5055"
     method: GET
     payload: "?id={{id}}&lat={{lat}}&lon={{lon}}&altitude={{alt}}&batt={{batt}}&speed={{speed}}"

Note - http not https!

I didn’t use the Blueprint but just added the following automation…

alias: Traccar Update
description: >-
  This, together with the Rest command in configuration.yaml, updates Traccar
  with the position of the entities in order to store the history
triggers:
  - trigger: state
    entity_id:
      - device_tracker.uis8581a
conditions:
  - condition: template
    value_template: "{{ trigger.from_state.state not in ['unknown', 'unavailable'] }}"
  - condition: template
    value_template: "{{ trigger.to_state.state not in ['unknown', 'unavailable'] }}"
actions:
  - action: rest_command.update_traccar
    continue_on_error: true
    data:
      id: "{{ trigger.entity_id }}"
      lat: "{{ state_attr(trigger.entity_id, \"latitude\")  | float(0) }}"
      lon: "{{ state_attr(trigger.entity_id, \"longitude\") | float(0) }}"
      alt: "{{ state_attr(trigger.entity_id, \"altitude\") | float(0) }}"
      batt: "100"
      speed: "{{ state_attr(trigger.entity_id, \"speed\")  | float(0) }}"
mode: parallel
max: 10

Battery wasn’t being reported by my device so I just set it to 100.
I used the entity id device_tracker.uis8581a as the corresponding id when setting up the device in Traccar.

Note - If you try to run the Actions manually in the automation to test it, it will fail as there is no trigger.entity_id. So i tested it by going into Developer Tools/States selecting the entity and changing its state to away (then back to home). Traccar got the updates and reported it as online. Examination of the automation trace showed it all worked fine.

Thanks again - I learnt some stuff working through this.

1 Like