diff --git a/devices/dimmable.go b/devices/dimmable.go index 3bad8a3..600fbbb 100644 --- a/devices/dimmable.go +++ b/devices/dimmable.go @@ -60,7 +60,6 @@ func (d *Dimmable) setTarget(target float64) { } func (d *Dimmable) ProcessRequest(request core.SwitchRequest) { - log.Printf("Setting %s to %s within %d seconds\n", d.GetMqttTopic(), request.Value, request.Duration) value, _ := strconv.ParseFloat(request.Value, 64) value = math.Min(value, 100) value = math.Max(value, 0) diff --git a/devices/rule.go b/devices/rule.go index cf388d3..7fd5a8a 100644 --- a/devices/rule.go +++ b/devices/rule.go @@ -98,6 +98,7 @@ func (r *Rule) Fire(channel chan core.SwitchRequest) []Receiver { } func (r *Rule) checkCondition(value any, condition string, target any) bool { + // log.Printf("Checking condition %v %s %v\n", value, condition, target) switch condition { case "==": return value == target @@ -106,7 +107,14 @@ func (r *Rule) checkCondition(value any, condition string, target any) bool { case ">": switch target.(type) { case int: - return value.(int) > target.(int) + var val int + switch value.(type) { + case int: + val = value.(int) + case int64: + val = int(value.(int64)) + } + return val > target.(int) case float64: return value.(float64) > target.(float64) } diff --git a/devices/sensor.go b/devices/sensor.go index 6932bbf..d2f404f 100644 --- a/devices/sensor.go +++ b/devices/sensor.go @@ -17,13 +17,13 @@ type Sensor struct { func MakeSensor(config core.DeviceConfig) Sensor { s := Sensor{} s.setBaseConfig(config) + s.MqttState = config.Topic s.Max = 100 s.Min = 0 s.Active = false - s.Current = 0 - s.Target = 0 + s.Type = "sensor" return s } @@ -46,19 +46,11 @@ type SensorMessageWrapper struct { func (s *Sensor) PublishValue(mqtt.Client) { } -func (s *Sensor) GetStateMqttTopic() string { - return s.MqttState -} - -func (s *Sensor) GetMessageHandler(channel chan core.SwitchRequest, _ DeviceInterface) mqtt.MessageHandler { +func (s *Sensor) GetMessageHandler(_ chan core.SwitchRequest, _ DeviceInterface) mqtt.MessageHandler { return func(client mqtt.Client, mqttMessage mqtt.Message) { payload := mqttMessage.Payload() - if s.GetCurrent() == 0 { - return - } - var data SensorMessageWrapper err := json.Unmarshal(payload, &data) if err != nil { @@ -69,11 +61,10 @@ func (s *Sensor) GetMessageHandler(channel chan core.SwitchRequest, _ DeviceInte message := data.TuyaReceived if message.Cmnd == 5 || message.Cmnd == 2 { + s.Active = true + now := time.Now() + s.LastChanged = &now log.Printf("Motion detected (%d)", message.Cmnd) - request, ok := s.GenerateRequest(message.CmndData) - if ok { - channel <- request - } } } @@ -81,7 +72,16 @@ func (s *Sensor) GetMessageHandler(channel chan core.SwitchRequest, _ DeviceInte func (s *Sensor) GetTriggerValue(key string) interface{} { if key == "noMotion" { - return time.Now().Unix() - s.LastChanged.Unix() + if s.LastChanged != nil { + return time.Now().Unix() - s.LastChanged.Unix() + } + return 0 + } + if key == "motion" { + if s.Active { + s.Active = false + return 1 + } } return nil }