Help Parsing HTTP payload information

I'm using an HTTP API (OctoPrint Enclosure plugin for those that care) that returns the following payload:

payload: "[{"status": false, "index_id": 3}, {"status": false, "index_id": 4}, {"status": false, "index_id": 5}, {"status": true, "index_id": 6}]"

I would like to take each status and use it to sync my input booleans in Home Assistant. Can someone help me with a node that would parse out the true/false for each index id?

Thank you!

If you look at the surrounding quotes, you can see that the payload is a stringified JSON array. Step one is to feed the payload to the builtin JSON node to turn it into a usable object. Next, you can use the builtin split node to handle each item in the array. See working with messages and read the part about message sequences too.

That is very helpful. It gets me four objects, with the following payload format:

58%20AM

How do I convert this to something useable, i.e. index 5 false means input_boolean X is "off"

One way is to use a function node and write javascript. You can access the msg.payload object directly. Need to be a bit more specific on your output.

get value of index_id: X=msg.payload.index_id // number
get value of status: status=msg.payload.status // bool

example Check specifically status is false and index_id is 5
var mystring;
if (!msg.payload.status && msg.payload.index_id ==5){
mystring = "input_boolean " + msg.payload.index_id + " is off";
} else {
mystring = "input_boolean " + msg.payload.index_id + " is on";
}

msg.payload=mystring;

Could you help me figure out the JS to make a separate output with payload on/off if status=true (or false) and index_id=5?

Also, the same for index 3,4, and 6 :slight_smile:

The following does not seem to be working or doing anything:

if (msg.payload[2].status ===false && msg.payload[2].index_id ==5)
{
mystring = "input_boolean " + msg.payload[2].index_id + " is off";
} else {
imystring = "input_boolean " + msg.payload[2].index_id + " is on";
}

return msg;

You could use something like this (works for any id and status):

index = msg.payload.index_id
status = msg.payload.false
strStatus = (status) ? "on" : "off" 
out = "input_boolean " + index + " " + strStatus 

return {payload:out};

Feeding this from the split node, I get the following result in the debug node:

payload: "input_boolean 5 off"

Regardless of the actual status (true/false) coming form the html call. All four objects are returning false values.

I think I got it working...thank you @bakman2! Was a slight typo in your response. Current working code:

index = msg.payload.index_id;
status = msg.payload.status;
strStatus = (status) ? "on" : "off" ;
out = "input_boolean " + index + " " + strStatus; 

return {payload:out};

One other add-on question:

If the payload is now "RelayX on" or RelayX off", what node would I use to get the payload to simply be on or off?

On/off as values: the boolean like it is now (true/false in status)
On/off as text: the strings like you have them now in your function

This splits the array into one msg per item. Then switch used to direct each msg to specific output (0-6). Then 6 is split further into on / off (true/false). from there you can set any string value. Lot more nodes this way but you should be able to accomplish. Suggest mapping out the the exact flow first, even if on paper. Like doing a math problem showing every operational step. Then think about existing node or nodes can accomplish the task.

