Function "if" why is not work

I have this flow...

[{"id":"229f5611c9af1f6e","type":"tab","label":"FVE - CELL SATUS","disabled":false,"info":"","env":[]},{"id":"11f0130edf74e467","type":"inject","z":"229f5611c9af1f6e","name":"cell_min 2.9","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_min","payload":"2.9","payloadType":"jsonata","x":150,"y":360,"wires":[["14d6e162ec373377"]]},{"id":"b92accaf1dfcf58f","type":"inject","z":"229f5611c9af1f6e","name":"cell_min 3.0","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_min","payload":"3.0","payloadType":"jsonata","x":150,"y":400,"wires":[["14d6e162ec373377"]]},{"id":"863ff59a6ff0566f","type":"inject","z":"229f5611c9af1f6e","name":"cell_min 3.1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_min","payload":"3.1","payloadType":"jsonata","x":150,"y":440,"wires":[["14d6e162ec373377"]]},{"id":"1d0f2908b0cd6c99","type":"inject","z":"229f5611c9af1f6e","name":"cell_min 3.2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_min","payload":"3.2","payloadType":"jsonata","x":150,"y":480,"wires":[["14d6e162ec373377"]]},{"id":"0425d641f43661b7","type":"inject","z":"229f5611c9af1f6e","name":"cell_min 3.3","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_min","payload":"3.3","payloadType":"jsonata","x":150,"y":520,"wires":[["14d6e162ec373377"]]},{"id":"c6b0af4e2615be44","type":"inject","z":"229f5611c9af1f6e","name":"cell_min 3.45","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_min","payload":"3.45","payloadType":"jsonata","x":150,"y":560,"wires":[["14d6e162ec373377"]]},{"id":"5e1409f01ce0c8c6","type":"inject","z":"229f5611c9af1f6e","name":"cell_max 2.9","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_max","payload":"2.9","payloadType":"jsonata","x":150,"y":80,"wires":[["3bd2e60daacc5d57"]]},{"id":"c61fdb92442da94e","type":"inject","z":"229f5611c9af1f6e","name":"cell_max 3.0","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_max","payload":"3.0","payloadType":"jsonata","x":150,"y":120,"wires":[["3bd2e60daacc5d57"]]},{"id":"ee99d849ae07ce95","type":"inject","z":"229f5611c9af1f6e","name":"cell_max 3.1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_max","payload":"3.1","payloadType":"jsonata","x":150,"y":160,"wires":[["3bd2e60daacc5d57"]]},{"id":"92d111892f16f6f7","type":"inject","z":"229f5611c9af1f6e","name":"cell_max 3.2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_max","payload":"3.2","payloadType":"jsonata","x":150,"y":200,"wires":[["3bd2e60daacc5d57"]]},{"id":"01d819e4d42e154d","type":"inject","z":"229f5611c9af1f6e","name":"cell_max 3.3","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_max","payload":"3.3","payloadType":"jsonata","x":150,"y":240,"wires":[["3bd2e60daacc5d57"]]},{"id":"7f70490197626bd9","type":"inject","z":"229f5611c9af1f6e","name":"cell_max 3.45","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_max","payload":"3.45","payloadType":"jsonata","x":150,"y":280,"wires":[["3bd2e60daacc5d57"]]},{"id":"3bd2e60daacc5d57","type":"range","z":"229f5611c9af1f6e","minin":"2.900","maxin":"3.450","minout":"0","maxout":"100","action":"scale","round":false,"property":"payload","name":"2.9-3.45V to 0-100%","x":440,"y":280,"wires":[["09c1dc6daf0ae65a"]]},{"id":"14d6e162ec373377","type":"range","z":"229f5611c9af1f6e","minin":"2.900","maxin":"3.450","minout":"0","maxout":"100","action":"scale","round":false,"property":"payload","name":"2.9-3.45V to 0-100%","x":440,"y":560,"wires":[["d65e3c691b372797"]]},{"id":"09c1dc6daf0ae65a","type":"function","z":"229f5611c9af1f6e","name":"to 0.00","func":"//msg.payload = msg.payload[86].value\nmsg.payload = parseFloat(msg.payload.toFixed(2))\nreturn msg;\n\n//const result = msg.payload.find(e => e.itemID == 'payload')\n//if (result) { msg.payload = result.value }\n//msg.payload = parseFloat(msg.payload.toFixed(2))\n//return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":340,"wires":[["8f99cbd4688ccde4","6b139a7965a247e0"]]},{"id":"d65e3c691b372797","type":"function","z":"229f5611c9af1f6e","name":"to 0.00","func":"//msg.payload = msg.payload[86].value\nmsg.payload = parseFloat(msg.payload.toFixed(2))\nreturn msg;\n\n//const result = msg.payload.find(e => e.itemID == 'payload')\n//if (result) { msg.payload = result.value }\n//msg.payload = parseFloat(msg.payload.toFixed(2))\n//return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":500,"wires":[["8f99cbd4688ccde4","6b139a7965a247e0"]]},{"id":"8f99cbd4688ccde4","type":"debug","z":"229f5611c9af1f6e","name":"debug_percentage","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":450,"y":420,"wires":[]},{"id":"6b139a7965a247e0","type":"function","z":"229f5611c9af1f6e","name":"function","func":"var outputMsg = 0;\nvar batt_cell_max = 0;\nvar batt_cell_min = 0;\n\nif (msg.topic == \"cell_max\") {\n batt_cell_max = { payload: msg.payload, topic: \"my_topic_cell_max\" };\n }\n else if (msg.topic == \"cell_min\")\n {\n batt_cell_min = { payload: msg.payload, topic: \"my_topic_cell_min\" };\n }\n else if (batt_cell_max > 50)\n {\n outputMsg = batt_cell_max;\n return outputMsg;\n }\n else if (batt_cell_max <= 50)\n {\n outputMsg = batt_cell_min;\n return outputMsg;\n }\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":420,"wires":[["367a2b647ceb7d46"]]},{"id":"367a2b647ceb7d46","type":"debug","z":"229f5611c9af1f6e","name":"debug_func","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":850,"y":420,"wires":[]}]

