Help with how to parse this message

This is the message:

rgb,3,0,20,0

And I want to switch the message depending on the (in this case) 3.
It can be 1, 2 or 3.

Thanks in advance.

You could use a function node and use the split function then set the payload to element [1]

Or use a switch node and test the payload contains rgb,1, or rgb,2, etc

errrr... I have never heard of that idea.

Is it ....

(After you edit)

Yeah. That would work.

Next part - sorry I didin't include it in the question:
How to get the last part:
(eg)0,20,0

I am wanting to send that to a node and use that to set the colour.
So I will also need to be told how to do that.

I think it is #(numbers) but am not too sure.

You really should say in you post what you need (it changes the solution)

So, I would, in a function node, call .split() to break up that string by comma, pop the first element off (lose the rgb part) then pop the next element (get the 1,2,3 part). The remaining 3 elements are the bit you are after - you can then call .join() to put these back together and send as a payload.

Give it a go (search the net for js string split, is array pop and js string join for clues)

I'll be back at a computer in 10 mins to help out if need be.

1 Like

Thanks. Shall try.

I tried the element[1] thing, but it didn't like me. Yeah, I probably made a mistake.

Thanks.

I'll start work on it.

You might also want to look into regex.

Where is that coming from? Perhaps it could be converted to a js object earlier in the flow.

Oooh. I wasn't heading down that rabbit hole.

Only joking Andrew - I couldn't resist. No offence intended.

The message is coming from a device and it is used to .......

Hey, yeah! Silly me. Earlier on in the flow it has names.....

Yeah, that may work.

(D'oh!)

No problems. None taken.

I deserve it in ways.

I have a whole lot of things on my plate and I probably shouldn't keep putting more things on until I get the remainder resolved.

Alas life doesn't always work like that.

All's well that ends well Andrew. :+1:


In case you were curious (or want to keep this as a future snippet for when you dont have control over the incoming data format)...

var spl = msg.payload.split(",");
var rgbPart = spl[0];   //get the   rgb   part  (element 0)
var idPart = spl[1];    //get the   id    part  (element 1)
var rgb = spl.slice(2); //get  remaining  parts (elements 2 ~ the end)
var rgbStr = rgb.join(","); // join these last three parts by comma back to a string

//set the payload as required - example...
msg.payload = {
  id: idPart,
  rgb: rgb,
  rgbStr: rgbStr
}
return msg;

image

Hang on! Something is not right.

Argh! Silly me.

Ok, I got confused. (as per normal.)
(I think it is getting worse)

This is an example incoming message:
{"topic":"POWER/","payload":"REQUEST","qos":0,"retain":false,"_msgid":"f1d1ac73.f1322","device":"TABBYCAT"}

payload and device change.

I put that through a change node and set message variables.......

Argh! (again: silly me.) I forgot to include the change node in the code when testing.

Hang on.

(Anyone got a spare brain?)

Great.

So this is the message:

{"topic":"POWER/","payload":{"id":"4","rgb":["200","0","0"],"rgbStr":"200,0,0"},"qos":0,"retain":false,"_msgid":"4f32902a.d7c2a","device":"TABBYCAT","REQUEST":"200,0,0","DETECTED":"0,20,0","FULL":"0,255,0","REMOVED":"0,0,0","led":4}

So I need to send msg.rgbStr into a node and use that to set the colour.

So all I need to do in said node is set the color to msg.rgbStr and then it changes colour to match the message - yes?

Afraid I have no idea what lies beyond that bit of code - but I would if anything it'd be msg.payload.rgbStr not msg.rgbStr

1 Like

Oh. Yeah. Oops for me.

I'll deal with the next part. I just wanted to check I am (at this point) on the correct page.

1 Like

So when I am sending the colour to a node....

The code is (eg:)

payload:'<font color = "red"> <i class="fa fa-bullseye fa-2x" ></i></font>'

Ok. So I want to change the color to what the rgbStr part is.

I see I can't just do:

payload:'<font color = ' + rgbStr + '<i calss="fa fa-bullseye fa-2x" ></i></font>',

Although that is sent correctly the node doesn't understand that.

I need to qualify the rgbStr as a rgb code.

That's with a # in front of it - yes?
Oh, and get rid of the ,. Done. Doesn't seem to work.

I'm missing an elephant somewhere.

This is what I am sending:
{"payload":"<font color = #20000<i calss=\"fa fa-bullseye fa-2x\" ></i></font>","device":"TABBYCAT","_msgid":"149286cd.433c19"}

That's with a # in front of it - yes?

only if it is a hex color.

payload:'<font color = ' + rgbStr + '<i calss="fa fa-bullseye fa-2x" ></i></font>',

That is incorrectly formatted html and you should send strings as strings.

{"payload":"<font color='rgb("+rgbStr+")'><i class='fa fa-bullseye fa-2x'></i></font>"}

Then you should end up with (if you didnt modify rgbStr):

{"payload":"<font color='rgb(200,0,0)'><i class='fa fa-bullseye fa-2x'></i></font>"}

Thanks @bakman2.

Yes, I really need to learn the structures.

Indulge me.

I copy/pasted the code you gave me.

For clarity the colour I am sending is RGB.
R 20 G 0 B 0.
But I'm guessing it wants the numbers to be 00 and 00. So it is causing problems with the colour I am getting.

Weirdly (I say) it isn't the colour I expect. Granted it is missing numbers.
But it should be some semblance of RED. Yes? No. It is some weird GREEN colour.

me and my stupid ideas to include what is being shown on an LED also on the Dashboard.

So how do I make it format the 0 and 00 for the numbers which are less than 10?

Ah! All I need to do is add the extra digits when I define the values.
(I wonder how the LED is going to deal with this now.)

First step is to determine which color format you want to use:

<font color="rgb(200,0,0)"></font> // rgb format
<font color="argb(200,0,0,0.5)"></font> // argb - alpha channel + rgb
<font color="#ff0000"></font> // hex format
<font color="hsl(89, 43%, 51%)"></font> // hue saturation luminance
<font color="hsla(89, 43%, 51%, 0.6)"></font> // hue saturation luminance + alpha channel

If you have RGB available (20,0,0) you can use that directly.

Ok.

rgb(200,0,0) is good for me.

So I put the , back in and I get 200,0,0 sent and that works.

I say works because (at time of posting) when I send 0,80,0 I don't see GREEN.
It sets it initially at RED, but then stops.

Shall go and check the gremlins haven't messed with the code.

Oh! Those values are actually what is in the rgbStr part of the message.
That's the variable. So it can't be fixed numbers.

But the structure is as show. (well for now with extra 0s.)

Someone shoot me.

Be back shortly.

Right:
This:
{"payload":"<font color='rgb(200,0,0)'><i class='fa fa-bullseye fa-2x'></i></font>","device":"TABBYCAT","_msgid":"917c1fbc.0446"}

I see GREEN (dark).

If I change the message to:

{"payload":"<font color='rgb(200,0,0)'></font><i class='fa fa-bullseye fa-2x'></i>","device":"TABBYCAT","_msgid":"f635e1e7.6e73c"}

The icon is WHITE.

(BTW, how do you get the better format when you post code? I seem to have also forgotten how to do that)