Hi
I'm quite new with Node-RED, therefore, my question may seem a little stupid?:
Initial situation:
Node "A" sends a number payload every 1 minute
Node "B" is a dashboard button which sends a boolen payload "true" or "false" if pressed
Node "C" should eihter relay the number payload from Node "A" or replace it with a default number payload, depending on if Node "B" was pressed (= true) or not pressed (= false).
I wonder, if I have to use a function node with some own code, or if this is possible only with the other predefined standart Node-RED nodes? (Or do I have to install additional nodes anyway?)
Kind regards
Roman
Hi @RK_aus_S and welcome to the forum.
It is common in Node-red that you want to use message payloads that come from two different inputs.
But most nodes can only work with a single message at a time.
They don't "remember" a message even for a millisecond after it has passed further along the wire.
There are two ways to address this.
Either join the messages into a single one with both payloads, or save one message in a "context variable".
Examples of the two approaches:
Firstly using a context variable
. The flow might look like this.
Messages from the button and the regular input take completely separate paths, but the last button input is saved as a context variable
flow.button.
Secondly joining the messages.
Which one to use?
Option 1 is appealing to users of other programming languages because we are used to saving a value into a variable to use later on in the code.
Because the messages are handled completely differently, things can get out of sync, especially if the messages arrive very frequently.
Option 2 is more "Node-red like" because every message contains all of the data you need to process it.
It's easier to see what's going on by looking at the flow. There is no "magic" interaction between unconnected sections of the flow.
Two problems though: No message will be sent out of the Join node at all until there has been both a button press and a regular input.
When you press the button again, the Join node sends a message containing the most recent regular input again.
I have not posted the actual flows, that's an exercise for you ![:cowboy_hat_face: :cowboy_hat_face:](https://emoji.discourse-cdn.com/twitter/cowboy_hat_face.png?v=12)
Note that in a switch node you can't test if msg.payload equals true, there is an option in the dropdown "Is true" and another option "Otherwise"
1 Like
@jbudd
Great! I choosed the "clean" way with the context variable. ![:slight_smile: :slight_smile:](https://emoji.discourse-cdn.com/twitter/slight_smile.png?v=12)
Many thanks and kind regards
Roman
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.