The required function...

  • when i input "cell_max 3.2" (over 50% charge) and "cell_min 3.1" so msg.payload "cell_max".
  • when i input "cell_max 3.1" (under 50% charge) and "cell_min 3.0" so msg.payload "cell_min".
    ... i dont see any payload in "debug_func" now.

Thank you very much in advance for your help.

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

Since messages arriving in your function "function" always have msg.topic set to either "cell_min" or "cell_max", the code in your second and third else if blocks is never executed.

var outputMsg = 0;
var batt_cell_max = 0;
var batt_cell_min = 0;

if (msg.topic == "cell_max") {   // "EITHER cell_max
   batt_cell_max = { payload: msg.payload, topic: "my_topic_cell_max" };
}
else if (msg.topic == "cell_min")  // OR cell_min
{
   batt_cell_min = { payload: msg.payload, topic: "my_topic_cell_min" };
}
else if (batt_cell_max > 50)   // NEVER
{
   outputMsg = batt_cell_max;
   return outputMsg;
}
else if (batt_cell_max <= 50)  //NEVER
{
   outputMsg = batt_cell_min;
   return outputMsg;
}

ps Maybe you can explain what this function node is supposed to do? I'm afraid it makes no sense to me.

i updated but still not working.

The required function.

  • when i input "cell_max 3.2" (over 50% charge) and "cell_min 3.1" so output msg.payload "cell_max".
  • when i input "cell_max 3.1" (under 50% charge) and "cell_min 3.0" so output msg.payload "cell_min".
    ... i see only "cell_min" in "debug_func" now.

Thank you very much in advance for your help.

