Page 1 of 2

Help in script logic

Posted: 25 Feb 2015 20:00
by MURTUMA
I have a small script which reads battery related values and acts accordingly. There are five conditions on which it acts upon:
1. Battery is full
2. Battery is charging (not full)
3. Battery is discharging (not full)
4. Battery is on critical(<=33%) and charging
5. Battery is on critical and discharging

Code: Select all

if (battery_percentage >= 99)
	{a = "Battery is full";
	}
	else
		{if (battery_plugged == 0 AND battery_percentage <= 33)
			{a = "Battery is on CRITICAL and DISCHARGING"; 
			}
		if (battery_plugged != 0 AND battery_percentage <= 33)
			{a = "Battery is on CRITICAL and CHARGING"; 
			}
			else
				{if (battery_plugged == 0)
					{a = "Battery is DISCHARGING"; 
					}
				if (battery_plugged != 0)
					{a = "Battery is CHARGING"; 
					}; 
				}; 
		}; 

b = "{battery_percentage, numberformat, 0}%"; 

//final speech output
if (battery_percentage >= 99)
	{d = "{a}"; 
	}
	else
		{d = "{a} at {b}"; 
		}
It works in every other instance except the last I listed. When that condition is true, the script always returns as if the battery in not in critical low but still discharging.

The issue is not big as I already have re-wrote the code differently and it works flawlessly, but I do not understand why this code doesn't work. I'd like to know that.

Re: Help in script logic

Posted: 26 Feb 2015 14:56
by tschaedl
It's cause you dont "exit" after finding "Critical and discharching". You don't use a else so afterwards it always runs in the if (batter_plugged == 0) where "a" will be overwritten with only "discharging".

Regards,
Thomas

Re: Help in script logic

Posted: 27 Feb 2015 05:12
by MURTUMA
I'm not sure what you mean. Could you elaborate it step by step?

Yeah.. Scripting is not my kung-fu.

Re: Help in script logic

Posted: 27 Feb 2015 05:16
by Nerey
Reformatted code

Code: Select all

if (battery_percentage >= 99) {
	a = "Battery is full";
} else {
	if (battery_plugged == 0 AND battery_percentage <= 33) {
		a = "Battery is on CRITICAL and DISCHARGING"; 
	}
	
	if (battery_plugged != 0 AND battery_percentage <= 33) {
		a = "Battery is on CRITICAL and CHARGING"; 
	} else {
		if (battery_plugged == 0) {
			a = "Battery is DISCHARGING"; 
        }
		if (battery_plugged != 0) {
			a = "Battery is CHARGING"; 
	   }; 
	};
}; 
Now you should see
Try something like this

Code: Select all

if (battery_percentage >= 99) {
	a = "Battery is full";
} else {
	if (battery_plugged == 0) {
		if (battery_percentage <= 33) {
			a = "Battery is on CRITICAL and DISCHARGING"; 
		} else {
			a = "Battery is DISCHARGING";
		}
	} else {
		if (battery_percentage <= 33) {
			a = "Battery is on CRITICAL and CHARGING";
		} else {
			a = "Battery is CHARGING"; 
		}
	}
}
(just from notepad)

Re: Help in script logic

Posted: 27 Feb 2015 14:16
by MURTUMA
@Nerev: Thanks for your input. However, as I said I already have a working code, so I was more hoping for an informal/educational answer to my actual question than help in fixing a problem I don't have anymore.

Nevertheless, your example gave me some useful tips I hadn't thought of.

EDIT: Now I think I partly partly understand, where the problem lies. I may have misunderstood how the if and else statements works. Does anybody care to give me a short tutorial on how to use them in more complex structures?

Re: Help in script logic

Posted: 05 Mar 2015 20:22
by Bushmills
it may be that the assertion "Battery is on critical(>33%) and charging" adds to confusion, as my feeling is that for "critical" the battery is meant to have a remaining charge of less than 33%. Whether that confusion plays a part in your logic I haven't checked, because basing logic on false assumptions usually leads to a need of reviewing code anyway.

Re: Help in script logic

Posted: 06 Mar 2015 12:39
by MURTUMA
That number is independent from android reporting critical battery charge. Also, it does not effect to script logic but only to when the specific condition is true.

The reason I chose that number is either my battery is at the end of its life or android's very inaccurate reporting of the battery level. Because of that my phone can shut down of low battery anywhere between 0 and about one third of battery.

Re: Help in script logic

Posted: 06 Mar 2015 12:45
by Bushmills
Sorry, I don't understand the meaning of what you say. For example, when expecting a condition to be the case when a value is >33 (as you say you intend to), testing that value for <=33 (which is what you do) will cause the reversal of your intended condition. In short, "if" and "else" part change places. With additional logic to combine more conditions (which is what you do), the needed logic may need to be changed too - witness: ( a and b ) is equivalent to (not ((not a) or (not b))) - superfluous parenthesises for clarity. watch how "and" changed to "or" by inverting.

Re: Help in script logic

Posted: 06 Mar 2015 14:34
by MURTUMA
Sorry, I misunderstood what you meant.

Look at the script in the first pots. There's a correct operator (<=). It seems I made a typo in the condition explanations (>). It is corrected now.

Re: Help in script logic

Posted: 07 Mar 2015 20:38
by Bushmills
In the case of your incorrectly handled condition (not plugged, percentage low), this happens:

# this is first - correctly - executed
{if (battery_plugged == 0 AND battery_percentage <= 33)
{a = "Battery is on CRITICAL and DISCHARGING";
}

if (battery_plugged != 0 AND battery_percentage <= 33)
# not executed
else
# executed
{if (battery_plugged == 0)
# executed, thereby overwriting the previous assignment to a
{a = "Battery is DISCHARGING";
}
if (battery_plugged != 0)
# not executed
};
};

One thing I'd do is: simplify. The simpler the logic, the less likely you are to miss such flaws.