Extract mac and raw

{"msg_id":3004,"device_info":{"device_id":"0016","mac":"4091518a489a"},"data":[{"type":8,"value":{"timestamp":"2023-02-21T22:15:56+05:00","type":"Unknown","mac":"bc572900a054","rssi":-36,"name":"KBPro_290155","raw":"0201060303aafe1216aafe21010f0e5be8d148f3000ffff103f7091680207001000000000d094b4250726f5f323930313535"}},{"type":8,"value":{"timestamp":"2023-02-21T22:15:56+05:00","type":"Unknown","mac":"bc572900a097","rssi":-87,"name":"KBPro_290222","raw":"0201060303aafe0c16aafe2101070bf216e3427e091680206901000000000d094b4250726f5f323930323232"}},{"type":8,"value":{"timestamp":"2023-02-21T22:15:55+05:00","type":"Unknown","mac":"bc572900a046","rssi":-48,"name":"KBPro_290141","raw":"0201060303aafe1216aafe21010f0e5bf1aa41d3ffd2fbf9ffe1091680207001000000000d094b4250726f5f323930313431"}}]}

how to extract all "raw" and "mac" values
from this

Pass this through a JSON node and read this, which will help you extract the information you want.

https://nodered.org/docs/user-guide/messages

{"msg_id":3004,"device_info":{"device_id":"0016","mac":"4091518a489a"},"data":[{"type":8,"value":{"timestamp":"2023-02-21T22:33:35+05:00","type":"Unknown","mac":"bc57290093db","rssi":-62,"name":"Test1","raw":"0201060303aafe1216aafe21010f0e5b05ff1f3cfff103c8005d0916802070010000000006095465737431"}},{"type":8,"value":{"timestamp":"2023-02-21T22:33:34+05:00","type":"Unknown","mac":"bc5729009fe9","rssi":-43,"name":"KBPro_290048","raw":"0201060303aafe1216aafe21010f0e5820762ada001f03e8ff83091680207001000000000d094b4250726f5f323930303438"}},{"type":8,"value":{"timestamp":"2023-02-21T22:33:34+05:00","type":"Unknown","mac":"bc572900a054","rssi":-58,"name":"KBPro_290155","raw":"0201060303aafe1216aafe21010f0e5be99448e2000f001f03f7091680207001000000000d094b4250726f5f323930313535"}}]}

using json got this now how to extract raw and mac into a new message

Extract them how, in what format should the output be?

In a change node usingJSONata Expression

 $$.payload.data.value.mac

Would return an array of all mac's contained in the data.value property of the payload.

image
tried this but not decoding

image

image

{"msg_id":3004,"device_info":{"device_id":"0016","mac":"4091518a489a"},"data":[{"type":8,"value":{"timestamp":"2023-02-21T22:42:13+05:00","type":"Unknown","mac":"bc572900a054","rssi":-36,"name":"KBPro_290155","raw":"0201060303aafe1216aafe21010f0e5bea344943000f002e03f7091680207001000000000d094b4250726f5f323930313535"}},{"type":8,"value":{"timestamp":"2023-02-21T22:42:12+05:00","type":"Unknown","mac":"bc572900a046","rssi":-49,"name":"KBPro_290141","raw":"0201060303aafe1216aafe21010f0e5bf3e84fe0ffd2fbf9ffe1091680207001000000000d094b4250726f5f323930313431"}},{"type":8,"value":{"timestamp":"2023-02-21T22:42:11+05:00","type":"Unknown","mac":"bc572900a046","rssi":-41,"name":"KBPro_290141","raw":"0201060303aafe1216aafe21010f0e5bf3e84fe0ffc2fbf9ffd2091680207001000000000d094b4250726f5f323930313431"}}]}

Show us how you have set up your JSON node, you still have a string instead of a JavaScript object. @E1cid.'s suggestion only works on a JavaScript object.

Is there a question hidden there somewhere?

You need to select jsonata J: not string a/z. Also you have to input an object not a json (string)


example flow

