Skip to content

Commit

Permalink
Added option to specify custom names for MQTT topics (closes: #13).
Browse files Browse the repository at this point in the history
  • Loading branch information
a-bali committed Sep 4, 2023
1 parent 6fc088d commit 199fceb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release-binaries.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ jobs:
github_token: ${{ secrets.GITHUB_TOKEN }}
goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }}
goarm: ${{ matrix.goarm }}
build_command: make
extra_files: LICENSE README.md config.yml
2 changes: 2 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ monitor:
- topic: "/sensors/aqara_inside/#"
# Custom timeout threshold (fixed value in seconds) to be used for the topic (default: standardtimeout as defined above)
timeout: 3600
# Custom name to show instead of MQTT topic (note: does not work with wildcard topic names, only exact matches)
name: "Inside temperature"
- topic: "/sensors/aqara_outside/#"
timeout: 3600
- topic: "/sensors/#"
Expand Down
17 changes: 15 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Config struct {
StandardTimeout float64
Targets []struct {
Topic string
Name string
Timeout int
}
}
Expand Down Expand Up @@ -110,6 +111,7 @@ type TimedEntry struct {

// MQTTTopic stores status information on a MQTT topic.
type MQTTMonitorData struct {
Name string
FirstSeen time.Time
LastSeen time.Time
LastError time.Time
Expand Down Expand Up @@ -565,6 +567,16 @@ func connectMqttAlert() {
}
}

// finds a custom name in the configuration for a given topic
func findTopicName(topic string) string {
for _, t := range getConfig().Monitor.MQTT.Targets {
if t.Topic == topic && t.Name != "" {
return t.Name
}
}
return topic
}

// Receives an MQTT message and updates status accordingly.
func onMessageReceived(client mqtt.Client, message mqtt.Message) {
debug("MQTT: " + message.Topic() + ": " + string(message.Payload()))
Expand All @@ -576,6 +588,7 @@ func onMessageReceived(client mqtt.Client, message mqtt.Message) {
if !ok {
monitorData.MQTT[message.Topic()] = &MQTTMonitorData{}
e = monitorData.MQTT[message.Topic()]
e.Name = findTopicName(message.Topic())
}

if e.Deleted {
Expand Down Expand Up @@ -682,7 +695,7 @@ func evaluateMQTT() {

if elapsed > timeout {
if v.Status != STATUS_ERROR {
alert("MQTT", topic, STATUS_ERROR, v.LastSeen, fmt.Sprintf("timeout %.2fs", timeout))
alert("MQTT", v.Name, STATUS_ERROR, v.LastSeen, fmt.Sprintf("timeout %.2fs", timeout))
v.LastError = time.Now()
v.Alerts++
}
Expand All @@ -691,7 +704,7 @@ func evaluateMQTT() {
v.Status = STATUS_WARN
} else {
if v.Status == STATUS_ERROR {
alert("MQTT", topic, STATUS_OK, v.LastError, "")
alert("MQTT", v.Name, STATUS_OK, v.LastError, "")
}
v.Status = STATUS_OK
}
Expand Down
2 changes: 1 addition & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ <h3>MQTT targets</h3>
<input type="hidden" name="type" value="mqtt">
<input type="hidden" name="name" value="{{ $topic }}">
</form>
{{ $topic }}
<span title="{{ $topic }}"> {{ $value.Name }} </span>
<span class="delete">(<a
href="javascript:submitDelete('delete_mqtt_{{ id $topic }}')">x</a>)</span>
</div>
Expand Down

0 comments on commit 199fceb

Please sign in to comment.