Custom Functions Help

Edit: I did get a helpful response that helped at the git-hub Floorplan community forum. I can’t quite mark it as ‘solved’ as it doesn’t get the same treatment as the example I used, but it is a cleaner solution than what I happened on.

I posted this over at the git-hub Floorplan community forum but thought I’d post it here to maybe get a few more eyes on it. Whichever forum I get a solution from I’ll post it to the other for consistancy.

First time poster, new to HA, loving Floorplan, and comfortable with coding and configuration files; but…

Full Disclosure: Javascript is my coding nemesis.

I’m trying to implement a custom function using copy/paste script from the examples available. I used the light example to get the RGB colour of a light onto a floorplan and it worked marvelously, huge thanks to its author. But I have many lights, and re-using code like that is bad form. I then went to the ring example and copy/pasted/modified it into the yaml file (using UI Raw Editor) and it failed altogether without any log output.

I think the Functions: entry is fine because I got it to work, how I got it to work though… I started trying to see if I could get some kind of output from the custom function using floorplan.text_set:. I did this, service_data: '${JSON.stringify(functions.getRGBColor(entity), null, 10)}'. I did this just to see if it would throw an error before implementing it into floorplan.text_set; shockingly, to me, the function worked as expected and the light behaves as it should in HA Floorplan.

This is the Custom Functions script:

          functions: |
            >
            return {

              getRGBColor: (entity) => {
                var element = [
                  `${entity.entity_id}`,
                  'stop2',
                ];
  
                var color = 'rgb(0, 0, 0)';
  
                var opacity = 0;
  
                if (entity.state === 'on') {
                  if (entity.attributes.color_temp) {
                    var rgb = util.color.miredToRGB(entity.attributes.color_temp);
                    color = `rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})`;
                    opacity = 1;
                  }
                  else if (entity.attributes.rgb_color) {
                    var rgb = entity.attributes.rgb_color;
                    color = `rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})`;
                    opacity = 1;
                  }
                };
  
                var style = `fill: ${color}; stop-color: ${color}; stop-opacity: ${opacity};`;
  
                return {
                  element: element,
                  style: style,
                };

              },
            };

This (taken from the ring example)…

service_data: ${functions.getRGBColor(entity)}

and this…

service_data: '${functions.getRGBColor(entity)}'

do not work. But this does…

service_data: '${JSON.stringify(functions.getRGBColor(entity), null, 10)}'

Like WTF?

I’ll thank you in advance for any and all help, greatly appreciated.