Traccar.org (GPS/GPRS Tracker) custom platform

Hi,

some weeks ago i bought a cheap GPS/GPRS tracker with an ODBII port to track my car.
https://nl.aliexpress.com/store/product/gps-Tracker-GPS306B-SMS-Real-time-tracking-2-4G-Attendance-management-Plug-Play-SOS-Alarm-Truck/530436_1889292868.html

These cheap chinese and other GPS/GPRS trackers can be configured to work with http://traccar.org, an excellent tool for these devices and fleet management.
But as it it turns out the tracker i bought doesn’t seem to get the ODO and fuel level information via ODB from my Ford (Ford issue i think) so i started looking what else i could do with that thing…

This ended up in me making a custom device tracker for HASS that talks to the traccar api.
The traccar platform for hass works as the same as any other device tracker.
It will add a device for every device you have setup in traccar and you can use these for presence detection, google travel times etc.

add to your configuration:

device_tracker:
   platform: traccar
   username: traccar_user
   password: traccar_password
   host: traccar_host ip/name including port number

put the following file in a path cal custom_components/device_tracker/

many thanks to @w1ll1am23 as i borrowed most of the code from his trackr device tracker platform.

8 Likes

If i well understand it will show the position on the integrated map in hass right ? Awesome as traccar works just fine

yes it should (but for me the map isn’t working in HASS for some reason unrelated to this platform plugin).
The devices you get in HASS are created the same way as any other devices that is tracked like the iCloud or router device trackers

these traccar attributes are added to the HASS devices:

           ['latitude'], ['longitude'])
           ['accuracy']
           ['totalDistance'],
           ['ip'],
           ['protocol'],
           ['speed'],
           ['address'],
           ['status'],
           ['lastUpdate']

@koen01
This is very interesting and I’m currently looking into this.
It seems that you can even host your own traccar server on a raspberry pi.
Based on the GSP306B manual it is only possible to configure an IP.
My IP changes on a daily basis, so a dnydns hostname was good.
Do you know, if it is possible to use also an hostname with the device?

From the documentation:
6.36.3. IP and Port Setup
Send SMS as below via a cell phone: adminip+password+Space+IPAddress+Space+Port Number.
If succeeded, “adminip OK” is returned by the device in SMS.
E.g. Send SMS command “adminip123456 202.104.150.75 9000” to the tracker device, If
succeeded, “adminip OK” is returned by the device in SMS. ( 123456 is default password,
202.104.150.75 is IP,9000 is port).

Thanks

i’m not sure, maybe ask at the traccar forums? i’m not sure if there is a ip forwarding service like dyndns but on a ip level.

I am using my orange pi with traccar and dns from noip.com… WORKS awesome… i will now integrate this platfirm and give feedback :slight_smile:

17-04-16 22:53:33 ERROR (MainThread) [homeassistant.components.device_tracker] Error setting up platform traccar
Traceback (most recent call last):
File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/components/device_tracker/__init__.py", line 159, in async_setup_platform
disc_info)
File "/usr/lib/python3.4/asyncio/futures.py", line 388, in __iter__
yield self  # This tells Task to wait for completion.
File "/usr/lib/python3.4/asyncio/tasks.py", line 286, in _wakeup
value = future.result()
File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result
raise self._exception
File "/usr/lib/python3.4/concurrent/futures/thread.py", line 54, in run
result = self.fn(*self.args, **self.kwargs)
File "/home/hass/.homeassistant/custom_components/device_tracker/traccar.py", line 27, in setup_scanner
TraccarDeviceScanner(hass, config, see)
File "/home/hass/.homeassistant/custom_components/device_tracker/traccar.py", line 40, in __init__
self._update_info()
File "/home/hass/.homeassistant/custom_components/device_tracker/traccar.py", line 47, in _update_info
self._devices = requests.get(self._host + '/api/devices', auth=(self._username, self._password))
File "/srv/hass/hass_venv/lib/python3.4/site-packages/requests/api.py", line 70, in get
return request('get', url, params=params, **kwargs)
File "/srv/hass/hass_venv/lib/python3.4/site-packages/requests/api.py", line 56, in request
return session.request(method=method, url=url, **kwargs)
File "/srv/hass/hass_venv/lib/python3.4/site-packages/requests/sessions.py", line 488, in request
resp = self.send(prep, **send_kwargs)
File "/srv/hass/hass_venv/lib/python3.4/site-packages/requests/sessions.py", line 603, in send
adapter = self.get_adapter(url=request.url)
File "/srv/hass/hass_venv/lib/python3.4/site-packages/requests/sessions.py", line 685, in get_adapter
raise InvalidSchema("No connection adapters were found for '%s'" % url)
requests.exceptions.InvalidSchema: No connection adapters were found for 'emevth.no-ip.biz:8082/api/devices'

