This is an example for a gateway component which receives MQTT messages from
- OpenDTU
- OpenMQTTGateway
- Shelly (Generic status update)
- Sensor data (Klimalogger, CircuitPy-Logger)
and writes the data into InfluxDB / TimescaleDB (PostgreSQL) time series databases.
File config.yml in root folder:
mqttUrl: "mqtts://<hostname>:8883" # use mqtt:// for an unencrypted connection
mqttClientId: "sensors_gateway"
mqttUsername: "${MQTT_USERNAME}" # optional
mqttPassword: "${MQTT_PASSWORD}" # optional
sources:
- name: "Sensor data"
type: "sensor"
prefix: "sensors"
patterns: # optional; selects non-JSON payloads
- topic: "sensors/+/temperature" # MQTT filter (+ one level, # the rest)
format: "float" # float = bare number (json = default)
sensor: "raw" # optional `sensor` tag, defaults to location
- topic: "sensors/+/count" # integer payloads
format: "int" # int = bare signed integer
sensor: "raw"
targets:
- type: "influxdb"
url: "http://<host>:8086"
database: "sensors"
- type: "postgresql"
host: "<postgres host>"
port: 5432
user: "<psql username>"
password: "${POSTGRES_PASSWORD}"
database: "sensors"
tls: false
- name: "Shelly data"
type: "shelly"
prefix: "shellies"
targets:
- type: "influxdb"
url: "http://<influx host>:8086"
database: "shelly"
token: "${INFLUX_TOKEN}"
- type: "postgresql"
host: "<postgres host>"
port: 5433
user: "<psql username>"
password: "${POSTGRES_PASSWORD}"
database: "shelly"
- name: "PV data"
type: "opendtu"
prefix: "solar"
targets:
- type: "influxdb"
url: "http://<influx host>:8086"
database: "solar"
- MQTT broker:
mqttUrl,mqttClientIdand optionalmqttUsername/mqttPassword. A secure scheme (ssl://,tls://,mqtts://orwss://) enables TLS with server-certificate verification. - InfluxDB targets take a
url(for examplehttp://<host>:8086) and eitheruser/passwordor atoken. - PostgreSQL targets are only supported for
sensorandshellysources, because only those provide the requiredlocation/sensor/valueschema. Settls: trueto require a verified TLS connection. - Source
prefix: one or more topic segments separated by/(for examplehome/shelly). A prefix must not be empty, start or end with/, or contain the wildcards+or#; configuration validation rejects those at startup. Each received message is routed to the source with the longest prefix that matches the topic on a segment boundary. - Sensor
patterns: an optional list of MQTT topic filters that select how a message under the prefix is decoded. Each entry has atopicfilter (+matches one level,#matches the remaining levels) and aformatofjson(the default{ sensor, time, value }object),float(a bare number such as19.45), orint(a bare signed integer such as42). The first matching pattern wins and unmatched topics keep the default JSON decoding. Forfloatandintpatterns an optionalsensortag can be set (it defaults to the topic's location segment); the reading is stamped with the receive time because a bare number carries no timestamp. Anintpattern only accepts strict integers: a payload with a fraction or exponent (for example19.0or1.9e1) is dropped with a warning. - Environment variables: any
${VAR_NAME}placeholder in the configuration file is replaced with the value of that environment variable. Referencing an undefined variable is a configuration error. This lets secrets stay out of the file (for example via Docker/Compose secrets).