ricardo2
Posts: 26
Joined: Tue Dec 09, 2014 12:01 am
Location: Brazil

Odometer Interval

Thu Jan 22, 2015 6:10 pm

I'm trying to create an event that triggers a notification if a car travels more than X KM in the same day, then reset the trigger every new day, but I can't figure out how to do this.

I've created an _Odometer Interval Expression, set start value to 30 and interval length to 30 but as soon as I turn it on all online cars triggers the event.

There are currently 3 cars on the test system, and their odometer value is more than 30 KM, so I guess I'm not using this expression the right way.

I want it to trigger the interval independently of the odometer value, but using only the distance it traveled on the same day.

Any help is appreciated.

Ricardo

User avatar
johan
GpsGate
Posts: 15225
Joined: Wed Aug 04, 2004 10:40 pm
Location: Sweden
Contact: Website

RE: Odometer Interval

Thu Jan 22, 2015 9:38 pm

You can use a Script Expression:
http://gpsgate.com/blogComment/script_expressions

And use a thinking similar to what is described in this guide:
http://gpsgate.com/blogComment/taking_p ... _2_minutes

Regards,
Johan

GpsGate Support

ricardo2
Posts: 26
Joined: Tue Dec 09, 2014 12:01 am
Location: Brazil

RE: Odometer Interval

Thu Jan 22, 2015 11:31 pm

Thanks for the reply.

What shall I use to get the current odometer value instead of Date().getTime() ?

context.setState(new Date().getTime());

Ricardo

User avatar
johan
GpsGate
Posts: 15225
Joined: Wed Aug 04, 2004 10:40 pm
Location: Sweden
Contact: Website

RE: Odometer Interval

Thu Jan 22, 2015 11:34 pm

fields.get("_Odometer", 0)

Read the guides linked to above, it helps.

Regards,
Johan

GpsGate Support

ricardo2
Posts: 26
Joined: Tue Dec 09, 2014 12:01 am
Location: Brazil

RE: Odometer Interval

Fri Jan 23, 2015 1:24 am

I did read, I'm just having a hard time fitting it all in my head.

I must map the odometer variable first right? It doesn't have the underline before Odometer (_Odometer), shall I really use the underline?


So is this correct?

Code: Select all

var INTERVAL = 30; // 30km
 
if (!context.state) {
    // "active" first time (no set state)
 
    context.setState(fields.get(“Odometer”,0));
    return true;
}
else {
    // not first time, since there is stuff in state
 
    var oldDistance = context.state; 
    var now = fields.get("Odometer", 0);
    var diffKMs = now - oldDistance;
 
    if (diffKMs < INTERVAL) {
      // interval didn't pass yet
      return false;
    }
    else {
      // interval passed, trigger again, set new time reference
      context.setState(now);
      return true;
    }
}
Thanks

Ricardo

ricardo2
Posts: 26
Joined: Tue Dec 09, 2014 12:01 am
Location: Brazil

RE: Odometer Interval

Fri Jan 23, 2015 1:40 am

I've tried with 30km, 30000km, _Odometer and Odometer but I keep getting false events.



Thanks

Ricardo

Juan
Posts: 35
Joined: Fri Sep 20, 2013 7:19 pm

RE: Odometer Interval

Fri Jan 23, 2015 1:03 pm

Hi Ricardo,

You should use this script:

Code: Select all

var odometer= fields.get('_Odometer', 0); // Get Odometer value
var DAILY_LIMIT = 30000; // 30km

var state = context.state;

//If no state or a new day
if(!state || state == '' || (new Date(state.date) < trackPoint.utcTimestamp)){
   var tonightMidnight = trackPoint.utcTimestamp;
   tonightMidnight.setUTCHours(0,0,0,0);
   tonightMidnight.setUTCDate( tonightMidnight.getUTCDate()+1);

   var myObject = {
      'date': tonightMidnight, //reset midnight tomorrow
      'odometer': odometer     //Odometer value
   }
   context.setState(myObject);
}
else if( (odometer - state.odometer) >= DAILY_LIMIT){ // if odometer reached the limit
   return true;
}

return false;
This will create an event if the daily driven distance is above 30km and the rule will reset daily.

In order to use it you need to enable the Odometer for each user you want by right clicking on it on the "Vehicles" panel and then enabling the odometer in the Accumulators section.

Regards,
Juan

GpsGate Support

ricardo2
Posts: 26
Joined: Tue Dec 09, 2014 12:01 am
Location: Brazil

RE: Odometer Interval

Fri Jan 23, 2015 1:08 pm

Thanks Juan, I'll try that

piter197
Posts: 832
Joined: Sun Jan 09, 2011 3:32 am

RE: Odometer Interval

Sat Jan 24, 2015 3:23 am

Hi,

Thank you for giving more script examples.

Is it 'Odometer' the same as '_Odometer' ?
In Script Editor window, I can not find _Odometer under fields.get for Test values.



Best Regards,
piter197

elcano
Posts: 13
Joined: Wed Jul 28, 2010 4:18 pm

RE: Odometer Interval

Wed Feb 04, 2015 9:03 am

Hi Juan,

If instead of daily, I want to set it for every 2 days, do I change the value 1 to 2 in :

tonightMidnight.setUTCDate( tonightMidnight.getUTCDate()+1);

Thanks.

Return to “GpsGate for Developers”