Progressively longer location check intervals

Post your questions and help other users.

Moderator: Martin

Post Reply
Almir
Posts: 2
Joined: 02 Jan 2015 10:23

Progressively longer location check intervals

Post by Almir » 02 Jan 2015 10:28

I'm trying to create a flow that checks my location and then if I am static at the same location, it increases the length it waits until the next check. So, for instance, the flow is set to check every five minutes. I would like it to notice if I am not moving any more and then progressively reduce the checks from one in five minutes to one in 10 minutes, then to 30 and then to one hour etc. How would I go about this in the most simple way possible?

Almir

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Progressively longer location check intervals

Post by Martin » 02 Jan 2015 15:04

Hi,

You could build multiple flows each with a trigger Periodic Location Update with different timings like 5m, 10m and 30m and use an action Set Flow State to enable only the flow you are currently interested in.
Alternatively you could use trigger Global Variable Date/Time and an action Init Variable Location and set the variable that holds the time of the next trigger time using a script.

Something like this might work (untested). The flow doubles the update interval each time when you are still at the same place.
-trigger Global Variable Date/Time
-action Init Variable Location
-action Script

Code: Select all

if (global_last_location == null)
{
   // initially set the update interval to one minute when the flow is executed for the first time
   global_update_interval = 60000;
}
else
{
   // calculate distance of the last location and the currently known location
   d = distance(global_last_location, location);

   if (d > 1000)
   {
      log("moved more than 1000m, reset update interval to one minute");
      global_update_interval = 60000;
   }
   else
   {
      global_update_interval = global_update_interval * 2;
      log("not moved, increase update interval to {global_update_interval}");
   }
}
global_last_location = location;

global_trigger_date = getDate() + global_update_interval;
You could also split the script into multiple conditions Expression and multiple actions Script.

The getDate() function call in the last line returns the current time of the system. It's better to use getDate() than variable triggertime in this case to ensure that the next check really is in the future and not already passed when initializing the location takes a long time and triggertime is in the past. It's not an issue when you set the timeout in Init Variable Location to a value that's shorter than the minimum of global_update_interval.

Regards,
Martin

Almir
Posts: 2
Joined: 02 Jan 2015 10:23

Re: Progressively longer location check intervals

Post by Almir » 02 Jan 2015 15:13

Thanks very much for the elaborate response, Martin, and for the code you provided. I'll try and make that work with my flows as you suggest.

Thinking about this, it occurred to me how practical it would be to be able to change trigger parameters from inside a flow. This would make it much simpler to implement things like this and many other scenarios I suspect.

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Progressively longer location check intervals

Post by Martin » 02 Jan 2015 15:23

It's on my list of features to support variables in triggers at some point which should also make such things a little bit easier but it will certainly take some time until I can start working on this feature.

Post Reply