# I need a sanity check

**URL:** <https://discourse.nodered.org/t/i-need-a-sanity-check/29113>\
**Category:** General\
**Created:** [28 June 2020 14:12 UTC](https://discourse.nodered.org/t/i-need-a-sanity-check/29113 "2020-06-28T14:12:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![injector22](https://avatars.discourse-cdn.com/v4/letter/i/e9bcb4/32.png) [@injector22](https://discourse.nodered.org/u/injector22)\
**Post date:** [28 June 2020 14:12 UTC](https://discourse.nodered.org/t/i-need-a-sanity-check/29113/1 "2020-06-28T14:12:18Z")

</div>

I think I'm either going nuts or I'm missing something obvious. I have a relay board that returns a string 0 or 1 based on the relay state. I'm trying to convert the "int" to on/off and pass it as the payload to a switch.

For some reason I can't get the function node to convert this correctly.

Function Node:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/1/a18dff693f89feaace3cc4e00fccca0900c93854.png)

Output from the exec node that is passed to the function node above:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/d/ed5526606d473512bf7606fa934bb003cfaf591b.png)

---

<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:** [28 June 2020 14:24 UTC](https://discourse.nodered.org/t/i-need-a-sanity-check/29113/2 "2020-06-28T14:24:00Z")

</div>

Hard to tell with those screen shots & without seeing the whole flow i can only guess. Perhaps `msg.payload` has a newline or other non printable character in it? - meaning msg.payload != "0"

e.g...

![TcEQLrjM5r](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/a/caa123380f6fe50236f1e1b96dc1fbe9d8deac76.gif)

You could try trimming before checking...

```auto
var pl = msg.payload.trim()
if(pl == "0" )
  msg.payload = "off"
else if(pl == "1" )
  msg.payload = "on"
return msg;

```

EDIT...  
if necessary, check payload is a string before calling trim  
e.g.

```auto
if(typeof msg.payload == "string")
  // ok, its a string - to a trim()

```

---

<div class="post-metadata">

**Author:** ![injector22](https://avatars.discourse-cdn.com/v4/letter/i/e9bcb4/32.png) [@injector22](https://discourse.nodered.org/u/injector22)\
**Post date:** [28 June 2020 14:27 UTC](https://discourse.nodered.org/t/i-need-a-sanity-check/29113/3 "2020-06-28T14:27:05Z")

</div>

Trim did it. I knew it was something simple I was missing. Thanks

---

<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:** [12 July 2020 14:27 UTC](https://discourse.nodered.org/t/i-need-a-sanity-check/29113/4 "2020-07-12T14:27:09Z")

</div>

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