I've just stumbled across this Python script for calculating sun position:
https://github.com/s-bear/sun-position.
It's based on an algorithm which claims to calculate "the solar zenith and azimuth angles in the period from the year −2000 to 6000, with uncertainties of ±0.0003°".
Save it as
Code: Select all
/path_to_python_scripts/sunposition.py
It runs happily on a RPi under Python 2.7 (may need some changes for Python 3.x).
I've created a separate Python script to read azimuth and zenith values from sunposition.py (zenith is converted to altitude: altitude = 90 - zenith), and then writes the values to two custom sensor devices in Domoticz:

Save the following script in the same folder as above, and run it with cron at your required frequency.
- Spoiler: show
Code: Select all
#!/usr/bin/env python
#
# Uses sunposition.py
# to get sun altitude and azimuth
# and sends to Domoticz
from sunposition import sunpos
from datetime import datetime
import requests
# Local values
lat = <your latitude in decimal degrees>
lon = <your longitude in decimal degrees>
e = <your elevation in metres, or 0)
# Domoticz command stub and IDx of altitude and azimuth
baseURL = 'http://<domoticz url:port>/json.htm?type=command¶m=udevice&nvalue=0'
altIdx = <idx for altitude custom sensor>
azIdx = <idx for azimuth custom sensor>
# Get sun position values
now = datetime.utcnow()
azimuth, zenith = sunpos(now, lat, lon, e)[:2] #discard RA, dec, H
azimuth = round(azimuth, 1)
#convert zenith to elevation
altitude = round(90.0 - zenith, 1)
#print altitude, azimuth
# Send data to Domoticz
url = baseURL + "&idx=%s&svalue=%s" % (altIdx, altitude)
r = requests.get(url)
url = baseURL + "&idx=%s&svalue=%s" % (azIdx, azimuth)
r = requests.get(url)