Set or reset circular geofence radius/coordinates

Hi, is it possible to use analysis to create or update an existing geofence (or multiple geofences)? If possible, how to do that?

My goal is to have a simple circular geofence and I would like to (re)set the coordinates and radius. So the if the gps sensor is on a different place, simply reset the coordinates of the geofence to the current place.
The radius would be something set by the end user as a setting.

Thanks!
Vincent

Hi vicent,

The idea here is:

  1. fetch the geofence variable you’ve stored.
  2. Delete the current (old) one.
  3. Re-insert a new variable.

To achieve that, you will need to create an action which will run each time your GPS sends the variable containing the location, lets say “my_location”.

That action will run a script where should update that variable with the steps listed above;

For the new data, you should change the radius property, see an geofence variable structure example below:

 {
      "id": "61155cfaaa74eb00170b1d8b",
      "origin": "60d4910e22742f001ae3c4bd",
      "serie": "1628790004721",
      "time": "2021-08-12T17:40:04.721Z",
      "value": "Geofence #2",
      "variable": "geofence",
      "metadata": {
        "event": "1111",
        "geolocation": {
          "coordinates": [
            26.05122386604599,
            -80.21217674016954
          ],
          "radius": 108.68409251287716, //CHANGE THIS BEFORE STEP 3
          "type": "Point"
        },
        "id": "Q5AnmGJNtTK9kwn8wMHfR",
        "eventColor": "black",
        "eventDescription": "when this is a event 10!!!!"
      }
    }

deleteData and sendData calls from our SDK will help you achieve that.

https://js.sdk.tago.io/

Please try to implement that and let us know if it has worked.

-Guilherme

Thanks for the steps, I’ll try and let you know!!

Vincent

Hi, thanks again for your help.

Question: for some reason my updated geofence object is incorrect and I can’t figure out what is wrong. If I compare old/updated json geofence value, they look identical, except of course the coordinates.

Per your advise, I fetch the old values, update the fetched values with the current location values, remove the current geofence data and write the updated geofence back to the device.

When I look in the map, after the update the geofence is [object Object]. What step am I missing or what step could be wrong?

Screen Shot 2021-08-15 at 8.53.45 PM

Thanks!
Vincent

Hi @vincent.raaijmakers

The last “sendData” is wrong.

Notice, you fetch the geofences and get an array of objects “gps_device_geofences”, then you loop through each obj of that array and push into updated_geofence, at the last sendData you then put that updated_geofences on value. Thats why you’re getting an [object Object] on your screen.

Two things here, you can do something like "await gps_device.sendData(updated_geofences) directly because its an array of objects using the previous geofence format, which is already into tago format.

second thing, please, prefer provide the code instead of screenshots, it’s extremely useful and easier for whom are trying to help/dubugging.

-Guilherme

Thanks Guilherme, it is working perfectly fine now!
I would not have figured this out, I truly appreciate your help.

However, this is still my confusion. I really want to understand this.

So in this part I get my geofences data from the variable “geofence”
const gps_device_geofences = await gps_device.getData({ variable: "geofence"});

So it seems so logical to put back the updated data to this variable again:

await gps_device.sendData({ variable: “geofence”, value: updated_geofences,

So, although this is working, where is the relation to the “geofence” variable in here?

await gps_device.sendData(updated_geofences);

I’m clearly missing the ‘aha’ moment :slight_smile:
Perhaps I assume that I get back the value of the variable ‘geofence’ from this call:
const gps_device_geofences = await gps_device.getData({ variable: "geofence"});

Which is not the case… or what does it give back?

Vincent

Re: “So it seems so logical to put back the updated data to this variable again:”

Place console logs/context.log(updated_geofences) or context.log(gps_device_geofences).

That you will understand better. without seeing what structure is which data its impossible to understand. You can also run externally and place breakpoints.

updated_geofences is holding something with the structure like:

 {
      "id": "61155cfaaa74eb00170b1d8b",
      "origin": "60d4910e22742f001ae3c4bd",
      "serie": "1628790004721",
      "time": "2021-08-12T17:40:04.721Z",
      "value": "Geofence #2",
      "variable": "geofence",
      "metadata": {
        "event": "1111",
        "geolocation": {
          "coordinates": [
            26.05122386604599,
            -80.21217674016954
          ],
          "radius": 108.68409251287716, //CHANGE THIS BEFORE STEP 3
          "type": "Point"
        },
        "id": "Q5AnmGJNtTK9kwn8wMHfR",
        "eventColor": "black",
        "eventDescription": "when this is a event 10!!!!"
      }
    }

Placing that on value property, thats not right.

Thanks Guilherme. Yeah it had to sink into my brains. Sometimes you need to step out… give it a break and come back.

1 Like