Reading variables don't lead to expected values

Post your questions and help other users.

Moderator: Martin

Post Reply
Joska
Posts: 3
Joined: 01 May 2020 11:09

Reading variables don't lead to expected values

Post by Joska » 05 May 2020 08:16

Hello
I am new here and come - of course - with a kind greeting to all and one or more questions;
My first flow is supposed to isolate a problem that occurs on my phone again and again, but not always, when I use the APP locusmap.
Unpredictably, there is a sharp drop of the battery level from time to time.

Now my idea: To start the APP (locusmap) I want to log the battery start level, then check the actual state of the battery every 15 minutes and in case of a severe battery loss I want to send a message/an SMS/let the phone vibrate etc. etc.

The problems:
1. automagic registers the start level beo start of the APP, but not the battery level query during the loop.
2. every time the APP is fetched from the background, the start trigger is triggered again.

Somewhere in the forum I found out that variables are scooped by conditions, but I can't find this again.
I read somewhere in the forum that AM accesses variables that can be selected in the context menu.
Somewhere in the forum I read that you can "perhaps" access these variables.

Now I'm just confused, because I can't get any further with all my attempts.

I attach the flow, and I am grateful for every tip, why the access to the variables is partly "NULL" and partly works.

Regards, Joska

I agree, my flow is incomplete, faulty and hopeful :-)

Translated with www.DeepL.com/Translator (free version)
Attachments
flow_Locucmap_Batmon_test_20200505_100600.xml
(8.19 KiB) Downloaded 765 times

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

Re: Reading variables don't lead to expected values

Post by Desmanto » 05 May 2020 12:53

Hi,

It seems you main purpose is to get warning when there is severe battery drain. I think you can extend that to all app, not only just for Locusmap. But let's focus on the locusmap only.

There are some variable you put it at the end of the script but doesn't assigned with anything. These varibales don't have any use in those cases, such as "global_battery_level". The global_battery_drain is missing probably because you commented it out in the script above it. You don't need global variable for both these.

To get current battery level inside a looped flow, you need at least a single condition "Battery Level", higher than 0%, inside the loop. This condition will always produce true. So use the true branch, and all your variable will be refreshed to current battery stat after the condition.

But using loop with sleep inside a flow is not a good way to do it. It wakelock your CPU and drain more battery when idle. Better separate it to another flow, with trigger Periodict Timer : 10s. The flow will be execute every 10 seconds, but during that windows, the CPU wakelock is released, not draining as much battery compared to the loop-sleep method. You can save the loop count in global variable.

So now your flows should splitted to 3 flows
1. Flow 1
Trigger : App Task Started : menion.android.locus
Condition : Battery Level - Higher than 0%
True, Action : Script

Code: Select all

global_battery_startlevel = battery_level;
global_battery_loop = 0;
Action : Set Flow State : Flow 3 - Enable

2. Flow 2
Trigger : App Task Ended : menion.android.locus
Action : Set Flow State : Flow 3 - Disable

3. Flow 3
Trigger : Periodic Timer : 10s
Condition : Battery Level - Higher than 0%
True, Action : Script

Code: Select all

battery_drain = global_battery_startlevel - battery_level;
global_battery_loop = global_battery_loop + 1;
Action : Notification on screen
Locusmap started
{battery_drain}
{global_battery_startlevel}
{battery_level}
{global_battery_loop}

Flow 3 is the periodic timer to check every 10 seconds. Flow 1 is to detect the start of locusmap and set the battery start level, reset loop count. Flow 2 is to detect when locusmap close, disable the periodic timer flow.
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.

Joska
Posts: 3
Joined: 01 May 2020 11:09

Re: Reading variables don't lead to expected values

Post by Joska » 06 May 2020 16:02

Hello Desmanto
First of all, many thanks for the explanation, which is so easy to understand.


I had tried to use your examples and stumbled into some one-way streets, but suddenly, after a longer break, it works. :D

One question at the moment, you said I should use a single condition and follow the real branch.
Does AM need a condition to read the bound variables? I crawled through the forums over the weekend to get a hint, but found none.
I understood it to mean that the "built-in" variables, e.g. battery_level, always have the current content, global variables, e.g. global_battery_level, are assigned these values each.
I.e., if I write global_battery_startlevel=battery_level, global_battery_level will be assigned the current value of battery_level at that time.
But this did not work and I still understand why.
Is it true that the assignment is bound to conditions? Are there any other conditions for accessing variables? Can I read this somewhere myself?

