Greetings. Hoping this one is an easy one for you, it’s been troubling me for quite a while… what would be the easiest or most efficient way for extracting a specific ‘pattern’ in a payload ‘string’ regardless of the order within the payload msg (quite common for devices to ‘squeeze many wagons to a train’ payload msg and not always on a constant manner for consistent workflow decoding:
You see in the above msg the same payload string ‘patterns’ are within the message, but may be on different ‘train wagon’ position… I’m sure you get the idea. So far wasn’t an issue with brand X, but new device brand Y does this and not sure where to start 1st to solve it. As always, thanks.
Decoding custom-packed data is definitely one of the most complex things you’ll have to do in Losant. There’s really no way around using a Function Node and some good old fashioned code to break apart something like this. Here’s a version I made that decodes your two example strings (I made up some sensor types based on each prefix in your example). I have no idea if this will work for all possible data you’ll receive, but maybe it’ll provide a starting point:
let inputA = '02 00 00 03 67 00 E5 04 68 3A';
let inputB = '03 67 00 E5 04 68 3A 02 00 00';
let parse = (input) => {
let messages = [];
let parts = input.split(' ');
for(let idx = 0; idx < parts.length; idx++) {
// TODO: add some array bounds checking!
// 02 00 (temperature)
if(parts[idx] === '02' && parts[idx + 1] === '00') {
messages.push({
type: 'temperature',
value: parts[idx + 2]
});
idx += 2;
continue;
}
// 04 68 (humidity)
if(parts[idx] === '04' && parts[idx + 1] === '68') {
messages.push({
type: 'humidity',
value: parts[idx + 2]
});
idx += 2;
continue;
}
// 03 67 (voltage)
if(parts[idx] === '03' && parts[idx + 1] === '67') {
messages.push({
type: 'voltage',
value: parts[idx + 2]
});
idx += 2;
continue;
}
}
return messages;
};
payload.working = payload.working || {};
payload.working.resultsA = parse(inputA);
payload.working.resultsB = parse(inputB);
This function is iterating over each individual byte in the hex string and checking if it (plus the next byte) match a known prefix. If it does, it then grabs the value from the subsequent byte and advances the index. If you paste that into a Function Node and invoke it with a Virtual Button, this will be the output:
Holy cow @Brandon_Cannaday I didn’t think it was that complex! I thought by now, seeing how common this is in the mfr industry, there would be an already ‘standard’ process (or even a future WFL function/node, *wink *wink). I guess I was wrong! Thank you so much for the coding starting point and suggestion, this is definitely the help I needed to know where to begin!