Timescale Backend
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.
Config file: etc/plugins/emqx_backend_timescale.conf:
Parameters in hook rule:
Example:
## Store PUBLISH message whose topic is "sensor/#"
backend.influxdb.hook.message.publish.1 = {"topic": "sensor/#", "action": {"function": "on_message_publish"}, "pool": "pool1"}
## Store PUBLISH message whose topic is "stat/#"
backend.influxdb.hook.message.publish.2 = {"topic": "stat/#", "action": {"function": "on_message_publish"}, "pool": "pool1"}
Description of Timescale Persistence Hooks
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 charactersvalue
- Template, Json Object, used to convert MQTT Message intomeasurement,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:
The template format is as follows:
{
"sql": <SQL INSERT INTO>,
"param_keys": <Param Keys>
}
name
, sql
and param_keys
are required options.
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 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:
$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
.
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:
When an MQTT Message whose Topic is “sensor_data” has the following Payload:
{
"data":[
{
"location":"bedroom",
"humidity":40.3
},
"location":"bathroom",
"temperature":22.3,
"humidity":61.8
},
{
"location":"kitchen",
"temperature":29.5,
"humidity":58.7
}
]
["$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 , you get only ["bedroom"]
.
So in this scene, we will get the following SQL statement: