Sun and moon position using PyEphem

Python and python framework
Post Reply
MikeF
Posts: 227
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi
Domoticz version: V4.9700
Location: UK
Contact:

Sun and moon position using PyEphem

Post by MikeF » Tuesday 06 November 2018 21:14

I've recently come across PyEphem https://rhodesmill.org/pyephem/index.html, which is a Python library which enables you to calculate the position of the sun and moon - and other celestial bodies, if you're into astronomy.

This is a very comprehensive library, which enables you to write some very compact code. Install as follows:

Code: Select all

pip install pyephem
For example, the following will display the current sun altitude and azimuth at your location:

Code: Select all

import ephem
import datetime

home = ephem.Observer()
home.lat, home.lon = <your latitude in decimal degrees>, <your longitude in decimal degrees>
home.date = datetime.datetime.utcnow()

sun = ephem.Sun()
sun.compute(home)
print sun.alt, sun.az
I've created the following Python script, which will send the following to Domoticz:
- sun azimuth (°)
- sun altitude (°)
- moon azimuth (°)
- moon altitude (°)
- moon illumination (%)
- moon phase
- date & time of next moonrise
- date & time of next full moon

The first 5 are custom sensors, the last 3 are text sensors.
Spoiler: show

Code: Select all

#!/usr/bin/python
import datetime
import ephem
from math import degrees as deg
import requests

format = "%Y-%m-%d %H:%M"

# Local latitude, longitude coordinates - decimal degrees
# latitude is +ve in Northern hemisphere, longitude is +ve east of Greenwich
home_lat, home_lon = 'xx.xxxx','xx.xxxx'

# Domoticz command stub and Idx's
baseURL = 'http://<domoticz url:port>/json.htm?type=command&param=udevice&nvalue=0'
sun_azIdx, sun_altIdx = <xxx>, <xxx>		# custom sensors, label: °
moon_azIdx, moon_altIdx = <xxx>, <xxx>		# custom sensors, label: °
moon_illumIdx = <xxx>				# custom sensors, label: %
moon_riseIdx, moon_fullIdx = <xxx>, <xxx>	# text sensors
moon_phaseIdx = <xxx>				# text sensor

def human_moon(home):
	# Human-readable names for phases of the moon, taken from:
	# https://stackoverflow.com/questions/26702144/human-readable-names-for-phases-of-the-moon-with-pyephem/26707918
	target_date_utc = home.date
	target_date_local = ephem.localtime(target_date_utc).date()
	next_full = ephem.localtime(ephem.next_full_moon(target_date_utc)).date()
	next_new = ephem.localtime(ephem.next_new_moon(target_date_utc)).date()
	next_last_quarter = ephem.localtime(ephem.next_last_quarter_moon(target_date_utc)).date()
	next_first_quarter = ephem.localtime(ephem.next_first_quarter_moon(target_date_utc)).date()
	previous_full = ephem.localtime(ephem.previous_full_moon(target_date_utc)).date()
	previous_new = ephem.localtime(ephem.previous_new_moon(target_date_utc)).date()
	previous_last_quarter = ephem.localtime(ephem.previous_last_quarter_moon(target_date_utc)).date()
	previous_first_quarter = ephem.localtime(ephem.previous_first_quarter_moon(target_date_utc)).date()
	if target_date_local in (next_full, previous_full):
		return 'Full'
	elif target_date_local in (next_new, previous_new):
		return 'New'
	elif target_date_local in (next_first_quarter, previous_first_quarter):
		return 'First quarter'
	elif target_date_local in (next_last_quarter, previous_last_quarter):
		return 'Last quarter'
	elif previous_new < next_first_quarter < next_full < next_last_quarter < next_new:
		return 'Waxing crescent'
	elif previous_first_quarter < next_full < next_last_quarter < next_new < next_first_quarter:
		return 'Waxing gibbous'
	elif previous_full < next_last_quarter < next_new < next_first_quarter < next_full:
		return 'Waning gibbous'
	elif previous_last_quarter < next_new < next_first_quarter < next_full < next_last_quarter:
		return 'Waning crescent'

