Sun-up deactivation of lights to save energy
authorjweigele <jweigele@local>
Wed, 16 Aug 2023 21:17:15 +0000 (14:17 -0700)
committerjweigele <jweigele@local>
Wed, 16 Aug 2023 21:17:15 +0000 (14:17 -0700)
 * Default is not enabled, "NightOnly: True" in yaml to turn on

go.mod
lights/main.go
reprocess/main.go

diff --git a/go.mod b/go.mod
index d4aa3dc4ba67db40ca57847f18bbbefced62beaf..fe6da22d7a4743a74e9ff30381b26cd74d446859 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -8,6 +8,7 @@ require (
        github.com/leekchan/timeutil v0.0.0-20150802142658-28917288c48d
        github.com/lucasb-eyer/go-colorful v1.2.0
        github.com/mrflynn/go-aqi v0.0.9
+       github.com/nathan-osman/go-sunrise v1.1.0
        github.com/prometheus/client_golang v1.14.0
        github.com/rabbitmq/amqp091-go v1.5.0
        golang.org/x/exp v0.0.0-20221212164502-fae10dda9338
index 6b27b0fb4f288f5580bbd156498bacb11f4d276f..250660a769b5908bea5dffdd656a064c7ceb3f86 100644 (file)
@@ -18,6 +18,9 @@ import (
        "github.com/prometheus/client_golang/prometheus/promauto"
        "github.com/prometheus/client_golang/prometheus/promhttp"
 
+       // sunrise/sunset activation
+       "github.com/nathan-osman/go-sunrise"
+
        // color correction
        "github.com/lucasb-eyer/go-colorful"
        "golang.org/x/exp/slices"
@@ -38,6 +41,9 @@ var (
                },
                []string{"location"},
        )
+       // used for NightOnly calculations later, Seattle def
+       sunriseLat float64 = 47.608013
+       sunriseLon float64 = -122.335167
 )
 
 // structs
@@ -195,6 +201,7 @@ type RGBRelay struct {
        LastMotion        time.Time
        LastState         bool
        Dummy             bool `yaml:"Dummy"`
+       NightOnly         bool `yaml:"NightOnly"`
        logger            logr.Logger
        sendChannel       chan helper.RabbitSend
        switches          []*Switch `yaml:"Switches"`
@@ -287,8 +294,41 @@ func (relay *RGBRelay) ForceRefresh() {
        relay.LastUpdate = relay.LastUpdate.Add(-staleDuration)
 }
 
+func sunIsUp(localTime time.Time) bool {
+
+       year, month, day := localTime.Date()
+       // get the sunrise/sunset times for the current day
+       rise, set := sunrise.SunriseSunset(
+               sunriseLat, sunriseLon,
+               year, month, day,
+       )
+       oneDay, _ := time.ParseDuration("24h")
+       fudgeDuration, _ := time.ParseDuration("1h")
+       year, month, day = localTime.Add(-oneDay).Date()
+       risePrevious, setPrevious := sunrise.SunriseSunset(
+               sunriseLat, sunriseLon,
+               year, month, day,
+       )
+       logger.V(4).Info("Sunrise and sunset times", "sunrise", rise, "sunset", set, "localTime", localTime)
+       logger.V(4).Info("Previous sunrise and sunset times", "sunrise", risePrevious, "sunset", setPrevious)
+
+       if (localTime.After(rise.Add(fudgeDuration)) && localTime.Before(set.Add(-fudgeDuration))) || (localTime.After(risePrevious.Add(fudgeDuration)) && localTime.Before(setPrevious.Add(-fudgeDuration))) {
+               return true
+       } else {
+               return false
+       }
+
+}
 func (relay *RGBRelay) shouldDim() bool {
-       curTime := time.Now().UTC()
+       localTime := time.Now()
+       curTime := localTime.UTC()
+       // dim _only_ if during sun-up in current day, otherwise use regular logic
+       if relay.NightOnly {
+               if sunIsUp(localTime) {
+                       return true
+               }
+
+       }
        dimmingDuration := time.Duration(relay.DimmingTimeout) * time.Second
        if relay.LastMotion.Add(dimmingDuration).Before(curTime) {
                return true
index 74b8162b30bcabf5ffa124a3bd191099947e6e96..c5b06377d68cdbb476559bdcdc299add4717d94b 100644 (file)
@@ -675,7 +675,8 @@ func main() {
        devices = append(devices, newdoordevice("data_door"))
 
        // hex devices (super custom)
-       devices = append(devices, newhexdevice("0x7e6af7fefff95560", "Downstairs Test"))
+       //devices = append(devices, newhexdevice("0x7e6af7fefff95560", "Downstairs Test"))
+       devices = append(devices, newhexdevice("0x526af7fefff95560", "Downstairs Test"))
 
        devices = append(devices, newesp32wifidevice("esp32.sensor_info", ""))