convert input string to numbers and boolean

2,601 views
Skip to first unread message

mm

unread,
Dec 18, 2017, 5:20:04 AM12/18/17
to Node-RED
I'm using the csv node to create an array out of the incoming string. I noticed that numbers are automatically converted to "numbers" (var type) and I know how to convert the time (23:00) into a time format.
But how about the boolean values? Is there a trick to convert incoming "true" and "false" to boolean values? I know that the "JSON" node is capable of doing that for one l´payload.msg but how do I do that for a whole array??

Here are my nodes What I want to do is, receive the string, convert the "true" and "false" into "real" boolean, convert the time into "real" time and have then everything again in one array for further processing.
Is there a trick of some sort?

[{"id":"5ea0b7b7.df83f8","type":"inject","z":"d5747f12.b5ab1","name":"","topic":"","payload":"1,00:00:30,false,22:00,23:00,1,1,0,1,0,0,10:00,14:00,0,1,1","payloadType":"str","repeat":"","crontab":"","once":false,"x":370,"y":320,"wires":[["c7f7de.c595182"]]},{"id":"c7f7de.c595182","type":"csv","z":"d5747f12.b5ab1","name":"","sep":",","hdrin":"","hdrout":"","multi":"one","ret":"\\n","temp":"aoff,aotime,alight1,al1on, al1off, al1mon,al1tue,al1wed,al1thu,al1fri, alight2,al2on, al2off, al2sat,al2sun,al2hol","x":550,"y":320,"wires":[["948c91cf.3b2a8"]]},{"id":"948c91cf.3b2a8","type":"debug","z":"d5747f12.b5ab1","name":"","active":true,"console":"false","complete":"false","x":750,"y":320,"wires":[]}]

Thank you
Martin

Mark Setrem

unread,
Dec 18, 2017, 5:33:05 AM12/18/17
to Node-RED

Colin Law

unread,
Dec 18, 2017, 5:58:28 AM12/18/17
to node...@googlegroups.com
I think you will have to write a function node to step through the
array and convert the values.

Colin
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/2e57822e-f849-4dee-950d-8490d8180b8f%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

mm

unread,
Dec 18, 2017, 7:26:36 AM12/18/17
to Node-RED
Aaahhh...now that I "wanted" to use standard nodes... ;-)

I gues I dont have to take care of numericals or other string, since numbers are converted automaticly during the csv node (why?) and boolean not (why?) and other string I'd like to leave string

could you please point me to an example on how to do that? I guess these are the necessary steps:
- find out how many (key/values) in the object
- access the value of the item (how?)
- do conversion to boolean based on content (true/false)
- copy back to value of pair

or am i missing the simple way here???

Colin Law

unread,
Dec 18, 2017, 8:32:42 AM12/18/17
to node...@googlegroups.com
I think what you really need to do is to take a couple of days out and
work through some javascript tutorials, the questions you are asking
are questions on basic javascript and you will save yourself (and us)
a lot of time in the long run if you do some self education.

Colin
> https://groups.google.com/d/msgid/node-red/7135f1ec-89ef-4ecf-9393-02579ecf8f68%40googlegroups.com.

steve rickus

unread,
Dec 18, 2017, 9:37:14 AM12/18/17
to Node-RED
CSV files do not contain any metadata that would give the node a clue as to which columns should be interpreted as "booleans" -- and it's probably not practical to check every value in every column to see if it's a string like "true" or "false" (or for that matter, "False", "F", "0", etc)

I applaud your desire to use the core nodes as much as practical... bit I do agree with Colin that you will understand node-red much better by learning javascript. Personally, I use the W3Schools site all the time as a reference. There are some tasks (like this) that are best served with a bit of conditional programming, especially if you don't know ahead of time which columns may have boolean values in them (hint: check out the map() function)

Of course, in your example there is only one column that is "false", so if that is all you need modified, a change node is nice and simple. However, you will need to use a JSONata conditional statement (so still a bit of programming). Just add a rule to "Set" msg.payload.alight1 "to" J: payload = "true" ? true : false

And finally, I have to ask exactly WHY you feel you need to change the string "false" to a boolean false value? Is that required by your PLC? If not, you can just test for "true" or "false" in the downstream nodes, especially if they are ui nodes (most will accept either the string or boolean value).
--
Steve

mm

unread,
Dec 18, 2017, 10:30:38 AM12/18/17
to Node-RED
This one doesn't (in ui_template):

<md-button
   
ng-style="{background: !msg.payload.alight1 ? 'green' : 'red' }"
   
ng-click="send({payload.alight1: !msg.payload.alight1 })"
   
>
    Test
</md-button>

mm

unread,
Dec 18, 2017, 10:32:28 AM12/18/17
to Node-RED
But thanks for the hint Colin
will do

Dave C-J

unread,
Dec 18, 2017, 11:38:13 AM12/18/17
to node...@googlegroups.com
That's because it's a template... so how tolerant the tolerant the code in there is to variations is down the the author... (you ;-)
You could try changing the test to be (msg.payload.alight1 == "true")   - or maybe  !=   depending which way round it needs to be ...
Reply all
Reply to author
Forward
0 new messages