# Turn-on the light of a zone, with Homey Script

**URL:** https://community.homey.app/t/turn-on-the-light-of-a-zone-with-homey-script/101760
**Category:** Flows
**Tags:** flow, homey-pro
**Created:** [January 20, 2024, 1:43pm UTC](https://community.homey.app/t/turn-on-the-light-of-a-zone-with-homey-script/101760 "2024-01-20T13:43:50Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![HenriDO](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/henrido/32/84845_2.png) [@HenriDO](https://community.homey.app/u/HenriDO)
#### Post date: [January 20, 2024, 1:43pm UTC](https://community.homey.app/t/turn-on-the-light-of-a-zone-with-homey-script/101760/1 "2024-01-20T13:43:51Z")

</div>

(I’m starting with Homey Script 😉 )

Can you help me ? This script purpose it’s to turn on, all lights in one (or more zone). I did this as a test… but couldn’t find the issue.

```auto
let zoneName = "Bureau"; 
let devices = await Homey.devices.getDevices();

_.forEach(devices, async (device) => {
    if (device.zoneName === zoneName && device.capabilitiesObj && device.capabilitiesObj.onoff) {
        // Check if the device is a light
        if (device.class === 'light') {
            await Homey.devices.setCapabilityValue({
                deviceId: device.id,
                capabilityId: 'onoff',
                value: true
            });
            console.log(`Turned on light: ${device.name}`);
        }
    }
});

```

Thanks

---

<div class="post-metadata">

### Author: ![Andy\_Henderson](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/andy_henderson/32/80642_2.png) [@Andy\_Henderson](https://community.homey.app/u/Andy_Henderson)
#### Post date: [January 20, 2024, 2:54pm UTC](https://community.homey.app/t/turn-on-the-light-of-a-zone-with-homey-script/101760/2 "2024-01-20T14:54:13Z")

</div>

I don’t think there’s a device.zoneName property - I’m a novice myself so I could be wrong. I use device.id which returns the id of the zone the device belongs to.

I use the following to get an array of ids and names…

```auto
const zones = await Homey.zones.getZones();

```

and then to get the zone name for a given id, I use:

```auto
    
   let zoneName = 'Unknown';
   for (const zone of Object.values(zones)) {
      if(zone.id == device.zone) {
        zoneName = zone.name;
        break;
      }
    }

```

You could do something similar/better to get a zone id from a zone name.

Hope it helps,

Andy

---

<div class="post-metadata">

### Author: ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)
#### Post date: [January 20, 2024, 3:03pm UTC](https://community.homey.app/t/turn-on-the-light-of-a-zone-with-homey-script/101760/3 "2024-01-20T15:03:38Z")

</div>

This works  
Edit: for Pro 2019 that is

```auto
let zoneName = "[1.1] Keuken"; 
let devices = await Homey.devices.getDevices();

_.forEach(devices, async (device) => {
    if (device.zoneName === zoneName && device.class === 'light') {
            await Homey.devices.setCapabilityValue({
                deviceId: device.id,
                capabilityId: 'onoff',
                value: true
            });
    console.log(`Turned on light in zone ${device.zoneName}: ${device.name}`);
    }
});

return true;

```

> ![Screenshot_20240120-155652_Firefox](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/7/7/7703d067b498be7610847350870cefd0fe8af098.jpeg)

.

> [@Andy\_Henderson](#):
>
> I don’t think there’s a device.zoneName property

It works, it gets ‘proposed’ as well

> ![Screenshot_20240120-160655_Firefox](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/8/9/896e426f9b4ed6caf26e75090fc5119522f8fc3f.jpeg)

---

<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: [January 20, 2024, 3:14pm UTC](https://community.homey.app/t/turn-on-the-light-of-a-zone-with-homey-script/101760/4 "2024-01-20T15:14:35Z")

</div>

HP2023 or older Homey?

---

<div class="post-metadata">

### Author: ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)
#### Post date: [January 20, 2024, 3:16pm UTC](https://community.homey.app/t/turn-on-the-light-of-a-zone-with-homey-script/101760/5 "2024-01-20T15:16:43Z")

</div>

Sorry, this is @ Pro 2019

---

<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: [January 20, 2024, 3:19pm UTC](https://community.homey.app/t/turn-on-the-light-of-a-zone-with-homey-script/101760/6 "2024-01-20T15:19:21Z")

</div>

Yeah, with older Homey’s, `device.zoneName` exists, for the new Homey it doesn’t 😭

---

<div class="post-metadata">

### Author: ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)
#### Post date: [January 20, 2024, 3:34pm UTC](https://community.homey.app/t/turn-on-the-light-of-a-zone-with-homey-script/101760/7 "2024-01-20T15:34:09Z")

</div>

My guwdness. Why oh why remove 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: [January 20, 2024, 3:35pm UTC](https://community.homey.app/t/turn-on-the-light-of-a-zone-with-homey-script/101760/8 "2024-01-20T15:35:39Z")

</div>

To piss off developers?

---

<div class="post-metadata">

### Author: ![Colin\_Osborne](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/colin_osborne/32/79809_2.png) [@Colin\_Osborne](https://community.homey.app/u/Colin_Osborne)
#### Post date: [December 8, 2024, 6:14pm UTC](https://community.homey.app/t/turn-on-the-light-of-a-zone-with-homey-script/101760/9 "2024-12-08T18:14:41Z")

</div>

I was trying to do something (a bit different) with zones on a HomeyPro 2023, and bumped-into this thread on my journey, therefore offering a quick snippet of Homey Script on this topic for any similar future travellers. I’m sure it can be improved, but it might at least help point the way.

I chose to specifically iterate Philips Hue lights. I presume that could be skipped (or substituted) to control other types with the same device.class.

```auto
// Define string constants for matching Hue lights
let sMatchOwnerUri = "homey:app:nl.philips.hue"
let sMatchDeviceClass = "light"

// Customise this (or pass in as parameter from flow) for desired zone and on/off value to control
let sMatchZoneName = "Study"
let bOnOffValue = true // true to turn on, false to turn off

// Get Homey devices and zones
const myDevices = await Homey.devices.getDevices();
const zones = await Homey.zones.getZones();

// Get zone ID from zone name
var sMatchZoneId = undefined // Initial value
for (var zoneId in zones){
  if (zones[zoneId].name == sMatchZoneName) {
    sMatchZoneId = zoneId
  }
}
if(sMatchZoneId != undefined) {
  log("Matched zone name:", sMatchZoneName, "to zone ID:", sMatchZoneId)
} else {
  log("Failed to find zone name:", sMatchZoneName)
}
log('\n')

// If zone name successfully found in Homey zones
if (sMatchZoneId != undefined) {

  // Create list of matching devices, to enable on/off control in a separate loop, aiming for less delay between devices
  let ownerZoneDeviceList = []; // Initial value
  log('List of devices with ownerUri ==', sMatchOwnerUri, 'in zone', sMatchZoneName, ':\n')
  for (var iMyDevice in myDevices){
    const device = myDevices[iMyDevice]
    if ( (device.ownerUri == sMatchOwnerUri) && (device.zone == sMatchZoneId) ) {
      if ( device.class == sMatchDeviceClass) {
        ownerZoneDeviceList.push(iMyDevice)
        log(zones[device.zone].name, " : ", device.name);
      }
    }
  };
log('\n')

  // Iterate list of matching devices, and control them as needed
  // (deliberately in separate loop from the search, aiming for less delay between controlling devices)
  for (var iMyDevice of ownerZoneDeviceList) {
    const device = myDevices[iMyDevice]
    await Homey.devices.setCapabilityValue({
        deviceId: device.id,
        capabilityId: 'onoff',
        value: bOnOffValue
    });
    console.log(`Controlled light: ${device.name}, setting onoff state to: ${bOnOffValue}`);
  }
}

```
