Webhook parsing issue with quotes

I’ve got a weird issue with passing my payload from my electron to my REST receiver. In my code I have the following line.

message = "{\"_type\":\"location\",\"t\":\"a\",\"tid\":\"" + tid + "\",\"acc\":50,\"lat\":" + String(LATVAR) + ",\"lon\":" + String(LONVAR) + ",\"tst\":" + Time.now() + "}";

It creates a string similar to:


However, when I set up my webhook, with this json

  "topic": "owntracks/tC/tC",
  "payload": "{{PARTICLE_EVENT_VALUE}}"

I get this as a response on the other end:


Any ideas on how I can format my data on the electron or the webhook config to get it to pull the data through clean?

I basically need the equivalent of this.

curl -X POST -H "Content-Type: application/json" -d '{"topic":"owntracks/tC/tC","payload":"Test"}' http://server/api/services/mqtt/publish
1 Like

You’re getting the behavior because the webhook code currently assumes all mustache templates (like {{PARTICLE_EVENT_VALUE}} ) produce a string. And, then logically it must also escape all of the JSON special characters like double quotes in the string, which is why you get " and such.

If your server can accept numeric fields (like your acc, lat, etc.) as a string, not a number, then this can be worked out using a slightly more complicated template. I haven’t tested it, but it would look something like:

  "topic": "owntracks/tC/tC",
  "payload": {"_type":"{{_type}}", "tid":"{{tid}}", "acc":"{{acc}", "lat":"{{lat}}", "lon":"{{lon}}":"tst":"{{tst}}"}

Being able to have a mustache variable with an explicit type (boolean, int, float, string, or JSON) is a planned feature, but it’s not available yet, unfortunately.


Thanks, so that gave me this on the received side:


Based on what you did there though, does that mean there is a way I can pass multiple variables with values in my particle.publish command and have it assign them where they need to be?

Sorry, I was afraid that might happen. In some fields, like the json in a plain web hook, you can create a mustache template that references the elements within PARTICLE_EVENT_VALUE, parsing it as a JSON object. You can even grab values from inside objects and indexed arrays. It’s neat, but it doesn’t work for all fields, so I wasn’t sure it would work for payload.

1 Like

Ah, got it working with tedious elimination. I had it only pass one parameter from the string and worked my way up until I got to the lon/lat. Apparently that was tripping it up, I think when they didn’t have values it broke it. Added some error checking to assign 0.0 to both if it doesn’t have a valid GPS read. Now it’s passing through using your JSON.

Thanks :smiley:

1 Like

Just in case anyone else ever wants to do this, I’m using the electron to post MQTT data in owntracks format so that HomeAssistant can pick it up and use it for location data. Using MQTT from the electron directly used way too much data and for some reason I could never get HomeAssistant to parse it right. I discovered that HA actually has a REST API, I could pass my payload to http://HAServer/api/services/mqtt/publish. By using the following code on the device:

message = "{\"_type\":\"location\",\"tid\":\"" + tid + "\",\"acc\":50,\"lat\":" + String(LATVAR) + ",\"lon\":" + String(LONVAR) + ",\"tst\":" + Time.now() + "}";
Particle.publish("gpsinfo", String(message), 60, PRIVATE);

Combined with a tweaked version of Rick’s JSON in a webhook.

  "topic": "owntracks/vehicles/{{tid}}",
  "retain": true,
  "payload": "{\"_type\":\"{{_type}}\",\"tid\":\"{{tid}}\",\"acc\":{{acc}},\"lon\":{{lon}},\"lat\":{{lat}},\"tst\":{{tst}}}"

I was able to get this working, so now I can track our vehicles using homeassistant and the electrons. Next step will be working that into some automation smarts :smile:

Be sure to include the headers homeassisstant needs as part of the config for the webhook, there’s a section for that.
x-ha-access = your home assistant password
Content-Type = application/json

Hey Rick, there isn’t a way to reference returned items by index # from PARTICLE_EVENT_VALUE is there?

Like if I just had the Electron publish “TC,30.333333,-18.325654,846728292” and then used this JSON for the webhook?

  "topic": "owntracks/vehicles/{{PARTICLE_EVENT_VALUE}}[0]",
  "retain": true,
  "payload": "{\"_type\":\"location\",\"tid\":\"{{PARTICLE_EVENT_VALUE}}[0]\",\"acc\":50,\"lon\":{{PARTICLE_EVENT_VALUE}}[1],\"lat\":{{PARTICLE_EVENT_VALUE}}[2],\"tst\":{{PARTICLE_EVENT_VALUE}}[3]}"

As far as I know it doesn’t work like that. The array support works like this:



You can reference items:

{{a.0.x}}     -> 123
{{a.1.x}}     -> 456

That’s not really better than the way you had it before, but it can be useful in other cases.

1 Like

Hey, having some trouble with this myself. Trying to pass args=“1,100,30” to my device. I am able to hit the device, but the arguments are not passed. I’ve tried every imaginable formatting but have not had success. This does work with cURL and Postman.

switch 2:
  - platform: rest
    name: 'Particle Devices'
    resource: https://api.particle.io/v1/devices/1e0026001247343339383037/WARM_STRIP/?access_token=secret
    method: POST
    body_on: '{\"args\":\"1,100,30\"}'