How to block everything that is not needed?

I need to "function" node to send information only what i wrote in code and nothing else!
How to block everything else that "flies" through it after the node "mqtt"?

The code that I need to send!

var button_corridor = flow.get('button_corridor') || ""; //Что бы отслеживалось состояние кнопки.

if (button_corridor === "ON") { // Включаем возможность изменения яркости!
    msg.enabled = true;
    return msg;
} 
else if (msg.payload === "Off"){
    msg.enabled = false;
    msg.brightness = null;
    return msg;
}
else {
    msg.brightness = null;
    return null;
}

And this is the message that he passes through himself every time!

{"brightness":152,"color_temp":375,"linkquality":65,"state":"ON","update_available":true}

Use a switch node , and configure it to pass trought only the string/ value you want...

At beginning of code set msg = {}; That will set whole message to an empty object. This will affect msg.payload, so if you need it in your if statement, set var hold = msg; before you overwrite msg. Then use if(hold.payload = ... in your if statement.

2 Likes

You could just create a new msg object...
See - Easy way of adding only mytimes to a msg as an example.
(Similar to what @E1cid suggested)

1 Like

No, that isn't the message, that is what is in msg.payload, because that is what you have configured the debug node to show. If you set the debug node to Show Full Message then you will see that msg.enabled and msg.brightness (which is not the same as msg.payload.brightness) should have been set as your code dictates. Perhaps you meant
msg.payload = {enabled: true}
and
msg.payload = {enabled: false, brightness: null}
If you don't understand then have a look at the node-red docs page Working with Messages which should help.

1 Like

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