Page 1 of 1

Convert to Python?

PostPosted: Sun Jun 06, 2021 2:19 pm
by ckeyes888
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

Re: Convert to Python?

PostPosted: Sun Jun 06, 2021 2:43 pm
by whmoorejr
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?

Re: Convert to Python?

PostPosted: Sun Jun 06, 2021 2:48 pm
by FlyingDiver
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).

Re: Convert to Python?

PostPosted: Sun Jun 06, 2021 4:45 pm
by ckeyes888
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

Re: Convert to Python?

PostPosted: Sun Jun 06, 2021 5:01 pm
by ckeyes888
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

Re: Convert to Python?

PostPosted: Sun Jun 06, 2021 7:54 pm
by whmoorejr
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....

Re: Convert to Python?

PostPosted: Sun Jun 06, 2021 8:29 pm
by whmoorejr
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.

Re: Convert to Python?

PostPosted: Sun Jun 06, 2021 8:48 pm
by ckeyes888
All working great!

Thanks so much for the help!

Carl