diff --git a/plugins/outputs/mqtt/README.md b/plugins/outputs/mqtt/README.md index 30a174353..c52a1243a 100644 --- a/plugins/outputs/mqtt/README.md +++ b/plugins/outputs/mqtt/README.md @@ -201,6 +201,7 @@ while `homie_node_id` will provide a template for the `node-id` part of the topic. Both options can contain [Go templates][GoTemplates] similar to `topic` with `{{ .PluginName }}` referencing the metric name and `{{ .Tag "key"}}` referencing the tag with the name `key`. +[Sprig](http://masterminds.github.io/sprig/) helper functions are available. For example writing the metrics diff --git a/plugins/outputs/mqtt/homie.go b/plugins/outputs/mqtt/homie.go index 16f56af77..9d250c043 100644 --- a/plugins/outputs/mqtt/homie.go +++ b/plugins/outputs/mqtt/homie.go @@ -8,6 +8,8 @@ import ( "strings" "text/template" + "github.com/Masterminds/sprig/v3" + "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/internal" ) @@ -102,7 +104,7 @@ type HomieGenerator struct { } func NewHomieGenerator(tmpl string) (*HomieGenerator, error) { - tt, err := template.New("topic_name").Parse(tmpl) + tt, err := template.New("topic_name").Funcs(sprig.TxtFuncMap()).Parse(tmpl) if err != nil { return nil, err }