Replies: 3 comments 9 replies
-
@exetico ?? |
Beta Was this translation helpful? Give feedback.
-
Hi, I almost forgot this, but I'm trying to clear out old discussion entries in my mailbox. :) First: Thank you for raising a question, where I'd actually have a proper example, even though it's a more advance function! It looks like you've already touched the Usage > Custom Functions part of our docs. I noticed you're talking about Next: Personally I use functions as part of one key, like this (dummy example in my notes):
So I've extended your example, with lots of other things, just so I had something to work with, and test if I was wrong or not, in some, or all of the cases. config:
console_log_level: info
defaults:
hover_action: hover-info
tap_action: more-info
image: /local/floorplan/floorplan1.svg?v=1.1.14
log_level: info
functions: |
>
return {
getRGBColor: (entity) => {
try{
const element = [
'sensor.bob1test',
];
let color = 'rgb(0, 0, 0)';
let opacity = 0;
if (entity.state === 'on') {
let rgb = null;
if("color_temp" in entity.attributes) rgb = entity.attributes.color_temp;
if("rgb_color" in entity.attributes) rgb = entity.attributes.rgb_color;
if(rgb){
color = `rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})`;
opacity = 1;
}
};
const style = `fill: ${color}; stop-color: ${color}; stop-opacity: ${opacity};`;
console.info("Text in return:", style);
console.info("Element ref. in return:", element);
console.log("config", config);
console.log("element", element);
return {
element: element,
text: style,
};
}
catch(e){
console.info("Error in custom function", e);
return {
element, text: ''
};
}
},
getSimple: (entity) => {
return entity.state === 'on' ? {
element: 'sensor.bob1test', text: 'bob dummy val text ON'
} : {element: 'sensor.bob1test', text: 'bob dummy val text OFF'};
},
getCrazySimple: (entity) => {
return 'bob ok?';
},
}
rules:
- element: sensor.bob1test # So I have something to toggle the state..
entity: light.ledstrip1_light
state_action:
action: call-service
service: floorplan.text_set
#service_data: ${JSON.stringify(functions.getRGBColor(entity), null, 10)}
service_data: ${JSON.stringify(functions.getRGBColor(entity))}
#service_data: ${functions.getRGBColor(entity), null, 20}
#service_data: ${JSON.stringify(functions.getSimple(entity), null, 30)}
#service_data:
# element: 'sensor.bob1test'
# # I know this will render the two keys returned by the dummy function, but that's just for my testing
# #text: '${JSON.stringify(functions.getCrazySimple(entity), null, 10)}'
# #text: '${functions.getCrazySimple(entity), null, 10}'
# #text: '${functions.getCrazySimple(entity)}'
# #text: ${functions.getCrazySimple(entity), null, 10}
# #text: ${functions.getCrazySimple(entity)}
# # This is how I normally use a function, simplified ofc.
# #text: |
# # >
# # const textValue = functions.getCrazySimple(entity)
# # return textValue;
##service_data: 'Nothing...'
tap_action: toggle
stylesheet: /local/floorplan/floorplan1_main.css?v=1.1.14
full_height: true
type: custom:floorplan-card I'm pretty sure it's related to how we break down the value, and how we evaluates it. As you've pushed the function into a string, it becomes a static property, instead of live-functions (i can't bring up the proper term right now). A bit odd, but, a great workaround for your case. I think it's simple as that. The step where your custom function are executed, are closer to the sun, and does have the proper things exposed. 🌞 I'd like to break it down at some point. Just to see where in the steps, you're actually making things even better. To be clear, you're doing a workaround, which we've not really though of. But, I'd like things to work, as you expect it to work, without stringifying it. Please open a feature request, with a reference to this discussion. I'd like to both check out: the source code, why it's "actually working", and possibly, see if I can implement something to smooth this out. Thank you for bringing this to the table 😄. |
Beta Was this translation helpful? Give feedback.
-
Would it be possible to use the lib-structure mentioned here for user-defined functions (UDFs)? I don’t know how the HACS installer works and what gets overwritten on updates, but maybe exclude anything that starts with |
Beta Was this translation helpful? Give feedback.
-
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 usingfloorplan.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 intofloorplan.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:
This (taken from the ring example)...
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.
Beta Was this translation helpful? Give feedback.
All reactions