[{"id":"229f5611c9af1f6e","type":"tab","label":"FVE - CELL SATUS","disabled":false,"info":"","env":[]},{"id":"11f0130edf74e467","type":"inject","z":"229f5611c9af1f6e","name":"cell_min 2.9","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_min","payload":"2.9","payloadType":"jsonata","x":150,"y":360,"wires":[["14d6e162ec373377"]]},{"id":"b92accaf1dfcf58f","type":"inject","z":"229f5611c9af1f6e","name":"cell_min 3.0","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_min","payload":"3.0","payloadType":"jsonata","x":150,"y":400,"wires":[["14d6e162ec373377"]]},{"id":"863ff59a6ff0566f","type":"inject","z":"229f5611c9af1f6e","name":"cell_min 3.1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_min","payload":"3.1","payloadType":"jsonata","x":150,"y":440,"wires":[["14d6e162ec373377"]]},{"id":"1d0f2908b0cd6c99","type":"inject","z":"229f5611c9af1f6e","name":"cell_min 3.2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_min","payload":"3.2","payloadType":"jsonata","x":150,"y":480,"wires":[["14d6e162ec373377"]]},{"id":"0425d641f43661b7","type":"inject","z":"229f5611c9af1f6e","name":"cell_min 3.3","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_min","payload":"3.3","payloadType":"jsonata","x":150,"y":520,"wires":[["14d6e162ec373377"]]},{"id":"c6b0af4e2615be44","type":"inject","z":"229f5611c9af1f6e","name":"cell_min 3.45","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_min","payload":"3.45","payloadType":"jsonata","x":150,"y":560,"wires":[["14d6e162ec373377"]]},{"id":"5e1409f01ce0c8c6","type":"inject","z":"229f5611c9af1f6e","name":"cell_max 2.9","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_max","payload":"2.9","payloadType":"jsonata","x":150,"y":80,"wires":[["3bd2e60daacc5d57"]]},{"id":"c61fdb92442da94e","type":"inject","z":"229f5611c9af1f6e","name":"cell_max 3.0","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_max","payload":"3.0","payloadType":"jsonata","x":150,"y":120,"wires":[["3bd2e60daacc5d57"]]},{"id":"ee99d849ae07ce95","type":"inject","z":"229f5611c9af1f6e","name":"cell_max 3.1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_max","payload":"3.1","payloadType":"jsonata","x":150,"y":160,"wires":[["3bd2e60daacc5d57"]]},{"id":"92d111892f16f6f7","type":"inject","z":"229f5611c9af1f6e","name":"cell_max 3.2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_max","payload":"3.2","payloadType":"jsonata","x":150,"y":200,"wires":[["3bd2e60daacc5d57"]]},{"id":"01d819e4d42e154d","type":"inject","z":"229f5611c9af1f6e","name":"cell_max 3.3","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_max","payload":"3.3","payloadType":"jsonata","x":150,"y":240,"wires":[["3bd2e60daacc5d57"]]},{"id":"7f70490197626bd9","type":"inject","z":"229f5611c9af1f6e","name":"cell_max 3.45","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_max","payload":"3.45","payloadType":"jsonata","x":150,"y":280,"wires":[["3bd2e60daacc5d57"]]},{"id":"3bd2e60daacc5d57","type":"range","z":"229f5611c9af1f6e","minin":"2.900","maxin":"3.450","minout":"0","maxout":"100","action":"scale","round":false,"property":"payload","name":"2.9-3.45V to 0-100%","x":440,"y":280,"wires":[["09c1dc6daf0ae65a"]]},{"id":"14d6e162ec373377","type":"range","z":"229f5611c9af1f6e","minin":"2.900","maxin":"3.450","minout":"0","maxout":"100","action":"scale","round":false,"property":"payload","name":"2.9-3.45V to 0-100%","x":440,"y":560,"wires":[["d65e3c691b372797"]]},{"id":"09c1dc6daf0ae65a","type":"function","z":"229f5611c9af1f6e","name":"to 0.00","func":"//msg.payload = msg.payload[86].value\nmsg.payload = parseFloat(msg.payload.toFixed(2))\nreturn msg;\n\n//const result = msg.payload.find(e => e.itemID == 'payload')\n//if (result) { msg.payload = result.value }\n//msg.payload = parseFloat(msg.payload.toFixed(2))\n//return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":340,"wires":[["8f99cbd4688ccde4","6b139a7965a247e0"]]},{"id":"d65e3c691b372797","type":"function","z":"229f5611c9af1f6e","name":"to 0.00","func":"//msg.payload = msg.payload[86].value\nmsg.payload = parseFloat(msg.payload.toFixed(2))\nreturn msg;\n\n//const result = msg.payload.find(e => e.itemID == 'payload')\n//if (result) { msg.payload = result.value }\n//msg.payload = parseFloat(msg.payload.toFixed(2))\n//return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":500,"wires":[["8f99cbd4688ccde4","6b139a7965a247e0"]]},{"id":"8f99cbd4688ccde4","type":"debug","z":"229f5611c9af1f6e","name":"debug_percentage","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":450,"y":420,"wires":[]},{"id":"6b139a7965a247e0","type":"function","z":"229f5611c9af1f6e","name":"function","func":"var outputMsg = 0;\nvar batt_cell_max = 0;\nvar batt_cell_min = 0;\n\nif (msg.topic == \"cell_max\") {\n batt_cell_max = { payload: msg.payload, topic: \"my_topic_cell_max\" };\n }\n else if (msg.topic == \"cell_min\")\n {\n batt_cell_min = { payload: msg.payload, topic: \"my_topic_cell_min\" };\n }\n \nif (batt_cell_max > 50)\n {\n outputMsg = batt_cell_max;\n return outputMsg;\n }\n else if (batt_cell_max <= 50)\n {\n outputMsg = batt_cell_min;\n return outputMsg;\n }\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":420,"wires":[["367a2b647ceb7d46"]]},{"id":"367a2b647ceb7d46","type":"debug","z":"229f5611c9af1f6e","name":"debug_func","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":850,"y":420,"wires":[]}]

