Timescale Backend

TIP

After EMQX version 3.1, a powerful rule engine is introduced to replace plug-ins. It is recommended that you use it. See Save data to Timescale to setup Save data to Timescale in rule engine.

Configure Timescale Server

Config file: etc/plugins/emqx_backend_timescale.conf:

  1. ## Timescale Server
  2. backend.timescale.pool1.server = 127.0.0.1:5432
  3. ## Timescale Pool Size
  4. backend.timescale.pool1.pool_size = 8
  5. ## Timescale Username
  6. backend.timescale.pool1.username = postgres
  7. ## Timescale Password
  8. backend.timescale.pool1.password = password
  9. ## Timescale Database
  10. backend.timescale.pool1.database = tutorial
  11. ## Timescale SSL
  12. backend.timescale.pool1.ssl = false
  13. ## SSL keyfile.
  14. ##
  15. ## Value: File
  16. ## backend.timescale.pool1.keyfile =
  17. ## SSL certfile.
  18. ##
  19. ## Value: File
  20. ## backend.timescale.pool1.certfile =
  21. ## SSL cacertfile.
  22. ##
  23. ## Value: File
  24. ## backend.timescale.pool1.cacertfile =
  25. ## Store Publish Message
  26. backend.timescale.hook.message.publish.1 = {"topic": "#", "action": {"function": "on_message_publish"}, "pool": "pool1"}

Parameters in hook rule:

OptionDescription
topicConfigure which topics need to execute hooks
actionConfigure specific action for hook, function is a built-in function provided as Backend for general functions
poolPool Name, used to connect multiple Timescale servers

Example:

  1. ## Store PUBLISH message whose topic is "sensor/#"
  2. backend.influxdb.hook.message.publish.1 = {"topic": "sensor/#", "action": {"function": "on_message_publish"}, "pool": "pool1"}
  3. ## Store PUBLISH message whose topic is "stat/#"
  4. backend.influxdb.hook.message.publish.2 = {"topic": "stat/#", "action": {"function": "on_message_publish"}, "pool": "pool1"}

Description of Timescale Persistence Hooks

hooktopicactionDescription
message.publish#on_message_publishStore published messages

Timescale Backend provides the template file named emqx_backend_timescale.tmpl, which is used to extract data from MQTT messages with different topics for writing to Timescale.

Template file use Json format:

  • key - MQTT Topic, Json String, support wildcard characters
  • value - Template, Json Object, used to convert MQTT Message into measurement,tag_key=tag_value,... field_key=field_value,... timestamp and write to InfluxDB。

You can define different templates for different topics or multiple templates for the same topic, likes:

  1. {
  2. <Topic 1>: <Template 1>,
  3. <Topic 2>: <Template 2>
  4. }

The template format is as follows:

  1. {
  2. "name": <Name of template>,
  3. "sql": <SQL INSERT INTO>,
  4. "param_keys": <Param Keys>
  5. }

name, sql and param_keys are required options.

name can be any string, just make sure there are no duplicates.

sql is SQL INSERT INTO statement for Timescale, like insert into sensor_data(time, location, temperature, humidity) values (NOW(), $1, $2, $3).

param_keys is a array, its first element corresponds to $1 appearing in sql and so on.

Any element in an array can be a fixed value, and the data type it supports depends on the table you define. More realistically, of course, you can access the data in the MQTT message through the placeholder we provide.

Currently, we support placeholders as follows:

PlaceholderDescription
$idMQTT Message UUID, assigned by EMQX
$clientidClient ID used by the Client
$usernameUsername used by the Client
$peerhostIP of Client
$qosQoS of MQTT Message
$topicTopic of MQTT Message
$payloadPayload of MQTT Message, must be valid Json data
$<Number>It must be used with $paylaod to retrieve data from Json Array
$timestampThe timestamp EMQX sets when preparing to forward messages, precision: Nanoseconds

$payload and $<Number>:

You can directly use $content to obtain the complete message payload, you can use ["$payload", <Key>, ...] to get the data inside the message payload.

For example payload is {"data": {"temperature": 23.9}}, you can via ["$payload", "data", "temperature"] to get 23.9.

In the case of array data type in Json, we introduced $0 and $<pos_integer>, $0 means to get all elements in the array, and $<pos_integer> means to get the <pos_integer>th element in the array.

A simple example, ["$payload", "$0", "temp"] will get [20, 21] from [{"temp": 20}, {"temp": 21}], and ["$payload", "$1", "temp"] will only get 20.

It is worth noting that when you use $0, we expect the number of data you get is same. Because we need to convert these arrays into multiple records and write it into Timescale, and when you have three pieces of data in one field and two in another, we won’t know how to combine the data for you.

Example

data/templates directory provides a sample template (emqx_backend_timescale_example.tmpl, please remove the “_example” suffix from the filename when using it formally) for the user’s reference:

  1. {
  2. "sensor_data": {
  3. "name": "insert_sensor_data",
  4. "sql": "insert into sensor_data(time, location, temperature, humidity) values (NOW(), $1, $2, $3)",
  5. "param_keys": [
  6. ["$payload", "data", "$0", "location"],
  7. ["$payload", "data", "$0", "temperature"],
  8. ["$payload", "data", "$0", "humidity"]
  9. ]
  10. },
  11. "sensor_data2/#": {
  12. "name": "insert_sensor_data2",
  13. "sql": "insert into sensor_data(time, location, temperature, humidity) values (NOW(), $1, $2, $3)",
  14. "param_keys": [
  15. ["$payload", "location"],
  16. ["$payload", "temperature"],
  17. ["$payload", "humidity"]
  18. ]
  19. },
  20. "easy_data": {
  21. "name": "insert_easy_data",
  22. "sql": "insert into easy_data(time, data) values (NOW(), $1)",
  23. "param_keys": [
  24. "$payload"
  25. ]
  26. }
  27. }

When an MQTT Message whose Topic is “sensor_data” has the following Payload:

  1. {
  2. "data":[
  3. {
  4. "location":"bedroom",
  5. "temperature":21.3,
  6. "humidity":40.3
  7. },
  8. {
  9. "location":"bathroom",
  10. "temperature":22.3,
  11. "humidity":61.8
  12. },
  13. {
  14. "location":"kitchen",
  15. "temperature":29.5,
  16. "humidity":58.7
  17. }
  18. ]
  19. }

["$payload", "data", "$0", "location"] will extract Payload from MQTT Message first.

If the format of Payload is json, backend continue to extract data from Payload.

And the value of data is an array, we use $0 to gets all elements in the array.

["$payload", "data", "$0", "location"] will help us get ["bedroom", "bathroom", "kitchen"] finally.

Accordingly if you replace $0 with $1, you get only ["bedroom"].

So in this scene, we will get the following SQL statement:

  1. insert into sensor_data(time, location, temperature, humidity) values (NOW(), 'bedroom', 21.3, 40.3)
  2. insert into sensor_data(time, location, temperature, humidity) values (NOW(), 'bathroom', 22.3, 61.8)
  3. insert into sensor_data(time, location, temperature, humidity) values (NOW(), 'kitchen', 29.5, 58.7)

Eventually Timescale Backend executes these SQL statements to write data to Timescale.