BlueWave Studio forum
  • Login
  • Register
  • Login Register
    Login
    Username:
    Password:
  • Home
  • Help
  • View Today's Post
  • More
    • BlueWave Studio
    • Shop
User Links
  • Login
  • Register
  • Login Register
    Login
    Username:
    Password:

    Quick Links Home Help View Today's Post
    More
    • BlueWave Studio
    • Shop
    BlueWave Studio forum Development Suggestions and Feedback Day/Night via Sunrise/Sunset

    Day/Night via Sunrise/Sunset
    Justindu
    Offline

    Member

    Posts: 150
    Threads: 25
    Joined: Jul 2020
    Reputation: 5
    #1
    12-09-2020, 07:26 PM
    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.
    BlueWave
    Offline

    Administrator

    Posts: 2,750
    Threads: 64
    Joined: Jun 2019
    Reputation: 67
    #2
    12-09-2020, 08:06 PM
    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.
    Justindu
    Offline

    Member

    Posts: 150
    Threads: 25
    Joined: Jul 2020
    Reputation: 5
    #3
    12-09-2020, 08:20 PM
    (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
    BlueWave
    Offline

    Administrator

    Posts: 2,750
    Threads: 64
    Joined: Jun 2019
    Reputation: 67
    #4
    12-09-2020, 08:59 PM
    It is well described in docs at https://www.bluewavestudio.io/community/...p?tid=2042&pid=11925#pid11925
    Justindu
    Offline

    Member

    Posts: 150
    Threads: 25
    Joined: Jul 2020
    Reputation: 5
    #5
    12-17-2020, 07:14 AM
    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)
    KreAch3R
    Offline

    Member

    Posts: 176
    Threads: 14
    Joined: Apr 2020
    Reputation: 12
    #6
    09-01-2021, 09:32 PM (This post was last modified: 09-02-2021, 10:08 AM by KreAch3R.)
    (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.
    Justindu
    Offline

    Member

    Posts: 150
    Threads: 25
    Joined: Jul 2020
    Reputation: 5
    #7
    09-02-2021, 04:40 PM
    (09-01-2021, 09:32 PM)KreAch3R Wrote:
    (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.

    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 &
    « Next Oldest | Next Newest »

    Users browsing this thread: 1 Guest(s)



    • View a Printable Version
    • Subscribe to this thread
    Forum Jump:

    Home · Help · BlueWave Studio · Shop

    Copyright © bluewavestudio.io. All rights reserved.

    Linear Mode
    Threaded Mode