Owntracks - How to Update Device Tracker via HTTP (Without Owntracks)

Hi, after the hassio update i am unable to update my device.tracker_XYZ via a HTTP API request. I used to do it via Automate and a JSON Request:

JSON POST
Request URL:

https://XYZ.duckdns.org/api/states/device_tracker.XYZ?api_password=XYZ

Request Content:

{“state”: location, “attributes”: {“source_type”: “gps”, “latitude”: lati, “longitude”: long, “gps_accuracy”: accuracy, “battery”: “100”, “tid”: “02”, “friendly_name”: “XYZ”, “entity_picture”: “/local/images/XYZ.jpg/)”}}

However, when i do this now i always get an error:

Login attempt or request with invalid authentication from [IP ADDRESS]

This only happens with this device tracker. Same style of JSON Post request with a switch or other device (also using API Password) works just fine.

Anybody have an idea how i can switch to a different authentication method within automate or a JSON Post ?

Ok found the solution:

The long lived token needs to go into the

Request Header:
{“Authorization”: “Bearer YOURLONGACCESSTOKEN” }

Remove the:

?api_password=XYZ

from Request URL
Everything else can stay the same

hey @AlmostSerious

are you using HTTP mode ? i want to publsih/remote config the android clients? but how do you publish this to an android client from HA?

I do not know about remote config of owntracks android client via HA. All im using is the http owntracks sensor via manually creating the input with a http rest api service call

I think I need that too…
Somewhere in HA I need to setup somekind of configuration, so all Android clients can read that json…

My goal is to define the monitoring mode from HA, so that I can disable gps on android from HA when it’s not needed… To preserve battery…

How did you setup that sensor?

You could probably employ a push message and then use tasker or automate to enable / disable GPS.

The Sensor can be setup as described in my first post. Simply send all the values via HTTP Rest API. You could copy the layout of an already existing sensor.

Hi, thnx, but thats the beauty of it, i dont need tasker normally, with http mode its actually possible to send remote config to the android client

so yes, i need something like you in the first post, but i dont understand the link?

how do you make a sensor, with the json below in it, and how do you push it trough a client?

{
"_type":"cmd",
"action":"setConfiguration",
"configuration":
{
"_type": "configuration",
"monitoring": 2
}
}

Just send it to the API. It will be created with the name you choose.
https://developers.home-assistant.io/docs/en/external_api_rest.html

yeah, if i send the state to the API, but how is it linked to the android client? where do you specify the android client were you want to send the remote config too ? is it that “tid”: “02”

is that somekind of unique id?

I am not sending anything to any android client. My Android will send the state to the API. Thats it. I use it to save my parking position with the car without having owntracks even installed. I just “emulate” the owntracks app.

ok, thnx

ok, gonna do a curl to get the state of the api , i should retrieve the monitoring mode normally and hopefulle somekind of id
hopefull i can do with a POST the update of that moniring mode

thnx for putting me in right direction with that rest api

can you help me one more time :slight_smile:
just struggling with this

with this command i can actually see states so GET command is working

curl -k -X GET -H "Authorization: Bearer xxxxxx" https://192.168.0.17:8123/api/states/device_tracker.fabio_owntracks

but where to send the remote config too with rest api ?

this gives me :

curl -k -X POST -H "Authorization: Bearer xxxxxxx" -d '{"_type":"cmd", "action":"setConfiguration", "configuration":{"_type": "configuration","monitoring": 2}}' https://192.168.0.17:8123/api/states/device_tracker.fabio_owntracks

error message : “no state specified”

so i still dont know how to send a message

more info here:

https://owntracks.org/booklet/tech/json/#_typeconfiguration

Looks like you are not updating a state to the home assistant sensor. Try my example in the above post and see what happens.

What should happen:
The state of your device_tracker.fabio_owntracks should update to the values you manually specified in your curl message.

I’m still not sure that what you actually want to achieve though, can be done with this way.

yeah, but your command is only changing states in HA , i need it pushed back to the android client :frowning:dont think its possible

I never thought of updating the device tracker directly…I ended up doing much the same thing, but with a webhook, and parsing the owntracks JSON and sending a device-tracker-compatible MQTT payload.

Can you share how you did it?

Sure.

device_tracker:
  - platform: mqtt_json
    devices:
      steve_location: location/tracker/steve

automation:
  - alias: location_owntracks
    trigger:
      - platform: webhook
        webhook_id: <webhook_id>

    action:
      - condition: template
        value_template: >
          {{ trigger.json.acc | int <= 400 }}

      - service: mqtt.publish
        data_template:
          topic: >
            {%-
              set people =
              {
                "st": "steve",
                "wi": "wife",
                "k1": "kid_1",
                "k2": "kid_2"
              }
            -%}
            {%- set person = people[trigger.json.tid | lower] |lower -%}
            location/tracker/{{ person }}
          retain: true
          payload: >-
            {{
              {
                'latitude': trigger.json.lat,
                'longitude': trigger.json.lon,
                'battery_level': trigger.json.batt | round(0) | int,
                'gps_accuracy': trigger.json.acc | round(0) | int,
                'provider': 'owntracks'
              } | to_json
            }}

What did you mean by you “need it pushed back to the android client”?

Yeah, I want to push settings/config to the Android client , settings like, monitoring mode, to enable location on/off

What android client do you mean? I’m just using the nabu casa web UI. If I want to shut off location tracking, I just turn off that automation.