Hi!
I’m quite new in Node-Red and js.
Please tell me how to read the values of my 2 variables to send it further to my PLC.
I’m not quite sure for the script to extract the boolean value of my_button and the nummeric value of my_slider.
I tried some syntax but still no success, and when it is wrong it kills my communication for some time.
I use Ubidots_in node as you see and try to extract Value ina function.
There’s a thing that I’d like to point out first to better explain how to work with the Ubidots_in node. This is the output format as described in this article and the Node’s Github repository (or the Docs within the nodes in Node-red).
– Last value checkbox enabled expected format: {"variable_label": {"value": 100}
– Last value checkbox disable expected format: {"variable_label": {"value": 100, "timestamp": 1583523586668, "context": { "key1": "value1", "key2": "value2"}, "created_at": 1583523586732}
With the above in mind and given the ability to subscribe to up to 10 variables within a single Ubidots_in node, you’ll notice that parsing the output is needed in order to detect which variable was updated from Ubidots. To that end, please enter the below script to your Function node. In a nutshell, it will parse the output JSON (above formats) and based on the variable label, it wil send the value through the respective outout of the function node. The order of the outputs is the same as in the const varLavbles list in the script:
const varLabels = [
"my_slider",
"my_button",
];
var payload = msg.payload;
var label = Object.keys(payload)[0];
var output = [];
varLabels.forEach(function (varLabel){
if (label == varLabel) {
msg.payload = payload[label].value;
output.push(msg);
} else {
output.push(null);
}
});
return output;