0
votes

Since I'm new and struggling with the Dashboard UI for Node Red, I'm asking the question.

How do I pass a Value of a Slider and a Switch into a Function to work with them. msg.payload doesn't work.

I'm sorry, I know that it is probably a pretty easy question. Just not used to programming with JS.

Thanks in advance

Thats my current code of the function just for testing: in the future i want to use the value of a slider to get passed by if the switch is true

if(msg.payload === true)
{
    return 20;
}
1
What do you mean by msg.payload doesn't work? All the UI nodes output values in msg.payload. Edit the question to include the code you have tried and explain what errors you are seeing. - hardillb

1 Answers

0
votes

The msg.payload from a slider node will be a numeric value so comparing it to true will always return false.

As for a swtich it will set the msg.payload to true or false so you don't need to compare this to anything, just use msg.payload as the whole condition e.g.

if (msg.payload) {
  ...
}

Also you can not return a raw value from a function node, it needs to be a msg object, with at least a payload field e.g.

if(msg.payload)
{
  return { payload: 20};
}