# MQTT JS function output format

**URL:** https://discourse.nodered.org/t/mqtt-js-function-output-format/30183
**Category:** General
**Created:** [16 July 2020 08:38 UTC](https://discourse.nodered.org/t/mqtt-js-function-output-format/30183 "2020-07-16T08:38:47Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![pautorras](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@pautorras](https://discourse.nodered.org/u/pautorras)
#### Post date: [16 July 2020 08:38 UTC](https://discourse.nodered.org/t/mqtt-js-function-output-format/30183/1 "2020-07-16T08:38:47Z")

</div>

Hi,

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.

Thanks!

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [16 July 2020 08:51 UTC](https://discourse.nodered.org/t/mqtt-js-function-output-format/30183/2 "2020-07-16T08:51:04Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [16 July 2020 09:00 UTC](https://discourse.nodered.org/t/mqtt-js-function-output-format/30183/3 "2020-07-16T09:00:24Z")

</div>

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 ??

---

<div class="post-metadata">

### Author: ![pautorras](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@pautorras](https://discourse.nodered.org/u/pautorras)
#### Post date: [16 July 2020 14:35 UTC](https://discourse.nodered.org/t/mqtt-js-function-output-format/30183/4 "2020-07-16T14:35:49Z")

</div>

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

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [16 July 2020 16:18 UTC](https://discourse.nodered.org/t/mqtt-js-function-output-format/30183/5 "2020-07-16T16:18:12Z")

</div>

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

---

<div class="post-metadata">

### Author: ![molesworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/molesworth/32/11748_2.png) [@molesworth](https://discourse.nodered.org/u/molesworth)
#### Post date: [16 July 2020 17:08 UTC](https://discourse.nodered.org/t/mqtt-js-function-output-format/30183/6 "2020-07-16T17:08:48Z")

</div>

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.

![MQTT-Out](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/6/f69fb4cbb657d121f44acbb185938eab2018360a.png)

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

---

<div class="post-metadata">

### Author: ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)
#### Post date: [16 July 2020 18:20 UTC](https://discourse.nodered.org/t/mqtt-js-function-output-format/30183/7 "2020-07-16T18:20:18Z")

</div>

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

```auto
// 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("\n\n\r");
        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);
    }
}

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [14 September 2020 18:20 UTC](https://discourse.nodered.org/t/mqtt-js-function-output-format/30183/8 "2020-09-14T18:20:25Z")

</div>

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