Time if times
Time if times
Good day,
I am new to MicroPython and Pico.
I got my test script to run in Python only to realize that MicroPython deals with dates and time differently, so I worked in my uPython IDE (Thonny) to get it working with those time constraints, moved to the Pico as the interpreter and now it doesn't like the time again.
I realize the Pico doesn't have realtime natively, my question is, once I get the RTC talking to the Pico, will it then address the time as I have it, or do I need to change it up for the Pico and if so, how do I know what the Pico will take or not that uPython offers? The code below works in the Thonny interpreter but not with the Pico interpreter.
Lastly, is there a better IDE to work with, I can't get the VS addon to work for some reason and while Thonny works, it gave me no warning of this issue.
TIA
from time import sleep
from time import localtime
i=0
while True:
now = localtime()
now_hour = now.tm_hour
now_min = now.tm_min
wd = now.tm_wday
start_h = 14
start_m = 48
end_h = 15
end_m = 50
if 0 <= wd < 6 and start_h <= now_hour <= end_h and start_m <= now_min < end_m:
if i <1:
print("LEDs STARTUP!")
sleep(5)
i = i + 1
else:
print("LEDs ON!!")
else:
continue
# print("LEDs OFF")
I am new to MicroPython and Pico.
I got my test script to run in Python only to realize that MicroPython deals with dates and time differently, so I worked in my uPython IDE (Thonny) to get it working with those time constraints, moved to the Pico as the interpreter and now it doesn't like the time again.
I realize the Pico doesn't have realtime natively, my question is, once I get the RTC talking to the Pico, will it then address the time as I have it, or do I need to change it up for the Pico and if so, how do I know what the Pico will take or not that uPython offers? The code below works in the Thonny interpreter but not with the Pico interpreter.
Lastly, is there a better IDE to work with, I can't get the VS addon to work for some reason and while Thonny works, it gave me no warning of this issue.
TIA
from time import sleep
from time import localtime
i=0
while True:
now = localtime()
now_hour = now.tm_hour
now_min = now.tm_min
wd = now.tm_wday
start_h = 14
start_m = 48
end_h = 15
end_m = 50
if 0 <= wd < 6 and start_h <= now_hour <= end_h and start_m <= now_min < end_m:
if i <1:
print("LEDs STARTUP!")
sleep(5)
i = i + 1
else:
print("LEDs ON!!")
else:
continue
# print("LEDs OFF")
Re: Time if times
There is only one interpreter. When using Thonny, the interpreter is the one of the Pico that you are executing. Thonny is just a super console.The code below works in the Thonny interpreter but not with the Pico interpreter.
But, when you connect the Pico to Thonny, Thonny sends the current time to the Pico so it can sets is RTC to the "correct time".
As the Pico has no RTC, at startup its own RTC is set to 00:00. And later you'll work with a time relative to the moment you powered the Pico.
If you add an external RTC, you'll be able to have the correct time.
Re: Time if times
Good day, thank you for the reply, I need to clarify I think.
The code will work without the Pico connected and Thonny is using Python 3.9 as the interpreter (under "Run->Select Interpreter->The same interpreter which runs Thonny (default))
But when I connect the Pico and use it as the "interpreter", it will not run and throws the error:
Traceback (most recent call last):
File "<stdin>", line 8, in <module>
AttributeError: 'tuple' object has no attribute 'tm_hour'
Any help would be appreciated.
The code will work without the Pico connected and Thonny is using Python 3.9 as the interpreter (under "Run->Select Interpreter->The same interpreter which runs Thonny (default))
But when I connect the Pico and use it as the "interpreter", it will not run and throws the error:
Traceback (most recent call last):
File "<stdin>", line 8, in <module>
AttributeError: 'tuple' object has no attribute 'tm_hour'
Any help would be appreciated.
Re: Time if times
So basically, runs on a host, Linux, Windows, etc, but not on a Pico. That's because the implementations of Python's time module and MicroPython's time/utime are different.deatz wrote: ↑Wed Oct 06, 2021 2:51 amThe code will work without the Pico connected and Thonny is using Python 3.9 as the interpreter (under "Run->Select Interpreter->The same interpreter which runs Thonny (default))
But when I connect the Pico and use it as the "interpreter", it will not run and throws the error:
Traceback (most recent call last):
File "<stdin>", line 8, in <module>
AttributeError: 'tuple' object has no attribute 'tm_hour'
Any help would be appreciated.
If you want to run the same code on both you will need to determine at run-time whether the code is running under Python or MicroPython and then have separate code or a means to provide the code with what it expects from what MicroPython provides.
There are a variety of ways to tell if code is running under Python or MicroPython. I have tended to favour -
Code: Select all
import sys
if len(sys.argv) == 0 : MicroPython
else : Python
Code: Select all
import sys; IS_MICROPYTHON = len(sys.argv) == 0
Re: Time if times
That does make sense, and I really only want it to run on the Pico, but I have yet to find an explanation of Utime that will allow the arguments I need. Essentially, I need to pull the current date and time, determine if it is a weekday and then at a certain time of day, between two times. When those parameters are met, drop an action. I was working on it without the Pico when I didn't have one available and of course when loaded it wouldn't run. If I am correct I will need to use "Utime" on the Pico? If so, after I pull a current time/date how do I parse out the day of week and just time of day. I found out how to do it with "datetime" and "time" and "calendar" but I haven't found documentation for Utime to pull a parsed component like the .tm_hour, .tm_min, and tm_weekday equivalents in "Utime".
Ima keep after it, seems like I've solved this three times now until I get the Pico involved
Thanks for all the input, I love to learn!
DeatZ
Ima keep after it, seems like I've solved this three times now until I get the Pico involved

Thanks for all the input, I love to learn!
DeatZ
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Time if times
Perhaps my schedule module might help. This works in a similar way to Unix cron: you can do things like schedule a callback to run on weekdays at 10:30.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Time if times
@deatz You can use time.localtime(), time.gmtime() or from the machine.RTC module datetime(). All provide the full date & time and the weekday. See also https://docs.micropython.org/en/latest/ ... .localtime and https://docs.micropython.org/en/latest/ ... achine.RTC.
Since there is no battery backed Clock in the Pico, you have to set the proper time/date after boot.
Since there is no battery backed Clock in the Pico, you have to set the proper time/date after boot.
Re: Time if times
I might be completely wrong (I never used a Pico), but what is wrong with the time module that seems to be part of the standard library ?
https://docs.micropython.org/en/latest/ ... /time.html
For your application, it's also easy to calculate the time you need from the epoch time returned by time.time().
Regards,
Thomas
https://docs.micropython.org/en/latest/ ... /time.html
For your application, it's also easy to calculate the time you need from the epoch time returned by time.time().
Regards,
Thomas
A few hours of debugging might save you from minutes of reading the documentation!
My repositories: https://github.com/karfas

My repositories: https://github.com/karfas
Re: Time if times
Finally got it, the Pico/uPython combination apparently is particular about how to parse/pull a snippet from the time function.
now = localtime()
now_hour = now[3]
now_min = now[4]
wd = now[6]
Why oh Why can't there be just a little more continuity between Python-family libraries..
Or, I'm just such a noob I can't put it all together yet, which is most likely the case.
Now, I have to figure out why it's a day behind for the day of week; year, month, date, time, year day, all good, but i thinks it's Wednesday....
Once I have that, I'll tie an RTC to it (thanks for the link Roberthh) and have what I need, I hope...
Thanks for all your input, each one helped me down the path.
Have a blessed day!
DeatZ
now = localtime()
now_hour = now[3]
now_min = now[4]
wd = now[6]
Why oh Why can't there be just a little more continuity between Python-family libraries..
Or, I'm just such a noob I can't put it all together yet, which is most likely the case.
Now, I have to figure out why it's a day behind for the day of week; year, month, date, time, year day, all good, but i thinks it's Wednesday....
Once I have that, I'll tie an RTC to it (thanks for the link Roberthh) and have what I need, I hope...
Thanks for all your input, each one helped me down the path.
Have a blessed day!
DeatZ
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Time if times
You seem to have found a bug there. On a Pyboard, on the Unix build and on CPython the day of the week is 5 (Saturday, with 0 being Monday). On the Pico it is 4.
I have raised this issue.
I have raised this issue.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.