Using mqtt.to() in Tasks

I have a complicated flux query to calculate a value from many different tables in Influx DB, and I want to use the mqtt.to() function to send the result to MQTT.

The query is working fine in Grafana (without mqtt), and I wanted to set up an hourly task to run the query and mqtt.to the results.

But when I run the task I get the following error:
error exhausting result iterator: error @28:8-28:12: undefined identifier mqtt

The help show that I need to use import to run experimental packages, but the task query does not accept import (it gets a syntax highlight).

Is it not possible to use mqtt.to in Tasks?

I am using the following versions:

InfluxDB 2.1.1
Server: 657e183
Frontend: cc65325

Best Regards,
Csongor

Hi @nygma2004,
Can you run the following query to check your flux version:

import "runtime"
import "array"

array.from(rows: [{_value: "${runtime.version()}"}])

These packages should be available I am running against 2.2.

I get this: v0.139.0

You should be fine! What error are you getting from the import?

import "experimental/mqtt"

Sometimes you can get weird syntax highlighting but i would run against the task and see if it fails

This is the error I get in the Task editor:

Hi @nygma2004,
Can you try to put your import before the task identification? I believe that might be causing the invalid statment.

Yes, putting the import on the first line fixed the issue.

If I may use this thread for the next issue. When I execute the query from Data Explorer, it returns the generated data, but the MQTT message is never sent. I don’t see any errors, there is nothing on the MQTT broker side.

This is how my mqtt.to line from the query looks like:

    |> mqtt.to(
        broker: "tcp://192.168.1.80:8883",
        topic: "growatt/balance",
        clientid: "cpu-flux",
        name: "balance",
        username: "xxx",
        password: "xxx",
        valueColumns: ["balance"],
        tagColumns: ["type"]
    )

Due to the calculation in the flux query the data which is returned in not a table stream, just a single table. I am not sure if that is the issue why nothing is sent:

Sorry, it was my bad. The default (non-TLS) port for MQTT is 1883 and not 8883. I replaced the port and now it is working.

1 Like