Change Data on a Service Node?

I want to be able to change a light bulb’s properties by passing a payload - eg by injecting a payload “blue” to the bulb. This doesn’t work:

[{"id":"c276bd38.0e4d4","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"decf1b33.2c0bb8","type":"api-call-service","z":"c276bd38.0e4d4","name":"Light","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.lohas_bulb2","data":"{\"color_name\": \"{payload}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":520,"y":180,"wires":[[]]},{"id":"2a15bfbc.4f1a78","type":"inject","z":"c276bd38.0e4d4","name":"","topic":"","payload":"\"blue\"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":330,"y":180,"wires":[["decf1b33.2c0bb8"]]}]

What would work, please?
Jim

Try this:

[{"id":"afe15518.86d2a8","type":"api-call-service","z":"cb934bc3.22d968","name":"Light","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.lohas_bulb2","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":460,"y":320,"wires":[[]]},{"id":"aba2955d.9f9d08","type":"inject","z":"cb934bc3.22d968","name":"","props":[{"p":"payload.data.color_name","v":"blue","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":330,"y":320,"wires":[["afe15518.86d2a8"]]}]

Thanks for the reply, Mauricio.

I tried copying and importing the above into a new flow, but whilst I can see in the clipboard “[{“p”:“payload.data.color_name”,“v”:”“blue”",“vt”:“str”}, it doesn’t appear in the nodes properties.

Maybe I’m not importing the JSON code properly?

Jim

What do you mean by that?

Try copying using the button on the far right and then import again.

Thanks.
I copied it with the RH button, imported it and then exported it. Here’s the exported JSON:

[{"id":"6aeda473.8d9d94","type":"api-call-service","z":"4831cf44.00ca9","name":"Light","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.lohas_bulb2","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":450,"y":220,"wires":[[]]}]

Jim

Which version of Node-RED are you running?

6.3.0 but I now see that 7.0.0 is available. I’ll update it now and give it another try.
Jim

A bit nearer, but it’s still not working.
There’s msg.payload.data.color_name=“blue” in the injection node, but in the service node there doesn’t appear to be a ‘place holder’ for it in the Data field. I’ve fiddled around with various configurations of curly brackets and msg.payload, to no effect.
Thanks for staying with me Mauricio.
Jim

For some reason there were too many quotes around the string blue in the code. It’s fixed now. You can import it again and it should work fine.

[{"id":"afe15518.86d2a8","type":"api-call-service","z":"cb934bc3.22d968","name":"Light","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.lohas_bulb2","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":460,"y":320,"wires":[[]]},{"id":"aba2955d.9f9d08","type":"inject","z":"cb934bc3.22d968","name":"","props":[{"p":"payload.data.color_name","v":"blue","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":330,"y":320,"wires":[["afe15518.86d2a8"]]}]

Works a treat Mauricio - thanks!

It looks simple now you’ve show it. I can also add other msg.payload items controlling other light parameters.

I’ve programmed quite a bit in ‘C’ and ‘Perl’ in the past, but am finding JSON and YAML a bit opaque. Maybe it’s an ‘age thing’ as I’m 80 years old and the grey matter isn’t quite so agile and receptive to new ideas!

Again - many thanks.
Jim

Further to the above:

Is it possible to pass more than one light parameter at the same time - eg ‘msg.payload.data.color_name=blue’ and (say) ‘msg.payload.data.brightness=100’, or does this have to be made with more than one call?
Jim

I should have stated that I want to do it from nodes that have one msg.payload field . I can do it with an inject node because it allows several msg.payload fields eg:

msg.payload.data.color_name = red
msg.payload.data.brightness = 100

But other nodes don’t appear to have this facility
Jim

Does this help?

[{"id":"a8be518.07fe3b","type":"api-call-service","z":"d0505e55.8a4b8","name":"Light","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.lohas_bulb2","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":670,"y":540,"wires":[[]]},{"id":"46a019f2.d29c88","type":"template","z":"d0505e55.8a4b8","name":"","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"{\n  \"data\": {\n      \"color_name\": \"blue\",\n      \"brightness\": 100\n  }\n}","output":"json","x":530,"y":540,"wires":[["a8be518.07fe3b"]]},{"id":"c392f231.f9968","type":"inject","z":"d0505e55.8a4b8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":380,"y":540,"wires":[["46a019f2.d29c88"]]}]
1 Like

I certainly does Mauricio - it works perfectly!

Many thanks - I owe you a favour.
Jim