# Geofencing - Node Red, Owntracks

**URL:** <https://discourse.nodered.org/t/geofencing-node-red-owntracks/63592>\
**Category:** General\
**Created:** [8 June 2022 00:39 UTC](https://discourse.nodered.org/t/geofencing-node-red-owntracks/63592 "2022-06-08T00:39:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chapeco6](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@chapeco6](https://discourse.nodered.org/u/chapeco6)\
**Post date:** [8 June 2022 00:39 UTC](https://discourse.nodered.org/t/geofencing-node-red-owntracks/63592/1 "2022-06-08T00:39:32Z")

</div>

Hey everyone,

Im working on a project for school where I have to track when the user enters or leaves a certain area. Im using OwnTracks to get the coordinates to the broker and the node Geofence to get the valeu for "inarea". I have a function to get the coordinates and then the Geofence node. Then I have a function to count the entrys and leaves in the area but it doesnt work.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/8/58f8ca8201ad90891f7b496903df02134a03f37a.png)  
First function:

```auto
msg.payload={
    "lat":msg.payload.lat,
    "lon":msg.payload.lon,
    "name":"a41774"
}
return msg;

```

Second function:

```auto
var entrada = context.get('entrada')||0;
var saida = context.get('saida')||0;

if(location.inarea == "true"){
    entry+= 1;
    context.set('entrada',entrada);
    msg.count = entrada;
}
else if(msg.payload.inarea == "false"){
    saida+= 1;
    context.set('saida',saida);
    msg.count = saida;
}

msg.payload={
    "entrada": entrada,
    "saida": saida
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![chapeco6](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@chapeco6](https://discourse.nodered.org/u/chapeco6)\
**Post date:** [8 June 2022 00:40 UTC](https://discourse.nodered.org/t/geofencing-node-red-owntracks/63592/2 "2022-06-08T00:40:07Z")

</div>

Here are the outputs.

Output function 1:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/2/328f974eb118012e973ab8fbb1d43212a21d547a.png)  
Output fucntion 2:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/5/95bca3e57a4f9be21ae663291366132e500888dc.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [8 June 2022 07:10 UTC](https://discourse.nodered.org/t/geofencing-node-red-owntracks/63592/4 "2022-06-08T07:10:39Z")

</div>

> [@chapeco6](#):
>
> `location.inarea == "true"`

"true" is a string , that should be `true` no quotes, or just `if(location.inarea)`

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [8 June 2022 10:18 UTC](https://discourse.nodered.org/t/geofencing-node-red-owntracks/63592/5 "2022-06-08T10:18:26Z")

</div>

> [@chapeco6](#):
>
> `if(location.inarea == "true"){`

Probably should be:

```auto
if ( location.inarea === true ){

```

Note the 3 `=` and, as mentioned by others, no quotes.

Then you don't need another `else if`, only an `else`.

---

<div class="post-metadata">

**Author:** ![chapeco6](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@chapeco6](https://discourse.nodered.org/u/chapeco6)\
**Post date:** [8 June 2022 12:46 UTC](https://discourse.nodered.org/t/geofencing-node-red-owntracks/63592/6 "2022-06-08T12:46:13Z")

</div>

Thank you guys!!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [7 August 2022 12:47 UTC](https://discourse.nodered.org/t/geofencing-node-red-owntracks/63592/7 "2022-08-07T12:47:04Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