home = ephem.Observer()
home.lat, home.lon = home_lat, home_lon
home.date = datetime.datetime.utcnow()

sun, moon = ephem.Sun(), ephem.Moon()

sun.compute(home)
sun_azimuth  = round(deg(float(sun.az)),1)
sun_altitude = round(deg(float(sun.alt)),1)
sunrise  = home.previous_rising(sun)
sunset   = home.next_setting(sun)

moon.compute(home)
moon_azimuth  = round(deg(float(moon.az)),1)
moon_altitude = round(deg(float(moon.alt)),1)
moon_illum = round(moon.phase,1)
moonrise = ephem.localtime(home.next_rising(moon)).strftime(format)
moonset  = ephem.localtime(home.next_setting(moon)).strftime(format)
full_moon = ephem.localtime(ephem.next_full_moon(home.date)).strftime(format)
moon_phase = human_moon(home)
'''
print "date / time", home.date
#print sunrise, sunset
print "sun  azimuth / altitude", sun_azimuth, sun_altitude
print "moon azimuth / altitude", moon_azimuth, moon_altitude
print "moon illumination", moon_illum
print "next moon rise / full moon", moonrise, full_moon
print "phase: ", moon_phase
'''
# Send data to Domoticz
url = baseURL + "&idx=%s&svalue=%s" % (sun_azIdx, sun_azimuth)
r = requests.get(url)

url = baseURL + "&idx=%s&svalue=%s" % (sun_altIdx, sun_altitude)
r = requests.get(url)

url = baseURL + "&idx=%s&svalue=%s" % (moon_azIdx, moon_azimuth)
r = requests.get(url)

url = baseURL + "&idx=%s&svalue=%s" % (moon_altIdx, moon_altitude)
r = requests.get(url)

url = baseURL + "&idx=%s&svalue=%s" % (moon_illumIdx, moon_illum)
r = requests.get(url)

url = baseURL + "&idx=%s&svalue=%s" % (moon_riseIdx, moonrise)
r = requests.get(url)

url = baseURL + "&idx=%s&svalue=%s" % (moon_fullIdx, full_moon)
r = requests.get(url)

url = baseURL + "&idx=%s&svalue=%s" % (moon_phaseIdx, moon_phase)
r = requests.get(url)

Edit latitude, longitude and Domoticz values as appropriate.

Run this with cron, every 5 minutes (or whatever - the text devices don't need to be updated that frequently).

Image

User avatar
EdwinK
Posts: 1382
Joined: Sunday 22 January 2017 22:46
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Sun and moon position using PyEphem

Post by EdwinK » Wednesday 07 November 2018 0:07

Just put it in the display ;)
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | RFxcom | Dashticz V2

User avatar
Xorfor
Posts: 103
Joined: Sunday 03 December 2017 23:37
Target OS: Raspberry Pi
Domoticz version: Beta
Contact:

Re: Sun and moon position using PyEphem

Post by Xorfor » Wednesday 07 November 2018 22:08

That is really coincidental!

I recently created a plugin https://github.com/Xorfor/Domoticz-SunMoon-Plugin also with Ephem, and with almost the same devices.

There are a lot more functions available, like next sun rise, etc. I am working on implementing these functions in my Domoticz API.

MikeF
Posts: 227
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi
Domoticz version: V4.9700
Location: UK
Contact:

Re: Sun and moon position using PyEphem

Post by MikeF » Wednesday 07 November 2018 23:18

Scary! :o

I saw some of the other functions, but decided not to implement them. Also, I used the function human_moon() - from StackExchange as referenced in my script - to provide a 'human-readable' phase description. It's an approximation, as it applies to the whole day on which a major phase (new, full, etc.) occurs, whereas these are instants in time, and the minor phases (waxing gibbous, etc.) are intervals. I read, too, that moon.phase actually returns the moon's illumination fraction (which is what I use).

