How Do You Read Msg.Config Values?

For some reason, I cannot post photos.

I’m trying to get a value from a button to a call service node.



I see that the parameter carries to the end node if I put it in the config section. Here’s the input to the Call Service node.

{"config":{"brightness":255,"rgb_color":[0,255,0]},"parts":{"id":"145cd0c99b907ea0","count":2,"index":1},"payload":{"entity_id":"light.eo_spot_2","state":"on","attributes":{"min_color_temp_kelvin":2000,"max_color_temp_kelvin":6535,"min_mireds":153,"max_mireds":500,"effect_list":["Solid","Wake up","Cycle up","Cycle down","Random"],"supported_color_modes":["color_temp","hs"],"color_mode":"color_temp","brightness":255,"color_temp_kelvin":2000,"color_temp":500,"hs_color":[30.601,94.547],"rgb_color":[255,136,13],"xy_color":[0.599,0.382],"effect":"Solid","friendly_name":"eo_110","supported_features":36},"context":{"id":"01HETPP9B1PGW6K0K1Y0YSEMT8","parent_id":null,"user_id":"402dabf9b4b7402faeabf019a46c9feb"},"last_changed":"2023-11-09T18:08:15.488Z","last_updated":"2023-11-09T18:49:40.021Z","timeSinceChangedMs":6339624},"_msgid":"9c53d1c909dcb71d"}

But how do I get the value back out? I can’t seem to read.

Expected ":", got "}"

Forgot the export of what I had.

[{"id":"ef92b9b3c635b667","type":"ha-button","z":"b65fc9f854534402","name":"btnGreen","version":0,"debugenabled":false,"outputs":1,"entityConfig":"3ee0082c08fd5924","outputProperties":[{"property":"config","propertyType":"msg","value":"{\t    \"brightness\": 255,\t    \"rgb_color\": [\t    0,\t    255,\t    0]\t    }\t","valueType":"jsonata"}],"x":140,"y":1280,"wires":[["d84cd99c88a91531"]]},{"id":"d84cd99c88a91531","type":"ha-get-entities","z":"b65fc9f854534402","name":"","server":"2d0c7ae266ba6fcd","version":1,"rules":[{"property":"entity_id","logic":"starts_with","value":"light","valueType":"str"},{"property":"state","logic":"is","value":"on","valueType":"str"}],"outputType":"split","outputEmptyResults":false,"outputLocationType":"msg","outputLocation":"payload","outputResultsCount":1,"x":330,"y":1280,"wires":[["2d166874516268fa","f3d0672cac05c194"]]},{"id":"f3d0672cac05c194","type":"debug","z":"b65fc9f854534402","name":"debug 6","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":680,"y":1220,"wires":[]},{"id":"2d166874516268fa","type":"api-call-service","z":"b65fc9f854534402","name":"","server":"2d0c7ae266ba6fcd","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["{{payload.entity_id}}"],"data":"{{config}}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":510,"y":1280,"wires":[["f3d0672cac05c194"]]},{"id":"3ee0082c08fd5924","type":"ha-entity-config","server":"2d0c7ae266ba6fcd","deviceConfig":"","name":"btnGreen","version":"6","entityType":"button","haConfig":[{"property":"name","value":"SetToGreen"},{"property":"icon","value":"mdi:cash"},{"property":"entity_picture","value":""},{"property":"entity_category","value":""},{"property":"device_class","value":""}],"resend":false,"debugEnabled":false},{"id":"2d0c7ae266ba6fcd","type":"server","name":"homeAssistant","version":5,"addon":false,"rejectUnauthorizedCerts":false,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":true,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

You’re trying to use mustache templates when you have the data field type set to JSONata. Mustache templates only work when it’s set to JSON. I would recommend keeping the data type set to JSONata but use JSONata expressions.

{
  "brightness": config.brightness,
  "rgb_color": config.rgb_color
}
1 Like

Man, I thought you had to prefix a $ to reference variables in JSONata. I wasted hours trying stuff like this.

{
  "brightness": $config.brightness,
  "rgb_color": $config.rgb_color
}

There are a lot of examples for maintaining state at the global and flow level, but I could not find anything for collecting params on a message in transit. Is this how people normally do it, adding leaves like config to the payload?

In Node-RED the message is both a trigger for getting a node to ‘do stuff’ and the carrier of ‘data’. Convention dictates the use of msg.payload as the primary input / output data field and the only part of the message modified by the node, however many nodes also use other fields. For example, we use msg.topic often, and the timer and queue node will accept msg.reset as a trigger for resetting.

There appears to be no limit to what can be added to a message. You can make msg.payload an object or add other fields to msg. Payload is usually just a primative data type, so it has to be set as an object first in order to add nested fields, but the message is an object to start with, so easy to use a change node to simply add msg.anything.

Providing a node that the message passes through does not itself either expect to read or write a particular field, that data will pass through. This can be a great way to build up a complex data object, although there are downsides as the message becomes very large and this can result in memory leakage.

The things to watch out for are nodes like the split node that adds msg.parts to the message (so this needs to be kept for later use by ‘join’), and nodes like many of the home assistant nodes that optionally merge, or block, the incoming message.

If you start to enter a field name into the change node, you should see a dropdown auto-complete list, based on what you are typing. This is a full list of the msg.fields that your current pallet knows about, and it even includes the names of the specific nodes that use the field. I use this to just check that whatever I am adding to the message is not ‘reserved’ or used by anything else.

The change node is a great node for also moving fields around (ie changing the name) and for deleting unwanted fields if the message gets too big.

I have several successful flows where I collect large data objects in various msg.fields as the flow progresses, node by node. It really is more a case of keeping tabs on what you have at each point! And, of course, using the debug node in ‘complete message’ mode rather than just looking at the ‘payload’.

1 Like