Using Node-RED as a Local Fallback Server

Thanks for the documentation link. I can see how the JSON works but think I need a practical example of what it looks like when placed inside a Node Red function node. I just need an example of how the JSON string payload is formatted and fits INside a Node Red function block. Just a single channel nominal temp deg c message ready to send to an MQTT Out Node.

I can manually make fixed numeric things things happen by inserting the numbers in one place and the txt for the payload (units, type, channel) in several ways now. I am missing the complete JSON and msg handling code, format and correct syntax. I need to see how a Variable number ( the value in my case is PID processed sensor data) is bought into the function node and then inserted into the JSON other bits of string and then sent out ready for the the MQTT Out node.

This will really help ‘join the dots’ on what I think is one of the most powerful features of Cayenne… the Auto Add + Widget setup functionality. (Green ‘+’ ) etc.

Here is a kind of commented sand box screen grab of what I am trying to do / figure out.
I am after the code that would go inside the middle function node to make it happen


Hope this helps.
Thanks

Here is some information that might be of use if processing numeric data within Node Red in a Function Module and then building the widget prefix and adding numeric data suffix as per:

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;

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

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

Being able to use Node Red to pre process sensor data and mix up streams of data, ideas and then pass the result onto Cayenne using the purpose MQTT In/Out nodes is well worth a look.

1 Like

Use this to set the topic (make sure you remove what you have in the MQTT out node as this overrides msg.topic):

var num = msg.payload
var rtn_msg = msg;

//Math processing PID etc goes on here
rtn_msg.payload = "temp,c=" + num;
rtn_msg.topic = "/some/topic/here";

return rtn_msg;
[{"id":"cc7599164c0568c2","type":"function","z":"4eb945c39e00282a","name":"num = msg.payload; rtn_msg.payload = \"temp,c=\" + num","func":"var num = msg.payload\nvar rtn_msg = msg;\n\n//Math processing PID etc goes on here\nrtn_msg.payload = \"temp,c=\" + num;\nrtn_msg.topic = \"/some/topic/here\";\n\nreturn rtn_msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":870,"y":380,"wires":[["2fb2bd02a75fd615"]]},{"id":"f60bf859d3a443f1","type":"inject","z":"4eb945c39e00282a","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":370,"y":380,"wires":[["cc7599164c0568c2"]]},{"id":"2fb2bd02a75fd615","type":"mqtt out","z":"4eb945c39e00282a","name":"v1/123/things/ABC/data/15","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"","x":1340,"y":380,"wires":[]}]

You can see other things you can set with msg.x in the help for the node

Have a look at this for an example:

function getHumidity(){
    var humidityValue = 75;

    return humidityValue;
}

var rtn_msg={};
var humidity = getHumidity();
var battery = global.get("battery");

var payload = [
    {
        "channel": 1,
        "value": msg.payload,
        "type": "temp",
        "unit": "c"
    },
    {
        "channel": 2,
        "value": humidity,
        "type": "rel_hum",
        "unit": "p"
    },
    {
        "channel": 5,
        "value": battery,
        "type": "batt",
        "unit": "v"
    }
];

rtn_msg.topic = "v1/username/things/clientID/data/json";
rtn_msg.payload = JSON.stringify(payload);

return rtn_msg;
[{"id":"cc7599164c0568c2","type":"function","z":"383ae72e844b07b5","name":"num = msg.payload; rtn_msg.payload = \"temp,c=\" + num","func":"function getHumidity(){\n    var humidityValue = 75;\n\n    return humidityValue;\n}\n\nvar rtn_msg={};\nvar humidity = getHumidity();\nvar battery = global.get(\"battery\");\n\nvar payload = [\n    {\n        \"channel\": 1,\n        \"value\": msg.payload,\n        \"type\": \"temp\",\n        \"unit\": \"c\"\n    },\n    {\n        \"channel\": 2,\n        \"value\": humidity,\n        \"type\": \"rel_hum\",\n        \"unit\": \"p\"\n    },\n    {\n        \"channel\": 5,\n        \"value\": battery,\n        \"type\": \"batt\",\n        \"unit\": \"v\"\n    }\n];\n\nrtn_msg.topic = \"v1/username/things/clientID/data/json\";\nrtn_msg.payload = JSON.stringify(payload);\n\nreturn rtn_msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1050,"y":520,"wires":[["2fb2bd02a75fd615"]]},{"id":"f60bf859d3a443f1","type":"inject","z":"383ae72e844b07b5","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":550,"y":520,"wires":[["cc7599164c0568c2"]]},{"id":"2fb2bd02a75fd615","type":"mqtt out","z":"383ae72e844b07b5","name":"v1/123/things/ABC/data/15","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"","x":1520,"y":520,"wires":[]}]
1 Like

Thanks, this is a good simplification of the process. Nice and tidy having the topic + msg all in the function.

1 Like