# Help with trick for message parsing true/false -\> set/clear

**URL:** <https://discourse.nodered.org/t/help-with-trick-for-message-parsing-true-false-set-clear/97470>\
**Category:** General\
**Created:** [7 June 2025 23:00 UTC](https://discourse.nodered.org/t/help-with-trick-for-message-parsing-true-false-set-clear/97470 "2025-06-07T23:00:29Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [7 June 2025 23:00 UTC](https://discourse.nodered.org/t/help-with-trick-for-message-parsing-true-false-set-clear/97470/1 "2025-06-07T23:00:30Z")

</div>

I know this is _Baby coding_ for people in the know.

Alas I'm not one of those.

Problem/scenario:

`let uv = msg.payload.underVoltage`

That is a `true/false` message. ☹  
Downstream, it needs to be _human readable_.

So what's that trick:

```auto
uv = (msg.payload == false "clear") || "set"

```

Ok, update:  
something like this:

```auto
msg.payload = msg.payload === true ? "set" : "clear"

```

Where it does the check, and if it is correct, it sets to one value and if false sets it to the second.

I worded that carefully to not reuse `true` and `false`.  
Ok, I maybe should have used a better original scenario.

But it is what it is.

Someone - please?

---

<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:** [8 June 2025 01:06 UTC](https://discourse.nodered.org/t/help-with-trick-for-message-parsing-true-false-set-clear/97470/2 "2025-06-08T01:06:35Z")

</div>

> [@Trying\_to\_learn](#):
>
> `uv = (msg.payload == false "clear") || "set"`

If you are certain that the message will be a Boolean true or false then its as simple as this.

`uv = msg.payload ? "set" : "clear"`

If it could be undefined, null or other values then you would need additional checks, something like this should be quite bullet proof 😉

```auto
msg.payload = msg.payload != null && 
              (msg.payload === true || 
               (typeof msg.payload === 'string' && msg.payload.toLowerCase() === "true") || 
               msg.payload === 1) ? "set" : "clear";
return msg;

```

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [8 June 2025 06:40 UTC](https://discourse.nodered.org/t/help-with-trick-for-message-parsing-true-false-set-clear/97470/3 "2025-06-08T06:40:39Z")

</div>

I don't think `uv = (msg.payload == false "clear") || "set"` is valid in JavaScript

It looks like you're trying to use a conditional or ternary-like expression with this syntax..  
`uv = (msg.payload == false) ? "clear" : "set";`

The construct you're referring to is called the **ternary operator** (or **conditional operator** ). It's a shorthand way of writing an `if...else` statement in many programming languages like JavaScript, C, and Python (with a different syntax).

It is equivalent to...

```auto
if (msg.payload) {
    uv = "set";
} else {
    uv = "clear";
}

```

The syntax is...  
`condition ? value_if_true : value_if_false;`

You can also use the same 'trick' with other data-types. For example integer values.

So in a Function Node you could use...

```auto
msg.uv = (msg.payload >= 47) ? "clear" : "set";
return msg;

```

Or this if you want the result written back into msg.payload...

```auto
msg.payload = (msg.payload >= 47) ? "clear" : "set";
return msg;

```

---

<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:** [8 June 2025 07:51 UTC](https://discourse.nodered.org/t/help-with-trick-for-message-parsing-true-false-set-clear/97470/4 "2025-06-08T07:51:43Z")

</div>

This is one of the few places that I would use JSONata. In a Change node:  
`payload ? "set" : "clear"`

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 June 2025 08:02 UTC](https://discourse.nodered.org/t/help-with-trick-for-message-parsing-true-false-set-clear/97470/5 "2025-06-08T08:02:02Z")

</div>

Thanks folks.

I've documented the structure for future reference.

Now I'm left with _WHO_ to say is the solution. 😉

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 June 2025 08:05 UTC](https://discourse.nodered.org/t/help-with-trick-for-message-parsing-true-false-set-clear/97470/6 "2025-06-08T08:05:33Z")

</div>

Yeah, thanks

I got carried away with that.

Yes, the ( ) should be around the test.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [8 June 2025 08:17 UTC](https://discourse.nodered.org/t/help-with-trick-for-message-parsing-true-false-set-clear/97470/7 "2025-06-08T08:17:49Z")

</div>

At the end of the day you need to use whatever 'format' you will be comfortable with. These 'tricks' (as you call them) are all well and good, but you need to consider... "will I be able to remember them in 6-months time?" This is especially true for people who dip in and out of programming at irregular times. So it might be easier to use a good old "if then else" construct.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 June 2025 08:21 UTC](https://discourse.nodered.org/t/help-with-trick-for-message-parsing-true-false-set-clear/97470/8 "2025-06-08T08:21:11Z")

</div>

You are correct.

I am hoping that if I note it down somewhere and can remember where... 😉

I can build on it.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [8 June 2025 08:25 UTC](https://discourse.nodered.org/t/help-with-trick-for-message-parsing-true-false-set-clear/97470/9 "2025-06-08T08:25:36Z")

</div>

> [@Trying\_to\_learn](#):
>
> Yes, the ( ) should be around the test.

The parentheses around the condition (msg.payload \>= 47) are not strictly required, but they improve readability and help avoid confusion, especially when more complex expressions are involved.

So both of these should work the same:

```auto
msg.payload = msg.payload >= 47 ? "clear" : "set";
msg.payload = (msg.payload >= 47) ? "clear" : "set";

```

---

<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 June 2025 08:27 UTC](https://discourse.nodered.org/t/help-with-trick-for-message-parsing-true-false-set-clear/97470/10 "2025-06-08T08:27:32Z")

</div>

Apologies for muddying the water but these different code snippets are **only** equivalent if the only possible values of msg.payload are boolean true or false.

IMHO, when handling boolean datatypes it's best to use the `=== true` operator because that make it clear that you are expecting a boolean. The alternatives `==` and `if (msg.payload)` behave differently if fed a numeric or string value.

So for me this is the clearest and most compact code:

```auto
uv = (msg.payload === true) ? "set" : "clear"

```

ie if (msg.payload is boolean and it's value is true) ....

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 June 2025 08:27 UTC](https://discourse.nodered.org/t/help-with-trick-for-message-parsing-true-false-set-clear/97470/11 "2025-06-08T08:27:37Z")

</div>

I agree. I think it helps brake things up into _sections_.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [8 June 2025 08:29 UTC](https://discourse.nodered.org/t/help-with-trick-for-message-parsing-true-false-set-clear/97470/12 "2025-06-08T08:29:36Z")

</div>

I agree with you.  
@Sean-McG mentioned that fact at the start of the thread.

---

<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 June 2025 08:33 UTC](https://discourse.nodered.org/t/help-with-trick-for-message-parsing-true-false-set-clear/97470/13 "2025-06-08T08:33:21Z")

</div>

> [@dynamicdave](#):
>
> @smcgann99 mentioned that fact

Yes he did.

His example code is not only bulletproof but a bit impenetrable. 😜

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 June 2025 08:37 UTC](https://discourse.nodered.org/t/help-with-trick-for-message-parsing-true-false-set-clear/97470/14 "2025-06-08T08:37:03Z")

</div>

Yeah, and sorry as I said, I was torn to who's is THE solution.

No offence to anyone.

---

<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:** [8 June 2025 09:13 UTC](https://discourse.nodered.org/t/help-with-trick-for-message-parsing-true-false-set-clear/97470/15 "2025-06-08T09:13:17Z")

</div>

> [@jbudd](#):
>
> if (msg.payload is boolean and it's value is true)

Andrew's premise was that the payload is true or false. If that is the case then there is no need to check for exact equality with true. If the value is actually something else, "x" or 7 for example then just using `if (msg.payload)` would result in "set", whereas `if (msg.payload === true)` would result in "clear". Which one of those is what Andrew would actually want should determine which syntax to use. If he is confident that it will always be a boolean, or does not care, then the simpler and more efficient `if (msg.payload)` can be used. If a non-boolean input should result in an error or other output then it is necessary to test for `=== true`, and if it is not then test for `=== false` and otherwise take appropriate other action.

---

<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 June 2025 09:14 UTC](https://discourse.nodered.org/t/help-with-trick-for-message-parsing-true-false-set-clear/97470/16 "2025-06-22T09:14:04Z")

</div>

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