[{"id":"21f3a38.97fed5c","type":"switch","z":"2d36d476.ac4cec","name":"","property":"payload.index_id","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"},{"t":"eq","v":"3","vt":"num"},{"t":"eq","v":"4","vt":"num"},{"t":"eq","v":"5","vt":"num"},{"t":"eq","v":"6","vt":"num"}],"checkall":"true","repair":false,"outputs":7,"x":890,"y":460,"wires":[["e58a22de.52f1a"],["fde85767.147a38"],["85af23a7.d2c9"],["e2091775.3a8ab8"],["4c114ee7.141d9"],["6601b1b.71b545"],["b6ff39a4.a4f8c8","e8872f4.17ecdd"]]},{"id":"1682af87.84b27","type":"function","z":"2d36d476.ac4cec","name":"simulate OctoPrint","func":"msg.payload=[{\"status\": false, \"index_id\": 3}, {\"status\": false, \"index_id\": 4}, {\"status\": false, \"index_id\": 5}, {\"status\": false, \"index_id\": 6}];\nreturn msg;","outputs":1,"noerr":0,"x":530,"y":460,"wires":[["202b2836.aa9a98","cda33154.483cb"]]},{"id":"19615fac.c38bf","type":"inject","z":"2d36d476.ac4cec","name":"","topic":"","payload":"button","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":330,"y":460,"wires":[["1682af87.84b27"]]},{"id":"e58a22de.52f1a","type":"debug","z":"2d36d476.ac4cec","name":"0","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1070,"y":360,"wires":[]},{"id":"fde85767.147a38","type":"debug","z":"2d36d476.ac4cec","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1070,"y":400,"wires":[]},{"id":"85af23a7.d2c9","type":"debug","z":"2d36d476.ac4cec","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1070,"y":440,"wires":[]},{"id":"e2091775.3a8ab8","type":"debug","z":"2d36d476.ac4cec","name":"3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1070,"y":480,"wires":[]},{"id":"4c114ee7.141d9","type":"debug","z":"2d36d476.ac4cec","name":"4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1070,"y":520,"wires":[]},{"id":"6601b1b.71b545","type":"debug","z":"2d36d476.ac4cec","name":"5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1070,"y":560,"wires":[]},{"id":"202b2836.aa9a98","type":"debug","z":"2d36d476.ac4cec","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1070,"y":240,"wires":[]},{"id":"cda33154.483cb","type":"split","z":"2d36d476.ac4cec","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":750,"y":460,"wires":[["21f3a38.97fed5c","68084b03.a8a284"]]},{"id":"68084b03.a8a284","type":"debug","z":"2d36d476.ac4cec","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1070,"y":280,"wires":[]},{"id":"b6ff39a4.a4f8c8","type":"debug","z":"2d36d476.ac4cec","name":"6","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1070,"y":600,"wires":[]},{"id":"e8872f4.17ecdd","type":"switch","z":"2d36d476.ac4cec","name":"seperate status 6","property":"payload.status","propertyType":"msg","rules":[{"t":"eq","v":"true","vt":"jsonata"},{"t":"eq","v":"false","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":2,"x":1120,"y":660,"wires":[["420eb42b.f9f43c"],["68c91688.c4f228"]],"outputLabels":["On","Off"]},{"id":"420eb42b.f9f43c","type":"debug","z":"2d36d476.ac4cec","name":"on","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1330,"y":620,"wires":[]},{"id":"68c91688.c4f228","type":"debug","z":"2d36d476.ac4cec","name":"off","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1330,"y":680,"wires":[]}]

Please export your flow again, read this post and edit your post.

Cool thing about NR...a lot of ways to skin the same cat. Here is what "I" came up with (with the help of this post)...very similar:

[{"id":"b9cb942f.daadd8","type":"tab","label":"Clipboard","disabled":false,"info":""},{"id":"62a0da9c.76e4dc","type":"http request","z":"b9cb942f.daadd8","name":"Status","method":"GET","ret":"txt","paytoqs":false,"url":"http://xxx.xxx.x.x/plugin/enclosure/getOutputStatus?apikey=xxxxxxx","tls":"","proxy":"","authType":"","x":210,"y":220,"wires":[["1192b25d.cba726"]]},{"id":"1192b25d.cba726","type":"json","z":"b9cb942f.daadd8","name":"","property":"payload","action":"","pretty":false,"x":330,"y":220,"wires":[["bbafd4f1.4f21e"]]},{"id":"dda1b26a.658678","type":"inject","z":"b9cb942f.daadd8","name":"","topic":"","payload":"","payloadType":"date","repeat":"15","crontab":"","once":true,"onceDelay":0.1,"x":250,"y":260,"wires":[["62a0da9c.76e4dc"]]},{"id":"bbafd4f1.4f21e","type":"split","z":"b9cb942f.daadd8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":450,"y":220,"wires":[["d6cbecb5.8b7ee8"]]},{"id":"d6cbecb5.8b7ee8","type":"function","z":"b9cb942f.daadd8","name":"on/off","func":"index = msg.payload.index_id;\nstatus = msg.payload.status;\nstrStatus = (status) ? \"on\" : \"off\" ;\nout = \"Relay\" + index + \" \" + strStatus; \n\nreturn {payload:out};","outputs":1,"noerr":0,"x":570,"y":220,"wires":[["701d86b.1b547f8"]]},{"id":"701d86b.1b547f8","type":"switch","z":"b9cb942f.daadd8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"Relay3","vt":"str"},{"t":"cont","v":"Relay4","vt":"str"},{"t":"cont","v":"Relay5","vt":"str"},{"t":"cont","v":"Relay6","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":690,"y":220,"wires":[["cc0cce0f.1c37f8"],["81d404e3.f5dbd8"],["2448b2ff.bf5dbe"],["311be413.718134"]]},{"id":"cc0cce0f.1c37f8","type":"string","z":"b9cb942f.daadd8","name":"on/off","methods":[{"name":"getRightMost","params":[{"type":"str","value":" "}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":840,"y":147,"wires":[["8c7eafd3.258f3"]]},{"id":"81d404e3.f5dbd8","type":"string","z":"b9cb942f.daadd8","name":"on/off","methods":[{"name":"getRightMost","params":[{"type":"str","value":" "}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":840,"y":194,"wires":[["6997b34a.7f7f1c"]]},{"id":"2448b2ff.bf5dbe","type":"string","z":"b9cb942f.daadd8","name":"on/off","methods":[{"name":"getRightMost","params":[{"type":"str","value":" "}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":840,"y":237,"wires":[["558a99c8.9f631"]]},{"id":"311be413.718134","type":"string","z":"b9cb942f.daadd8","name":"on/off","methods":[{"name":"getRightMost","params":[{"type":"str","value":" "}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":840,"y":282,"wires":[["4539a0e3.411b48"]]},{"id":"8c7eafd3.258f3","type":"rbe","z":"b9cb942f.daadd8","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":950,"y":147,"wires":[["8c3a2ed7.fbfde"]]},{"id":"6997b34a.7f7f1c","type":"rbe","z":"b9cb942f.daadd8","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":950,"y":194,"wires":[["d640e286.bc71b8"]]},{"id":"558a99c8.9f631","type":"rbe","z":"b9cb942f.daadd8","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":950,"y":238,"wires":[["5517309d.7fd968"]]},{"id":"4539a0e3.411b48","type":"rbe","z":"b9cb942f.daadd8","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":950,"y":282,"wires":[["dddfd9f8.c90f38"]]},{"id":"8c3a2ed7.fbfde","type":"api-call-service","z":"b9cb942f.daadd8","name":"HA Update","server":"6c32e276.37c47c","version":1,"service_domain":"homeassistant","service":"turn_{{payload}}","entityId":"input_boolean.printer_power","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1081,"y":147,"wires":[[]]},{"id":"d640e286.bc71b8","type":"api-call-service","z":"b9cb942f.daadd8","name":"HA Update","server":"6c32e276.37c47c","version":1,"service_domain":"homeassistant","service":"turn_{{payload}}","entityId":"input_boolean.printer_lights","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1081,"y":194,"wires":[[]]},{"id":"5517309d.7fd968","type":"api-call-service","z":"b9cb942f.daadd8","name":"HA Update","server":"6c32e276.37c47c","version":1,"service_domain":"homeassistant","service":"turn_{{payload}}","entityId":"input_boolean.printer_dryer","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1081,"y":239,"wires":[[]]},{"id":"dddfd9f8.c90f38","type":"api-call-service","z":"b9cb942f.daadd8","name":"HA Update","server":"6c32e276.37c47c","version":1,"service_domain":"homeassistant","service":"turn_{{payload}}","entityId":"input_boolean.printer_relay","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1081,"y":284,"wires":[[]]},{"id":"6c32e276.37c47c","type":"server","z":"","name":"HOME ASSISTANT","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":true}]