I am only able to look at your code on my phone just now so I can't be sure but I think you are setting a variable eg batt_cellmin to be an object, then testing batt_cellmin >50.

You can't get a coherent response to such a comparison.

I advise you to use simple numeric or string variables. Only create an object with payload and topic immediately before the return statement.

I'll take another look when I get back home

image

OTHER NOTES:

  • you never use batt_cell_min (and never return it)
  • It is bad practice (node-red speaking) to generate new objects - it is always preferred to simply update and return the original msg
  • try to use const and let instead of var (I will let you search the new for "why")

What are you actually trying to achieve?

A common trap for beginners is expecting a function node to know about a previous message while dealing with a new one.
I'm not sure if that's what you are trying to do here.
You have to save the previous message using "context storage".

Hi all,
Thank you all so much for pointing me in the right direction.
Now everything is working perfectly.
Exactly as I wanted :slight_smile:

let outputMsg;
let batt_cell_min;
let batt_cell_max;
let batt_limit = 50.00;
let mem_batt_cell_min;
let mem_batt_cell_max;

if (msg.topic == "cell_min") {
   batt_cell_min = msg.payload;
   context.set("context_batt_cell_min", batt_cell_min); // to store a variable
};

if (msg.topic == "cell_max") {
   batt_cell_max = msg.payload;
   context.set("context_batt_cell_max", batt_cell_max); // to store a variable
};

mem_batt_cell_min = context.get("context_batt_cell_min"); //to retrieve a variable
mem_batt_cell_max = context.get("context_batt_cell_max"); //to retrieve a variable

