# Function node, only gets active if

**URL:** <https://discourse.nodered.org/t/function-node-only-gets-active-if/52645>\
**Category:** General\
**Created:** [24 October 2021 08:30 UTC](https://discourse.nodered.org/t/function-node-only-gets-active-if/52645 "2021-10-24T08:30:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![terminus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/terminus/32/38378_2.png) [@terminus](https://discourse.nodered.org/u/terminus)\
**Post date:** [24 October 2021 08:30 UTC](https://discourse.nodered.org/t/function-node-only-gets-active-if/52645/1 "2021-10-24T08:30:25Z")

</div>

Hi there, just updated the power-monitor node. Have to adapt my function node, which sends me notification if e.g. the dishwasher has finished.  
I have to filter the messages since there are now messages incoming even if the dishwasher is running. I have to filter for the stop message, because only there are the data, I'm interested in. Thought about something like this:

```auto
var payload = msg.payload.event;
if (payload == "stop") {
  msg.payload.time = Math.round(msg.payload.time / 60);
  msg.payload.cost = Math.round(msg.payload.energy * 29.70) / 100;
  return [msg, null];
} else {
  return [null, msg];
}

```

but it's not a valid JSON, can you help me?

Regards

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 October 2021 08:42 UTC](https://discourse.nodered.org/t/function-node-only-gets-active-if/52645/2 "2021-10-24T08:42:42Z")

</div>

What do you mean it is not valid JSON?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [24 October 2021 08:44 UTC](https://discourse.nodered.org/t/function-node-only-gets-active-if/52645/3 "2021-10-24T08:44:25Z")

</div>

What is not valid json? the input or output?  
Are you getting an error? if so, what is it?

Have yoo added a `debug` node to what ever node is feeding your `function` node and another to the output of your ``function` node?

---

<div class="post-metadata">

**Author:** ![terminus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/terminus/32/38378_2.png) [@terminus](https://discourse.nodered.org/u/terminus)\
**Post date:** [24 October 2021 09:00 UTC](https://discourse.nodered.org/t/function-node-only-gets-active-if/52645/4 "2021-10-24T09:00:19Z")

</div>

I'm a beginner if it comes to coding. And I use a json validator and get errors. If I put it in a function node, it's meant to have two outputs. One if it's a "stop" event and one output for every other value. But there is only one output and non message comes through.

---

<div class="post-metadata">

**Author:** ![terminus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/terminus/32/38378_2.png) [@terminus](https://discourse.nodered.org/u/terminus)\
**Post date:** [24 October 2021 09:13 UTC](https://discourse.nodered.org/t/function-node-only-gets-active-if/52645/5 "2021-10-24T09:13:53Z")

</div>

Okay it's java script 😆, but it won't work either 🙂

---

<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:** [24 October 2021 09:16 UTC](https://discourse.nodered.org/t/function-node-only-gets-active-if/52645/6 "2021-10-24T09:16:07Z")

</div>

> [@terminus](#):
>
> But there is only one output and non message comes through.

Set the function node to have 2 outputs.

> [@terminus](#):
>
> And I use a json validator and get errors

What are you checking in json validator?

* * *

Did you add debug nodes before and after function as suggested...

> [@zenofmud](#):
>
> Have yoo added a `debug` node to what ever node is feeding your `function` node and another to the output of your ``function` node?

What do you see?

* * *

> [@terminus](#):
>
> I'm a beginner if it comes to coding

I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

---

<div class="post-metadata">

**Author:** ![terminus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/terminus/32/38378_2.png) [@terminus](https://discourse.nodered.org/u/terminus)\
**Post date:** [24 October 2021 09:30 UTC](https://discourse.nodered.org/t/function-node-only-gets-active-if/52645/7 "2021-10-24T09:30:58Z")

</div>

Yes I did add the debug node but there is nothing on the output, as i mentioned before.

Good hint is to set the node to two outputs. Didn't know that this is possible. Thought it does that by itself, if the code needs two or more. Will debug again now.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [24 October 2021 09:49 UTC](https://discourse.nodered.org/t/function-node-only-gets-active-if/52645/8 "2021-10-24T09:49:24Z")

</div>

Don't forget to add a `debug` node to the node feeding the `function` node. That way you will see the actual msg.payload going into the function node.

Also, as a debuggging tip, you can add `node.warn();` statements into the function node to see what is going on. The output will display inteh debug sidebar.

So you could have

```auto
var payload = msg.payload.event;
node.warn('payload= '+payload)
if (payload == "stop") {
  node.warn('in TRUE part of IF')
  msg.payload.time = Math.round(msg.payload.time / 60);
  msg.payload.cost = Math.round(msg.payload.energy * 29.70) / 100;
  return [msg, null];
} else {
  node.warn('in ELSE part of IF')
  return [null, msg];
}

```

This can help you see how the function node is working.

Also remember to copy and paste the results you see to a reply. That way we can see it and help you out.

---

<div class="post-metadata">

**Author:** ![terminus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/terminus/32/38378_2.png) [@terminus](https://discourse.nodered.org/u/terminus)\
**Post date:** [24 October 2021 10:03 UTC](https://discourse.nodered.org/t/function-node-only-gets-active-if/52645/9 "2021-10-24T10:03:43Z")

</div>

Thank you very much but as I added the second output my code worked 🙂

---

<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:** [7 November 2021 10:04 UTC](https://discourse.nodered.org/t/function-node-only-gets-active-if/52645/10 "2021-11-07T10:04:05Z")

</div>

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