Convert to Python?

Posted on
Sun Jun 06, 2021 2:19 pm
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Convert to Python?

Is this script convertible to python?

Code: Select all
set militaryTime to value of variable "sprk_Start_Time"
set standardTime to militaryTimeToStandardTime(militaryTime)

on militaryTimeToStandardTime(militaryTime)
   set militaryTime to militaryTime as text
   if length of militaryTime is equal to 4 then
      set theHour to text 1 thru 2 of militaryTime
      set theMinutes to text 3 thru 4 of militaryTime
   else if length of militaryTime is equal to 3 then
      set theHour to "" & text 1 of militaryTime
      set theMinutes to text 2 thru 3 of militaryTime
   else if length of militaryTime is equal to 2 then
      set theHour to "00"
      set theMinutes to text 1 thru 2 of militaryTime
   else if length of militaryTime is equal to 1 then
      set theHour to "00"
      set theMinutes to "0" & text 1 of militaryTime
   end if
   set theHourNumber to theHour as number
   if (theHourNumber is equal to 0) and (theMinutes is equal to 0) then
      set standardTime to "12:00 AM"
   else if theHourNumber is greater than 12 then
      set theHourNumber to theHourNumber - 12
      set standardTime to (theHourNumber as text) & ":" & theMinutes & " pm"
   else if theHourNumber = 12 then
      set standardTime to theHour & ":" & theMinutes & " pm"
   else if (theHourNumber is equal to 0) and (theMinutes is not equal to 0) then
      set standardTime to "12" & ":" & theMinutes & " am"
   else
      set standardTime to theHour & ":" & theMinutes & " am"
   end if
end militaryTimeToStandardTime

set value of variable "sprk_Start_Time_AmPm" to standardTime


Very much appreciate any help with it.

Thanks,

Carl

Posted on
Sun Jun 06, 2021 2:43 pm
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

Re: Convert to Python?

For getting military or standard time as a clock in indigo, I use the Clock Display plugin: https://www.indigodomo.com/pluginstore/164/

Once you create a clock device, you can pull the time in any format (Each format is a separate device state) in a python script or use on a control page in any format. On pages only I use, I use the 24 hour clock. For "Wife" pages, I use 12 hr am/pm.

Will that help tackle whatever you are trying to do?

Bill
My Plugin: My People

Posted on
Sun Jun 06, 2021 2:48 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Convert to Python?

ckeyes888 wrote:
Is this script convertible to python?


Almost certainly, but I would never even attempt it. You would be much better off not trying to mangle the strings, but converting the input string to a datetime object, then converting that back to a string with the format you want. To do that, forget that script even exists and just start from the two time strings you're working with (input and output).

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Jun 06, 2021 4:45 pm
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Convert to Python?

The Clock Display plugin looks promising except I need the 24 hr time without the colon. e.g. 16:42 needs to be 1642.
Maybe use a script to remove the colon?

Thanks,

Carl

Posted on
Sun Jun 06, 2021 5:01 pm
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Convert to Python?

This script is also a part of my sprinkler system.
Any help converting it appreciated.

Code: Select all
tell application "IndigoServer"
   set newDelay to the value of variable "TimerSprinklerStart" as real
   
   set newDelay to newDelay + 2200
   
   set the value of variable "TimerSprinklerStart" to newDelay
   set the value of variable "sprinklerTF" to "false"
end tell


Thanks,

Carl

Posted on
Sun Jun 06, 2021 7:54 pm
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

Re: Convert to Python?

ckeyes888 wrote:
The Clock Display plugin looks promising except I need the 24 hr time without the colon. e.g. 16:42 needs to be 1642.
Maybe use a script to remove the colon?


Thanks,

Carl


You can do that in the plugin....
Attachments
Screen Shot 2021-06-06 at 8.50.12 PM.png
Screen Shot 2021-06-06 at 8.50.12 PM.png (230.91 KiB) Viewed 2019 times

Bill
My Plugin: My People

Posted on
Sun Jun 06, 2021 8:29 pm
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

Re: Convert to Python?

ckeyes888 wrote:
This script is also a part of my sprinkler system.
Any help converting it appreciated.

Code: Select all
tell application "IndigoServer"
   set newDelay to the value of variable "TimerSprinklerStart" as real
   
   set newDelay to newDelay + 2200
   
   set the value of variable "TimerSprinklerStart" to newDelay
   set the value of variable "sprinklerTF" to "false"
end tell


Thanks,

Carl

Code: Select all
newDelay = indigo.variables[123456789].value
newDelay = int(newDelay) + 2200
indigo.variable.updateValue(123456789, value=str(newDelay))
indigo.variable.updateValue(987654321, value="false")

With python, you use the ID number of the variable instead of the name. Line 1 grabs the value of the variable by number. Line 2 changes the value from the default (string) to an integer, adds the delay amount to the starting number. Line 3 overwrites the variable with the "newDelay" as a string. Substitute 123456789 with the ID number of "TimerSprinklerStart" and 987654321 to the ID number for "sprinklerTF" and you should be good.

Bill
My Plugin: My People

Posted on
Sun Jun 06, 2021 8:48 pm
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Convert to Python?

All working great!

Thanks so much for the help!

Carl

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest