Day/Night via Sunrise/Sunset - Printable Version +- BlueWave Studio forum (https://bluewavestudio.io/community) +-- Forum: Development (https://bluewavestudio.io/community/forum-88.html) +--- Forum: Suggestions and Feedback (https://bluewavestudio.io/community/forum-65.html) +--- Thread: Day/Night via Sunrise/Sunset (/thread-2255.html) |
Day/Night via Sunrise/Sunset - Justindu - 12-09-2020 Having a static time preference is problematic for day/night switch. This requires constant updates. If the day/night switch used a sunset/sunrise option - with an +/- time offset for personal preference, then it would manage itself without having to wire in additional GPIO connections. The functions that find sunset/sunrise need a date and some local info, like a city (for longitude/latitude), to calculate the time dynamically. RE: Day/Night via Sunrise/Sunset - BlueWave - 12-09-2020 The easiest way so far would be to write some simple script that runs and checks sunset/sunrise time before each start of OpenAuto Pro and modifies its config accordingly. RE: Day/Night via Sunrise/Sunset - Justindu - 12-09-2020 (12-09-2020, 08:06 PM)BlueWave Wrote: The easiest way so far would be to write some simple script that runs and checks sunset/sunrise time before each start of OpenAuto Pro and modifies its config accordingly. Yes - can do. I need to know what file to update. Can you provide the location and syntax? Thank you Justin RE: Day/Night via Sunrise/Sunset - BlueWave - 12-09-2020 It is well described in docs at https://www.bluewavestudio.io/community/showthread.php?tid=2042&pid=11925#pid11925 RE: Day/Night via Sunrise/Sunset - Justindu - 12-17-2020 This is the solution I came up with: #!/usr/bin/env python3 # Author: Justin DuBois # Required library - astral - # If missing, execute 'pip3 install astral' import os import datetime from datetime import datetime from datetime import date from astral import LocationInfo from astral.sun import sun inifile="/home/pi/.openauto/config/openauto_system.ini" # --------- Find today's sunise and sunset hour and minuet in city location ---------------- city = LocationInfo("San Francisco", "Pacific", "America/Los_Angeles", 37.77397, -122.431297) #print(( # f"Information for {city.name}/{city.region}\n" # f"Timezone: {city.timezone}\n" # f"Latitude: {city.latitude:.02f}; Longitude: {city.longitude:.02f}\n" #)) s = sun(city.observer, date=date.today(), tzinfo=city.timezone) csrise = (s["sunrise"]).strftime('%H:%M') csset = (s["sunset"]).strftime('%H:%M') #-- search in OAP system file for sunrise and sunset values -- with open(inifile, 'r',encoding = 'utf-8') as f: for line in f: keyword = 'SunriseTime' if line.startswith(keyword): try: inisunrise = (line.split('=')[1].rstrip()) except: print ("Error finding SunriseTime in openauto_system.ini") keyword = 'SunsetTime' if line.startswith(keyword): try: inisunset = (line.split('=')[1].rstrip()) except: print ("Error reading SunsetTime in openauto_system.ini") # ----------- Update ini file ------------ # Read in the file with open(inifile, 'r',encoding = 'utf-8') as file : filedata = file.read() #print('inisunrise=',inisunrise) #print('csrise=',csrise) #print('inisunset=',inisunset) #print('csset=',csset) # Replace the target string filedata = filedata.replace('SunriseTime='+inisunrise, 'SunriseTime='+csrise) filedata = filedata.replace('SunsetTime='+inisunset, 'SunsetTime='+csset) # Write the file out again with open(inifile, 'w',encoding = 'utf-8') as file: file.write(filedata) RE: Day/Night via Sunrise/Sunset - KreAch3R - 09-01-2021 (12-17-2020, 07:14 AM)Justindu Wrote: ....................... Hey, Justin, thanks for sharing this! I think I got it to work. Can you tell me which way you used to make it run on each startup? EDIT: Well I liked the script too much and thought about making it better by adding dynamic geolocation using the GPS a lot of us have on our devices, and if that doesn't exist, IP based geolocation (needs internet). Retained the manual way though. And added a second way to get the sunrise/sunset values besides the astral module. You can find my revision here: https://github.com/KreAch3R/daynightlocation I thought about running it in the startup and I'm using the common systemd service way. RE: Day/Night via Sunrise/Sunset - Justindu - 09-02-2021 (09-01-2021, 09:32 PM)KreAch3R Wrote:(12-17-2020, 07:14 AM)Justindu Wrote: ....................... I created an executable .sh that I run at startup - like so: #!/bin/sh #Updatesuntime.sh #run this at startup. Add the line below before openauto launch in file /etc/xdg/lxsession/LXDE-pi/autostart #@/home/pi/Updatesuntime.sh DISPLAY=:0.0 XAUTHORITY=/home/pi/.Xauthority python3 /home/pi/Updatesuntime.py & |