Moon Phase Script?

Posted on
Tue Apr 04, 2023 8:28 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Moon Phase Script?

Since the Fantastic Weather plugins are no more I lost my moon phase device state info.
Before using the plugin I used an Applescript to determine the moon phase,
which of course is also defunct.

Hoping there might be a Python version of this script to return a numeric description of the current moon phase?

Code: Select all
tell application "IndigoServer"
   set theYear to year of (current date)
   set theMonth to month of (current date) as integer
   set theDay to day of (current date)
   
   if (theMonth = 1) then
      set theDay to theDay - 1
   else if (theMonth = 2) then
      set theDay to theDay + 30
   else
      set theDay to theDay + 28 + (theMonth - 2) * 3059 / 100
   end if
   set g to (theYear - 1900) mod 19 + 1
   set e to (11 * g + 18) mod 30
   if (((e = 25) and (g > 11)) or (e = 24)) then
      set e to e + 1
   end if
   set fullness to ((((e + theDay) * 6 + 11) mod 177) / 22) as integer
   if (fullness = 8) then set fullness to 0
   set value of variable "MoonPhase" to fullness
end tell


Thanks,

Carl

Posted on
Wed Apr 05, 2023 5:47 am
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Moon Phase Script?


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

Posted on
Wed Apr 05, 2023 2:38 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Moon Phase Script?

Looks great but how do I apply that to populating a variable?

Thanks,

Carl

Posted on
Wed Apr 05, 2023 2:39 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Moon Phase Script?

Instead of the print statement, save the value to an Indigo variable. How to do that is in the Wiki.

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

Posted on
Wed Apr 05, 2023 2:50 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Moon Phase Script?

Am I close? Cant get it to compile.

Thanks, Carl
Code: Select all
#!/usr/bin/env python

"""

moonphase.py - Calculate Lunar Phase

Author: Sean B. Palmer, inamidst.com

Cf. http://en.wikipedia.org/wiki/Lunar_phase#Lunar_phase_calculation

"""

import math, decimal, datetime

dec = decimal.Decimal


def position(now=None):

   if now is None:

      now = datetime.datetime.now()


   diff = now - datetime.datetime(2001, 1, 1)

   days = dec(diff.days) + (dec(diff.seconds) / dec(86400))

   lunations = dec("0.20439731") + (days * dec("0.03386319269"))


   return lunations % dec(1)


def phase(pos):

   index = (pos * dec(8)) + dec("0.5")

   index = math.floor(index)

   return {

      0: "New Moon",

      1: "Waxing Crescent",

      2: "First Quarter",

      3: "Waxing Gibbous",

      4: "Full Moon",

      5: "Waning Gibbous",

      6: "Last Quarter",

      7: "Waning Crescent"

   }[int(index) & 7]


def main():

   pos = position()

   phasename = phase(pos)


   roundedpos = round(float(pos), 3)

   indigo.variable.updateValue(indigo.variables[103086076], value= roundedpos)

   ##print "%s (%s)" % (phasename, roundedpos)


if __name__=="__main__":

   main()

Posted on
Wed Apr 05, 2023 2:59 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Moon Phase Script?

What's with all the blank lines?

This works for me. You have to cast the value to a string before you store it in the Indigo variable. I also removed the "main" function because that doesn't work in an embedded script.

Code: Select all
import math, decimal, datetime

dec = decimal.Decimal

def position(now = None):
   if now is None:
      now = datetime.datetime.now()

   diff = now - datetime.datetime(2001, 1, 1)
   days = dec(diff.days) + (dec(diff.seconds) / dec(86400))
   lunations = dec("0.20439731") + (days * dec("0.03386319269"))
   return lunations % dec(1)

def phase(pos):
   index = (pos * dec(8)) + dec("0.5")
   index = math.floor(index)
   return {
      0: "New Moon",
      1: "Waxing Crescent",
      2: "First Quarter",
      3: "Waxing Gibbous",
      4: "Full Moon",
      5: "Waning Gibbous",
      6: "Last Quarter",
      7: "Waning Crescent"
   }[int(index) & 7]

pos = position()
phasename = phase(pos)
roundedpos = round(float(pos), 3)

indigo.variable.updateValue(indigo.variables[504685077], value = str(roundedpos))
#print(roundedpos)

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

Posted on
Wed Apr 05, 2023 3:15 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Moon Phase Script?

Working great. Trying modify it to return a whole number without a decimal.

Got it working.

Thanks,

Carl

Posted on
Wed Apr 05, 2023 3:33 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Moon Phase Script?

Code: Select all
roundedpos = int(round(float(pos), 3))

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

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 13 guests

cron