First off yes I have spent a lot of time reading the documentation on JSON and Node Red working with messages as well as trying a number of different structures within the function node but I still am unable to resolve my issue.
This is the function...and it is working fine in terms of executing the subsequent nodes...however I am getting the dreaded "Function tried to send a message of type string" in the debug window. The msg.payload getting passed in is a number:
var email={};
if (msg.payload < 30)
{
email = "The battery temperature is: " + msg.payload;
return [email, msg.topic = "CPE Battery is too cold"];
}
else {
return [ null, null ];
}
You dont return strings, you return objects. Preferably the original msg object
var email={}; //<<< you created an object here
if (msg.payload < 30)
{
email = "The battery temperature is: " + msg.payload; //<<< then you killed in and turned it into a string
return [email, msg.topic = "CPE Battery is too cold"];
}
else {
return [ null, null ];
}
Better...
const email = { };
if (msg.payload < 30) {
email.payload = "The battery temperature is: " + msg.payload;
msg.topic = "CPE Battery is too cold"
return [email, msg];
} else {
return [ null, null ];
}
Better still...
if (msg.payload < 30) {
const email = { };
email.payload = "The battery temperature is: " + msg.payload;
email.topic = "CPE Battery is too cold"
return [email, msg]; //original, unmodified message comes out of 2nd output
}
I wish I were more adept at all of this, but with each challenge my dim lightbulb brightens a little. For my edification what does the const do that is different from var? And yes...of course your solution works perfectly!
If you had used const originally, you would have gotten an error when you run the code - indicating you did something wrong (i.e. created an object then tried to change it to a string)
Typically, try to use a const 1st, then if its not right, use a let (var has some weirdness I wont go into)
Lastly, you might benefit from enabling the monaco editor in your node-red, it has additional built in help, auto complete, code snippets & hints...
Also, why are you doing return [ null, null ]? That doesn't really make sense. Just don't have a return at all if you don't need the node to output a msg.
The Monaco editor is quite the install! However any assistance I can get in writing the functions will be great! Quick question for the theme though...My eyes die when looking at the sea of default white and with my previous editor I had a nice dark theme. However when trying to change the monaco editor theme following the comments in your code...I cannot find the directory you specify:
codeEditor: {
lib: "monaco", //can be "monaco" or "ace"
options: {
//// theme - must match the file name of a theme in
//// packages/node_modules/@node-red/editor-client/src/vendor/monaco/dist/theme
//// e.g. "tomorrow-night", "upstream-sunburst", "github", "my-theme", "vs"
theme: "tomorrow-night",
Is there another install for the themes that I need to execute?
That was there from me just trying to remember that I could have a different output if I needed one. When building my project I tend to copy and paste function nodes so I don't have to start from scratch. Thank you though!