0

I want to write a program that checks the time and day of the week and then switches operation modes for an energy management system based on that information. The program needs to be running and checking the day and time continuously every day throughout the whole day. It's the first part that I am struggling with the most. Based on previous work, I have tried the solution below, but I am not entirely sure if this is the most efficient or correct way to do this. I wanted to know if there was a better way to carry out this action.

#SE_v1_algorithm
#this algorithm is meant to modify the times at which the system charges and 
#discharges each day

import time
import datetime
import schedule
import os

exec(open("filename").read()) #this line runs another Python file that gives me 
#access to APIs to change operation mode to shifting energy mode

curr_day = datetime.today().weekday()

if curr_day == 5 #Saturday
    #this section of the code checks if the time is within 9:00 and 13:00; if it 
    #is, the battery discharges
    time_start = datetime.time(9,0,0)
    time_end = datetime.time(13,0,0)
    
    def time_in_range(time_start, time_end, x):
    
    if time_start <= time_end:
        return time_start <= x <= time_end
    else:
        return time_start <= x or x <= time_end
    #set to discharging (O2)
elif curr_day == 6
    time_start = datetime.time(7,0,0)
    time_end = datetime.time(13,0,0)
    
    def time_in_range(time_start, time_end, x):
    #there will be a function here to set a switch on or off for a connected system
    if time_start <= time_end:
        return time_start <= x <= time_end
    else:
        return time_start <= x or x <= time_end
    #set to discharging (O2)
else 
    time_start = datetime.time(9,30,0)
    time_end = datetime.time(15,30,0)
    
    def time_in_range(time_start, time_end, x):
    #there will be a function here to set a switch on or off for a connected system
    if time_start <= time_end:
        return time_start <= x <= time_end
    else:
        return time_start <= x or x <= time_end
    #set to discharging (O2)
martineau
  • 119,623
  • 25
  • 170
  • 301
  • What do you mean throughout the whole day? Is it supposed to check this module after every fixed period of time (say, every minute)? And is it supposed to run like a daemon (forever)? – vish4071 Apr 06 '22 at 19:39
  • Also, I don't find anything wrong with this module. It seems you want to perform task differently for Saturday, Sunday and Weekdays. However, it seems you are defining the function inside the if block. If it is supposed to have same behavior, you can define it outside the whole block and just call it here. (Also, what is `x`?) – vish4071 Apr 06 '22 at 19:43
  • @vish4071 The program is for a battery management system and I probably used the wrong term, but it's supposed to be self-regulating and run throughout the day and change the actions it takes based on the time – OopsIDidItAgain Apr 06 '22 at 20:04
  • Once you _know_ when you are, you can go to sleep until the next limit... It means that if you're Monday, 00h01, you can sleep until next Saturday 07:00, minus 1 minute, so (((5*24+7)*60 - 1)*60)=457,140 seconds... For RTC precision's sake, wake up one hour before, just in case. Nothing would happen until then. At least, you can sleep each time half the time until next check (or perform a test immediately if user is interacting / if you rebooted). No need to check date/time constantly. – Wisblade Apr 06 '22 at 20:26

0 Answers0