Help in script logic

Post your questions and help other users.

Moderator: Martin

User avatar
MURTUMA
Posts: 697
Joined: 05 Mar 2013 22:43

Help in script logic

Post by MURTUMA » 25 Feb 2015 20:00

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.
Last edited by MURTUMA on 06 Mar 2015 14:35, edited 1 time in total.

tschaedl
Posts: 45
Joined: 28 Feb 2013 22:17

Re: Help in script logic

Post by tschaedl » 26 Feb 2015 14:56

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

User avatar
MURTUMA
Posts: 697
Joined: 05 Mar 2013 22:43

Re: Help in script logic

Post by MURTUMA » 27 Feb 2015 05:12

I'm not sure what you mean. Could you elaborate it step by step?

Yeah.. Scripting is not my kung-fu.

User avatar
Nerey
Posts: 102
Joined: 07 Mar 2014 16:59

Re: Help in script logic

Post by Nerey » 27 Feb 2015 05:16

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)
Sorry for bad english, my native is russian.

User avatar
MURTUMA
Posts: 697
Joined: 05 Mar 2013 22:43

Re: Help in script logic

Post by MURTUMA » 27 Feb 2015 14:16

@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?

User avatar
Bushmills
Posts: 286
Joined: 23 Sep 2014 21:56

Re: Help in script logic

Post by Bushmills » 05 Mar 2015 20:22

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.

User avatar
MURTUMA
Posts: 697
Joined: 05 Mar 2013 22:43

Re: Help in script logic

Post by MURTUMA » 06 Mar 2015 12:39

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.

User avatar
Bushmills
Posts: 286
Joined: 23 Sep 2014 21:56

Re: Help in script logic

Post by Bushmills » 06 Mar 2015 12:45

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.

User avatar
MURTUMA
Posts: 697
Joined: 05 Mar 2013 22:43

Re: Help in script logic

Post by MURTUMA » 06 Mar 2015 14:34

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.

User avatar
Bushmills
Posts: 286
Joined: 23 Sep 2014 21:56

Re: Help in script logic

Post by Bushmills » 07 Mar 2015 20:38

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.

Post Reply