Another sun position calculator Topic is solved

Python and python framework

Moderator: leecollings

Post Reply
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Another sun position calculator

Post by MikeF »

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°". :o :o :o
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:
Image
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&param=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)

mash47
Posts: 13
Joined: Wednesday 04 December 2019 13:25
Target OS: Raspberry Pi / ODroid
Domoticz version: v4.10717
Location: Sheffield, UK
Contact:

Re: Another sun position calculator

Post by mash47 »

Hello,

This is my first post, having just migrated most of my devices from a Veralite system to Domoticz on a raspberry pi.

I had a sun position plugin on the Veralite to trigger a position change of my vertical blinds and I would like to run @MikeF 's script on the raspi. Python is new to me, I'm OK with c and Lua. I've placed the script & module in /usr/lib/python2.7 and tried to run it from the command line. This was the result ...

Code: Select all

pi@raspberrypi:/usr/lib/python2.7 $ python find_sunposition
Traceback (most recent call last):
  File "find_sunposition", line 7, in <module>
    from sunposition import sunpos
  File "/usr/lib/python2.7/sunposition.py", line 23, in <module>
    import numpy as np
ImportError: No module named numpy
Success after this ...

Code: Select all

pi@raspberrypi:/usr/lib/python2.7 $ sudo apt-get install python-numpy python-requests
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest