# Need to change 0's to 1 and 1's to 0

**URL:** <https://discourse.nodered.org/t/need-to-change-0s-to-1-and-1s-to-0/86082>\
**Category:** General\
**Created:** [2 March 2024 23:06 UTC](https://discourse.nodered.org/t/need-to-change-0s-to-1-and-1s-to-0/86082 "2024-03-02T23:06:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Goldog](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/goldog/32/97472_2.png) [@Goldog](https://discourse.nodered.org/u/Goldog)\
**Post date:** [2 March 2024 23:06 UTC](https://discourse.nodered.org/t/need-to-change-0s-to-1-and-1s-to-0/86082/1 "2024-03-02T23:06:35Z")

</div>

Hi, I'm a bit new to this and i'm having some issues in changing/replacing/inverting the messages from 0 to 1 / from 1 to 0. Its from some optocouplers that send a 0 when they are on and a 1 when they are off. I've tried using a change node and always seem to get 0's.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/1/81901c74e93a85ecbceb88f8c286b1b16f015c38.png)  
And the same zeros when using a function node.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/6/062e4f83c394a4f43cf95fe1b83035e930ab18dd.png)

---

<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:** [2 March 2024 23:20 UTC](https://discourse.nodered.org/t/need-to-change-0s-to-1-and-1s-to-0/86082/2 "2024-03-02T23:20:52Z")

</div>

Hello @Goldog welcome to the forum.

You need three operations in your change node:

- change 1 to "anything"
- change 0 to 1
- change "anything" to 0

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [2 March 2024 23:42 UTC](https://discourse.nodered.org/t/need-to-change-0s-to-1-and-1s-to-0/86082/3 "2024-03-02T23:42:37Z")

</div>

@Goldog That output is expected if you follow the logic you have given 🙂

1st - you change any 0 to 1, so a 0 payload is now 1 and a 1 payload is still 1  
2nd - you then change any 1 to 0, since any payload you sent is now 1 it will always change to zero.

You can do this in a function node

```auto
msg.payload = msg.payload === 1 ? 0 : 1;
return msg;

```

This checks if the payload is 1, if it is it returns 0, if not it returns 1

---

<div class="post-metadata">

**Author:** ![Goldog](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/goldog/32/97472_2.png) [@Goldog](https://discourse.nodered.org/u/Goldog)\
**Post date:** [2 March 2024 23:45 UTC](https://discourse.nodered.org/t/need-to-change-0s-to-1-and-1s-to-0/86082/4 "2024-03-02T23:45:51Z")

</div>

Hello @jbudd, Thanks so much i knew it had to be simple

---

<div class="post-metadata">

**Author:** ![Goldog](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/goldog/32/97472_2.png) [@Goldog](https://discourse.nodered.org/u/Goldog)\
**Post date:** [3 March 2024 00:05 UTC](https://discourse.nodered.org/t/need-to-change-0s-to-1-and-1s-to-0/86082/5 "2024-03-03T00:05:01Z")

</div>

@Sean-McG thanks for the logic explanation, makes perfect sense. i do get caught out by thinking in sequential terms and forgetting about the alternatives still flowing through.

---

<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 March 2024 08:31 UTC](https://discourse.nodered.org/t/need-to-change-0s-to-1-and-1s-to-0/86082/6 "2024-03-03T08:31:49Z")

</div>

For completeness, to explain the reason your function did not work, where you have  
`if (msg.payload = 1) {`  
the bit in the parenthesis, `=` is an assignment operator which says 'set message.payload to the value 1'. What you want is the comparison operator `===` which compares msg.payload to the value 1 and returns true or false. You could also use `==`. The difference is subtle and I will leave it for you to research the difference.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [3 March 2024 09:41 UTC](https://discourse.nodered.org/t/need-to-change-0s-to-1-and-1s-to-0/86082/7 "2024-03-03T09:41:23Z")

</div>

Just to point out that even if you had used the correct `===`, the function would still fail.  
So if payload equals 1

```auto
if(msg.payload === 1){
    msg.payload = 0 // payload now equal to 0
}
if(msg.payload === 0){
    msg.payload = 1 // payload was set to zero this now resets it to 1
}

```

You should use else or else if

```auto
if(msg.payload === 1){
    msg.payload = 0 // payload now equal to 0
}else if(msg.payload === 0){ // this section does not execute as first if is true.
    msg.payload = 1 
}

```

or

```auto
if(msg.payload === 1){
    msg.payload = 0 // payload now equal to 0
}else {
    msg.payload = 1 // this section only executes if payload does not equal 1
}

```

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [3 March 2024 13:05 UTC](https://discourse.nodered.org/t/need-to-change-0s-to-1-and-1s-to-0/86082/8 "2024-03-03T13:05:45Z")

</div>

If boolean as answer is no problem

```auto
msg.payload = !msg.payload

```

---

<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:** [1 June 2024 13:06 UTC](https://discourse.nodered.org/t/need-to-change-0s-to-1-and-1s-to-0/86082/9 "2024-06-01T13:06:14Z")

</div>

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