I have been using node-red only for a few days.
I would like to know if we can inject in any node the parameters that we can see on the right in the info tab.
Like for example "file node" I can inject the path in msg.filename.
If this is the case, I would like to inject the deviceId into the ewelink-generic-device node.
I tried several things:
msg.deviceId (authentication error)
msg.payload.deviceId (Device does not exist error)
in params in a function:
msg.payload = {"method": "setDevicePowerState", "params": ["on", "10005cc2b5"]};
return msg;
But none works.
Do you have any idea how I can inject deviceId? If it's possible?
With what I could read in the subject by modifying ewelink-connect.js, I was able to inject deviceId in the node ewelink-generic-device.
I have not tested the others node ewelink.
If it helps someone, modify in .node-red \ node_modules \ node-red-contrib-ewelink \ src \ utils \ ewelink-connect.js this:
initializeDeviceNode(RED, node, config, method, params) {
// Clean up device ID
const deviceId = config.deviceId ? config.deviceId.trim() : '';
// Log in to eWeLink
this.ready(RED, node, config).then(connection => {
// Once logged in we can listen to inputs
node.on('input', (msg) => {
// Get method name and build params
const evaluatedMethod = method || msg.payload.method;
const evaluatedParams = (typeof params === 'function' ? params(msg) : params) || msg.payload.params || [];
// First parameter should be always the device ID
evaluatedParams.unshift(deviceId);
// Call dynamically the method
connection[evaluatedMethod].apply(connection, evaluatedParams).then(result => {
node.send({ payload: result });
}).catch(error => node.error(error));
})
}).catch(error => node.error(error));
},
with this:
initializeDeviceNode(RED, node, config, method, params) {
// Log in to eWeLink
this.ready(RED, node, config).then(connection => {
// Once logged in we can listen to inputs
node.on('input', (msg) => {
// Clean up device ID
const deviceId = msg.deviceId ? msg.deviceId.trim() : '';
// Get method name and build params
const evaluatedMethod = method || msg.payload.method;
const evaluatedParams = (typeof params === 'function' ? params(msg) : params) || msg.payload.params || [];
// First parameter should be always the device ID
evaluatedParams.unshift(deviceId);
// Call dynamically the method
connection[evaluatedMethod].apply(connection, evaluatedParams).then(result => {
node.send({ payload: result });
}).catch(error => node.error(error));
})
}).catch(error => node.error(error));
},
restart node-red of course.
I don't know if it's really very clean, but it works for me.
Edit:Apparently @tyeth must have done something cleaner but I haven't tested it yet: github.com/tyeth....
yes - that is not the recommended way but it will work for you so no problem. Ideally if the user sets it in the edit config it should honour that as they set it for a reason so should not overwrite it. If left blank then yes overwrite it.
@juliendragz your code change is same thing I had pretty much, I then changed it a little when I added tests. Pretty bad at JavaScript myself and some of it's probably unnecessary, but it works and follows the expected config default and overwriting as far as I expected at least