Variables + Append Values in function node payload

I am setting up controls for Osram bulb and setting reusable flow variables in a system values node i.e. for my Osram dimmer bulb:
flow.set("OsramBulbDimmer", {"cluster_id":"0x0008", "endpoint_id": "3", "command_id": "0x00", "parent_command": "zcl level-control", });

In the function node I am trying use an entry like below where the dimmer params are appended to the flow variable in the payload (dimmerlevel is a numeric value already set), but I get get formatting errors every which way I try to spin it.
msg.payload = {flow.get("OsramBulbDimmer") , "params":{level: dimmerlevel, "transition time": 10}};

This way kind of works, but it creates a new object in the payload for the variable name "bulbval" and the bulb rejects the command.
var bulbval = flow.get("OsramBulbDimmer");
msg.payload = {bulbval, "params": {level: dimmerlevel,"transition time": 10}};

For comparison, if I set the entire payload in the function node like this then it works:
msg.payload = {
"cluster_id":"0x0008",
"endpoint_id": "3",
"command_id": "0x00",
"parent_command": "zcl level-control",
"params":{level: dimmerlevel,"transition time": 10}},

@notnice, maybe you could go this way:

var obd = flow.get("OsramBulbDimmer");

msg.payload = {
    "cluster_id":obd["cluster_id"],
    "endpoint_id": obd["endpoint_id"],
    "command_id": obd["command_id"],
    "parent_command": "zcl level-control",
    "params":{level: dimmerlevel,"transition time": 10}
}

return msg;

Hi Damians - thx for the reply. That will probably work as its very close to my working scenario where all the commands are individually specified in the function node. What I'm trying to do is get away from doing that and make those common commands flow level variable so I can re-use in other nodes. In my mind thats a neater solution.

Yes, the solution I suggested needs the attribute names to be hardcoded.
Not very pretty.

Try it like this:

var bulbval = flow.get("OsramBulbDimmer");
msg.payload = bulbval
msg.payload.params = {level: dimmerlevel,"transition time": 10}
return msg
1 Like

Cool - thats works and means I can use that as a template in other nodes to cut down on referencing the same commands in every node :slight_smile:

it could be even shorter

msg.payload = flow.get("OsramBulbDimmer")
msg.payload.params = {level: dimmerlevel,"transition time": 10}
return msg

Saving 1 line in every node is even better - thx :+1:

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