Failed to detect string in nr-if statement

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

<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:


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?

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

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

<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

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.

Single quote around string is there.

You also have a double equal. Does that matter?

1 Like
msg.payload = msg.payload.replace(/\\n/g,““);

or a change node

[{"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}]

1 Like

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?

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

1 Like

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

    <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%">

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.

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.

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

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.

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:

[{"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}]

<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

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.

1 Like

yes please the more info the better.

After further thinking are you sure you only get 1or 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:

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

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

1 Like

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