Templating language

To create dynamic templates that can be used directly in our backend we use Go:s built-in templating language. Below is a brief description of the syntax.

Basic syntax

Some variables as described below can be included in messages templates and other config parameters. The template string uses double {{}} brackets with the path to the variable from the root object denoted with a dot (.).

Example

Your {{.installation.Name}} reported a temperature of {{.payload.value}}{{.payload.unit}}.

Using this template on an installation named "Summer house" with parameters value (24.5 )and unit (°C) in the POST data will result in a message like the following.

Your Summer house reported a temperature of 24.5°C

More details about Golang Templating can be found in their documentationopen in new window.

Injected data

The following is a list of all data that is injected into the template when it's executed.

Var nameDescription
.installation.IDID of the installation
.installation.NameName of the installation
.installation.ClientIDClient ID of the installation
.organization.IDID of the organisations that the installation belongs to
.organization.NameName of the organisations that the installation belongs to
.organization.Address.AddressAddress of the organisations that the installation belongs to
.organization.Address.CityCity of the organisations that the installation belongs to
.organization.Address.CountryCountry of the organisations that the installation belongs to
.organization.Address.ZIPZIP code of the organisations that the installation belongs to
.organization.EmailContact email of the organisations that the installation belongs to
.organization.PhoneContact phone number of the organisations that the installation belongs to
.organization.NotesNotes in the organisations that the installation belongs to
.payload.*All JSON data included in the POST request when sending

Injected functions

Some special functions are injected into the templating engine as described below.

toTime

Converts the supplied time as seconds to a time object.

Happened at: {{ toTime .payload.timestamp }}

The output format can be specified according to Go timeopen in new window

Happened at: {{ (toTime .payload.timestamp).Format "2006-01-02 03:04:05" }}

toDuration

Converts a number of seconds to a human-readable time duration such as 2m or 5h.

Happened {{ toDuration .payload.age_seconds }} ago.

now

Returns a time object for the current timestamp (at execution of the message template).

Message executed at {{ now }}.

Advanced examples

This template specifies the number of decimals to use. The printf function accepts all format strings according to Go fmtopen in new window.

Value is: {{ printf "%.2f" .payload.value}}

This template uses an if/else-if/else statement to change the text in the message based on the payload value. The - inside of the brackets removes spaces and newline characters.

{{- if lt .payload.value 0.0 -}}
Less than zero
{{- else if gt .payload.value 0.0 -}}
Greater than zero
{{- else -}}
It's zero
{{- end -}}

This template calculates the time difference between the timestamp in the message and when the template was executed.

Message timestamp was: {{ ((toTime .payload.timestamp).Sub now).Abs }} ago.