One2Track integration

I tried this config but get stuck on the first login i guess.

2024-05-27 12:57:38.068 INFO (MainThread) [homeassistant.setup] Setting up rest
2024-05-27 12:57:38.075 DEBUG (MainThread) [homeassistant.components.rest.data] Updating from https://www.one2trackgps.com/users/***/devices
2024-05-27 12:57:38.705 INFO (MainThread) [custom_components.hacs] <HacsData restore> Restore done
2024-05-27 12:57:38.857 DEBUG (MainThread) [homeassistant.components.rest] Finished fetching rest data data in 0.783 seconds (success: True)
2024-05-27 12:57:38.858 INFO (MainThread) [homeassistant.setup] Setup of domain rest took 0.79 seconds
2024-05-27 12:57:38.954 INFO (MainThread) [homeassistant.components.sensor] Setting up rest.sensor
2024-05-27 12:57:38.955 DEBUG (MainThread) [homeassistant.components.rest.data] Data fetched from resource: {"error":"You need to sign in or sign up before continuing."}
2024-05-27 12:57:38.958 WARNING (MainThread) [homeassistant.components.rest.util] JSON result was not a dictionary or list with 0th element a dictionary

config looks like this:

rest:
  - authentication: basic
    username: "***" 
    password: "***"
    headers:
      Cookie: _iadmin=11bd777e43a06ad8638e77fdf73a8197
      Accept: application/json
    scan_interval: 300
    resource: https://www.one2trackgps.com/users/***/devices
    sensor:
      - name: "one2track_***"
        unique_id: one2track_***
        json_attributes_path: "$.[?(@.device.id==*notyetknown*)]"
        value_template: "OK"
        json_attributes:
          - "device"

Does anybody know what i’m doing wrong?

I had to change the json_attributes_path to this:

json_attributes_path: "$[?(@.device.id==*notyetknown*)]"

