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

(I’m starting with Homey Script :wink: )

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.

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

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…

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

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

    
   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

This works
Edit: for Pro 2019 that is

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;

.

It works, it gets ‘proposed’ as well

Screenshot_20240120-160655_Firefox

HP2023 or older Homey?

Sorry, this is @ Pro 2019

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

2 Likes

My guwdness. Why oh why remove it :no_mouth:

To piss off developers?

1 Like

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.

// 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}`);
  }
}