[{"id":"8b48b1d7ea63411d","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"msg_id\":3004,\"device_info\":{\"device_id\":\"0016\",\"mac\":\"4091518a489a\"},\"data\":[{\"type\":8,\"value\":{\"timestamp\":\"2023-02-21T22:15:56+05:00\",\"type\":\"Unknown\",\"mac\":\"bc572900a054\",\"rssi\":-36,\"name\":\"KBPro_290155\",\"raw\":\"0201060303aafe1216aafe21010f0e5be8d148f3000ffff103f7091680207001000000000d094b4250726f5f323930313535\"}},{\"type\":8,\"value\":{\"timestamp\":\"2023-02-21T22:15:56+05:00\",\"type\":\"Unknown\",\"mac\":\"bc572900a097\",\"rssi\":-87,\"name\":\"KBPro_290222\",\"raw\":\"0201060303aafe0c16aafe2101070bf216e3427e091680206901000000000d094b4250726f5f323930323232\"}},{\"type\":8,\"value\":{\"timestamp\":\"2023-02-21T22:15:55+05:00\",\"type\":\"Unknown\",\"mac\":\"bc572900a046\",\"rssi\":-48,\"name\":\"KBPro_290141\",\"raw\":\"0201060303aafe1216aafe21010f0e5bf1aa41d3ffd2fbf9ffe1091680207001000000000d094b4250726f5f323930313431\"}}]}","payloadType":"json","x":170,"y":4600,"wires":[["cf0b16afe5f152c9","a81b088bee7c3ea9"]]},{"id":"cf0b16afe5f152c9","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload.data.value.mac[]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":4600,"wires":[["9b498d9f9b9b0b73"]]},{"id":"a81b088bee7c3ea9","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 234","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":350,"y":4680,"wires":[]},{"id":"9b498d9f9b9b0b73","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 231","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":570,"y":4600,"wires":[]}]
1 Like

The data is received from MQTT

image

and the data array keeps changing like it may contain object from 1 to 4
inside each object

image

we have timestamp mac rssi name raw

I just need to further process this which i am unable to do

image

I am getting just mac address sometimes just 1 or in an array ,
how can i have mac and raw together in one

Some of the Array have 1 object but when it has more than 1 how to handle this condition

Step 1 ##
how to count number of object ?
image

and based on number of object if we can run loop to extract all values from inside

image

and them will send the data to influxdb ?

need help from step 1

[{"id":"338ef65f42fb415e","type":"inject","z":"5e0266844891bb1c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"msg_id\":3004,\"device_info\":{\"device_id\":\"0016\",\"mac\":\"4091518a489a\"},\"data\":[{\"type\":8,\"value\":{\"timestamp\":\"2023-02-21T22:15:56+05:00\",\"type\":\"Unknown\",\"mac\":\"bc572900a054\",\"rssi\":-36,\"name\":\"KBPro_290155\",\"raw\":\"0201060303aafe1216aafe21010f0e5be8d148f3000ffff103f7091680207001000000000d094b4250726f5f323930313535\"}},{\"type\":8,\"value\":{\"timestamp\":\"2023-02-21T22:15:56+05:00\",\"type\":\"Unknown\",\"mac\":\"bc572900a097\",\"rssi\":-87,\"name\":\"KBPro_290222\",\"raw\":\"0201060303aafe0c16aafe2101070bf216e3427e091680206901000000000d094b4250726f5f323930323232\"}},{\"type\":8,\"value\":{\"timestamp\":\"2023-02-21T22:15:55+05:00\",\"type\":\"Unknown\",\"mac\":\"bc572900a046\",\"rssi\":-48,\"name\":\"KBPro_290141\",\"raw\":\"0201060303aafe1216aafe21010f0e5bf1aa41d3ffd2fbf9ffe1091680207001000000000d094b4250726f5f323930313431\"}}]}","payloadType":"json","x":890,"y":80,"wires":[["1107097c9215ae0f"]]},{"id":"10ab00c9fa760a7d","type":"split","z":"5e0266844891bb1c","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":870,"y":160,"wires":[["3fec5ce32bb24029"]]},{"id":"1107097c9215ae0f","type":"change","z":"5e0266844891bb1c","name":"","rules":[{"t":"move","p":"payload.device_info.mac","pt":"msg","to":"mac","tot":"msg"},{"t":"move","p":"payload.data","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":900,"y":120,"wires":[["10ab00c9fa760a7d"]]},{"id":"fdb8241d0ab9f9a5","type":"debug","z":"5e0266844891bb1c","name":"debug 263","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":890,"y":240,"wires":[]},{"id":"3fec5ce32bb24029","type":"change","z":"5e0266844891bb1c","name":"","rules":[{"t":"move","p":"payload.value.raw","pt":"msg","to":"payload.raw","tot":"msg"},{"t":"move","p":"mac","pt":"msg","to":"payload.mac","tot":"msg"},{"t":"delete","p":"parts","pt":"msg"},{"t":"delete","p":"payload.type","pt":"msg"},{"t":"delete","p":"payload.value","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":900,"y":200,"wires":[["fdb8241d0ab9f9a5"]]}]


Is it possible to add substring in rules to extract values if

  1. MAC starts with BC
  2. Extract substring from raw eg. substr(32, 4)

var Temp = Number(hexTo8p8(msg.payload.data[i].value.raw.substr(32, 4)));
var Humidity = Number(hexTo8p8(msg.payload.data[i].value.raw.substr(36, 4)));

I expect you could do that in the change node using Jsonata, otherwise in a function node.

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