Python Forum

Full Version: schedule module conundrum
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I'm composing a simple temperature alarm notification system, and I'm stuck. I have everything working great with the exception of some scheduled functions ("reminder of active alarm" and "a weekly communication test". Currently I'm trying the "schedule module" but I'm having the same problems I did when I tried using "Timer=" and "threading" to accomplish this task. It appears that these function take a snapshot of the task and recycles it. I can't seem to get it to pull "current variables" like datetime and current temp before it executes the task. I'm pretty sure my problem is somewhere in *args & *kwargs but I can't figure it out. I've read the py.docs and googled the hell out of this, but I keep getting lost and I can't find an answer. None of the examples seem to illustrate this and I got no one to ask. I'm self-taught and I have to say I'm pretty impressed with how far I got. But, I'm stuuuck!! I've been banging my head on this little quirky problem for 4 days now. How does one pull current variables into a scheduled, timer or threaded function. Attached is a snip of my code where I think the problem is. If you run it, it illustrates the problem I'm referring to. datetime and currenttemp are getting recycled and are not getting updated prior to execution of the task/ I know I'm doing something fundamentally wrong, because I was getting similar results when I tried to use a timer and threading to accomplish this task.

import schedule
#import threading
import time
from datetime import datetime
currenttime = datetime.now()
#hr = int(currenttime.strftime("%-H")) 
dt = currenttime.strftime("%c")
tempss = 1 #sensor status
tempsh = 1 #sensor history
currenttemp = 43 #random set value, actual value called from device file.
def job():
    print (message) #actual job script contains smtp protocals with execute send 
    
while True:
    if tempss ==1 and tempsh == 1:
        message= """
        Alarm is still activated
        Current temperature is {}F
        {}""".format (currenttemp, dt)
        schedule.every(1).minutes.do(job), dt 
        schedule.run_pending()
    else:
        schedule.cancel_job()
Your description makes it very difficult to understand the issue. If you don't have an error message to post, could you post expected output vs actual output?
(May-28-2019, 08:42 PM)Gribouillis Wrote: [ -> ]Your description makes it very difficult to understand the issue. If you don't have an error message to post, could you post expected output vs actual output?

The message content should contain the current temp and the current date and time the message was sent. But the content of the message is repeating from the moment of first execution. Same "temp" & "time". over and over. The values are not being updated.

the currenttemp was set to a hard value in the example code for the purposes of this post. In my actual code, the temp is read from a device file and that's not getting updated either. (I couldn't figure out how to show that in the post.

The scheduled job is not "polling" the current "variable values" to insert into the content of the message. IE temp & time. this can be seen in my example code. the message will cycle, but the date and time don't change.

If I have the scheduled event set to take place every 1 min. the timestamp within the message should be 1 min apart from the previous message. It is not. It's like the "schedule module" took a snapshot of the message then re-issues it at whatever time-marker I set.

As simply as I can put it "the variables contained within the message and not getting updated prior to the execution of the scheduled event".. which is "send message"

if someone could show me the proper way to get the "current" datetime in the message prior to its scheduled excecution (IE:send message), I think I can figure out the temperature. atleast I hope.
Somthing like this so the values are updated, cant test it, don't have schedule installed
import schedule

# import threading
import time
from datetime import datetime

# currenttime = datetime.now()
# hr = int(currenttime.strftime("%-H"))
# dt = currenttime.strftime("%c")
tempss = 1  # sensor status
tempsh = 1  # sensor history
currenttemp = 43  # random set value, actual value called from device file.


def job():
    currenttime = datetime.now()
    dt = currenttime.strftime("%c")
    message = """
    Alarm is still activated
    Current temperature is {}F
    {}""".format(
        currenttemp, dt
    )
    print(message)  # actual job script contains smtp protocals with execute send


while True:
    if tempss == 1 and tempsh == 1:
        schedule.every(1).minutes.do(job)
        schedule.run_pending()
    else:
        schedule.cancel_job()
Edit: looking at the module example the job schedule should be outside of the while loop
schedule.every(1).minutes.do(job)

while True:
    if tempss == 1 and tempsh == 1:
        schedule.run_pending()
    else:
        schedule.cancel_job()
    time.sleep(1)
Re: Putting the time protocols in the job. I tried that yesterday.. no joy..

as far as putting it outside the loop, how can you control it once it's no longer needed?
See my edit above,your while loop is constantly adding job schedule's
outside the loop would work for a "weekly communication test".
But I can't see how I could manipulate that for a "hourly reminder of an alarm in an active state".
schedule.every().hour.do(job)
(May-28-2019, 10:09 PM)Yoriz Wrote: [ -> ]
schedule.every().hour.do(job)
sure. but outside the loop or "job" I can't call or cancel it.

Maybe some insight of the "application intention" is needed. Commercial cooler, set to 40F. If the temp goes above 40F; alarm triggered. An hourly reminder message containing the current temp would show "trending" up or down. Did they get a delivery, or did the unit take a dump?

You did just solve my problem of a "weekly comms test", Thank you! I am 1 step closer.

but I'm still stumped as to an reminder of an "active alarm" showing current (trending) temperature.

I'm 90% there. Woohoo! I'll just keep hitting it with a hammer till it works
If someone could propose a work-around. I’m open to suggestions. I still can’t figure out how to do pull a variable in a scheduled task from within a loop. It’s my very last piece of the puzzle before I can call this project done. I’m open to ideas and suggestions.

I know once a thread gets to long people stop looking
Pages: 1 2