From: jweigele Date: Wed, 16 Aug 2023 21:17:15 +0000 (-0700) Subject: Sun-up deactivation of lights to save energy X-Git-Url: http://git.hexthepla.net/?a=commitdiff_plain;h=02b46cfecd01bffa592e6f8aa861891ed066f454;p=rabbit_go Sun-up deactivation of lights to save energy * Default is not enabled, "NightOnly: True" in yaml to turn on --- diff --git a/go.mod b/go.mod index d4aa3dc..fe6da22 100644 --- 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 diff --git a/lights/main.go b/lights/main.go index 6b27b0f..250660a 100644 --- a/lights/main.go +++ b/lights/main.go @@ -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 diff --git a/reprocess/main.go b/reprocess/main.go index 74b8162..c5b0637 100644 --- a/reprocess/main.go +++ b/reprocess/main.go @@ -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", ""))