# Contrib-ewelink 1.0.2, how inject deviceId

**URL:** <https://discourse.nodered.org/t/contrib-ewelink-1-0-2-how-inject-deviceid/29948>\
**Category:** Developing Nodes\
**Created:** [12 July 2020 12:31 UTC](https://discourse.nodered.org/t/contrib-ewelink-1-0-2-how-inject-deviceid/29948 "2020-07-12T12:31:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliendragz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juliendragz/32/38764_2.png) [@juliendragz](https://discourse.nodered.org/u/juliendragz)\
**Post date:** [12 July 2020 12:31 UTC](https://discourse.nodered.org/t/contrib-ewelink-1-0-2-how-inject-deviceid/29948/1 "2020-07-12T12:31:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [12 July 2020 13:51 UTC](https://discourse.nodered.org/t/contrib-ewelink-1-0-2-how-inject-deviceid/29948/2 "2020-07-12T13:51:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![juliendragz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juliendragz/32/38764_2.png) [@juliendragz](https://discourse.nodered.org/u/juliendragz)\
**Post date:** [12 July 2020 14:14 UTC](https://discourse.nodered.org/t/contrib-ewelink-1-0-2-how-inject-deviceid/29948/3 "2020-07-12T14:14:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![juliendragz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juliendragz/32/38764_2.png) [@juliendragz](https://discourse.nodered.org/u/juliendragz)\
**Post date:** [12 July 2020 15:16 UTC](https://discourse.nodered.org/t/contrib-ewelink-1-0-2-how-inject-deviceid/29948/4 "2020-07-12T15:16:08Z")

</div>

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:

```auto
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:

```auto
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....](https://github.com/tyeth/node-red-contrib-ewelink/commit/ca7f581be6258aa5091c909b34ae37a11036149f)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [12 July 2020 16:59 UTC](https://discourse.nodered.org/t/contrib-ewelink-1-0-2-how-inject-deviceid/29948/5 "2020-07-12T16:59:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tyeth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tyeth/32/23442_2.png) [@tyeth](https://discourse.nodered.org/u/tyeth)\
**Post date:** [13 July 2020 01:47 UTC](https://discourse.nodered.org/t/contrib-ewelink-1-0-2-how-inject-deviceid/29948/6 "2020-07-13T01:47:22Z")

</div>

@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 😀

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [11 September 2020 01:47 UTC](https://discourse.nodered.org/t/contrib-ewelink-1-0-2-how-inject-deviceid/29948/7 "2020-09-11T01:47:58Z")

</div>

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