# How to convert a msg. string object to boolean

**URL:** <https://discourse.nodered.org/t/how-to-convert-a-msg-string-object-to-boolean/20068>\
**Category:** General\
**Created:** [8 January 2020 17:11 UTC](https://discourse.nodered.org/t/how-to-convert-a-msg-string-object-to-boolean/20068 "2020-01-08T17:11:51Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Palmhoej](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@Palmhoej](https://discourse.nodered.org/u/Palmhoej)\
**Post date:** [8 January 2020 17:11 UTC](https://discourse.nodered.org/t/how-to-convert-a-msg-string-object-to-boolean/20068/1 "2020-01-08T17:11:51Z")

</div>

First of i am new to node red, but have some experience from arduino.

I am trying to controll my Phillips Hue lights with a Hue switch, that outputs the following when pressed (in this case the "on" button have been pushed):

button: 1002

name: "On"

action: "short released"

updated: "2020-01-08T12:28:17+00:00"

When i listen to msg.payload.name i get "on" and "off" respectively. How do i turn these two string values into a boolean true and false as output?

I know i should be able to do this with the function block, but i am suspecting that there should be an easier way to do this?

---

<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 January 2020 17:43 UTC](https://discourse.nodered.org/t/how-to-convert-a-msg-string-object-to-boolean/20068/2 "2020-01-08T17:43:04Z")

</div>

In a function you can do

```auto
msg.payload = (msg.payload.name === "on") ? true : false
return msg

```

or you can configure a Change node like this  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/9afa011a0174da87648d4883c435ce6f0bab6bdb.png)

which does basically the same thing but using a JSONata expression to test the value.

**[Edit]** Actually the statement in the function can just be  
`msg.payload = msg.payload.name === "on"`

---

<div class="post-metadata">

**Author:** ![Palmhoej](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@Palmhoej](https://discourse.nodered.org/u/Palmhoej)\
**Post date:** [8 January 2020 19:47 UTC](https://discourse.nodered.org/t/how-to-convert-a-msg-string-object-to-boolean/20068/3 "2020-01-08T19:47:54Z")

</div>

I could not get your suggestions to work, maybe i did something wrong?

However this function does what i need:

if(msg.payload.name === "On"){  
msg.payload = true;  
}  
if(msg.payload.name === "Off"){  
msg.payload = false;  
}  
return msg;

But if anyone has a more elegant way i would like to try that 🙂

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [8 January 2020 19:51 UTC](https://discourse.nodered.org/t/how-to-convert-a-msg-string-object-to-boolean/20068/4 "2020-01-08T19:51:08Z")

</div>

In his examples he had "on" rather than the "On" you show in yours... did you try his with "On" instead ?

---

<div class="post-metadata">

**Author:** ![Palmhoej](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@Palmhoej](https://discourse.nodered.org/u/Palmhoej)\
**Post date:** [8 January 2020 20:27 UTC](https://discourse.nodered.org/t/how-to-convert-a-msg-string-object-to-boolean/20068/5 "2020-01-08T20:27:47Z")

</div>

ahh nice spottet.... now it works... sort of. Problem here being that there are 4 buttons (On, Off, Dim, Bright). This means that Dim and Bright are also reported as Off, using the first suggestions.

---

<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 January 2020 20:54 UTC](https://discourse.nodered.org/t/how-to-convert-a-msg-string-object-to-boolean/20068/6 "2020-01-08T20:54:52Z")

</div>

Have a look at the javascript switch statement  
[https://www.w3schools.com/js/js\_switch.asp](https://www.w3schools.com/js/js_switch.asp)

---

<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 January 2020 20:58 UTC](https://discourse.nodered.org/t/how-to-convert-a-msg-string-object-to-boolean/20068/7 "2020-01-08T20:58:14Z")

</div>

> [@Palmhoej](#):
>
> I could not get your suggestions to work,

In my defence you did say 'When i listen to msg.payload.name i get "on" and "off" respectively'. I had not noticed that this was inconsistent with data a few lines earlier.

---

<div class="post-metadata">

**Author:** ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)\
**Post date:** [9 January 2020 05:32 UTC](https://discourse.nodered.org/t/how-to-convert-a-msg-string-object-to-boolean/20068/8 "2020-01-09T05:32:13Z")

</div>

Try to use `(msg.payload.name. toLowerCase()===“on”)` or

```auto
switch (msg.payload.name.toLowerCase()) {
 case “on”:
  break;
 case “off”:
  break;
}

```

to avoid this kind of problems

---

<div class="post-metadata">

**Author:** ![Palmhoej](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@Palmhoej](https://discourse.nodered.org/u/Palmhoej)\
**Post date:** [13 January 2020 22:20 UTC](https://discourse.nodered.org/t/how-to-convert-a-msg-string-object-to-boolean/20068/9 "2020-01-13T22:20:51Z")

</div>

Ahh yes that is a nice way to avoid that.

Thank you for all the replies 😃

---

<div class="post-metadata">

**Author:** ![Palmhoej](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@Palmhoej](https://discourse.nodered.org/u/Palmhoej)\
**Post date:** [13 January 2020 22:22 UTC](https://discourse.nodered.org/t/how-to-convert-a-msg-string-object-to-boolean/20068/10 "2020-01-13T22:22:59Z")

</div>

Hi Colin.

Thank you for your reply. I was in no way suggesting that you had anyway of knowing that there were 4 cases. You did answer correctly with the info given by me... i just recaped why i ended up not using your suggestion, because i forgot to mention the full case 🙂

---

<div class="post-metadata">

**Author:** ![robingreig](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/robingreig/32/81840_2.png) [@robingreig](https://discourse.nodered.org/u/robingreig)\
**Post date:** [7 August 2023 16:49 UTC](https://discourse.nodered.org/t/how-to-convert-a-msg-string-object-to-boolean/20068/11 "2023-08-07T16:49:43Z")

</div>

Just wanted to jump in and say thanks to everyone!

I've been using Node-RED with my raspberry pi for awhile using MQTT to control LED lights and a solar powered container watering system. Got stuck when trying Peter Scargills Big Timer which outputs a string to my node-RED Dashboard switch which needed a boolean.

I chose the Change node and need to take some time to learn to use the function node