i get this error … when i call this URL : emevth.no-ip.biz:8082/api/devices I see my device

Traceback (most recent call last):
File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/components/device_tracker/__init__.py", line 159, in async_setup_platform
disc_info)
File "/usr/lib/python3.4/asyncio/futures.py", line 388, in __iter__
yield self  # This tells Task to wait for completion.
File "/usr/lib/python3.4/asyncio/tasks.py", line 286, in _wakeup
value = future.result()
File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result
raise self._exception
File "/usr/lib/python3.4/concurrent/futures/thread.py", line 54, in run
result = self.fn(*self.args, **self.kwargs)
File "/home/hass/.homeassistant/custom_components/device_tracker/traccar.py", line 27, in setup_scanner
TraccarDeviceScanner(hass, config, see)
File "/home/hass/.homeassistant/custom_components/device_tracker/traccar.py", line 40, in __init__
self._update_info()
File "/home/hass/.homeassistant/custom_components/device_tracker/traccar.py", line 48, in _update_info
self._device_data = json.loads(self._devices.text)
File "/usr/lib/python3.4/json/__init__.py", line 318, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.4/json/decoder.py", line 343, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.4/json/decoder.py", line 361, in raw_decode
raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)

CONFIG:

  - platform: traccar
    username: Thorsten
    password: password
    host: http://emevth.no-ip.biz:8082

Can you post the /devices and /positions output your Traccar server gives?
I could only test with my devices, maybe your output differs?

u mean what is written im : emevth.no-ip.biz:8082/api/devices ?

[{"id":1,"attributes":{},"name":"Thorsten","uniqueId":"788818","status":"unknown","lastUpdate":"2017-04-16T20:35:36.301+0000","positionId":2237,"groupId":0,"geofenceIds":[],"phone":"","model":"","contact":"","category":null}]
17-04-17 10:52:23 ERROR (MainThread) [homeassistant.components.device_tracker] Error setting up platform traccar
Traceback (most recent call last):
File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/components/device_tracker/__init__.py", line 159, in async_setup_platform
disc_info)
File "/usr/lib/python3.4/asyncio/futures.py", line 388, in __iter__
yield self  # This tells Task to wait for completion.
File "/usr/lib/python3.4/asyncio/tasks.py", line 286, in _wakeup
value = future.result()
File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result
raise self._exception
File "/usr/lib/python3.4/concurrent/futures/thread.py", line 54, in run
result = self.fn(*self.args, **self.kwargs)
File "/home/hass/.homeassistant/custom_components/device_tracker/traccar.py", line 27, in setup_scanner
TraccarDeviceScanner(hass, config, see)
File "/home/hass/.homeassistant/custom_components/device_tracker/traccar.py", line 40, in __init__
self._update_info()
File "/home/hass/.homeassistant/custom_components/device_tracker/traccar.py", line 48, in _update_info
self._device_data = json.loads(self._devices.text)
File "/usr/lib/python3.4/json/__init__.py", line 318, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.4/json/decoder.py", line 343, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.4/json/decoder.py", line 361, in raw_decode
raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)

Can you add an extra device in the traccar frontend? Just like ‘dummy’ device? I want to check if that fixes your problem.

Just adding a device with device ID imaginary ?

Devices:

[{“id”:1,“attributes”:{},“name”:“Thorsten”,“uniqueId”:“788818”,“status”:“unknown”,“lastUpdate”:“2017-04-17T13:22:40.408+0000”,“positionId”:2529,“groupId”:0,“geofenceIds”:[],“phone”:"",“model”:"",“contact”:"",“category”:null},{“id”:4,“attributes”:{},“name”:“dummy”,“uniqueId”:“123456”,“status”:“offline”,“lastUpdate”:null,“positionId”:0,“groupId”:0,“geofenceIds”:[],“phone”:"",“model”:"",“contact”:"",“category”:null}]

And positions

[{“id”:2529,“attributes”:{“battery”:“78.0”,“ip”:“37.168.140.67”,“distance”:14.74,“totalDistance”:33867.13},“deviceId”:1,“type”:null,“protocol”:“osmand”,“serverTime”:null,“deviceTime”:“2017-04-17T13:22:40.000+0000”,“fixTime”:“2017-04-17T13:22:40.000+0000”,“outdated”:false,“valid”:true,“latitude”:48.89721361,“longitude”:2.52762264,“altitude”:128.0,“speed”:0.0,“course”:0.0,“address”:“10 Boulevard de l’Est, Le Raincy, ÃŽle-de-France, FR”,“accuracy”:0.0,“network”:null}]

