MQTT Out to Cayenne function node example please

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


Thanks

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?

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

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.

[{"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":[]}]

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

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.

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

[{"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

Something like this ?
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

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;


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

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

[{"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":""}]

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.

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