time = '%02d:%02d:%02d' % (dt.hour, dt.minute, dt.second)
the time is always displayed with 2 digits for hr, min and seconds. I like however to strip the leading zero for the hour, and leave seconds out completely if at the full minute exactly.
using:
time = '%2d:%02d:%d' % (dt.hour, dt.minute, dt.second)
does the correct thing for the hour (at least i think so, have to see what happens at 00:01:01…), but still shows me only 0 for the seconds when it is under the first 10 seconds of the minute.
Can I somehow do this, without introducing complete new if then else logic?
It is merely a question of saving space in a card, and optical hygiene. Can leave the seconds out if they’re :00, need both digits always for the minutes, even at :00, and can leave out the leading 0 for the hour:
its to display this:
last automation ran at 10:10:00 so could have been dislplayed as 10:10.
It’s no big deal, but since anything is possible in HA/Python, I was looking for a way
No, dt.hour is an integer, so it doesn’t show a leading zero. Same goes for dt.minute and dt.second.
The error you get is due to the f-string literal notation to represent a string, looks like this isn’t supported in home assistant scripts, I tested it in AppDaemon and there it worked
Try this:
if dt.second == 0:
time = ‘{:d}:{:02d}’.format(dt.hour, dt.minute)
else:
time = ‘{:d}:{:02d}:{:02d}’.format(dt.hour, dt.minute, dt.second)
Basically if you want a leading 0 -> {:02d}
if you don’t want a leading 0 -> {:d}
Python also allows formatting strings using strftime. It doesn’t have logic to shave off seconds when zero, but it has a built-in way of handling hours.
from datetime import time
dt = time(0, 0, 1)
mytime = f'{dt:%-H:%M:%S}' if dt.second else f'{dt:%-H:%M}'
print(mytime)
Yes totally forgot about strftime
However with your code, I’m not sure if it will work due to the f-string literal notation, which didn’t work for him in my example above.
if I remember correctly from my earlier efforts in other scripts, these imports aren’t allowed either…
Fri Oct 11 2019 11:54:42 GMT+0200 (CEST)
Error executing script: __import__ not found
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 195, in execute
exec(compiled.code, restricted_globals, local)
File "last_automation.py", line 1, in <module>
ImportError: __import__ not found
yes it does, and i must confess it was what I started to look for in the beginning (the dash ‘%-h’) but didn’t realize it could be done directly in python scripts… I use it in yaml all the time duh.
no it doesnt… sorry:
Fri Oct 11 2019 13:16:44 GMT+0200 (CEST)
Error executing script: '__import__'
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 195, in execute
exec(compiled.code, restricted_globals, local)
File "last_automation.py", line 21, in <module>
KeyError: '__import__'
thought it to show correctly at startup, but now that I dont see anything happening, checked the log and above appears…