# Initiating devices

**URL:** <https://community.homey.app/t/initiating-devices/45923>\
**Category:** Developers\
**Tags:** devices\
**Created:** [April 8, 2021, 8:14am UTC](https://community.homey.app/t/initiating-devices/45923 "2021-04-08T08:14:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jan-Ivar\_Boyum](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jan-ivar_boyum/32/29997_2.png) [@Jan-Ivar\_Boyum](https://community.homey.app/u/Jan-Ivar_Boyum)\
**Post date:** [April 8, 2021, 8:14am UTC](https://community.homey.app/t/initiating-devices/45923/1 "2021-04-08T08:14:00Z")

</div>

I’m brand new to Homey programming. I’m trying to develop an app to control my Wiz light bulbs. The bulbs use WiFi interface. I have encountered a problem I do not fully understand. When I pair the first bulb, it works perfectly. When I add a new device (bulb) I find the bulb with the correct mac (used as id) and ip address. When I return data in pair I get the following message: “[ManagerDrivers] [wizfilament] [1] Error: invalid\_type at Remote Process” and the device that worked gets its ip address overwritten. I can not find anything in the documentation that explains the problem. Can anyone help?

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [April 8, 2021, 8:40am UTC](https://community.homey.app/t/initiating-devices/45923/2 "2021-04-08T08:40:21Z")

</div>

Code or it didn’t happen 😉

---

<div class="post-metadata">

**Author:** ![Jan-Ivar\_Boyum](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jan-ivar_boyum/32/29997_2.png) [@Jan-Ivar\_Boyum](https://community.homey.app/u/Jan-Ivar_Boyum)\
**Post date:** [April 8, 2021, 10:12am UTC](https://community.homey.app/t/initiating-devices/45923/3 "2021-04-08T10:12:09Z")

</div>

The driver code is almost directly from the documentation:

```
async onPairListDevices() {
	let devices = [];
	devices = this.getDevices();
	var funnet = false;
	if (devices.length > 0) {
		for (var i = 0; i < devices.length; i++) {
			devId.push(devices[i].getData().id);
		}
	}

	discover = new Discover();
	var obj = await discover.getIpAndMacAddress(2, devId);
	if (obj != null) {
		console.log("NY!: " + obj.macId + " - " + obj.ipAdr);
		var deviceDescriptor = {
			"name": devName,
			"data": {
				"id": obj.macId,
				"ip": obj.ipAdr,
			},
			capabilities: ["onoff", "dim", "wiz_kelvin", "wiz_scene"]
		};
		obj = null;
		return deviceDescriptor;
	} 
	return null;
}

```

The first device works fine, when I add another device then everthing starts to go wrong.

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [April 8, 2021, 10:29am UTC](https://community.homey.app/t/initiating-devices/45923/4 "2021-04-08T10:29:08Z")

</div>

Do you have the full code online somewhere (for instance on Github)? The code you’re showing seems only half finished, and it’s missing some parts (like what `Discover` is).

Some remarks about specifically this code:

- not sure what `devId` is being used for and how it’s declared
- `devName` is undefined
- `onPairListDevices` should return an array

---

<div class="post-metadata">

**Author:** ![Jan-Ivar\_Boyum](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jan-ivar_boyum/32/29997_2.png) [@Jan-Ivar\_Boyum](https://community.homey.app/u/Jan-Ivar_Boyum)\
**Post date:** [April 8, 2021, 10:54am UTC](https://community.homey.app/t/initiating-devices/45923/5 "2021-04-08T10:54:15Z")

</div>

The code will be on the github, but currently it’s only local. The devName is a global variable (string), the devId is the mac address on the bulb (string). The discover is a javascript (NodeJs) accessing the unoffical API for discovering and getting the bulb settings. I tried to add the device description to a array, but the problem was the same.  
I have updated the github, but it’s privat. I have set you up so you can see it.

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [April 8, 2021, 11:18am UTC](https://community.homey.app/t/initiating-devices/45923/6 "2021-04-08T11:18:02Z")

</div>

Typically, “first device works, subsequent devices fail” point towards variable-reuse, for instance by using global variables that get overwritten. The `Discover` class is using various such global variables (`adress`, `devName`, `success`, `devices`) and the `Driver` too.

You need proper encapsulation of all data that should not be “shared” across different instances of a class.

---

<div class="post-metadata">

**Author:** ![Jan-Ivar\_Boyum](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jan-ivar_boyum/32/29997_2.png) [@Jan-Ivar\_Boyum](https://community.homey.app/u/Jan-Ivar_Boyum)\
**Post date:** [April 8, 2021, 11:36am UTC](https://community.homey.app/t/initiating-devices/45923/7 "2021-04-08T11:36:22Z")

</div>

Thank you, I going to look into it. Is it a good idea to check this in as an app when it’s finished? The API is unofficial and Wiz connect seemed very uninterested when I contacted them about the API and Homey app.

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [April 8, 2021, 11:38am UTC](https://community.homey.app/t/initiating-devices/45923/8 "2021-04-08T11:38:13Z")

</div>

There are plenty of apps that use unofficial API’s, so go ahead and publish it when it works! 👍🏻

---

<div class="post-metadata">

**Author:** ![Jan-Ivar\_Boyum](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jan-ivar_boyum/32/29997_2.png) [@Jan-Ivar\_Boyum](https://community.homey.app/u/Jan-Ivar_Boyum)\
**Post date:** [April 12, 2021, 7:18am UTC](https://community.homey.app/t/initiating-devices/45923/9 "2021-04-12T07:18:03Z")

</div>

The problem is solved. Actually a bit strange, but when I returned the array from onPairListDevices () this way:

var deviceDescriptor = {  
“name”: name,  
“data”: {  
“id”: macAdr,  
“ip”: ipAddr,  
},  
capabilities: [“onoff”, “dim”]  
});

got all devices for the same driver ip the address of the last device that was paired. When I removed the ip address from “data” and entered a “settings” and used the ip address there everything works as normal.
