Node red service node not using msg.data

Can someone help me to understand how the msg.data should work when calling service node? It keeps ignoring my data.

I have very simple flow
image

[{"id":"97ceb809.9d0808","type":"api-call-service","z":"82057366.45478","name":"Living room lights","server":"7d80e26.52c471c","service_domain":"light","service":"turn_on","data":"{\"entity_id\":\"light.katto_yla\"}","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":830,"y":2940,"wires":[[]]},{"id":"5f9a5ddd.9bd3e4","type":"function","z":"82057366.45478","name":"Bright","func":"msg.data = {};\n\nmsg.data[\"transition\"]=3;\nmsg.data[\"kelvin\"]=3000;\nmsg.data[\"brightness_pct\"]=100;\nreturn msg;","outputs":1,"noerr":0,"x":590,"y":3020,"wires":[["97ceb809.9d0808","475262c6.41b6fc"]]},{"id":"f467cf74.74b3b","type":"inject","z":"82057366.45478","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":480,"y":2940,"wires":[["5f9a5ddd.9bd3e4"]]},{"id":"475262c6.41b6fc","type":"debug","z":"82057366.45478","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":730,"y":2880,"wires":[]},{"id":"7d80e26.52c471c","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

I set the msg.data in the function like this:

msg.data = {};

msg.data["transition"]=3;
msg.data["kelvin"]=3000;
msg.data["brightness_pct"]=100;
return msg;

And can see it in the debug node. But the light reacts like I would be using just turn_on service with no parameters. If I paste the JSON to the service node data field it works. But I want to randomize e.g. the brightness value.

I tried to serialize the json so that it would be a string but no luck there either.
Gives me this

msg.data = {};

msg.data["transition"]=3;
msg.data["kelvin"]=3000;
msg.data["brightness_pct"]=100;

var myJSON = JSON.stringify(msg.data);
msg.data = myJSON;
return msg;

in the debug:

data: "{"transition":3,"kelvin":3000,"brightness_pct":100}"

It should be msg.payload.data

2 Likes

Makes a good feeling to sometimes have only a small thinking mistake :smiley:

Thanks!
And if anyone is using the same:

msg.payload = {};
msg.payload.data = {};

msg.payload.data["transition"]=3;
msg.payload.data["kelvin"]=3000;
msg.payload.data["brightness_pct"]=20;

return msg;

You can also just use a change node. You don’t have to use a function

I was thinking that it is easier to randomize values in the function node. Or at least not sure how to do it in the service node.