Toggle Room On or Off

Is there a way to toggle all of the lights on or off in a room with one flow card instead of if/then logic or groups?

I ran into the same thing when I started. The simplest way I found was to group the lights for the room first, then use that one group in a flow, rather than adding each light separately. It keeps the flow much tidier, and you can still turn one light on or off by itself when you need to.

HAve a look at the app from Jamie Peak < groep > for advanced groups etc.

Ummmm:

If you have defined your room as a Zone there is a card that enables you to turn all devices defined as type Lamp on or off:

In this example ‘Slaapkamer’ is a zone.

Type is ‘Alle lampen’ which translates to all lamps present in the room.

A flow like this toggles the lights in a zone:

You’ll find these flowcards by adding an ‘And’ or ‘Then’ card, then browse to the main zone (Home):

and then browse to the preferred zone:

I created a Homeyscript to do it so I don’t have to rely on groups. It toggles the lights on or off and sets the brightness and color temperature to a specific level based on variables

Any additions or comments to improve it would be very welcome

const roomName = String(args[0] ?? '').trim();
if (!roomName) {
  throw new Error('Pass a Homey zone name, such as Dining Room.');
}

const [zones, devices] = await Promise.all([
  Homey.zones.getZones(),
  Homey.devices.getDevices(),
]);

const matchingZones = Object.values(zones).filter(
  zone => zone.name.toLowerCase() === roomName.toLowerCase()
);

if (matchingZones.length === 0) {
  throw new Error(`No Homey zone named "${roomName}".`);
}
if (matchingZones.length > 1) {
  throw new Error(`More than one Homey zone is named "${roomName}".`);
}

const [zone] = matchingZones;
const lights = Object.values(devices).filter(device =>
  device.zone === zone.id &&
  (device.class === 'light' || device.virtualClass === 'light') &&
  device.available !== false &&
  typeof device.capabilitiesObj?.onoff?.value === 'boolean'
);

if (lights.length === 0) {
  throw new Error(`No available lights with a known on/off state in "${zone.name}".`);
}

const onCount = lights.filter(light => light.capabilitiesObj.onoff.value).length;
const turnOn = onCount * 2 < lights.length;

log(`${zone.name}: ${onCount}/${lights.length} on. Setting room ${turnOn ? 'ON' : 'OFF'}.`);

function canSetBrightness(light) {
  const driver = light.driverId ?? '';
  const app = light.ownerUri ?? '';
  const isShelly = /shelly/i.test(driver) || /shelly/i.test(app) || /^shelly\b/i.test(light.name);
  return !isShelly && light.capabilities.includes('dim');
}

function readPercent(setting, name) {
  const value = setting?.value;
  if (Number.isFinite(value) && value >= 0 && value <= 100) return value / 100;
  log(`Warning: ${name} must be a number from 0 to 100.`);
}

let temperature;
let brightness;
const hasTemperatureLights = lights.some(light => light.capabilities.includes('light_temperature'));
const hasBrightnessLights = lights.some(canSetBrightness);
if (turnOn && (hasTemperatureLights || hasBrightnessLights)) {
  try {
    const variables = Object.values(await Homey.logic.getVariables());
    if (hasTemperatureLights) {
      const color = variables.find(variable => variable.name === 'LightColor')
        ?? variables.find(variable => variable.name === 'ColorofLights');
      temperature = readPercent(color, 'LightColor');
    }
    if (hasBrightnessLights) {
      const dim = variables.find(variable => variable.name === 'LightBrightness');
      brightness = readPercent(dim, 'LightBrightness');
    }
  } catch (error) {
    log(`Warning: Could not read light settings: ${error.message ?? error}`);
  }
}

for (const light of lights) {
  if (light.capabilitiesObj.onoff.value !== turnOn) {
    try {
      await light.setCapabilityValue('onoff', turnOn);
    } catch (error) {
      log(`Warning: Could not switch ${light.name}: ${error.message ?? error}`);
      continue;
    }
  }

  if (brightness !== undefined && canSetBrightness(light)) {
    try {
      await light.setCapabilityValue('dim', brightness);
    } catch (error) {
      log(`Warning: Could not set brightness for ${light.name}: ${error.message ?? error}`);
    }
  }

  if (temperature === undefined || !light.capabilities.includes('light_temperature')) continue;

  try {
    if (light.capabilities.includes('light_mode') && light.capabilitiesObj.light_mode?.value === 'color') {
      await light.setCapabilityValue('light_mode', 'temperature');
    }
    await light.setCapabilityValue('light_temperature', temperature);
  } catch (error) {
    log(`Warning: Could not set color temperature for ${light.name}: ${error.message ?? error}`);
  }
}

return true;

Hi,

Please format the code part as code.

Howto:
https://community.homey.app/t/tip-please-use-preformatted-text-for-logs-and-code-blocks-for-cleaner-posts/149488?u=peter_kawa