Contrib-ewelink 1.0.2, how inject deviceId

hello,

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?

It looks loke someone else has opened an issue on the github page for the node about this same thing: https://github.com/ottoszika/node-red-contrib-ewelink/issues/95

You might want to add your name to it too so the author will see several people wanting this functionality.

Thank you, I had not found this topic. I will go see.
Which also answers my first question, we cannot put the parameters on all nodes ...

thanks

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....

1 Like

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 :grinning:

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