17-04-17 15:46:36 ERROR (MainThread) [pydroid_ipcam] Failed to communicate with IP Webcam: [Errno 113] Cannot connect to host 192.168.1.17:8080 ssl:False [Can not connect to 192.168.1.17:8080 [Connect call failed (‘192.168.1.17’, 8080)]]
17-04-17 15:46:38 ERROR (MainThread) [pydroid_ipcam] Failed to communicate with IP Webcam: [Errno 113] Cannot connect to host 192.168.1.34:8080 ssl:False [Can not connect to 192.168.1.34:8080 [Connect call failed (‘192.168.1.34’, 8080)]]
17-04-17 15:46:49 ERROR (MainThread) [pydroid_ipcam] Failed to communicate with IP Webcam: [Errno 113] Cannot connect to host 192.168.1.34:8080 ssl:False [Can not connect to 192.168.1.34:8080 [Connect call failed (‘192.168.1.34’, 8080)]]
17-04-17 15:46:50 ERROR (MainThread) [pydroid_ipcam] Failed to communicate with IP Webcam: [Errno 113] Cannot connect to host 192.168.1.17:8080 ssl:False [Can not connect to 192.168.1.17:8080 [Connect call failed (‘192.168.1.17’, 8080)]]
17-04-17 15:47:03 ERROR (MainThread) [pydroid_ipcam] Failed to communicate with IP Webcam: [Errno 113] Cannot connect to host 192.168.1.34:8080 ssl:False [Can not connect to 192.168.1.34:8080 [Connect call failed (‘192.168.1.34’, 8080)]]
17-04-17 15:47:04 ERROR (MainThread) [pydroid_ipcam] Failed to communicate with IP Webcam: [Errno 113] Cannot connect to host 192.168.1.17:8080 ssl:False [Can not connect to 192.168.1.17:8080 [Connect call failed (‘192.168.1.17’, 8080)]]
Stopping Home Assistant…
Config directory: /home/hass/.homeassistant
Starting Home Assistant…
Started Home Assistant.
17-04-17 15:47:47 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_registered[L]: service=turn_off, domain=homeassistant>
17-04-17 15:47:47 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_registered[L]: service=turn_on, domain=homeassistant>
17-04-17 15:47:47 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_registered[L]: service=toggle, domain=homeassistant>
17-04-17 15:47:47 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_registered[L]: service=stop, domain=homeassistant>
17-04-17 15:47:47 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_registered[L]: service=restart, domain=homeassistant>
17-04-17 15:47:47 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_registered[L]: service=check_config, domain=homeassistant>
17-04-17 15:47:47 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_registered[L]: service=reload_core_config, domain=homeassistant>
17-04-17 15:47:48 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_registered[L]: service=create, domain=persistent_notification>
17-04-17 15:47:48 INFO (MainThread) [homeassistant.bootstrap] Home Assistant core initialized
17-04-17 15:47:48 INFO (MainThread) [homeassistant.loader] Loaded logger from homeassistant.components.logger
17-04-17 15:47:48 INFO (MainThread) [homeassistant.setup] Setting up logger
17-04-17 15:47:59 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: Entity IDs can not be None for dictionary value @ data[‘trigger’][0][‘entity_id’]. Got None. (See /home/hass/.homeassistant/configuration.yaml, line 131). Please check the docs at https://home-assistant.io/components/automation/
17-04-17 15:48:03 ERROR (Thread-4) [homeassistant.components.sensor.rest] Error fetching data: <PreparedRequest [GET]>
17-04-17 15:48:03 ERROR (Thread-9) [homeassistant.components.sensor.rest] Error fetching data: <PreparedRequest [GET]>
17-04-17 15:48:03 ERROR (Thread-10) [homeassistant.components.sensor.rest] Error fetching data: <PreparedRequest [GET]>
17-04-17 15:48:03 ERROR (Thread-4) [homeassistant.components.sensor.rest] Unable to fetch REST data
17-04-17 15:48:03 ERROR (Thread-9) [homeassistant.components.sensor.rest] Unable to fetch REST data
17-04-17 15:48:03 ERROR (Thread-10) [homeassistant.components.sensor.rest] Unable to fetch REST data
17-04-17 15:48:10 ERROR (MainThread) [homeassistant.components.device_tracker] Error setting up platform traccar
Traceback (most recent call last):
File “/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/components/device_tracker/init.py”, line 159, in async_setup_platform
disc_info)
File “/usr/lib/python3.4/asyncio/futures.py”, line 388, in iter
yield self # This tells Task to wait for completion.
File “/usr/lib/python3.4/asyncio/tasks.py”, line 286, in _wakeup
value = future.result()
File “/usr/lib/python3.4/asyncio/futures.py”, line 277, in result
raise self._exception
File “/usr/lib/python3.4/concurrent/futures/thread.py”, line 54, in run
result = self.fn(*self.args, **self.kwargs)
File “/home/hass/.homeassistant/custom_components/device_tracker/traccar.py”, line 27, in setup_scanner
TraccarDeviceScanner(hass, config, see)
File “/home/hass/.homeassistant/custom_components/device_tracker/traccar.py”, line 40, in init
self._update_info()
File “/home/hass/.homeassistant/custom_components/device_tracker/traccar.py”, line 48, in _update_info
self._device_data = json.loads(self._devices.text)
File “/usr/lib/python3.4/json/init.py”, line 318, in loads
return _default_decoder.decode(s)
File “/usr/lib/python3.4/json/decoder.py”, line 343, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File “/usr/lib/python3.4/json/decoder.py”, line 361, in raw_decode
raise ValueError(errmsg(“Expecting value”, s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)
17-04-17 15:48:12 ERROR (MainThread) [pydroid_ipcam] Failed to communicate with IP Webcam: [Errno 113] Cannot connect to host 192.168.1.34:8080 ssl:False [Can not connect to 192.168.1.34:8080 [Connect call failed (‘192.168.1.34’, 8080)]]
17-04-17 15:48:12 ERROR (MainThread) [pydroid_ipcam] Failed to communicate with IP Webcam: [Errno 113] Cannot connect to host 192.168.1.17:8080 ssl:False [Can not connect to 192.168.1.17:8080 [Connect call failed (‘192.168.1.17’, 8080)]]
^Cpi@home-assistant:~ $

Still same error after adding dummy device .

comment out line 59 & 60 (add a # )

‘totalDistance’: position_id[‘attributes’][‘totalDistance’],
‘ip’: position_id[‘attributes’][‘ip’],

and try again.

17-04-18 14:42:17 ERROR (MainThread) [homeassistant.components.device_tracker] Error setting up platform traccar
Traceback (most recent call last):
File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/components/device_tracker/__init__.py", line 159, in async_setup_platform
disc_info)
File "/usr/lib/python3.4/asyncio/futures.py", line 388, in __iter__
yield self  # This tells Task to wait for completion.
File "/usr/lib/python3.4/asyncio/tasks.py", line 286, in _wakeup
value = future.result()
File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result
raise self._exception
File "/usr/lib/python3.4/concurrent/futures/thread.py", line 54, in run
result = self.fn(*self.args, **self.kwargs)
File "/home/hass/.homeassistant/custom_components/device_tracker/traccar.py", line 27, in setup_scanner
TraccarDeviceScanner(hass, config, see)
File "/home/hass/.homeassistant/custom_components/device_tracker/traccar.py", line 40, in __init__
self._update_info()
File "/home/hass/.homeassistant/custom_components/device_tracker/traccar.py", line 48, in _update_info
self._device_data = json.loads(self._devices.text)
File "/usr/lib/python3.4/json/__init__.py", line 318, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.4/json/decoder.py", line 343, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.4/json/decoder.py", line 361, in raw_decode
raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)

