Skip to content

Commit

Permalink
addon to turn on for only 4 hours and shut off after
Browse files Browse the repository at this point in the history
  • Loading branch information
drnoa committed Apr 4, 2024
1 parent 98bf2be commit 6eeb496
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
7 changes: 4 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"shellyTempURL": "http://192.168.178.1/rpc/Temperature.GetStatus?id=102",
"shellyHeatingOnURL": "http://192.168.178.1/rpc/Switch.Set?id=0&on=true",
"shellyTempURL": "http://[yourIP]/rpc/Temperature.GetStatus?id=102",
"shellyHeatingOnURL": "http://[yourIP]/rpc/Switch.Set?id=0&on=true",
"shellyHeatingOffURL": "http://[yourIP]/rpc/Switch.Set?id=0&on=false",
"temperatureThreshold": 55,
"temperatureTurnOff": 60,
"checkInterval": 5,
"weeklyCheckInterval": 168

}
55 changes: 50 additions & 5 deletions heating_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
type Config struct {
ShellyURL string `json:"shellyTempURL"` // URL of the Shelly device temperature addon.
ShellyHeatingOnURL string `json:"shellyHeatingOnURL"` // URL to turn Shelly heating on.
ShellyHeatingOffURL string `json:"shellyHeatingOffURL"` // URL to turn Shelly heating off.
TemperatureThreshold float64 `json:"temperatureThreshold"` // Temperature threshold in Celsius.
TemperatureTurnOff float64 `json:"temperatureTurnOff"` // Temperature at which to turn off the heating.
CheckInterval int `json:"checkInterval"` // Check interval in minutes.
WeeklyCheckInterval int `json:"weeklyCheckInterval"` // Weekly check interval in hours.

Expand Down Expand Up @@ -64,7 +66,7 @@ func (hm *HeatingManager) StartWeeklyCheck() {
defer weeklyCheckTimer.Stop()

for range weeklyCheckTimer.C {
hm.weeklyCheck(hm.Config.ShellyHeatingOnURL)
hm.weeklyCheck(hm.Config.ShellyHeatingOnURL, hm.Config.ShellyHeatingOffURL)
weeklyCheckTimer.Reset(hm.nextWeeklyCheckDuration())
}
}
Expand Down Expand Up @@ -128,18 +130,18 @@ func getTemperature(shellyTempURL string) (float64, error) {
}

// weeklyCheck checks if the temperature threshold has been exceeded and turns on the Shelly heating if necessary.
func (hm *HeatingManager) weeklyCheck(shellyHeatingOnURL string) {
func (hm *HeatingManager) weeklyCheck(shellyHeatingOnURL string, shellyHeatingOffURL string) {
if !hm.TemperatureExceeded {
if err := hm.turnShellyOn(shellyHeatingOnURL); err != nil {
if err := hm.turnShellyOn(shellyHeatingOnURL, shellyHeatingOffURL); err != nil {
log.Printf("Failed to turn on Shelly: %v", err)
}
}
hm.TemperatureExceeded = false
hm.saveLastCheckTime()
}

// turnShellyOn turns on the Shelly heating.
func (hm *HeatingManager) turnShellyOn(shellyHeatingOnURL string) error {
// turnShellyOn turns on the Shelly heating, schedules it to turn off after 4 hours, and checks if the temperature exceeds 60°C.
func (hm *HeatingManager) turnShellyOn(shellyHeatingOnURL, shellyHeatingOffURL string) error {
resp, err := http.Get(shellyHeatingOnURL)
if err != nil {
return fmt.Errorf("failed to turn on Shelly: %v", err)
Expand All @@ -151,6 +153,49 @@ func (hm *HeatingManager) turnShellyOn(shellyHeatingOnURL string) error {
}

fmt.Println("Shelly turned on.")

// Schedule to turn off after 4 hours
offTimer := time.AfterFunc(4*time.Hour, func() {
if err := hm.turnShellyOff(shellyHeatingOffURL); err != nil {
log.Printf("Failed to turn off Shelly: %v", err)
}
fmt.Println("Shelly turned off.")
hm.TemperatureExceeded = false
})

// Check temperature every minute to see if it exceeds 60°C
checkTimer := time.NewTicker(5 * time.Minute)
go func() {
for range checkTimer.C {
temp, err := getTemperature(hm.Config.ShellyURL)
if err != nil {
log.Printf("Error checking temperature: %v", err)
continue
}
if temp > hm.Config.TemperatureTurnOff {
fmt.Println("Temperature exceeded. Turning off Shelly.")
checkTimer.Stop()
offTimer.Stop()
hm.TemperatureExceeded = false
}
}
}()
return nil
}

// turnShellyOff turns off the Shelly heating.
func (hm *HeatingManager) turnShellyOff(shellyHeatingOffURL string) error {
resp, err := http.Get(shellyHeatingOffURL)
if err != nil {
return fmt.Errorf("failed to turn off Shelly: %v", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to turn off Shelly: status code %d", resp.StatusCode)
}

fmt.Println("Shelly turned off.")
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion heating_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestCheckTemperature(t *testing.T) {

func TestWeeklyCheck(t *testing.T) {
manager, _ := NewHeatingManager()
manager.weeklyCheck("someURL")
manager.weeklyCheck("someURL", "someOtherURL")
}

func TestGetTemperature(t *testing.T) {
Expand Down

0 comments on commit 6eeb496

Please sign in to comment.