I'm making a Artificial Inteligence battery monitor look's like on iOS13 and i need to log the battery percentage/hour/plugged only when the user connect or disconnect the charger plug.
i tried to do something like:
if str(plugged) == "True":
log_file.write(current_info + "\r\n")
elif str(plugged) == "False"
log_file.write(current_info + "\r\n")
but the script don't stop to loop on "True"
Here is the main function of my code
log_file = open("activity_log.txt", "w")
while True:
battery = psutil.sensors_battery()
# Check if charger is plugged in or not
plugged = battery.power_plugged
# Check for current battery percentage
percent = str(battery.percent)
# Check for the current system time
sys_time = datetime.datetime.now()
current_info = percent + " " + str(sys_time) + " " + str(plugged)
if str(plugged) == "True":
log_file.write(current_info + "\r\n")
log_file.close()
the project on github if you want to test or implement it: https://github.com/peterspbr/battery-ai
If I have understood you correctly you want to exit the loop when variable plugged
is True? Something to take into account is that Python is a string typing language, that means, that it is not the same "True" and True.
log_file = open("activity_log.txt", "w")
plugged = False
while not plugged:
battery = psutil.sensors_battery()
# Check if charger is plugged in or not
plugged = battery.power_plugged
# Check for current battery percentage
percent = str(battery.percent)
# Check for the current system time
sys_time = datetime.datetime.now()
current_info = percent + " " + str(sys_time) + " " + str(plugged)
if str(plugged) == "True":
log_file.write(current_info + "\r\n")
log_file.close()
PD: I am assuming variable batery.power_plug
is a bool type.