I have had some experience with AHK, but I realize that it does not fit.

Best regards, Joska

Translated with www.DeepL.com/Translator (free version)

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

Re: Reading variables don't lead to expected values

Post by Desmanto » 07 May 2020 16:11

Did you come from tasker? In tasker, built-in variable is just like global variables and provide the current value regardless where it is in the tasks. In Automagic, the built-in variables require previous elements to supply it. For example, Condition Battery Level produce multiple variables such as {battery_level}, {battery_percentage}, and so on. You can see this in the condition battery level, tap the question mark icon and you can see the supplied variables. Or you can search in Automagic help section : https://automagic4android.com/condition ... el_en.html

You can do this with any elements and see what variables it provide. If you just use Automagic, there is no way you can know/remember every possible built-in variables it has. This is why I spend my time at the beginning to read all the documentation (4 days). I won't remember everything, but at least I get a vague list of things Automagic can do, and it really gives me a boost. Now we have a full documentation you can read for every elements. You can try to read it here if you have time : https://automagic4android.com/components_all_en.html

So although they are called as built-in variables, you still need the elements to get the variable's value. For {battery_level} it is provided by several elements, such as trigger power connected or condition Battery Level (you can search at the link above). Since you need the variable after flow is trigger, so condition battery level is the only one can be used. And since condition may return true or false, we use the higher than 0% to ensure it is always true and then use the true branch. If we have something called action "Init variable battery state" (don't search, this is made up), which can provide this {battery_level} variable, then we can use that.

There are several built-in variables that provided only by certain condition. Such as condition "Wifi Available" which provide several useful variable for available wifi nearby. Other maybe provided by trigger or action. By default, when flow executed, other than variables provided by any element, it only has 3 fixed built-in variables : {flow_name}, {trigger} and {triggertime}.

Based on the same idea, to refresh a variable value inside a loop, you can't just use global variable. You need to add the elements which provide the variables; which in this case is the condition Battery Level. After execution passed thru that, all the supplied variables are renew with the new value at that moment.
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.

Joska
Posts: 3
Joined: 01 May 2020 11:09

Re: Reading variables don't lead to expected values

Post by Joska » 08 May 2020 16:06

Hello Desmant
Thank you again.
Did you come from tasker?
I come from..ahk..a little bit, more correct would be to say, I'm no programmer :D
In Automagic, the built-in variables require previous elements to supply it. For example, Condition Battery Level produce multiple variables such as {battery_level}, {battery_percentage}, and so on.
That is a major information, I'd looked for.
I spend my time at the beginning to read all the documentation (4 days). I won't remember everything, but at least I get a vague list of things Automagic can do....
Good word, I believe, I do unterstand, what you say. In AKH, I did it a little bit similar, never was able to store all commands in my brain, but had an imagine, what could lead to this or this solution....
So although they are called as built-in variables, you still need the elements to get the variable's value. For {battery_level} it is provided by several elements, such as trigger power connected or condition Battery Level (you can search at the link above). Since you need the variable after flow is trigger, so condition battery level is the only one can be used.
Next goot hint, to understand the "thoughts" of AM.
There are several built-in variables that provided only by certain condition. Such as condition "Wifi Available" which provide several useful variable for available wifi nearby. Other maybe provided by trigger or action. By default, when flow executed, other than variables provided by any element, it only has 3 fixed built-in variables : {flow_name}, {trigger} and {triggertime}.

Based on the same idea, to refresh a variable value inside a loop, you can't just use global variable. You need to add the elements which provide the variables; which in this case is the condition Battery Level. After execution passed thru that, all the supplied variables are renew with the new value at that moment.
That's the thickest chunk to digest, but I feel, it's of worth to do ;-)

Best Regards, Joska
Last edited by Joska on 13 May 2020 15:04, edited 1 time in total.

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

Re: Reading variables don't lead to expected values

Post by Desmanto » 08 May 2020 17:34

Hmmm, I just sense that you see the built-in variables just like from tasker, since I am ex-tasker (maybe I am too obsessed :D). I also use autohotkey, but almost only for keymapping and autotext only, hence not familiar with the syntax (mostly copy paste). I use Eventghost to pair Automagic to PC, it has better integration with Automagic.

It takes time to get used with the new scripting syntax. If you ever learn javascript before, Automagic would be somewhat more familiar. Anyway, if you encounter problems, you can ask here again. You can also check at my index for various usage cases.
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