# Forward payload or replace it with own payload - depending on a condition?

**URL:** https://discourse.nodered.org/t/forward-payload-or-replace-it-with-own-payload-depending-on-a-condition/89345
**Category:** General
**Created:** [8 July 2024 07:13 UTC](https://discourse.nodered.org/t/forward-payload-or-replace-it-with-own-payload-depending-on-a-condition/89345 "2024-07-08T07:13:04Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![RK\_aus\_S](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rk_aus_s/32/92170_2.png) [@RK\_aus\_S](https://discourse.nodered.org/u/RK_aus_S)
#### Post date: [8 July 2024 07:13 UTC](https://discourse.nodered.org/t/forward-payload-or-replace-it-with-own-payload-depending-on-a-condition/89345/1 "2024-07-08T07:13:04Z")

</div>

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

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [8 July 2024 09:05 UTC](https://discourse.nodered.org/t/forward-payload-or-replace-it-with-own-payload-depending-on-a-condition/89345/2 "2024-07-08T09:05:21Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/0/6062417c10af688dd4f13a83453cdf6a91a45d4e.png)

Secondly joining the messages.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/0/90561960396c00786e7bc794a320db1f362d0f46.png)

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 🤠

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"

---

<div class="post-metadata">

### Author: ![RK\_aus\_S](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rk_aus_s/32/92170_2.png) [@RK\_aus\_S](https://discourse.nodered.org/u/RK_aus_S)
#### Post date: [8 July 2024 10:07 UTC](https://discourse.nodered.org/t/forward-payload-or-replace-it-with-own-payload-depending-on-a-condition/89345/3 "2024-07-08T10:07:34Z")

</div>

@jbudd

Great! I choosed the "clean" way with the context variable. 🙂

Many thanks and kind regards  
Roman

---

<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: [22 July 2024 10:07 UTC](https://discourse.nodered.org/t/forward-payload-or-replace-it-with-own-payload-depending-on-a-condition/89345/4 "2024-07-22T10:07:37Z")

</div>

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