Hi, I have an array of booleans which I try to change to 0s and 1s.
I have tried to use the change fucntion do replace a boolean to a number.
it doesnt do the transformation as the output is still a boolean.
How can I accomplish this?
thank you!
Hi, I have an array of booleans which I try to change to 0s and 1s.
I have tried to use the change fucntion do replace a boolean to a number.
it doesnt do the transformation as the output is still a boolean.
How can I accomplish this?
thank you!
In a function
msg.payload = msg.payload.map(b => b ? 1 : 0)
return msg
in a flow...
split -> switch (ROW1: payload is true?, ROW2: otherwise) -> change -> join
[{"id":"918931e237933f0c","type":"inject","z":"0411e9573c7c5449","name":"[true, false, false, true]","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[true, false, false, true]","payloadType":"json","x":500,"y":200,"wires":[["4202930d9be35c82"]]},{"id":"4202930d9be35c82","type":"split","z":"0411e9573c7c5449","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":670,"y":200,"wires":[["9b284a38ca54d249"]]},{"id":"9b284a38ca54d249","type":"switch","z":"0411e9573c7c5449","name":"true? \\n false?","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":800,"y":200,"wires":[["ee70c5f8b75bd71d"],["c1a560b939454e23"]]},{"id":"ee70c5f8b75bd71d","type":"change","z":"0411e9573c7c5449","name":"set payload=1","rules":[{"t":"set","p":"payload","pt":"msg","to":"1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":980,"y":180,"wires":[["0dd716d18e444cd0"]]},{"id":"c1a560b939454e23","type":"change","z":"0411e9573c7c5449","name":"set payload=0","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":980,"y":220,"wires":[["0dd716d18e444cd0"]]},{"id":"0dd716d18e444cd0","type":"join","z":"0411e9573c7c5449","name":"","mode":"auto","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":"false","timeout":"","count":"","reduceRight":false,"x":1160,"y":200,"wires":[["15ddcd656068900b"]]},{"id":"15ddcd656068900b","type":"debug","z":"0411e9573c7c5449","name":"debug 318","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1300,"y":200,"wires":[]},{"id":"3710ad431e845b96","type":"inject","z":"0411e9573c7c5449","name":"[true, false, false, true]","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[true, false, false, true]","payloadType":"json","x":500,"y":280,"wires":[["a928e6bf7d2de762"]]},{"id":"a928e6bf7d2de762","type":"function","z":"0411e9573c7c5449","name":"function 10","func":"msg.payload = msg.payload.map(b => b ? 1 : 0)\nreturn msg\n","outputs":1,"timeout":30,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":280,"wires":[["06d8e89780c6a11c"]]},{"id":"06d8e89780c6a11c","type":"debug","z":"0411e9573c7c5449","name":"debug 319","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1300,"y":280,"wires":[]}]
Or have a split before your change node and a join after (switch not necessary)
[{"id":"de2c627bf5695048","type":"debug","z":"820b503366768e45","name":"debug 460","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":870,"y":700,"wires":[]},{"id":"193bbf2b38ff05e0","type":"inject","z":"820b503366768e45","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[true, false]","payloadType":"json","x":300,"y":700,"wires":[["50161b86b6e297cb"]]},{"id":"26e9fa06f1cf6f4b","type":"change","z":"820b503366768e45","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"true","fromt":"bool","to":"1","tot":"num"},{"t":"change","p":"payload","pt":"msg","from":"false","fromt":"bool","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":700,"wires":[["7d3de54c0e10e124"]]},{"id":"50161b86b6e297cb","type":"split","z":"820b503366768e45","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":430,"y":700,"wires":[["26e9fa06f1cf6f4b"]]},{"id":"7d3de54c0e10e124","type":"join","z":"820b503366768e45","name":"","mode":"auto","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":"false","timeout":"","count":"","reduceRight":false,"x":730,"y":700,"wires":[["de2c627bf5695048"]]}]
but much more visually descriptive
Three cheers for easily understood code!
Working great!
thank you very much!
In fairness, it was deliberately terse to encourage a better visual approach
If i were encouraging use of function node, I would have wrote...
// first ensure the payload is an array
if (Array.isArray(msg.payload)) {
// loop every element of the payload array
for (let i = 0; i < msg.payload.length; i++) {
// if the element of the array is boolean true, set it to 1
if (msg.payload[i] === true) {
msg.payload[i] = 1
} else {
// otherwise set it to 0
msg.payload[i] = 0
}
}
return msg // send the msg to next node
}
node.error('payload is not an array', msg) // inform user of error
return null // halt flow
You can also use JSONata in a change node and the $number function
e.g.
[{"id":"a3166e7879679525","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"zone2","payload":"[true,false]","payloadType":"json","x":300,"y":1600,"wires":[["5c88e750c1328b74"]]},{"id":"5c88e750c1328b74","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload.$number($)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":1600,"wires":[["303927168087d09b"]]},{"id":"303927168087d09b","type":"debug","z":"d1395164b4eec73e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":1560,"wires":[]}]
expression
$$.payload.$number($)
This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.