Skip to content

Commit

Permalink
Add droppedBy and increasedBy trigger options (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
alec-pinson authored May 30, 2023
1 parent 2478a24 commit 72815e4
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 19 deletions.
29 changes: 17 additions & 12 deletions cmd/terrarium-bot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ type Switch struct {
}

type Sensor struct {
Id string `yaml:"id"`
Url string `yaml:"url"`
Insecure bool `yaml:"insecure"`
JsonPath string `yaml:"jsonPath"`
Unit string `yaml:"unit"`
Value float64
Id string `yaml:"id"`
Url string `yaml:"url"`
Insecure bool `yaml:"insecure"`
JsonPath string `yaml:"jsonPath"`
Unit string `yaml:"unit"`
Value float64
PreviousValue float64
}

type Notification struct {
Expand Down Expand Up @@ -90,14 +91,18 @@ type Alert struct {

type When struct {
Day struct {
Below float64 `yaml:"below"`
Above float64 `yaml:"above"`
Every time.Duration `yaml:"every"`
Below float64 `yaml:"below"`
Above float64 `yaml:"above"`
DroppedBy float64 `yaml:"droppedBy"`
IncreasedBy float64 `yaml:"increasedBy"`
Every time.Duration `yaml:"every"`
} `yaml:"day"`
Night struct {
Below float64 `yaml:"below"`
Above float64 `yaml:"above"`
Every time.Duration `yaml:"every"`
Below float64 `yaml:"below"`
Above float64 `yaml:"above"`
DroppedBy float64 `yaml:"droppedBy"`
IncreasedBy float64 `yaml:"increasedBy"`
Every time.Duration `yaml:"every"`
} `yaml:"night"`
}

Expand Down
7 changes: 7 additions & 0 deletions cmd/terrarium-bot/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ trigger:
endpoint: /prevent-mist
action:
- trigger.mist.disable.40m # disable the below trigger for 40m
- id: doorOpened
sensor: humidity
when:
day:
droppedBy: 5 # usually when terrarium doors are opened
action:
- trigger.mist.disable.30m # disable the below trigger for 30m
- id: mist
sensor: humidity
when:
Expand Down
9 changes: 9 additions & 0 deletions cmd/terrarium-bot/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,22 @@ func InitSensors() {
}

func (s *Sensor) SetValue(value float64) {
if s.Value == 0 {
s.PreviousValue = value
} else {
s.PreviousValue = s.Value
}
s.Value = value
}

func (s *Sensor) GetValue() float64 {
return s.Value
}

func (s *Sensor) GetPreviousValue() float64 {
return s.PreviousValue
}

func (s *Sensor) getSensorValue() float64 {
r, respCode, err := SendRequest(s.Url, s.Insecure, 3, s.JsonPath != "")
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions cmd/terrarium-bot/sensor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,13 @@ func TestSensorGetValue(t *testing.T) {
t.Errorf("Expected sensor value to be 42, but got %v", v)
}
}

func TestSensorGetPreviousValue(t *testing.T) {
s := &Sensor{}
s.SetValue(42)
s.SetValue(12)
v := s.GetPreviousValue()
if v != 42 {
t.Errorf("Expected sensor value to be 42, but got %v", v)
}
}
28 changes: 21 additions & 7 deletions cmd/terrarium-bot/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ func GenerateReason(value float64, unit string, maxValue float64) string {

func (t *Trigger) monitor() {
var (
s *Sensor
runAction bool
reason string
value float64
s *Sensor
runAction bool
reason string
value float64
previousValue float64
)

// get trigger sensor if one is set
Expand All @@ -46,6 +47,7 @@ func (t *Trigger) monitor() {
for {
runAction = false
reason = ""
previousValue = 0
value = 0

if t.isDisabled() {
Expand All @@ -59,9 +61,9 @@ func (t *Trigger) monitor() {

if s != nil {
// get value from the sensor
previousValue = s.GetPreviousValue()
value = s.GetValue()
}

valueSet := value != 0
dayAbove := t.When.Day.Above != 0
dayBelow := t.When.Day.Below != 0
Expand All @@ -70,9 +72,18 @@ func (t *Trigger) monitor() {
dayEvery := t.When.Day.Every != 0
nightEvery := t.When.Night.Every != 0

// has the value dropped/increased by x or more
dayDroppedBy := t.When.Day.DroppedBy != 0 && value <= previousValue-t.When.Day.DroppedBy
nightDroppedBy := t.When.Night.DroppedBy != 0 && value <= previousValue-t.When.Night.DroppedBy
dayIncreasedBy := t.When.Day.IncreasedBy != 0 && value >= previousValue+t.When.Day.IncreasedBy
nightIncreasedBy := t.When.Night.IncreasedBy != 0 && value >= previousValue+t.When.Night.IncreasedBy

// check triggers based on time of day and value
if isDayTime() {
if valueSet && dayAbove && value > t.When.Day.Above {
if valueSet && (dayDroppedBy || dayIncreasedBy) {
runAction = true
reason = fmt.Sprintf("%.2f", previousValue) + s.Unit + "->" + fmt.Sprintf("%.2f", value) + s.Unit
} else if valueSet && dayAbove && value > t.When.Day.Above {
runAction = true
reason = GenerateReason(value, s.Unit, t.When.Day.Above)
} else if valueSet && dayBelow && value < t.When.Day.Below {
Expand All @@ -83,7 +94,10 @@ func (t *Trigger) monitor() {
runAction = true
}
} else {
if valueSet && nightAbove && value > t.When.Night.Above {
if valueSet && (nightDroppedBy || nightIncreasedBy) {
runAction = true
reason = fmt.Sprintf("%.2f", previousValue) + s.Unit + "->" + fmt.Sprintf("%.2f", value) + s.Unit
} else if valueSet && nightAbove && value > t.When.Night.Above {
runAction = true
reason = GenerateReason(value, s.Unit, t.When.Night.Above)
} else if valueSet && nightBelow && value < t.When.Night.Below {
Expand Down
58 changes: 58 additions & 0 deletions cmd/terrarium-bot/trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,65 @@ func TestMonitorTrigger(t *testing.T) {
t.Errorf("Trigger should have turned on the mock-switch but didn't")
}

// reuse same trigger to test DroppedBy
trigger.When.Day.Above = 0
// set when dropped by to 5
trigger.When.Day.DroppedBy = 5

// set the sensor to 60
s.SetValue(60)
// call trigger monitor to make sure it turns off the switch
trigger.monitor()
if ss.getStatus() != "off" {
t.Errorf("DropBy trigger should have turned off the mock-switch but didn't")
}

// set the sensor to 56
s.SetValue(56)
// call trigger monitor to make sure it turns off the switch
trigger.monitor()
if ss.getStatus() != "off" {
t.Errorf("DropBy trigger should have turned off the mock switch but didn't. Current sensor val: %v, previous sensor val: %v", s.Value, s.PreviousValue)
}

// drop sensor value by 5
s.SetValue(51)
// call trigger monitor to make sure it turns off the switch
trigger.monitor()
if ss.getStatus() != "on" {
t.Errorf("DropBy trigger should have turned on the mock switch but didn't. Current sensor val: %v, previous sensor val: %v", s.Value, s.PreviousValue)
}

// reset the switch
s.SetValue(51)
trigger.monitor()
if ss.getStatus() != "off" {
t.Errorf("Trigger should have turned off the mock switch but didn't. Current sensor val: %v, previous sensor val: %v", s.Value, s.PreviousValue)
}

// set when dropped by to 0
trigger.When.Day.DroppedBy = 0
// now to test increased by
trigger.When.Day.IncreasedBy = 5

// set the sensor to 53
s.SetValue(53)
// call trigger monitor to make sure it turns off the switch
trigger.monitor()
if ss.getStatus() != "off" {
t.Errorf("IncreaseBy trigger should have turned off the mock-switch but didn't")
}

// increase sensor value by 5+
s.SetValue(60)
// call trigger monitor to make sure it turns off the switch
trigger.monitor()
if ss.getStatus() != "on" {
t.Errorf("IncreaseBy trigger should have turned on the mock switch but didn't. Current sensor val: %v, previous sensor val: %v", s.Value, s.PreviousValue)
}

// reset
config.Debug = false
config.DryRun = false
isTesting = false
}
Expand Down

0 comments on commit 72815e4

Please sign in to comment.