Extract rgb value from payload

Good evening, I would like to extract the RGB values from the message below, and use them to change rgb in css

data: object
entity_id: "light.bec"
state: "on"
attributes: object
supported_color_modes: array[2]
color_mode: "hs"
brightness: 10
hs_color: array[2]
rgb_color: array[3]
0: 255
1: 255
2: 36
xy_color: array[2]
friendly_name: "Bec "
supported_features: 0

Providing this is the object in payload

In a function node.

const RGBValues = msg.payload.rgb_color
const color = `rgb(${RGBValues[0]},${RGBValues[1]},${RGBValues[2]})`
return {payload:color}

returns "rgb(255,255,36)"

various ways - but this uses the function node

Thanks this is my flow

 [{"id":"03a1773829ea545a","type":"api-current-state","z":"0b3973a44a088610","name":"","server":"9976ac94.2c258","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.bec_camera_de_zi","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":370,"y":180,"wires":[["c13cdba89f43e854"]]},{"id":"9976ac94.2c258","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

this is the error TypeError: Cannot read properties of undefined (reading '0')

Ok,

Can you paste the exact payload you have provided above using the debug panel to copy it?
copy the whole payload object

Dec-23-2022 13-05-26

Looks like the values are not in msg.payload.rgb_color but msg.payload.data,rgb_color or maybe msg.data.rgb_color. It would have been better to show us the whole message!

And your posted "flow" seems to be just two nodes, "api-current-state" and "server", neither of which are core Node-red nodes.

1 Like

payload

{"_msgid":"e47242b7bfea685a","payload":"on","topic":"","data":{"entity_id":"light.bec_camera_de_zi","state":"on","attributes":{"supported_color_modes":["brightness","hs"],"color_mode":"hs","brightness":10,"hs_color":[60,85.7],"rgb_color":[255,255,36],"xy_color":[0.441,0.511],"friendly_name":"Bec camera de zi","supported_features":0},"last_changed":"2022-12-23T13:33:40.943Z","last_updated":"2022-12-23T13:33:40.943Z","context":{"id":"01GMZK0YZ6NJ87V7F14VE7DXW9","parent_id":null,"user_id":"1b5064a06dfd4f7ea3007839d7e15372"},"timeSinceChangedMs":5522}}

{"_msgid":"e47242b7bfea685a","payload":"on","topic":"","data":{"entity_id":"light.bec_camera_de_zi","state":"on","attributes":{"supported_color_modes":["brightness","hs"],"color_mode":"hs","brightness":10,"hs_color":[60,85.7],"rgb_color":[255,255,36],"xy_color":[0.441,0.511],"friendly_name":"Bec camera de zi","supported_features":0},"last_changed":"2022-12-23T13:33:40.943Z","last_updated":"2022-12-23T13:33:40.943Z","context":{"id":"01GMZK0YZ6NJ87V7F14VE7DXW9","parent_id":null,"user_id":"1b5064a06dfd4f7ea3007839d7e15372"},"timeSinceChangedMs":5522}}

So yes! thats quite different.

const RGBValues = msg.data.attributes.rgb_color
const color = `rgb(${RGBValues[0]},${RGBValues[1]},${RGBValues[2]})`
return {payload:color}

My mistake, really now I got what I wanted, thank you, happy holidays.

1 Like

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