0

I want to write Telegraf config file which will:

  1. Uses openweathermap input or custom http request result
{
  "fields": {
      ...
      "humidity": 97,
      "temperature": -11.34,
      ...
  },
  "name": "weather",
  "tags": {...},
  "timestamp": 1675786146
}
  1. Splits result on two similar JSONs:
{
  "sensorID": "owm",
  "timestamp": 1675786146,
  "value": 97,
  "type": "humidity"
}

and

{
  "sensorID": "owm",
  "timestamp": 1675786146,
  "value": -11.34,
  "type": "temperature"
}
  1. Sends this JSONs into MQTT queue

Is it possible or I must create two different configs and make two api calls?

lmasikl
  • 193
  • 2
  • 16

1 Answers1

0

I found next configuration which solves my problem:

[[outputs.mqtt]]
  servers = ["${MQTT_URL}", ]
  topic_prefix = "owm/data"
  data_format = "json"
  json_transformation = '{"sensorID":"owm","type":"temperature","value":fields.main_temp,"timestamp":timestamp}'


[[outputs.mqtt]]
  servers = ["${MQTT_URL}", ]
  topic_prefix = "owm/data"
  data_format = "json"
  json_transformation = '{"sensorID":"owm","type":"humidity","value":fields.main_humidity,"timestamp":timestamp}'

[[inputs.http]]
  urls = [
    "https://api.openweathermap.org/data/2.5/weather?lat={$LAT}&lon={$LON}2&appid=${API_KEY}&units=metric"
  ]

  data_format = "json"

Here we:

  1. Retrieve data from OWM in input plugin.
  2. Transform received data structure in needed structure in two different output plugins. We use this language https://jsonata.org/ for this aim.
lmasikl
  • 193
  • 2
  • 16