# Failed to detect string in nr-if statement

**URL:** <https://discourse.nodered.org/t/failed-to-detect-string-in-nr-if-statement/24534>\
**Category:** Dashboard\
**Created:** [9 April 2020 16:16 UTC](https://discourse.nodered.org/t/failed-to-detect-string-in-nr-if-statement/24534 "2020-04-09T16:16:39Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![chuckf201](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chuckf201/32/91382_2.png) [@chuckf201](https://discourse.nodered.org/u/chuckf201)\
**Post date:** [9 April 2020 16:16 UTC](https://discourse.nodered.org/t/failed-to-detect-string-in-nr-if-statement/24534/1 "2020-04-09T16:16:39Z")

</div>

I'm send in an opened or closed string in msg.payload and opening a jpg based on that value in a template node.

```auto
<div>
    <img ng-if="msg.payload === 'opened' " src="http://xx.yy.zzz.124:1882/GaragePicForSecurity-Open.jpg" style="width:40%"> 
    <img ng-if="msg.payload === 'closed' " src="http://xx.yy.zzz.124:1882/GaragePicForSecurity-Close.jpg" style="width:40%"> 
</div>

```

This code produced the pic I need:

```auto

My debug output is:
{"_msgid":"be89d563.7e3a58","topic":"","payload":"opened\n","rc":{"code":0}}

There are no errors and So what am I missing here?
```

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [9 April 2020 16:38 UTC](https://discourse.nodered.org/t/failed-to-detect-string-in-nr-if-statement/24534/2 "2020-04-09T16:38:15Z")

</div>

i think you have to use {{msg.payload}} instead of just msg.payload, at least that is what you have to do elsewhere to access the msg object in the ui template node.  
Johannes

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [9 April 2020 17:35 UTC](https://discourse.nodered.org/t/failed-to-detect-string-in-nr-if-statement/24534/3 "2020-04-09T17:35:13Z")

</div>

actually you don’t. The problem is you were missing the single quotation marks around the string you test.

```auto
<div>
    <h1 ng-if="msg.payload == 'open'">
        Open
    </h1>
    <h1 ng-if="msg.payload == 'closed'">
        Closed
    </h1>
</div>

```

I tested it this way and it worked

---

<div class="post-metadata">

**Author:** ![chuckf201](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chuckf201/32/91382_2.png) [@chuckf201](https://discourse.nodered.org/u/chuckf201)\
**Post date:** [9 April 2020 17:36 UTC](https://discourse.nodered.org/t/failed-to-detect-string-in-nr-if-statement/24534/4 "2020-04-09T17:36:30Z")

</div>

Still no fix. Looking closer at the debug I noticed a \n in the string.  
It might not work with the check. I didn't add the newline, how do I remove it.

---

<div class="post-metadata">

**Author:** ![chuckf201](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chuckf201/32/91382_2.png) [@chuckf201](https://discourse.nodered.org/u/chuckf201)\
**Post date:** [9 April 2020 17:37 UTC](https://discourse.nodered.org/t/failed-to-detect-string-in-nr-if-statement/24534/5 "2020-04-09T17:37:28Z")

</div>

Single quote around string is there.

You also have a double equal. Does that matter?

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [9 April 2020 17:38 UTC](https://discourse.nodered.org/t/failed-to-detect-string-in-nr-if-statement/24534/6 "2020-04-09T17:38:25Z")

</div>

```auto
msg.payload = msg.payload.replace(/\\n/g,““);

```

or a change node

```auto
[{"id":"a816f868.5a2c9","type":"ui_template","z":"f954564f.03e718","group":"d619b4d0.780aa8","name":"","order":3,"width":0,"height":0,"format":"<div>\n <h1 ng-if=\"msg.payload == 'open'\">\n Open\n </h1>\n</div>","storeOutMessages":true,"fwdInMessages":true,"templateScope":"local","x":520,"y":960,"wires":[["1a9dbc76.5e7a54"]]},{"id":"a720a089.bba69","type":"inject","z":"f954564f.03e718","name":"","topic":"","payload":"open\\n","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":960,"wires":[["7e9fb79b.64198"]]},{"id":"1a9dbc76.5e7a54","type":"debug","z":"f954564f.03e718","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":670,"y":960,"wires":[]},{"id":"7e9fb79b.64198","type":"change","z":"f954564f.03e718","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"\\n","fromt":"str","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":960,"wires":[["a816f868.5a2c9"]]},{"id":"d619b4d0.780aa8","type":"ui_group","z":"","name":"Countdown","tab":"b2566dcf.0415","order":2,"disp":true,"width":"6","collapse":false},{"id":"b2566dcf.0415","type":"ui_tab","z":"","name":"Countdown","icon":"dashboard","order":2,"disabled":false,"hidden":false}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/d/6d30ea7ff1dae1fb630788cf7ccec27a4f651b5b.jpeg)

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [9 April 2020 17:41 UTC](https://discourse.nodered.org/t/failed-to-detect-string-in-nr-if-statement/24534/7 "2020-04-09T17:41:04Z")

</div>

> [@chuckf201](#):
>
> You also have a double equal. Does that matter?

> **[JavaScript — Double Equals vs. Triple Equals](https://codeburst.io/javascript-double-equals-vs-triple-equals-61d4ce5a121a)**
>
> Learn equality in JavaScript in 3 minutes

---

<div class="post-metadata">

**Author:** ![chuckf201](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chuckf201/32/91382_2.png) [@chuckf201](https://discourse.nodered.org/u/chuckf201)\
**Post date:** [9 April 2020 18:25 UTC](https://discourse.nodered.org/t/failed-to-detect-string-in-nr-if-statement/24534/8 "2020-04-09T18:25:58Z")

</div>

Sure, I can see where 'loose equality' would be more lax but, in the strictest sense, both objects should be equal in value and type. Yes?

---

<div class="post-metadata">

**Author:** ![chuckf201](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chuckf201/32/91382_2.png) [@chuckf201](https://discourse.nodered.org/u/chuckf201)\
**Post date:** [9 April 2020 18:37 UTC](https://discourse.nodered.org/t/failed-to-detect-string-in-nr-if-statement/24534/9 "2020-04-09T18:37:23Z")

</div>

Your example worked great...thanks.  
On to bigger and better way to learn and get into trouble.

---

<div class="post-metadata">

**Author:** ![chuckf201](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chuckf201/32/91382_2.png) [@chuckf201](https://discourse.nodered.org/u/chuckf201)\
**Post date:** [9 April 2020 20:01 UTC](https://discourse.nodered.org/t/failed-to-detect-string-in-nr-if-statement/24534/10 "2020-04-09T20:01:03Z")

</div>

I'm still not out of the woods yet.  
This fails to bring up my jpg.

```auto
    <h1 ng-if="msg.payload == 'opened' ">
        <img src="http://x.y.z.124:1882/GaragePicForSecurity-Open.jpg" alt="My-Open Attempt-FAILED" style="width:40%">
        Open
    </h1>
    <h1 ng-if="msg.payload == 'closed' ">
        <img src="http://x.y.z.124:1882/GaragePicForSecurity-Close.jpg" alt="My-Close-Attempt-FAILED" style="width:40%">
        Closed
    </h1>
</div>

```

any thoughts?  
This stand-alone stmt works:  
`<img src="http://x.y.z.124:1882/GaragePicForSecurity-Open.jpg" alt="My-Attempt-FAILED" style="width:100%">`

---

<div class="post-metadata">

**Author:** ![chuckf201](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chuckf201/32/91382_2.png) [@chuckf201](https://discourse.nodered.org/u/chuckf201)\
**Post date:** [10 April 2020 19:41 UTC](https://discourse.nodered.org/t/failed-to-detect-string-in-nr-if-statement/24534/11 "2020-04-10T19:41:54Z")

</div>

I realize that the logic and code is correct. Up-street, I'm using python3 to send opened and closed using a function node. Python uses a print statement with stdout and always has a \r\n. I can't remove it so I'll need another approach.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [10 April 2020 19:50 UTC](https://discourse.nodered.org/t/failed-to-detect-string-in-nr-if-statement/24534/12 "2020-04-10T19:50:04Z")

</div>

But if it is always the same at the end of the string you receive why don’t you use one of the methods i was showing you to remove it from the string. Another way would be to use mqtt to send things from the python script to nodered.

Johannes

Edit: how do you run the python script? Because I have a few python scripts that I run with the exec or daemon node for long running scripts that communicate with nodered over stdout where I don’t have this problem when receiving the payload.

---

<div class="post-metadata">

**Author:** ![chuckf201](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chuckf201/32/91382_2.png) [@chuckf201](https://discourse.nodered.org/u/chuckf201)\
**Post date:** [10 April 2020 20:48 UTC](https://discourse.nodered.org/t/failed-to-detect-string-in-nr-if-statement/24534/13 "2020-04-10T20:48:59Z")

</div>

Changed string return(print) in python script to integer 0 or 1.  
I'm using exec node to a file on RasPi.  
So, to test, I changed the python print stmt to print print(int(1)) and connect it to a  
function node

```auto
msg.payload = Number(msg.payload);
   return msg;

```

debug shows a single character 1 or 0.  
Got rid of the cr nl but ng-if stmt fails.

No diff.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [10 April 2020 21:14 UTC](https://discourse.nodered.org/t/failed-to-detect-string-in-nr-if-statement/24534/14 "2020-04-10T21:14:51Z")

</div>

Have a close look at what comparison you do with what object type. Have a look at this and how different comparisons work with different payload types:

```auto
[{"id":"bb4f191a.e43b68","type":"inject","z":"f954564f.03e718","name":"String 1","topic":"","payload":"1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":600,"wires":[["5606e10b.a83b8","9bdb6260.30e078"]]},{"id":"a2264de3.870e8","type":"inject","z":"f954564f.03e718","name":"String 0","topic":"","payload":"0","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":660,"wires":[["5606e10b.a83b8","9bdb6260.30e078"]]},{"id":"5606e10b.a83b8","type":"ui_template","z":"f954564f.03e718","group":"5b810075.4590d","name":"","order":2,"width":0,"height":0,"format":"<div>\n <p ng-if=\"msg.payload === '1'\">\n String 1\n </p>\n <p ng-if=\"msg.payload === '0'\">\n String 0\n </p>\n <p ng-if=\"msg.payload === 1\">\n Number 1\n </p>\n <p ng-if=\"msg.payload === 0\">\n Number 0\n </p>\n <p ng-if=\"msg.payload == 1\">\n Doesnt matter 1\n </p>\n <p ng-if=\"msg.payload == 0\">\n Doesnt matter 0\n </p>\n</div>","storeOutMessages":true,"fwdInMessages":true,"templateScope":"local","x":380,"y":660,"wires":[[]]},{"id":"2ca0a9b3.9056e6","type":"inject","z":"f954564f.03e718","name":"Number 1","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":720,"wires":[["5606e10b.a83b8","9bdb6260.30e078"]]},{"id":"907cc9aa.cfc438","type":"inject","z":"f954564f.03e718","name":"Number 0","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":780,"wires":[["5606e10b.a83b8","9bdb6260.30e078"]]},{"id":"9bdb6260.30e078","type":"debug","z":"f954564f.03e718","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":390,"y":720,"wires":[]},{"id":"5b810075.4590d","type":"ui_group","z":"","name":"Items","tab":"b83efc1a.370b88","order":1,"disp":true,"width":"6","collapse":false},{"id":"b83efc1a.370b88","type":"ui_tab","z":"","name":"Scheduler","icon":"call_split","order":2,"disabled":false,"hidden":false}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/a/9aa68f4c0901526dd795ebee66dd07873778810f.jpeg)

```auto
<div>
    <p ng-if="msg.payload === '1'">
        String 1
    </p>
    <p ng-if="msg.payload === '0'">
        String 0
    </p>
    <p ng-if="msg.payload === 1">
        Number 1
    </p>
    <p ng-if="msg.payload === 0">
        Number 0
    </p>
    <p ng-if="msg.payload == 1">
        Doesnt matter 1
    </p>
    <p ng-if="msg.payload == 0">
        Doesnt matter 0
    </p>
</div>

```

Johannes

---

<div class="post-metadata">

**Author:** ![chuckf201](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chuckf201/32/91382_2.png) [@chuckf201](https://discourse.nodered.org/u/chuckf201)\
**Post date:** [10 April 2020 23:01 UTC](https://discourse.nodered.org/t/failed-to-detect-string-in-nr-if-statement/24534/15 "2020-04-10T23:01:33Z")

</div>

The only difference between your code and mine is nothing except the python that's feeding it.  
tried number and string my template doesn't check the payload.  
I hooked up my python mode and my msg.payload drops through the ng-if statements.  
I can send my short python script and my/your tab. What are you thoughts. This isn't rocket science yet we're doing the same thing...I think.  
Appreciate your help.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [11 April 2020 04:03 UTC](https://discourse.nodered.org/t/failed-to-detect-string-in-nr-if-statement/24534/16 "2020-04-11T04:03:52Z")

</div>

yes please the more info the better.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [11 April 2020 06:57 UTC](https://discourse.nodered.org/t/failed-to-detect-string-in-nr-if-statement/24534/17 "2020-04-11T06:57:45Z")

</div>

After further thinking are you sure you only get `1`or `0` because when I use a print statement from a python script with the exec node I do get the payload with a carriage return at the end. Try using:

```auto
msg.payload = msg.payload.replace(/[\n\r]+/g, "");
return msg;

```

---

<div class="post-metadata">

**Author:** ![chuckf201](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chuckf201/32/91382_2.png) [@chuckf201](https://discourse.nodered.org/u/chuckf201)\
**Post date:** [11 April 2020 14:39 UTC](https://discourse.nodered.org/t/failed-to-detect-string-in-nr-if-statement/24534/18 "2020-04-11T14:39:09Z")

</div>

That's it. works like it supposed to.  
Thanks a ton.

---

<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 April 2020 14:39 UTC](https://discourse.nodered.org/t/failed-to-detect-string-in-nr-if-statement/24534/19 "2020-04-25T14:39:10Z")

</div>

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