(it lacks the period after $ before [)

even if i change that, before it comes to that stage it must login on the website and it seems that doesnt work.

If the API + documentation could be publicized somehow, that would help.

1 Like

Very stupid response but: did you login on the online dashboard first? I think I had the same first (but while trying scrape). Then update the cookie value after if it’s changed.

Another stupid thing might be that the resource link isn’t right if you have a space in the username. Change spaces to %20. Or check what the link to the dashboard is in the browser while logged in.

You could also change the link to just the base link and see where that gets you.

Apparently your response is not stupid at all:
After checking on typing errors (there where none) i logged in with chrome on one2trackgps.com.
Toggled both options:

  • agree to terms
  • remember me

When logged in (chrome only) F12 > application > storage > cookies > one2track… > _iadmin, copy value.
I pasted that value in configuration.yaml rebooted hass it started working and after i set the right device id (reboot) i was able to see the attributes of the entity.

Update: to make sure it will keep working login with an incognito window and close that window after copy of values without login out.
It almost the same as on this page only with other values.

1 Like

For me it would be pretty much the same:

  • current location
  • (force) update current location
  • is in a safezone yes/no
  • battery level
  • sim balance
  • last seen/updated

Thank you! I am very glad to read it seems possible to integrate the One2Track watches into HA. However I’m stucked following your steps.

I do have 2 sensors after pasting the YAML-code into configuration.yaml (and rebooting) - I do see two entities in Developer tools > States. I’ve replaced all uppercase parts except ‘ONE2TRACK_DEVICEID’.

The only line containing ‘one2track’ is this one:
2024-07-07 21:07:51.454 DEBUG (MainThread) [homeassistant.components.rest.data] Updating from One2Track (with my username not containing spaces instead of ‘USERNAME’)

I have no idea how to find the correct value in log files. Can you enlighten me?

I do own multiple watches. In your YAML snippet it seems to be a generic value and not device specific - is that right?

Hi all,

I am trying to link the one2track device to my HA. Unfortunately it isnt working yet. I got some warning and the sensor is visible but without any attributes

JSON result was not a dictionary or list with 0th element a dictionary

It seems that the data could be fetch when I look at the log:

2024-07-12 13:45:06.974 DEBUG (MainThread) [homeassistant.components.rest.data] Updating from https://www.one2trackgps.com/users/USERNAME/devices
2024-07-12 13:45:07.263 DEBUG (MainThread) [homeassistant.components.rest.data] Data fetched from resource: [{"device":{"id":ID,"serial_number":"SERIAL","name":"NAME","phone_number":"0031XXXXXXX","status":"WIFI","uuid":"XXXX-XXXX-XXXX","last_location":{"id":XXXX,"last_communication":"2024-07-12T13:40:01.000+02:00","last_location_update":"2024-07-12T09:08:12.000+02:00","address":"XXXXX, XXXXBD XXXXXX","latitude":"51.XXXXXX","longitude":"4.XXXXXX","altitude":"0.0","location_type":"WIFI","signal_strength":48,"satellite_count":0,"speed":"0.0","battery_percentage":60,"meta_data":null,"host":"5.XXX.66.64","port":4000},"simcard":{"balance_cents":XXXXX,"tariff_type":"prepaid"}}}]
2024-07-12 13:45:07.271 WARNING (MainThread) [homeassistant.components.rest.util] JSON result was not a dictionary or list with 0th element a dictionary

config looks like this:

rest:
  - authentication: basic
    username: "XXXX"
    password: "XXXXXX"
    headers:
      Cookie: _iadmin=3ad61c590ec1582580e490c18f8e4bf5
      Accept: application/json
    scan_interval: 900
    resource: https://www.one2trackgps.com/users/XXXX/devices
    sensor:
      - name: "ONE2TRACK_XXXX"
        json_attributes_path: "$.[?(@.device.id==0123456789)]"
        value_template: "OK"
        json_attributes:
          - "device"

Please your support with what i am doing wrong.

The device id is device specific. You should be able to find it in the log, but the line is not labeled One2track. (Which is why you couldn’t find it, I think.) Search for any of the values, latitude maybe? It should be in there. If you look at the log added by the poster below you, you see this line:

2024-07-12 13:45:07.263 DEBUG (MainThread) [homeassistant.components.rest.data] Data fetched from resource: [{"device":{"id":ID,"serial_number":"SERIAL","name":"NAME","phone_number":"0031XXXXXXX","status":"WIFI","uuid":"XXXX-XXXX-XXXX","last_location":{"id":XXXX,"last_communication":"2024-07-12T13:40:01.000+02:00","last_location_update":"2024-07-12T09:08:12.000+02:00","address":"XXXXX, XXXXBD XXXXXX","latitude":"51.XXXXXX","longitude":"4.XXXXXX","altitude":"0.0","location_type":"WIFI","signal_strength":48,"satellite_count":0,"speed":"0.0","battery_percentage":60,"meta_data":null,"host":"5.XXX.66.64","port":4000},"simcard":{"balance_cents":XXXXX,"tariff_type":"prepaid"}}}]

That’s the log you need.

After some struggling I’ve created my first ever hass plugin:

Any feedback is appreciated. Currently it only exports one entity, most likely we’d like to add some more entities based on what are now attributes.

5 Likes

I’ve updated the plugin, it seemed to only update on restarts of HA :expressionless:
It now also takes zones into account and changed the icon to a watch-icon.

@one2track in what unit is the “signal_strength” field? HA likes a location accuracy field in meters. Is there any way I can convert it like that? Or is signal_strength the cellular strength? And any change you can add the currency of the balance to the web api?

2 Likes

Nice job, worked instantly.

Enjoy the Coffee.

1 Like

nice HACS!

maybe a stupid question which not belongs in this topic, but how to remove the location from the watch when its not online? the last known location is displayed in the map and is counting for a zone, as zone.home

great work, thanks!

@onsmam what do you expect exactly? Are you saying that when the watch goes offline you want it to no longer give a location but rather be something like ‘unknown’?

I’m not sure that would be something for in the plugin or something to manage somewhere inside HASS. But I could look into something like that as an option.

yes, ive now managed to do this via an automation.
and an extra device tracker which i can manipulate.
So i can use it in the zone.home count, and see that the kids are home, so when i leave the house does not go into offline mode.

alias: One2Track
description: ""
trigger:
  - platform: state
    entity_id:
      - device_tracker.device_2
    id: 2
  - platform: state
    entity_id:
      - device_tracker.device_1
    id: 1
  - platform: time
    at: "20:00:00"
    id: tijd
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - 2
        sequence:
          - action: device_tracker.see
            data:
              gps: >
                {% if state_attr('device_tracker.device_2', 'status') != 'OFFLINE'
                %}
                  [{{ state_attr('device_tracker.device_2', 'latitude') }}, {{ state_attr('device_tracker.device_2', 'longitude') }}]
                {% else %}
                  [0,0]
                {% endif %}
              dev_id: extra_tracker_2
              gps_accuracy: |
                {{ state_attr('device_tracker.device_2', 'gps_accuracy') }}
              battery: |
                {{ state_attr('device_tracker.device_2', 'battery_level') }}
              location_name: >
                {% if state_attr('device_tracker.device_2', 'status') != 'OFFLINE'
                %}

                {% else %}
                  not_home
                {% endif %}
        alias: 2
      - conditions:
          - condition: trigger
            id:
              - 1
        sequence:
          - action: device_tracker.see
            data:
              gps: >
                {% if state_attr('device_tracker.device_1', 'status') !=
                'OFFLINE' %}
                  [{{ state_attr('device_tracker.device_1', 'latitude') }}, {{ state_attr('device_tracker.device_1', 'longitude') }}]
                {% else %}
                  [0,0]
                {% endif %}
              dev_id: extra_tracker1
              gps_accuracy: |
                {{ state_attr('device_tracker.device_1', 'gps_accuracy') }}
              battery: |
                {{ state_attr('device_tracker.device_1', 'battery_level') }}
              location_name: >
                {% if state_attr('device_tracker.device_1', 'status') !=
                'OFFLINE' %}

                {% else %}
                  not_home
                {% endif %}
        alias: 1
      - conditions:
          - condition: trigger
            id:
              - tijd
        sequence:
          - if:
              - condition: or
                conditions:
                  - condition: state
                    entity_id: device_tracker.xxx
                    attribute: status
                    state: ONLINE
                  - condition: state
                    entity_id: device_tracker.xxx
                    attribute: status
                    state: ONLINE
            then:
              - action: notify.mobile_app_iphone_van_xxx
                data:
                  message: Horloge's van de kinderen staan nog aan
              - action: notify.mobile_app_iphone_van_xxx
                data: {}
                enabled: true
        alias: Tijd
mode: single

Awesome Jeffrey!

I took the liberty to expand on your codebase, still a little bit in testing mode, but my fork of your repo supports a service send_device_command, which allows to send commands that are available through the web api of One2Track (anyone can research e.g. using chrome dev console).

My fork for now is GitHub - jurrienk/one2track: HomeAssistant plugin for one2track GPS tracking watches .Let me know if you want to work together and commit this to your repo.

e.g. shutting down a watch through home assistant can now be done like:

action: one2track.send_device_command
data:
device_id: xxxxx
cmd_code: “0048”

1 Like

Or force refresh location with cmd_code 0039
Or update GPS interval with

cmd_code: “0077”
cmd_value: 300 or 600 or 3600

etc. etc.

@jurrienx awesome, would be great to work on this together. Happy to merge any PRs into my repo.

Wouldn’t it be nicer to split out the command we know into different services? So that you can just call a shuwdown-device or force-update-location service on the devices?