how to limit the number of times a flow can be executed in a certain range of time

Post your questions and help other users.

Moderator: Martin

Post Reply
kenvega
Posts: 12
Joined: 19 Nov 2019 17:57

how to limit the number of times a flow can be executed in a certain range of time

Post by kenvega » 29 Jul 2020 23:07

Is there an easy way to limit how many times the same flow can be executed in a range of time?

Use case: a trigger can be my screen turning on to get data from an API. But I don't want to be getting data from the API too often so the flow could be stopped if I noticed that the flow ran in less than 10m ago. This way also if I don't check my phone all day I won't be getting data from the API which is good for this case.

I've tried execution count from here https://automagic4android.com/condition ... nt_en.html
but it seems to do other thing for me

Following my use case, execution count will count the number of times I turned on my screen in a certain range of time. But I just need a way to see if I did the flow some time ago. Maybe I didn't understand it correctly?

What I can also do is to set a boolean global variable to true when running the flow and then set a timer for 10m to toggle it back to false/null. And check for that variable at the start of my flow. But I want to do this for so many flows and it takes time and creates global variables :/

Any ideas?

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: how to limit the number of times a flow can be executed in a certain range of time

Post by Desmanto » 30 Jul 2020 05:50

You can't limit the trigger unless you disable the flow. But after the flow triggered, you can prevent it from continuing to main branch or any branch that retrieve the API.

If you look at my covid-19 tracker : viewtopic.php?f=3&t=8651
I do timestamp check at the beginning of the flow. If the json file saved from previous execution is not older than 60 minutes, then no need to re-retrieve the json.

Code: Select all

getDate() > addMinutes(file_last_modified, 60) //jsonfile longer than 60 minutes
True, continue to retrieve the jsonfile. False continue the flow without retrieving jsonfile.

I still put option in the input dialog to force re-retrieve the jsonfile if we need realtime data at that moment. So it is flexible to our need.

In your case, you can simply save the last execution time of the flow to the glovar. Don't save just true/false value, save the timestamp. You can put the time in nested glovar map, so 1 glovar can accomodate multiple flows' timestamp. Create a glovar map with the name global_last_execute as map type.

Then everytime it executes, save the time after successfully retrieving the API

Code: Select all

global_last_execute[flow_name] = getDate();
I use getDate() instead of triggertime as there might be some time elapsed since the flow triggered.

Later to do the check, just use

Code: Select all

getDate() > addMinutes(global_last_execute[flow_name], 60) //if last execute is longer than 60 minutes
True, continue retrieve the API. False, continue without retrieve API.

Alternative
There is other way to do it without glovar, by extracting last execute time from flows.xml. However this require additional file init, regex parsing. And it only logs last execution, so a failed API retrieval still logs as if it is success.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

kenvega
Posts: 12
Joined: 19 Nov 2019 17:57

Re: how to limit the number of times a flow can be executed in a certain range of time

Post by kenvega » 30 Jul 2020 14:09

Didn't occur to me that you could set a map for the global var. Thanks Desmanto!

I also got an easy workaround (but not sure if is good to do it):

I set a sleep timer of 10m at the end of the flow and then set the options of the flow as `Skip execution when an instance is already executing`. The issue with this one is that you don't even get to make a quick condition at the start because all the flow is skipped if repeated. And then you also see the flow as still executing in red color in the app.

Micky Micky
Posts: 179
Joined: 16 Oct 2019 17:38

Re: how to limit the number of times a flow can be executed in a certain range of time

Post by Micky Micky » 30 Jul 2020 17:52

Hello

You can use TRIGGER Global variable date/time

Add a ACTION after to change the variable. For example, add 20mins. You have to use UNIX time.

I sometimes use a random generator to add between, for example, 10mins and 30mins.

Sleep 10mins is not a good idea IMO.

Hope this helps

Micky
Crude but it works.

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: how to limit the number of times a flow can be executed in a certain range of time

Post by Desmanto » 31 Jul 2020 18:48

@kenvega : Skip + sleep at the end of flow, is equivalent to cooldown timer in tasker. Basically this will limit the flow execution at most 10 minutes once.

However, besides the downside you mention above, using sleep for longer time than 1 minute is not efficient. Automagic will create unnecessary wakelock during this duration, wasting battery for nothing. If you untick keep awake, the duration will be indefinited depends on the "maintenance window" of the doze mode.

Better use the glovar timestamp method. In most cases, we want to execute the flow. We only want to skip the API retrieval part by checking the timestamp. As you can see from the COVID-19 Tracker, I can execute the flow many times to see statistic from different parts, but only the first one retrieve the json in that one hour. I have another tracker for my country, which retrieve only daily (since my country only provide update on daily basis).

@Micky : kenvega can't use the trigger glovar datetime method for this case, as the sleep above is used as cooldown timer
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Micky Micky
Posts: 179
Joined: 16 Oct 2019 17:38

Re: how to limit the number of times a flow can be executed in a certain range of time

Post by Micky Micky » 31 Jul 2020 20:24

Hello

Yeah, I see your point. Your method is quite efficient and contained in the flow. Mine would need two switches and that's messy.

Many thanks

Micky
Crude but it works.

Post Reply