# Toggle boolean state of context value

**URL:** https://discourse.nodered.org/t/toggle-boolean-state-of-context-value/82788
**Category:** General
**Created:** [11 November 2023 16:00 UTC](https://discourse.nodered.org/t/toggle-boolean-state-of-context-value/82788 "2023-11-11T16:00:19Z")
**Posts on this page:** 7
**Page:** 1

<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: [11 November 2023 16:00 UTC](https://discourse.nodered.org/t/toggle-boolean-state-of-context-value/82788/1 "2023-11-11T16:00:19Z")

</div>

Is there a way of toggling a context value  
i.e. if it's false - set it true and vice versa

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/1/f1b815e2c2e8dda47337dac6f98888fe1ddb45d4.png)  
(apart from a function node method)  
while typing I came up with this  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/8/a8e08cb481e780611c10807ce9782b2b550e6b85.png)

but is there a nicer way?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [11 November 2023 16:07 UTC](https://discourse.nodered.org/t/toggle-boolean-state-of-context-value/82788/2 "2023-11-11T16:07:09Z")

</div>

Perhaps JSONata...

Set `msg.payload.state`  
To the value `msg.payload.state != true`

When the value is false, the equation will return true. When the value is true, the equation will return false.

_untested_

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [11 November 2023 16:13 UTC](https://discourse.nodered.org/t/toggle-boolean-state-of-context-value/82788/3 "2023-11-11T16:13:35Z")

</div>

Sorry that's for a msg - you can get flow context but to be honest a function would be easier

Alternatively, you can have a multi-step change node. First would copy the flow value into a temporary msg property, then do the equation I said, then copy the result back in the flow property.

---

<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: [11 November 2023 16:57 UTC](https://discourse.nodered.org/t/toggle-boolean-state-of-context-value/82788/4 "2023-11-11T16:57:45Z")

</div>

I actually can't get my method to work either - I think I may be being hit by JS what it true (or false) issues!

Function node it'll have to be ☹

---

<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: [11 November 2023 17:29 UTC](https://discourse.nodered.org/t/toggle-boolean-state-of-context-value/82788/5 "2023-11-11T17:29:22Z")

</div>

> [@Steve-Mcl](#):
>
> Alternatively, you can have a multi-step change node. First would copy the flow value into a temporary msg property, then do the equation I said, then copy the result back in the flow property.

You would not need a multi step change node.  
e.g.

 ![chrome_screenshot_1699723404799](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/8/08c7df2ec206e40ca4add0d0a5df822baf0e853e.png)

@cymplecy Using the change option would never work as the order of the search would need to be reversed for true and false. Would be possible if you added a switch node so the replace for true/false could be separated.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [11 November 2023 20:56 UTC](https://discourse.nodered.org/t/toggle-boolean-state-of-context-value/82788/6 "2023-11-11T20:56:05Z")

</div>

> [@E1cid](#):
>
> You would not need a multi step change node

Yep...

> [@Steve-Mcl](#):
>
> you can get flow context but to be honest a function would be easier

But I was on mobile and not able to remember the exact jsonata fn call for getting context.

---

<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: [25 November 2023 20:56 UTC](https://discourse.nodered.org/t/toggle-boolean-state-of-context-value/82788/7 "2023-11-25T20:56:47Z")

</div>

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