Partly a question on efficiency and a piece of code that others might find useful.
I have a block of conditional code that governs when some events should run (based on sensor inputs), however one of these events is a pump that recharges the main pressure tank (Aeroponics)
My cut off points in the code below are 22:16 (to stop triggering) and then begin again at 7:16)
So for anyone not familiar with the code in the basic form - it means that after 10:15pm the events will not trigger again until 7:16am…
import datetime
now = datetime.datetime.now()
hour = now.hour
minute = now.minute
if ((hour >= 22 and minute <= 15) or (hour <= 7 and minute <=15)):
trigger = False
print("False value Triggered")
else:
trigger = True
print("TRUE value Triggered")
I’m wondering if there is there a more efficient way of running the time code to determine if the events should trigger than I have here?
It looks pretty efficient to me (as well as readable and minimal). But this is what came to mind for better readability:
if (hour == 22 and minute >= 15) or hour in [23, 0, 1, 2, 3, 4, 5, 6] or (hour == 7 and minute <= 15):
trigger = False
I also have a function I created for triggers, time_between_range(start_time, end_time).
This is how it can be used within a Conditional:
from mycodo.utils.system_pi import time_between_range
start_time = "22:15"
end_time = "7:15"
if time_between_range(start_time, end_time):
# do this
else:
# do that