MQTT JS function output format


I'm communicating NR with an ESP8266 with MQTT.
In dashboard, using a switch it works perfect. The switch output contains "on" or "off" and it works perfect.

I would like to use an MQTT output, with the output of a function. The function turns on or off a water valve depending on a time shedule.

¿Wich format needs the function output to "match" with the MQTT out?

PD- I have searched in the forum, but I hav'nt find it.


Hi @pautorras

the MQTT Out node will publish whatever value you pass it in msg.payload.

So if you want to publish the value "on" then that is what you should set msg.payload to.

What firmware are you using in the ESP8266 ??
e.g. ESP-Easy, Tasmota, ESP-Home, etc...

How is the water valve connected to the ESP8266 ??
By a relay on a GPIO pin ??

I'm programming ESP8266 with C++ using Arduino IDE

I'll have to leave that (to others on the forum) as C++ is not my field.

I'm doing something similar, using @knolleary's "PubSubClient" library, but I would expect other libs to work in a similar way. As an example of how to do it, here's my setup for an MQTT out node which sends a regular "tick" to any ESP8266 devices which need to keep an accurate time.

The message is generated by a simple Inject node sending a timestamp every 15 minutes, but as mentioned already, whatever you pass in as the message payload will be sent.


The code in the ESP8266 simply subscribes to "sys/tick" to receive the messages.

You can also simply send specific strings from the MQTT out node in Node-RED to your esp devices

In my esp32 I have the following C code that receives MQTT messages and "decode" them. Very simple but effective solution

// MQTT data callback
void mqtt_data_callback(mqtt_client *client, mqtt_event_data_t *event_data)
    char cmd_string[64];
    //printf("DATA=%.*s\r\n", event_data->data_length, event_data->data);
    sprintf(cmd_string, "%.*s\r\n", event_data->data_length, event_data->data);
    if (strstr(cmd_string, "init_task") != NULL) {
        printf("%s", cmd_string);
        vTaskDelete( xHandle );
        counter = 0;
        vTaskDelay(1000 / portTICK_RATE_MS);
        xTaskCreate(&read_temp, "read_temp_task", 10000, client, 5, &xHandle);

    if (strstr(cmd_string, "hello") != NULL) {
        mqtt_publish(client, "/hello", "hello to you too", 16, 0, 0);

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.