Skip to content

Commit

Permalink
Fix humiture environmental sensor
Browse files Browse the repository at this point in the history
There can be multiple topics with the same last segment, which causes
incorrect calculations for humiture as we're mix-matching data sources.

Since the topic is a string, use the whole thing as a key which is
unique.
  • Loading branch information
daenney committed Jun 30, 2022
1 parent 8b60e59 commit 640f15a
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 9 deletions.
4 changes: 2 additions & 2 deletions collectors/environmental.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (c *EnvironmentalCollector) Collect(ch chan<- prometheus.Metric) {
log.Print(err.Error())
continue
}
humidity[last(s.Info().Topic, "/")] = v
humidity[s.Info().Topic] = v
ch <- prometheus.MustNewConstMetric(c.relativeHumidity,
prometheus.GaugeValue, v, s.Info().Topic)
}
Expand All @@ -156,7 +156,7 @@ func (c *EnvironmentalCollector) Collect(ch chan<- prometheus.Metric) {
log.Print(err.Error())
continue
}
temperature[last(s.Info().Topic, "/")] = v
temperature[s.Info().Topic] = v
ch <- prometheus.MustNewConstMetric(c.temperature,
prometheus.GaugeValue, v, s.Info().Topic)
}
Expand Down
7 changes: 0 additions & 7 deletions collectors/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package collectors // import "hemtjan.st/sensorer/collectors"

import (
"strconv"
"strings"
)

// namespace is the Prometheus namespaces for all collectors in this package
Expand All @@ -16,9 +15,3 @@ func toFloat(s string) (float64, error) {
}
return f, nil
}

// last returns the last element of s split on sep
func last(s, sep string) string {
sp := strings.Split(s, sep)
return sp[len(sp)-1]
}

0 comments on commit 640f15a

Please sign in to comment.