# MQTT Out to Cayenne function node example please

**URL:** <https://discourse.nodered.org/t/mqtt-out-to-cayenne-function-node-example-please/51088>\
**Category:** General\
**Created:** [16 September 2021 05:06 UTC](https://discourse.nodered.org/t/mqtt-out-to-cayenne-function-node-example-please/51088 "2021-09-16T05:06:52Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Picaxe](https://avatars.discourse-cdn.com/v4/letter/p/d07c76/32.png) [@Picaxe](https://discourse.nodered.org/u/Picaxe)\
**Post date:** [16 September 2021 05:06 UTC](https://discourse.nodered.org/t/mqtt-out-to-cayenne-function-node-example-please/51088/1 "2021-09-16T05:06:52Z")

</div>

As a complete novice with JSON I would like an example of the code needed in the function node to create a message for a single channel that is sent to Cayenne MyDevices using an MQTT Out node.  
There are good JSON examples here:  
[https://developers.mydevices.com/cayenne/docs/cayenne-mqtt-api/#cayenne-mqtt-api-mqtt-messaging-topics](https://developers.mydevices.com/cayenne/docs/cayenne-mqtt-api/#cayenne-mqtt-api-mqtt-messaging-topics)  
I would some simple example of the JSON ideas for what is inside a function node. This will add live variable numeric data flow into the other information such as cannel number, type, unit, name string txt.  
Here is a rough idea of a kind of a sand box concept that I hope describes what I am after:

 ![JSON Bits of String](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/5/65c70e6c5822108102ce1967bc0d14e238d4ce12.jpeg)  
Thanks

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [16 September 2021 06:29 UTC](https://discourse.nodered.org/t/mqtt-out-to-cayenne-function-node-example-please/51088/2 "2021-09-16T06:29:20Z")

</div>

Welcome to the forum @Picaxe

To publish to MQTT you have to set msg.topic to the mqtt topic and msg.payload to the value to be published. Do you mean you don't know how to set the topic and payload or do you mean you don't know what set them to for that device?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [16 September 2021 14:49 UTC](https://discourse.nodered.org/t/mqtt-out-to-cayenne-function-node-example-please/51088/3 "2021-09-16T14:49:07Z")

</div>

The first example here is what you need  
[https://developers.mydevices.com/cayenne/docs/cayenne-mqtt-api/#cayenne-mqtt-api-mqtt-messaging-topics-examples](https://developers.mydevices.com/cayenne/docs/cayenne-mqtt-api/#cayenne-mqtt-api-mqtt-messaging-topics-examples)

```auto
msg.topic="v1/A1234B5678C/things/0123-4567-89AB-CDEF/data/2";
//v1/username/things/clientID/data/channel
msg.payload="temp,c=20.7";
// type,unit=value

```

Example of adding a value to the string for channel 2.

```auto
[{"id":"d286a64.5511858","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"20","payloadType":"num","x":820,"y":4260,"wires":[["12699ddb.160842"]]},{"id":"12699ddb.160842","type":"function","z":"b779de97.b1b46","name":"","func":"msg.topic=\"v1/A1234B5678C/things/0123-4567-89AB-CDEF/data/2\";\nmsg.payload=\"temp,c=\" + msg.payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":980,"y":4280,"wires":[["c996ccb5.bee9d"]]},{"id":"c996ccb5.bee9d","type":"debug","z":"b779de97.b1b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1140,"y":4320,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Picaxe](https://avatars.discourse-cdn.com/v4/letter/p/d07c76/32.png) [@Picaxe](https://discourse.nodered.org/u/Picaxe)\
**Post date:** [17 September 2021 07:41 UTC](https://discourse.nodered.org/t/mqtt-out-to-cayenne-function-node-example-please/51088/4 "2021-09-17T07:41:13Z")

</div>

Thanks the issue is my lack of JSON skills. 2 week novice !  
This is the bit I am after:  
msg.payload="temp,c=" + msg.payload;  
return msg;  
I think this is what I am after, how to blend the mix of live variable numeric data into the other information needed (type, units, channel) into one message ready for MQTT Out to forward to Cayenne.  
Cayenne Can handle just the number + manual setting up within Cayenne. But adding in the other data allows auto configuration 'pop up' all ready to go on a Cayenne MyDevices dashboard and all the mixed plot/gauge options as well.  
Hope to be able report back with the end product and some client photos ( live penguins )  
Thanks ~A

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [17 September 2021 08:29 UTC](https://discourse.nodered.org/t/mqtt-out-to-cayenne-function-node-example-please/51088/5 "2021-09-17T08:29:07Z")

</div>

> [@Picaxe](#):
>
> But adding in the other data allows auto configuration 'pop up' all ready to go

Can you post a link showing the format of mqtt data that Cayanne expects for this? I had a quick look but can't immediately see it. Rather than a string containing the value and units I would have expected it to be a JSON string containing the properties required.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [17 September 2021 08:37 UTC](https://discourse.nodered.org/t/mqtt-out-to-cayenne-function-node-example-please/51088/6 "2021-09-17T08:37:14Z")

</div>

Hi here is a another solution using template nodes, less code involved  
The incoming payload is used to replace the {{ }} elements

```auto
[{"id":"d73dc21f.27ed1","type":"inject","z":"190c8195.72b3be","name":"","props":[{"p":"payload.value","v":"20","vt":"num"},{"p":"payload.units","v":"c","vt":"str"},{"p":"payload.type","v":"temp","vt":"str"},{"p":"payload.channel","v":"2","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":310,"y":380,"wires":[["92d7423f.eba6f"]]},{"id":"92d7423f.eba6f","type":"template","z":"190c8195.72b3be","name":"set topic","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"v1/A1234B5678C/things/0123-4567-89AB-CDEF/data/{{payload.channel}}","output":"str","x":460,"y":400,"wires":[["ed920794.90dd8"]]},{"id":"ed920794.90dd8","type":"template","z":"190c8195.72b3be","name":"set payload","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.type}},{{payload.units}}={{payload.value}}","output":"str","x":650,"y":400,"wires":[["aed6a948.29dd68"]]},{"id":"aed6a948.29dd68","type":"debug","z":"190c8195.72b3be","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":590,"y":440,"wires":[]}]

```

This way you can copy and paste any JSON and use {{ }} to replace any element of that JSON.  
@Colin see links above they take you straight to the formats in my browser

---

<div class="post-metadata">

**Author:** ![Picaxe](https://avatars.discourse-cdn.com/v4/letter/p/d07c76/32.png) [@Picaxe](https://discourse.nodered.org/u/Picaxe)\
**Post date:** [17 September 2021 08:44 UTC](https://discourse.nodered.org/t/mqtt-out-to-cayenne-function-node-example-please/51088/7 "2021-09-17T08:44:27Z")

</div>

Something like this ?  
[https://developers.mydevices.com/cayenne/docs/cayenne-mqtt-api/#cayenne-mqtt-api-mqtt-messaging-topics](https://developers.mydevices.com/cayenne/docs/cayenne-mqtt-api/#cayenne-mqtt-api-mqtt-messaging-topics)

v1/ **username** /things/ **clientID** /data/json (for multi channel data option)  
For single channel (what I am after)  
Topic: v1/ **username** /things/ **clientID** /data/ **channel**  
msg string: type,unit=value

"The data type and/or unit can be added to prefix the value, allowing the backend to process and display data without the need of configuring the data channel from the dashboard:"  
This is esp what I want to happen as it is so nice and fast for setting things up. Completely formatted widgets just pop up with the live data and appear automatically on the dashboard.

As a complete Node Red + JSON Novice the issue has been getting the above parts into the correct places within a function node and an MQTT Out node using the correct syntax.

I think I have this sorted now thanks everyone

---

<div class="post-metadata">

**Author:** ![Picaxe](https://avatars.discourse-cdn.com/v4/letter/p/d07c76/32.png) [@Picaxe](https://discourse.nodered.org/u/Picaxe)\
**Post date:** [17 September 2021 08:51 UTC](https://discourse.nodered.org/t/mqtt-out-to-cayenne-function-node-example-please/51088/8 "2021-09-17T08:51:39Z")

</div>

Provisional function node solution:  
num = msg.payload  
var rtn\_msg = msg;  
//Math processing PID etc goes on here  
rtn\_msg.payload = "temp,c=" + num;  
return rtn\_msg;

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/e/7e992965a33b1fcbacffd2a40dc593a3519786f6.png)  
So far I have not been able to pull the channel number from the topic back into the function node so have left it in the 'MQTT Out' node. This is fine for now unless anyone can spot the syntax / format  
Thanks again ~ A

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [17 September 2021 09:00 UTC](https://discourse.nodered.org/t/mqtt-out-to-cayenne-function-node-example-please/51088/9 "2021-09-17T09:00:02Z")

</div>

You need to post data not images. You need to capture the incoming data using the debug nodes, there is an option to copy value when you hoover the mouse over the object names. You can press ctrl e after selecting nodes to export a flow. You can then copy the flow json and paste here using the \</\> button in the post editor

---

<div class="post-metadata">

**Author:** ![Picaxe](https://avatars.discourse-cdn.com/v4/letter/p/d07c76/32.png) [@Picaxe](https://discourse.nodered.org/u/Picaxe)\
**Post date:** [17 September 2021 09:05 UTC](https://discourse.nodered.org/t/mqtt-out-to-cayenne-function-node-example-please/51088/10 "2021-09-17T09:05:41Z")

</div>

```auto
[{"id":"efe6c5d9.c788d8","type":"function","z":"e258a7a0.4dbb78","name":"num = msg.payload; rtn_msg.payload = \"temp,c=\" + num","func":"num = msg.payload\nvar rtn_msg = msg;\n//Math processing PID etc goes on here\nrtn_msg.payload = \"temp,c=\" + num;\nreturn rtn_msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":730,"y":440,"wires":[["ff7bf729.ed2f68"]]},{"id":"e5569388.d612c","type":"inject","z":"e258a7a0.4dbb78","name":"Manually Inject value //numeric number","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"32.1","payloadType":"num","x":230,"y":440,"wires":[["efe6c5d9.c788d8"]]},{"id":"ff7bf729.ed2f68","type":"mqtt out","z":"e258a7a0.4dbb78","name":"v1/123/things/ABC/data/15","topic":"v1/123/things/ABC/data/15","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"6f156b34.a3fc54","x":1200,"y":440,"wires":[]},{"id":"6f156b34.a3fc54","type":"mqtt-broker","name":"picaxe@mqtt.mydevices","broker":"mqtt.mydevices.com","port":"1883","clientid":"60999de0-093c-11eb-b767-3f1a8f1211ba","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [17 September 2021 09:48 UTC](https://discourse.nodered.org/t/mqtt-out-to-cayenne-function-node-example-please/51088/11 "2021-09-17T09:48:19Z")

</div>

Need to see the data from the PID so can help more.(not an image) use the debug node set to show complete msg object. and use copy value function and paste here.

---

<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:** [16 November 2021 09:48 UTC](https://discourse.nodered.org/t/mqtt-out-to-cayenne-function-node-example-please/51088/12 "2021-11-16T09:48:56Z")

</div>

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