# Best practice on handling two states

**URL:** <https://discourse.nodered.org/t/best-practice-on-handling-two-states/33672>\
**Category:** General\
**Created:** [2 October 2020 12:25 UTC](https://discourse.nodered.org/t/best-practice-on-handling-two-states/33672 "2020-10-02T12:25:46Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![LarsAndersen](https://avatars.discourse-cdn.com/v4/letter/l/8491ac/32.png) [@LarsAndersen](https://discourse.nodered.org/u/LarsAndersen)\
**Post date:** [2 October 2020 12:25 UTC](https://discourse.nodered.org/t/best-practice-on-handling-two-states/33672/1 "2020-10-02T12:25:47Z")

</div>

I have two states (person 1 home/away, person 2 home/away). I would like to create a function or something similar that can take the two states in concideration.

Something like:

if (person 1 = home or person 2 = home ) {  
//do something  
} else {  
// do something else  
}

I was thinking the join function might do the work but I do not know how to create the logic if this is the right choice.

Any help is appriciated

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [2 October 2020 12:38 UTC](https://discourse.nodered.org/t/best-practice-on-handling-two-states/33672/2 "2020-10-02T12:38:47Z")

</div>

Have you considered the "switch" node ?

---

<div class="post-metadata">

**Author:** ![LarsAndersen](https://avatars.discourse-cdn.com/v4/letter/l/8491ac/32.png) [@LarsAndersen](https://discourse.nodered.org/u/LarsAndersen)\
**Post date:** [2 October 2020 12:39 UTC](https://discourse.nodered.org/t/best-practice-on-handling-two-states/33672/3 "2020-10-02T12:39:57Z")

</div>

The swich node can only take one input right?, I have two inputs

---

<div class="post-metadata">

**Author:** ![LarsAndersen](https://avatars.discourse-cdn.com/v4/letter/l/8491ac/32.png) [@LarsAndersen](https://discourse.nodered.org/u/LarsAndersen)\
**Post date:** [2 October 2020 12:48 UTC](https://discourse.nodered.org/t/best-practice-on-handling-two-states/33672/4 "2020-10-02T12:48:14Z")

</div>

Found a solution, using join node and then after using the switch node, creating 4 scenarios. Works fine, but maybe there is a smarter way?

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [2 October 2020 12:59 UTC](https://discourse.nodered.org/t/best-practice-on-handling-two-states/33672/5 "2020-10-02T12:59:01Z")

</div>

If the 2 states or not stored in the message (payload) then

1. I would store them in a flow context variable.
2. Each time one of the 2 states should be updated, you update the respective flow context variable (e.g. in a change node) and then you route that to the switch node (that is doings the switch based on the values of the 2 context variables).

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 October 2020 18:15 UTC](https://discourse.nodered.org/t/best-practice-on-handling-two-states/33672/6 "2020-10-02T18:15:57Z")

</div>

> [@LarsAndersen](#):
>
> creating 4 scenarios

Not sure what you mean by that, you should be able to do it with a Join and two Switches, though I would probably use a Join and a Function.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [2 October 2020 22:32 UTC](https://discourse.nodered.org/t/best-practice-on-handling-two-states/33672/7 "2020-10-02T22:32:18Z")

</div>

There's two people each with two states so that gives 4 scenarios

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 October 2020 08:04 UTC](https://discourse.nodered.org/t/best-practice-on-handling-two-states/33672/8 "2020-10-03T08:04:04Z")

</div>

> [@cymplecy](#):
>
> two people each with two states so that gives 4 scenarios

There are four input scenarios but only two out.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 October 2020 08:11 UTC](https://discourse.nodered.org/t/best-practice-on-handling-two-states/33672/9 "2020-10-03T08:11:00Z")

</div>

Actually this can be done in one node using the OR gate from node-red-contrib-bool-gate. That node will do the Join and the OR test for you.

---

<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:** [2 December 2020 08:11 UTC](https://discourse.nodered.org/t/best-practice-on-handling-two-states/33672/10 "2020-12-02T08:11:04Z")

</div>

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