if (Number(mem_batt_cell_max) < batt_limit) {
   return outputMsg = { payload: Number(mem_batt_cell_min), topic: "my_new_topic_batt_cell_min" };
}
else if (Number(mem_batt_cell_max) >= batt_limit) {
   return outputMsg = { payload: Number(mem_batt_cell_max), topic: "my_new_topic_batt_cell_max" };
}
else {
   return outputMsg = null;
};
[{"id":"229f5611c9af1f6e","type":"tab","label":"FVE - CELL SATUS","disabled":false,"info":"","env":[]},{"id":"11f0130edf74e467","type":"inject","z":"229f5611c9af1f6e","name":"cell_min 2.9","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_min","payload":"2.9","payloadType":"jsonata","x":150,"y":360,"wires":[["14d6e162ec373377"]]},{"id":"b92accaf1dfcf58f","type":"inject","z":"229f5611c9af1f6e","name":"cell_min 3.0","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_min","payload":"3.0","payloadType":"jsonata","x":150,"y":400,"wires":[["14d6e162ec373377"]]},{"id":"863ff59a6ff0566f","type":"inject","z":"229f5611c9af1f6e","name":"cell_min 3.1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_min","payload":"3.1","payloadType":"jsonata","x":150,"y":440,"wires":[["14d6e162ec373377"]]},{"id":"1d0f2908b0cd6c99","type":"inject","z":"229f5611c9af1f6e","name":"cell_min 3.2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_min","payload":"3.2","payloadType":"jsonata","x":150,"y":480,"wires":[["14d6e162ec373377"]]},{"id":"0425d641f43661b7","type":"inject","z":"229f5611c9af1f6e","name":"cell_min 3.3","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_min","payload":"3.3","payloadType":"jsonata","x":150,"y":520,"wires":[["14d6e162ec373377"]]},{"id":"c6b0af4e2615be44","type":"inject","z":"229f5611c9af1f6e","name":"cell_min 3.45","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_min","payload":"3.45","payloadType":"jsonata","x":150,"y":580,"wires":[["14d6e162ec373377"]]},{"id":"5e1409f01ce0c8c6","type":"inject","z":"229f5611c9af1f6e","name":"cell_max 2.9","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_max","payload":"2.9","payloadType":"jsonata","x":150,"y":80,"wires":[["3bd2e60daacc5d57"]]},{"id":"c61fdb92442da94e","type":"inject","z":"229f5611c9af1f6e","name":"cell_max 3.0","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_max","payload":"3.0","payloadType":"jsonata","x":150,"y":120,"wires":[["3bd2e60daacc5d57"]]},{"id":"ee99d849ae07ce95","type":"inject","z":"229f5611c9af1f6e","name":"cell_max 3.1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_max","payload":"3.1","payloadType":"jsonata","x":150,"y":160,"wires":[["3bd2e60daacc5d57"]]},{"id":"92d111892f16f6f7","type":"inject","z":"229f5611c9af1f6e","name":"cell_max 3.2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_max","payload":"3.2","payloadType":"jsonata","x":150,"y":200,"wires":[["3bd2e60daacc5d57"]]},{"id":"01d819e4d42e154d","type":"inject","z":"229f5611c9af1f6e","name":"cell_max 3.3","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_max","payload":"3.3","payloadType":"jsonata","x":150,"y":240,"wires":[["3bd2e60daacc5d57"]]},{"id":"7f70490197626bd9","type":"inject","z":"229f5611c9af1f6e","name":"cell_max 3.45","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cell_max","payload":"3.45","payloadType":"jsonata","x":150,"y":280,"wires":[["3bd2e60daacc5d57"]]},{"id":"3bd2e60daacc5d57","type":"range","z":"229f5611c9af1f6e","minin":"2.900","maxin":"3.450","minout":"0","maxout":"100","action":"scale","round":false,"property":"payload","name":"2.9-3.45V to 0-100%","x":440,"y":280,"wires":[["09c1dc6daf0ae65a"]]},{"id":"14d6e162ec373377","type":"range","z":"229f5611c9af1f6e","minin":"2.900","maxin":"3.450","minout":"0","maxout":"100","action":"scale","round":false,"property":"payload","name":"2.9-3.45V to 0-100%","x":440,"y":560,"wires":[["d65e3c691b372797"]]},{"id":"09c1dc6daf0ae65a","type":"function","z":"229f5611c9af1f6e","name":"to 0.00","func":"//msg.payload = msg.payload[86].value\nmsg.payload = parseFloat(msg.payload.toFixed(2))\nreturn msg;\n\n//const result = msg.payload.find(e => e.itemID == 'payload')\n//if (result) { msg.payload = result.value }\n//msg.payload = parseFloat(msg.payload.toFixed(2))\n//return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":340,"wires":[["8f99cbd4688ccde4","6b139a7965a247e0"]]},{"id":"d65e3c691b372797","type":"function","z":"229f5611c9af1f6e","name":"to 0.00","func":"//msg.payload = msg.payload[86].value\nmsg.payload = parseFloat(msg.payload.toFixed(2))\nreturn msg;\n\n//const result = msg.payload.find(e => e.itemID == 'payload')\n//if (result) { msg.payload = result.value }\n//msg.payload = parseFloat(msg.payload.toFixed(2))\n//return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":500,"wires":[["8f99cbd4688ccde4","6b139a7965a247e0"]]},{"id":"8f99cbd4688ccde4","type":"debug","z":"229f5611c9af1f6e","name":"debug_percentage","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":450,"y":420,"wires":[]},{"id":"6b139a7965a247e0","type":"function","z":"229f5611c9af1f6e","name":"function","func":"let outputMsg;\nlet batt_cell_min;\nlet batt_cell_max;\nlet batt_limit = 50.00;\nlet mem_batt_cell_min;\nlet mem_batt_cell_max;\n\nif (msg.topic == \"cell_min\") {\n   batt_cell_min = msg.payload;\n   context.set(\"context_batt_cell_min\", batt_cell_min); // to store a variable\n};\n\nif (msg.topic == \"cell_max\") {\n   batt_cell_max = msg.payload;\n   context.set(\"context_batt_cell_max\", batt_cell_max); // to store a variable\n};\n\nmem_batt_cell_min = context.get(\"context_batt_cell_min\"); //to retrieve a variable\nmem_batt_cell_max = context.get(\"context_batt_cell_max\"); //to retrieve a variable\n\nif (Number(mem_batt_cell_max) < batt_limit) {\n   return outputMsg = { payload: Number(mem_batt_cell_min), topic: \"my_new_topic_batt_cell_min\" };\n}\nelse if (Number(mem_batt_cell_max) >= batt_limit) {\n   return outputMsg = { payload: Number(mem_batt_cell_max), topic: \"my_new_topic_batt_cell_max\" };\n}\nelse {\n   return outputMsg = null;\n};\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":420,"wires":[["367a2b647ceb7d46"]]},{"id":"367a2b647ceb7d46","type":"debug","z":"229f5611c9af1f6e","name":"debug_func","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":850,"y":420,"wires":[]}]

Thanks again.

1 Like

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