Combining node messages - Node Red

:bulb:
ahkay ... then feed that into a function from there then? two - two functions?

That or do it in change nodes instead of a function node. But either way works fine.

If the function node is already close to working, probably easier to just use it.

The function node just announces the single action and I'd have no idea how to define and if/then in a function. I've never looked at that before. (json for dummies)

which would you choose? Function or change node?

Probably a switch node that checks for msg.payloada.data=null + an "otherwise branch".

Each path going to its own function node, one for manual events one for codes.

Switch:

1 Like

I'll tackle this and look for a critique in a bit.

Could try...(in a function node)

if (msg.payload.data === null) {

  • Do stuff for manual
    } else {
  • Do code-specific stuff
    }
1 Like

Yup, that would definitely work too.

Ok so, I've got it working. There's one thing that I don't like and maybe you can tell me how to extract that information from the output but here's the flow as it stands. It's an all in one solution and works with all 3 locks connected to the flow.

function for null
image

Function for Code
image

But I don't like the output for code. It announces the physical state:
image

Is there any way to define the data but only the name?
image

A bit more...

var LockText = msg.payload.displayName;
var LockState = msg.payload.value;
var LockType = "";
var UnlockedBy = "";

if (msg.payload.data === null) {
LockType = "manually";
} else {
LockType = "with code by";
if (msg.payload.data.includes("####")) { <-- Not sure if this will work, would need one for each code
UnlockedBy = "User matching #### Code";
}
}

var OutputText = LockText + " " + LockState + " " + LockType + " " + UnlockedBy;

return {
"payload": {
"type": "speak",
"payload": {
"type": "announcement",
"volume": 70,
"devices": ["Downstairs", "Upstairs"],
"text": OutputText
}
}
};

Not so sure about that one.....

without the switches then?

Just need to parse out the name from the JSON object. I haven't worked with enough to come up with an elegant solution although I'm sure it's likely quite simple for some. I was trying to cheat and just look for specific codes. Much better to pull the Name from the data.

There are probably a dozen ways to do it depending if you prefer to write code or use nodes. No right/wrong answer.

To answer your question is it quite easy to get rid of [physical] or use the data value directly.

I mean, I'd like to get rid of [physical] at the end, but is it hurting anything? no. I'm just being picky.

You could cheat once again....
Var DescriptionText = msg.payload.descriptionText.split("[");

and just change your "text": to...
"text": DescriptionText[0]

can i add that into the existing function that I've created?

image

Yes, just place it before your return line (Line 1)

getting an error
expected an assignment or function call and instead saw an expression
missing "," before statement

image

Change the Var to var.....

Error gone ... i'll go twist some locks