Hey all,
I just discovered how to create functions that are available throughout the entire flow, so I’m rebuilding some function-nodes that are using a lot of the same functions. I use node.status() a lot (among other things) to display relevant information. So I thought "wouldn’t it be nice to have setNodeStatus() function in my flow functions. That way i can also use then inside the flow functions.
Partial code from the function-node that “declares” the flow functions. Triggered with an injection-node on start:
var flow_functions = {
setStatusText: function setStatusText(_color, _shape, _text) {
var date = new Date().toLocaleDateString('en-US', {
month: 'short', day: 'numeric', hour12: false,
hour: 'numeric', minute: 'numeric'
}); // snippet grabbed from stackexchange
node.status({fill: _color, shape: _shape, text: statusText });
//return {fill: _color, shape: _shape, text: statusText };
},
turn_on: function turn_on(entity_id) {
if ( this.doSomething(entity_id, "on") ) {
this.setStatusText("green", "dot", `${entity_id} to on`);
} else {
this.setStatusText("green", "dot", `${entity_id} to on`);
}
},
doSomething: function doSomething(entity_id, state) {
if ( /* checks to see if entity_id exists or whatever */ ) {
/* do more stuff */
return true;
} else {
return false;
}
},
}
flow.set("flow_functions",flow_functions);
And then in the actual function-node where I need the functions:
var entity_id = msg.data.entity_id;
var func = flow.get("flow_functions");
if ( func.hasEntityId( entity_id ) ) {
func.turn_on( entity_id ) ;
}
I find this strange behaviour.
But… the node.status appears on the flow where those functions are defined… Don’t understand that… It only works if I reconstruct it like this (and ofc return the data that is needed my node.status() instead of calling node.status()
var entity_id = msg.data.entity_id;
var func = flow.get("flow_functions");
if ( func.hasEntityId( entity_id ) ) {
if ( func.turn_on( entity_id ) ) {
node.status(func.setStatusText("green","dot",`Set ${entity_id} to on`));
} else {
node.status(func.setStatusText("red","ring",`Error setting ${entity_id} to on`));
}
}
Can this be fixed?
And I hope I explained the problem clearly
Regards,
Sjoerd