openhab / openhab-addons

Add-ons for openHAB
https://www.openhab.org/
Eclipse Public License 2.0
1.88k stars 3.59k forks source link

[astro] Moon phase WANING_GIBBOUS reported before Full moon #6571

Closed Rossko57 closed 1 year ago

Rossko57 commented 4 years ago

Astro binding 2.4

Things astro:moon:fossmoon [ geolocation="51.783, -1.902", interval=600 ] Item String moon_phase "Moon Phase" {channel="astro:moon:fossmoon:phase#name"} results 2019-12-11 12:54:37.662 [vent.ItemStateChangedEvent] - moon_phase changed from NULL to WANING_GIBBOUS

Full moon is due "tomorrow" i.e. 12/12/19 so correct phase would be Waxing gibbous

Problem still present in 2.5M6 https://community.openhab.org/t/astro-binding-reports-wrong-moon-phase/87391

shutterfreak commented 4 years ago

I think the main problem is that the moon phase name seems to be calculated only once (at midnight of the Julian date) whereas on a given calendar day more than one lunar phase can occur (e.g., WAXING_GIBBOUSFULLWANING_GIBBOUS).

A possible workaround is to check the instant moon age (astro:moon:home:phase#agePercent: value between 0 and 1) as it is definitely instantly computed by the astro binding.

Creating a workaround rule is rather straightforward.

Rossko57 commented 4 years ago

I'm on GMT and today is the 11th. The Full moon is on the 12th. The whole of the 11th should be Waxing, or Full at stretch, and in no case be Waning.

Certainly worth looking at phase age too - I added Number:Dimensionless moon_age "Moon Age" {channel="astro:moon:fossmoon:phase#agePercent"} with result 2019-12-11 13:28:00.314 [vent.ItemStateChangedEvent] - moon_phase changed from NULL to WANING_GIBBOUS 2019-12-11 13:28:00.317 [vent.ItemStateChangedEvent] - moon_age changed from NULL to 50.46354969058292 % which looks like a clue `

shutterfreak commented 4 years ago

I still believe the problem lies somewhere at either a time zone conversion or at a different midnight in use (astronomers have more than one definition of year or day).

I posted a code snippet with a fairly accurate approximation of the lunar phase owing to Jean Meeus. See my reply in the related openHAB community thread.

Rossko57 commented 4 years ago

Here in GMT it is still the 11th, Full moon is due in a couple of hours "just" into the 12th at 00:12 - I will report again then. Meantime Astro is still updating age each hour as expected from my Thing - 2019-12-11 22:30:35.083 [vent.ItemStateChangedEvent] - moon_age changed from 51.713532126958796 % to 51.73700221403369 % Reckon this stuff is just a day out.

Update - now the 12th, logs from Astro midnight recalc

2019-12-12 00:00:30.183 [vent.ItemStateChangedEvent] - moon_age changed from 51.92476478825858 % to 51.94803917196705 %
2019-12-12 00:00:30.184 [vent.ItemStateChangedEvent] - moon_phase changed from WANING_GIBBOUS to FULL

So there's a surprise - moon phase is now FULL Not sure what conclusion to draw from that in relation to age%, but definitely incorrect to precede Full with Waning

EDIT - for completeness, Later that same calendar date, but on the following night 2019-12-12 22:13:04.699 [vent.ItemStateChangedEvent] - moon_age changed from NULL to 55.075640113909294 % 2019-12-12 22:13:04.700 [vent.ItemStateChangedEvent] - moon_phase changed from NULL to FULL Here FULL looks correct to me, we are still in the calendar date of full moon.

And finally 2019-12-13 00:00:30.089 [vent.ItemStateChangedEvent] - moon_age changed from 55.31034399668286 % to 55.32776519507749 % 2019-12-13 00:00:30.124 [vent.ItemStateChangedEvent] - moon_phase changed from FULL to WANING_GIBBOUS

Conclusion - FULL phase working as expected, being set for the duration of the calendar day containing a full moon.

shutterfreak commented 4 years ago

I think that the moon age might indeed be off by about 1 day.

shutterfreak commented 4 years ago

Here's the output of computing the lunar age for 72 hours: yestarday, today and tomorrow (TZ = CET = GMT/UTC + 1):

2019-12-12 12:02:48.039 [INFO ] Current instant lunar age is 51.33 %
2019-12-12 12:02:48.042 [INFO ] Lunar age for yesterday at 00 h is 46.24 %
2019-12-12 12:02:48.045 [INFO ] Lunar age for yesterday at 01 h is 46.38 %
2019-12-12 12:02:48.048 [INFO ] Lunar age for yesterday at 02 h is 46.52 %
2019-12-12 12:02:48.051 [INFO ] Lunar age for yesterday at 03 h is 46.67 %
2019-12-12 12:02:48.053 [INFO ] Lunar age for yesterday at 04 h is 46.81 %
2019-12-12 12:02:48.056 [INFO ] Lunar age for yesterday at 05 h is 46.95 %
2019-12-12 12:02:48.058 [INFO ] Lunar age for yesterday at 06 h is 47.09 %
2019-12-12 12:02:48.061 [INFO ] Lunar age for yesterday at 07 h is 47.23 %
2019-12-12 12:02:48.064 [INFO ] Lunar age for yesterday at 08 h is 47.37 %
2019-12-12 12:02:48.066 [INFO ] Lunar age for yesterday at 09 h is 47.51 %
2019-12-12 12:02:48.069 [INFO ] Lunar age for yesterday at 10 h is 47.65 %
2019-12-12 12:02:48.071 [INFO ] Lunar age for yesterday at 11 h is 47.79 %
2019-12-12 12:02:48.073 [INFO ] Lunar age for yesterday at 12 h is 47.94 %
2019-12-12 12:02:48.076 [INFO ] Lunar age for yesterday at 13 h is 48.08 %
2019-12-12 12:02:48.078 [INFO ] Lunar age for yesterday at 14 h is 48.22 %
2019-12-12 12:02:48.081 [INFO ] Lunar age for yesterday at 15 h is 48.36 %
2019-12-12 12:02:48.083 [INFO ] Lunar age for yesterday at 16 h is 48.50 %
2019-12-12 12:02:48.086 [INFO ] Lunar age for yesterday at 17 h is 48.64 %
2019-12-12 12:02:48.088 [INFO ] Lunar age for yesterday at 18 h is 48.78 %
2019-12-12 12:02:48.090 [INFO ] Lunar age for yesterday at 19 h is 48.92 %
2019-12-12 12:02:48.093 [INFO ] Lunar age for yesterday at 20 h is 49.06 %
2019-12-12 12:02:48.095 [INFO ] Lunar age for yesterday at 21 h is 49.21 %
2019-12-12 12:02:48.098 [INFO ] Lunar age for yesterday at 22 h is 49.35 %
2019-12-12 12:02:48.100 [INFO ] Lunar age for yesterday at 23 h is 49.49 %
2019-12-12 12:02:48.103 [INFO ] Lunar age for today at 00 h is 49.63 %
2019-12-12 12:02:48.105 [INFO ] Lunar age for today at 01 h is 49.77 %
2019-12-12 12:02:48.108 [INFO ] Lunar age for today at 02 h is 49.91 %
2019-12-12 12:02:48.110 [INFO ] Lunar age for today at 03 h is 50.05 %
2019-12-12 12:02:48.113 [INFO ] Lunar age for today at 04 h is 50.19 %
2019-12-12 12:02:48.115 [INFO ] Lunar age for today at 05 h is 50.33 %
2019-12-12 12:02:48.118 [INFO ] Lunar age for today at 06 h is 50.48 %
2019-12-12 12:02:48.120 [INFO ] Lunar age for today at 07 h is 50.62 %
2019-12-12 12:02:48.123 [INFO ] Lunar age for today at 08 h is 50.76 %
2019-12-12 12:02:48.125 [INFO ] Lunar age for today at 09 h is 50.90 %
2019-12-12 12:02:48.128 [INFO ] Lunar age for today at 10 h is 51.04 %
2019-12-12 12:02:48.130 [INFO ] Lunar age for today at 11 h is 51.18 %
2019-12-12 12:02:48.133 [INFO ] Lunar age for today at 12 h is 51.32 %
2019-12-12 12:02:48.136 [INFO ] Lunar age for today at 13 h is 51.46 %
2019-12-12 12:02:48.138 [INFO ] Lunar age for today at 14 h is 51.60 %
2019-12-12 12:02:48.141 [INFO ] Lunar age for today at 15 h is 51.74 %
2019-12-12 12:02:48.143 [INFO ] Lunar age for today at 16 h is 51.89 %
2019-12-12 12:02:48.145 [INFO ] Lunar age for today at 17 h is 52.03 %
2019-12-12 12:02:48.148 [INFO ] Lunar age for today at 18 h is 52.17 %
2019-12-12 12:02:48.150 [INFO ] Lunar age for today at 19 h is 52.31 %
2019-12-12 12:02:48.153 [INFO ] Lunar age for today at 20 h is 52.45 %
2019-12-12 12:02:48.155 [INFO ] Lunar age for today at 21 h is 52.59 %
2019-12-12 12:02:48.158 [INFO ] Lunar age for today at 22 h is 52.73 %
2019-12-12 12:02:48.161 [INFO ] Lunar age for today at 23 h is 52.87 %
2019-12-12 12:02:48.163 [INFO ] Lunar age for tomorrow at 00 h is 53.01 %
2019-12-12 12:02:48.166 [INFO ] Lunar age for tomorrow at 01 h is 53.16 %
2019-12-12 12:02:48.168 [INFO ] Lunar age for tomorrow at 02 h is 53.30 %
2019-12-12 12:02:48.170 [INFO ] Lunar age for tomorrow at 03 h is 53.44 %
2019-12-12 12:02:48.173 [INFO ] Lunar age for tomorrow at 04 h is 53.58 %
2019-12-12 12:02:48.176 [INFO ] Lunar age for tomorrow at 05 h is 53.72 %
2019-12-12 12:02:48.178 [INFO ] Lunar age for tomorrow at 06 h is 53.86 %
2019-12-12 12:02:48.181 [INFO ] Lunar age for tomorrow at 07 h is 54.00 %
2019-12-12 12:02:48.183 [INFO ] Lunar age for tomorrow at 08 h is 54.14 %
2019-12-12 12:02:48.185 [INFO ] Lunar age for tomorrow at 09 h is 54.28 %
2019-12-12 12:02:48.188 [INFO ] Lunar age for tomorrow at 10 h is 54.43 %
2019-12-12 12:02:48.190 [INFO ] Lunar age for tomorrow at 11 h is 54.57 %
2019-12-12 12:02:48.193 [INFO ] Lunar age for tomorrow at 12 h is 54.71 %
2019-12-12 12:02:48.196 [INFO ] Lunar age for tomorrow at 13 h is 54.85 %
2019-12-12 12:02:48.199 [INFO ] Lunar age for tomorrow at 14 h is 54.99 %
2019-12-12 12:02:48.201 [INFO ] Lunar age for tomorrow at 15 h is 55.13 %
2019-12-12 12:02:48.204 [INFO ] Lunar age for tomorrow at 16 h is 55.27 %
2019-12-12 12:02:48.206 [INFO ] Lunar age for tomorrow at 17 h is 55.41 %
2019-12-12 12:02:48.209 [INFO ] Lunar age for tomorrow at 18 h is 55.55 %
2019-12-12 12:02:48.211 [INFO ] Lunar age for tomorrow at 19 h is 55.70 %
2019-12-12 12:02:48.214 [INFO ] Lunar age for tomorrow at 20 h is 55.84 %
2019-12-12 12:02:48.216 [INFO ] Lunar age for tomorrow at 21 h is 55.98 %
2019-12-12 12:02:48.218 [INFO ] Lunar age for tomorrow at 22 h is 56.12 %
2019-12-12 12:02:48.221 [INFO ] Lunar age for tomorrow at 23 h is 56.26 %

That could also help ing giving an indication of where the problem arises.

The code for generating this overview is:

from org.joda.time import DateTime

rule_init_timestamp = DateTime.now()
logTitle = "time_of_day.py@{ts}".format(ts=rule_init_timestamp.toString("HH:mm:ss"))
ruleTimeStamp = " -- (Rule set initialised {date} at {ts})".format(
    date=rule_init_timestamp.toString("E d MMM yyyy"),
    ts=rule_init_timestamp.toString("HH:mm:ss (z)"),
)
rulePrefix = "Time Of Day | "

def moon():
    ### See https://continuambigu.art/blog/maanfase/maanfase/
    dt_now = DateTime.now()
    k = 12.3685 * ( dt_now.getMillis() / 31557600000.0 - 30.0144003346 ) % 1
    LogAction.logInfo("Instant Lunar Age", u"Current instant lunar age is {} %".format("%.2f" % (k * 100)))
    h = -24
    while h < 48:
        dt = dt_now.withTimeAtStartOfDay().plusHours(h)
        k = 12.3685 * ( dt.getMillis() / 31557600000.0 - 30.0144003346 ) % 1
        LogAction.logInfo("Computed Lunar Age", u"Lunar age for {} at {} h is {} %".format(
            ("yesterday" if h < 0 else ("today" if h < 24 else "tomorrow")),
            ("%02d" % (h%24)),
            ("%.2f" % (k * 100)),
        ))
        h += 1

moon()
shutterfreak commented 4 years ago

And for what it's worth, here's the hourly output of today's lunar phase computation from the binding:

2019-12-12 00:00:24.948 Astro_Moon_Age_Percent changed from 51.80602553571009 % to 51.80701125010757 %
2019-12-12 00:00:31.752 Astro_Moon_Age_Percent changed from 51.80701125010757 % to 51.80723382699244 %
2019-12-12 00:00:31.760 Astro_Moon_Age_Percent changed from 51.80723382699244 % to 51.80723973369008 %
2019-12-12 00:00:59.834 Astro_Moon_Age_Percent changed from 51.80723973369008 % to 51.808372254950285 %
2019-12-12 01:00:24.949 Astro_Moon_Age_Percent changed from 51.94684715344114 % to 51.94783208549456 %
2019-12-12 02:00:24.886 Astro_Moon_Age_Percent changed from 52.087669123227016 % to 52.088653937928825 %
2019-12-12 02:00:59.737 Astro_Moon_Age_Percent changed from 52.088653937928825 % to 52.09001627275643 %
2019-12-12 03:00:24.928 Astro_Moon_Age_Percent changed from 52.2284912494817 % to 52.22947586859749 %
2019-12-12 03:00:59.734 Astro_Moon_Age_Percent changed from 52.22947586859749 % to 52.23083804695629 %
2019-12-12 04:00:24.943 Astro_Moon_Age_Percent changed from 52.36931306279876 % to 52.37029791661777 %
2019-12-12 04:00:59.800 Astro_Moon_Age_Percent changed from 52.37029791661777 % to 52.37166009497657 %
2019-12-12 05:00:00.158 Astro_Moon_Age_Percent changed from 52.50778788305521 % to 52.5101359322803 %
2019-12-12 05:00:24.963 Astro_Moon_Age_Percent changed from 52.5101359322803 % to 52.51111972993483 %
2019-12-12 06:00:24.884 Astro_Moon_Age_Percent changed from 52.650956806784485 % to 52.6519416606035 %
2019-12-12 06:00:59.675 Astro_Moon_Age_Percent changed from 52.6519416606035 % to 52.6533038389623 %
2019-12-12 07:00:24.856 Astro_Moon_Age_Percent changed from 52.79177889392197 % to 52.79276363038937 %
2019-12-12 07:00:59.816 Astro_Moon_Age_Percent changed from 52.79276363038937 % to 52.7941261999202 %
2019-12-12 08:00:24.896 Astro_Moon_Age_Percent changed from 52.93260074635623 % to 52.933585561058045 %
2019-12-12 08:00:59.834 Astro_Moon_Age_Percent changed from 52.933585561058045 % to 52.934947778534045 %
2019-12-12 09:00:24.948 Astro_Moon_Age_Percent changed from 53.0734226770249 % to 53.074408430539584 %
2019-12-12 09:00:59.893 Astro_Moon_Age_Percent changed from 53.074408430539584 % to 53.07576970920271 %
2019-12-12 10:00:24.977 Astro_Moon_Age_Percent changed from 53.21424468592798 % to 53.215229461512585 %
2019-12-12 10:00:59.849 Astro_Moon_Age_Percent changed from 53.215229461512585 % to 53.216592070160615 %
2019-12-12 11:00:24.872 Astro_Moon_Age_Percent changed from 53.355066538362244 % to 53.35605139218125 %
2019-12-12 11:00:59.755 Astro_Moon_Age_Percent changed from 53.35605139218125 % to 53.35741360965726 %
2019-12-12 12:00:47.062 Astro_Moon_Age_Percent changed from NULL to 53.497675773151514 %
2019-12-12 12:00:47.101 Astro_Moon_Age_Percent changed from 53.497675773151514 % to 53.4976794892858 %
2019-12-12 12:00:47.346 Astro_Moon_Age_Percent changed from 53.4976794892858 % to 53.49768586538988 %
2019-12-12 12:00:47.357 Astro_Moon_Age_Percent changed from 53.49768586538988 % to 53.4976900118134 %
2019-12-12 13:00:45.723 Astro_Moon_Age_Percent changed from 53.63616428442901 % to 53.638511590427235 %
2019-12-12 14:00:45.707 Astro_Moon_Age_Percent changed from 53.77698629333208 % to 53.77933395138514 %
2019-12-12 15:00:45.663 Astro_Moon_Age_Percent changed from 53.91780802841473 % to 53.92015506059255 %
2019-12-12 16:00:45.654 Astro_Moon_Age_Percent changed from 54.05862999820061 % to 54.06097695214402 %
2019-12-12 17:00:45.656 Astro_Moon_Age_Percent changed from 54.19945200710369 % to 54.201798921929885 %
2019-12-12 18:00:45.646 Astro_Moon_Age_Percent changed from 54.340273781303544 % to 54.34262085259856 %
2019-12-12 19:00:45.714 Astro_Moon_Age_Percent changed from 54.481096572550676 % to 54.483443213556455 %
2019-12-12 20:00:45.705 Astro_Moon_Age_Percent changed from 54.62191783822689 % to 54.62426498775631 %
2019-12-12 21:00:45.707 Astro_Moon_Age_Percent changed from 54.76274051212242 % to 54.76508680107337 %

So it appears that the values are 15-16 hours off.

shutterfreak commented 4 years ago

I think there might be 2 problems in MoonCalc.java, more precisely in: setMoonPhase(Calendar calendar, Moon moon): 1 phase.setAge( (int) age) might have adverse side effects as I don't get the point in rounding age to a number of whole days.

  1. It also appears that the waxing / waning definition is defined by the half length of a lunar cycle expressed in 86.400 second days: boolean isWaxing = age < (29.530588853 / 2); This number (29.530588853) represents the length of a synodic month, i.e., the time between 2 same lunar phases. The problem is that the Earth's and the Moon's orbit are elliptical, hence the actual synodic month varies over time. From Wikipedia:

Since Earth's orbit around the Sun is elliptical and not circular, the speed of Earth's progression around the Sun varies during the year. Thus, the angular rate is faster nearer periapsis and slower near apoapsis. The same is so for the Moon's orbit around the Earth. Because of these variations in angular rate, the actual time between lunations may vary from about 29.18 to about 29.93 days. The long-term average duration is 29.530587981 days[5] (29 d 12 h 44 min 2.8016 s). The synodic month is used to calculate eclipse cycles.[6]

Rossko57 commented 4 years ago

That half a day variation is significant - will push from one calendar date to another, sometimes.

5iver commented 4 years ago

I noticed this yesterday and found this issue in GH. I haven't read all the comments (yet), but think astro:moon:local:phase#age needs to be updated more frequently than 24h and use decimals. Today, at 3pm UT, we were at 29d 17h 18m, but this Channel started showing 0 right after midnight.

BTW, there is some great info and images here... https://svs.gsfc.nasa.gov/Gallery/moonphase.html. If you drill into the images, there are details... https://svs.gsfc.nasa.gov/vis/a000000/a004700/a004768/frames/5760x3240_16x9_30p/fancy/comp.1288.tif.

lsiepel commented 1 year ago

@shutterfreak I guess this is not fixed yet?

  1. At this moment the age is set to a full day. It could be rounded to hours, but that will be a breaking change. I'm not very into astro stuff ;-) but if this is needed, i could make a PR to change this age channel from day to hour.
  2. Can you come up with a more precise/reliable calculation to calculate isWaxing? I could also create a PR to get this fixed.
shutterfreak commented 1 year ago

I was unaware that the moon phase was only updated on a daily basis.

Normally I would rely on my books by Jean Meeus for finding good equations related to astronomy but these books are currently still packed in a box somewhere.

Meanwhile I came across this simple solution based on the Julian Date (JD): https://www.subsystems.us/uploads/9/8/9/4/98948044/moonphase.pdf

Instead of rounding the JD to the day, you can use any JD timestamp. Hence you could compute the moon phase for any timestamp.

lsiepel commented 1 year ago

Preparing for a PR i changed the int age to double age https://github.com/lsiepel/openhab-addons/commit/bb2c25dcdb2f2912f409ca067ec3f0c2849d5d5b

As far as i can see the calculation for isWaxing in the binding is exactly the same (and even more precise) then in the pdf you mentioned.

lsiepel commented 1 year ago

@shutterfreak an updated binding has been supplied at the PR, could you test if this fixed this issue?