Still same error

ok. i had some time to experiment with your data. I think it has to do with the the non ascii characters in the address that traccar returns.

Add a

# -*- coding: utf-8 -*-

as the first line in traccar.py and restart home-assistant.

I did and this is the error message:

17-04-18 16:13:54 ERROR (MainThread) [homeassistant.components.device_tracker] Error setting up platform traccar
Traceback (most recent call last):
File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/components/device_tracker/__init__.py", line 159, in async_setup_platform
disc_info)
File "/usr/lib/python3.4/asyncio/futures.py", line 388, in __iter__
yield self  # This tells Task to wait for completion.
File "/usr/lib/python3.4/asyncio/tasks.py", line 286, in _wakeup
value = future.result()
File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result
raise self._exception
File "/usr/lib/python3.4/concurrent/futures/thread.py", line 54, in run
result = self.fn(*self.args, **self.kwargs)
File "/home/hass/.homeassistant/custom_components/device_tracker/traccar.py", line 28, in setup_scanner
TraccarDeviceScanner(hass, config, see)
File "/home/hass/.homeassistant/custom_components/device_tracker/traccar.py", line 41, in __init__
self._update_info()
File "/home/hass/.homeassistant/custom_components/device_tracker/traccar.py", line 49, in _update_info
self._device_data = json.loads(self._devices.text)
File "/usr/lib/python3.4/json/__init__.py", line 318, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.4/json/decoder.py", line 343, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.4/json/decoder.py", line 361, in raw_decode
raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)

Wait. between your post and your file there’s a difference i just test :

#-*- coding: utf-8 -*-
````in your code and 

```# -*- coding: utf-8 -*- ```