Function node, only gets active if

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:

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

What do you mean it is not valid JSON?

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?

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.

Okay it's java script :laughing:, but it won't work either :slight_smile:

Set the function node to have 2 outputs.

What are you checking in json validator?


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

What do you see?


I recommend watching this playlist: Node-RED Essentials. 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.

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.

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

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.

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

1 Like

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