# Function "if" why is not work

**URL:** <https://discourse.nodered.org/t/function-if-why-is-not-work/73209>\
**Category:** General\
**Created:** [5 January 2023 09:51 UTC](https://discourse.nodered.org/t/function-if-why-is-not-work/73209 "2023-01-05T09:51:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![JosefNov](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josefnov/32/73801_2.png) [@JosefNov](https://discourse.nodered.org/u/JosefNov)\
**Post date:** [5 January 2023 09:51 UTC](https://discourse.nodered.org/t/function-if-why-is-not-work/73209/1 "2023-01-05T09:51:23Z")

</div>

I have this flow...

```auto
[{"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.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [5 January 2023 10:38 UTC](https://discourse.nodered.org/t/function-if-why-is-not-work/73209/2 "2023-01-05T10:38:48Z")

</div>

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 `````)

````auto
``` 
   code goes here 
```

````

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

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [5 January 2023 11:10 UTC](https://discourse.nodered.org/t/function-if-why-is-not-work/73209/3 "2023-01-05T11:10:50Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![JosefNov](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josefnov/32/73801_2.png) [@JosefNov](https://discourse.nodered.org/u/JosefNov)\
**Post date:** [5 January 2023 14:56 UTC](https://discourse.nodered.org/t/function-if-why-is-not-work/73209/4 "2023-01-05T14:56:54Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [5 January 2023 15:14 UTC](https://discourse.nodered.org/t/function-if-why-is-not-work/73209/5 "2023-01-05T15:14:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [5 January 2023 15:29 UTC](https://discourse.nodered.org/t/function-if-why-is-not-work/73209/6 "2023-01-05T15:29:36Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/8/78c3cf7763e5eae6c9a53a03a7d9768483bd2ba2.png)

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?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [5 January 2023 18:53 UTC](https://discourse.nodered.org/t/function-if-why-is-not-work/73209/7 "2023-01-05T18:53:01Z")

</div>

> [@JosefNov](#):
>
> 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".

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](https://nodered.org/docs/user-guide/context)".

---

<div class="post-metadata">

**Author:** ![JosefNov](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josefnov/32/73801_2.png) [@JosefNov](https://discourse.nodered.org/u/JosefNov)\
**Post date:** [5 January 2023 20:39 UTC](https://discourse.nodered.org/t/function-if-why-is-not-work/73209/8 "2023-01-05T20:39:36Z")

</div>

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

```auto
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;
};

```

```auto
[{"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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [19 January 2023 20:39 UTC](https://discourse.nodered.org/t/function-if-why-is-not-work/73209/9 "2023-01-19T20:39:38Z")

</div>

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