Question about timer (inexact)

Post your questions and help other users.

Moderator: Martin

Post Reply
soutre
Posts: 15
Joined: 18 Aug 2019 07:01

Question about timer (inexact)

Post by soutre » 24 Aug 2019 15:10

I've made a small flow that will give me a notification about the air quality in my area. There are 4 options for the inexact timer trigger:

15 minutes, 30 minutes, 60 minutes, 12 hours, 24 hours.

I'd like to have the notification roughly every 45 minutes and it is not essential that my phone receives the notification (or runs the flow) until I am looking at my device or it's on. In other words, the information is not urgent. But I also don't want it to be running every time I power on my device. For battery's sake and peace of mind.

Solution 1) Add a sleep chain in the flow. If my device is powered off at the execution time, it will continue to loop until I'm back to using the device. This seems counter intuitive because Automagic needs to be using resources in the meantime to support the sleep chain?

Solution 2) Keep the inexact timer, which will set a global variable if the device is powered off. A trigger will run when the device unlocks, checking for the global variable, and run the flow.

Solution 3) Add a simple date modified check in the flow to a created file. If the flow runs when the phone is off it will touch a file and stop the flow. When the device is powered on the flow will run, check the time between now and file touch time, and then run the flow.

I am concerned about resource use in the background with these approaches. And maybe there is one that's much better? I've carefully read the way Android doze/sleep mode works, and wakes, etc. Am I just better off entirely using the inexact timer set at 30 minutes? It's kind of unnerving to receive updates about information I don't need at the moment.

Thanks!

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

Re: Question about timer (inexact)

Post by Desmanto » 25 Aug 2019 17:48

If you want a stop-periodic timer, don't use periodic timer trigger. But use Global Variable Datetime, which I think it is one of the best time-related feature from Automagic.
A periodic timer will always run within the set interval. So 60 minutes will always guarantee it will execute 24 times a day, regardless you are looking at it or not. But using Glovardt, you can add the interval only if you interact with the last execution. So even if you add interval 1 hour, but if you don't interact with the last execution from 8 hours ago, it won't re-execute again during that 8 hours.

Main Flow
You can use trigger glovardt with global_air_check.
Add condition, screen on
- true, execute your air quality check flow. Show the message dialog (if you use that one) or any interaction you use.
- Once you press OK (you acknowledge you have seen this execution), then use script to add 45 minutes from now to the glovardt.

Code: Select all

global_air_check = addMinutes(getDate(), 45);
for false (from screen on), enable another helper flow, Air Check Screen on. Done.
So the false branch pass the checking to another helper flow, to check the next time when you have turned on the screen.

Air Check Screen on flow
Use trigger user present (triggered once you unlock the phone). First action is to set flow state, itself to disable (so it is a one time trigger). Next action is to execute the main flow. This means this is one time helper flow to re-execute the main flow the next time you turn on screen. This flow only used when you missed the 45 minutes check.

Combined together, this will have the best efficiency trigger count. Suppose you put your phone down for 8 hours with interval 60 minutes, then the main flow only execute twice (once to pass the job to the helper flow and another after you have turn on the screen). 3 times if you count the helper flow execution too. While using periodic timer, you surely will have 8 times execution. Besides, this also guarantee you do the air quality check once you have unlock the phone, not some data from 8 hours ago.

Another best combination with the glovardt is you can use custom interval at different time! My language learning flow shows new words at the interval of 1 hour during workhour, 30 minutes after workhour and suspended after 00:00 until next morning. You can choose to add different interval based on the time you execute it, not at a fixed interval. The difference at my flow is I don't use helper flow, since I show it in widget, which stays until I interact to it. I also don't need latest data (execution).
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.

soutre
Posts: 15
Joined: 18 Aug 2019 07:01

Re: Question about timer (inexact)

Post by soutre » 28 Aug 2019 22:45

Sorry for the delayed response these days are busy and short time to give a careful response. Very nice solutions and I did some testing with periodic timer (inexact) and it did go off on somewhat regular intervals for the past two days even when I wasn't using the device.

I like your idea though would prefer everything to be inside one flow without a helper flow? So to check a global variable with an expression in the flow itself like:

Code: Select all

triggertime-global_previous_trigger>=45*60*1000 or trigger=="Manual"
And then later in the flow:

Code: Select all

global_previous_trigger=triggertime
When the flow starts, if false it will end the flow. If true it will continue and reset the global variable to the new trigger time. This combined with User Present should give a similar result? Though that is only if using a single user present flow. Since trigger time is a variable generated when the flow starts, this expression would calculate the difference between the trigger times.

It seems the positive side to a single helper flow is there wouldn't be too many flows using "user present" and flows could be disabled and enabled more fluidly. I'll probably end up switching to that method in the future because it seems like a great idea. Thank you so much for the great idea.

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

Re: Question about timer (inexact)

Post by Desmanto » 29 Aug 2019 19:24

Sometimes it can't helped. I also always prefer one flow for all the similar logic flow. But for flow that can be triggered too many times without doing anything, better split it to another flow and enable/disable on occassion. Trigger user present, if enabled all the time will be triggered everytime we unlock the phone. Yes, we can add check to the triggertime to make sure it doesn't execute the main flow. But most of the unlock, we will ended in false, and the flow triggered but doing nothing, wasting unnecessary resource. Hence better keep it disabled if not needed.

Your method will works too, and if it works well, no need to change it. Since period timer inexact will usually triggered only on next wake.
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.

Post Reply