Python minutes to H:M:

Posted on
Tue Nov 26, 2013 6:27 pm
ELWOOD offline
Posts: 225
Joined: Feb 11, 2007
Location: Ramsey, NJ

Python minutes to H:M:

I have a variable that indicates a time in minutes I want to display it as H:M:S. Tried the following script but get an error.


# import the python timedelta class from the datetime module
from datetime import timedelta

# get the variable value
TimeMinutes = indigo.variables[519147712]

#convert to seconds
Seconds = TimeMinutes * 60


# create HH:MM:SS string to set the variable to
h = (Seconds*24)+ Seconds.seconds//3600
m = (Seconds.seconds//60)%60
s = ((newTimeDelta.days*24*60*60) + (Seconds.seconds)) - ((m*60) + (h*60*60))
newValue = "%02i:%02i:%02i" % (h, m, s)


#print to log, if it work i will then set it to a new variable
indigo.server.log (newValue)


This is the error i get

Script Error embedded script: unsupported operand type(s) for *: 'Variable' and 'int'
Script Error Exception Traceback (most recent call shown last):

embedded script, line 8, at top level
TypeError: unsupported operand type(s) for *: 'Variable' and 'int'

so i think my trouble is with converting the the minutes to seconds

Thanks in advance for any help

Elwood

Posted on
Tue Nov 26, 2013 6:39 pm
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: Python minutes to H:M:

ELWOOD wrote:
Code: Select all
# get the variable value
TimeMinutes = indigo.variables[519147712]

should look like...
Code: Select all
# get the variable value
TimeMinutes = int(indigo.variables[519147712].value)

Note that there is no error checking here, so if that variable ever has anything other than a string that looks like an integer in it, your script will throw an error and fail.

Posted on
Tue Nov 26, 2013 10:30 pm
ELWOOD offline
Posts: 225
Joined: Feb 11, 2007
Location: Ramsey, NJ

Re: Python minutes to H:M:

Thanks nsheldon

Your advice fixed my first problem. I made a few more changes to the script and have it working
with no error messages. But my seconds to hours conversion I don't have correct.
With TimeMinutes=130 minutes, the scripts runs and updates the variable TimeHM to 187202:10
Minutes are OK but my hours are way off.

Here is my current script:

# import the python timedelta class from the datetime module
from datetime import timedelta

# get the variable value
TimeMinutes = int(indigo.variables[519147712].value)

#convert to seconds
TimeSeconds = (TimeMinutes)*(60)



# create the new HH:MM string to set the variable to
h = (TimeSeconds*24) + TimeSeconds//3600 ### is this correct?
m = (TimeSeconds//60)%60

newValue = "%02i:%02i" % (h, m)

indigo.variable.updateValue(1451216940, value=newValue)


Thanks again
Elwood

Posted on
Wed Nov 27, 2013 1:26 am
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: Python minutes to H:M:

ELWOOD wrote:
h = (TimeSeconds*24) + TimeSeconds//3600 ### is this correct?

No. That'll get you a very large hour number. If we substitute values in for variables we get:
Code: Select all
TimeSeconds = 130 * 60 = 7800

h = (7800 * 24) + 7800 / 3600 = 187202.16666667 # The decimals are truncated on integer arithmetic.

If you take out the TimeSeconds * 24 part, you should get the right answer.
Code: Select all
h = 7800 / 3600 = 2.1666667 # The decimal value will be dropped in the calculation.

Posted on
Wed Nov 27, 2013 11:55 am
ELWOOD offline
Posts: 225
Joined: Feb 11, 2007
Location: Ramsey, NJ

Re: Python minutes to H:M:

Thanks nsheldon

With your help I have the script working. All of my script writing consists of finding peaces of other peoples
scripts that look like it does part of what I want and than doing some copy and past. With a little more hacking
I can get to do what I want. When I get stuck with a quick post to the indigo board there is always someone who can help.

So thanks again to all the posters for there help.

Elwood

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 9 guests