User avatar
Xorfor
Posts: 103
Joined: Sunday 03 December 2017 23:37
Target OS: Raspberry Pi
Domoticz version: Beta
Contact:

Re: Sun and moon position using PyEphem

Post by Xorfor » Wednesday 07 November 2018 23:46

Hmmm, perhaps nice to combine your script, my plugin and https://github.com/ycahome/MoonPhases to one plugin?

User avatar
EdwinK
Posts: 1382
Joined: Sunday 22 January 2017 22:46
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Sun and moon position using PyEphem

Post by EdwinK » Wednesday 07 November 2018 23:50

Xorfor wrote:
Wednesday 07 November 2018 22:08
That is really coincidental!

I recently created a plugin https://github.com/Xorfor/Domoticz-SunMoon-Plugin also with Ephem, and with almost the same devices.

There are a lot more functions available, like next sun rise, etc. I am working on implementing these functions in my Domoticz API.

Do you use another version of Ephem? When runnin your version i keep getting the message the Ephem isn't found
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | RFxcom | Dashticz V2

User avatar
EdwinK
Posts: 1382
Joined: Sunday 22 January 2017 22:46
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Sun and moon position using PyEphem

Post by EdwinK » Wednesday 07 November 2018 23:53

Xorfor wrote:
Wednesday 07 November 2018 23:46
Hmmm, perhaps nice to combine your script, my plugin and https://github.com/ycahome/MoonPhases to one plugin?
Yes, please ;)
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | RFxcom | Dashticz V2

User avatar
Xorfor
Posts: 103
Joined: Sunday 03 December 2017 23:37
Target OS: Raspberry Pi
Domoticz version: Beta
Contact:

Re: Sun and moon position using PyEphem

Post by Xorfor » Thursday 08 November 2018 0:02

EdwinK wrote:
Wednesday 07 November 2018 23:50
Do you use another version of Ephem? When runnin your version i keep getting the message the Ephem isn't found
Yes, I am using the python3 install. Did you do the Ephem installation as described in https://github.com/Xorfor/Domoticz-SunM ... /README.md? That is why my version is still in test phase. If you use:

Code: Select all

sudo pip install ephem
, the package is not installed in the paths of Domoticz python plugins. For my plugin, please use:

Code: Select all

sudo pip3 install ephem -t /usr/lib/python3.5
This will install Ephem in '/usr/lib/python3.5' and Domoticz will find this package.

MikeF
Posts: 227
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi
Domoticz version: V4.9700
Location: UK
Contact:

Re: Sun and moon position using PyEphem

Post by MikeF » Thursday 08 November 2018 10:25

Forgot to say that I’m running my script under Python 2.7 (so I did ‘sudo pip install pyephem’) but I realise you need 3.x for a Domoticz plugin.

User avatar
EdwinK
Posts: 1382
Joined: Sunday 22 January 2017 22:46
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Sun and moon position using PyEphem

Post by EdwinK » Thursday 08 November 2018 10:55

Xorfor wrote:
Thursday 08 November 2018 0:02
EdwinK wrote:
Wednesday 07 November 2018 23:50
Do you use another version of Ephem? When runnin your version i keep getting the message the Ephem isn't found
Yes, I am using the python3 install. Did you do the Ephem installation as described in https://github.com/Xorfor/Domoticz-SunM ... /README.md? That is why my version is still in test phase. If you use:

Code: Select all

sudo pip install ephem
, the package is not installed in the paths of Domoticz python plugins. For my plugin, please use:

Code: Select all

sudo pip3 install ephem -t /usr/lib/python3.5
This will install Ephem in '/usr/lib/python3.5' and Domoticz will find this package.
Must not have seen that. But now I did the pip3 install. So I guess the problem is over now.
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | RFxcom | Dashticz V2

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests