node-zwave-js/docs/examples/change-led-color.md

1.8 KiB

🚦 Change LED Colors

NOTE: The following example builds on the Quick Start guide and the Blink a light with Basic CC example. Make sure you have worked through both before continuing.

Modifying the color of an LED is done through the Color Switch CC API. The set method accepts an object with named color components.

The following code turns an RGB LED red:

await node.commandClasses["Color Switch"].set({
	red: 255,
	green: 0,
	blue: 0,
});

Alternatively, the numeric color components (as defined in the Z-Wave specifications can be used):

await node.commandClasses["Color Switch"].set({
	2: 255, // red
	3: 0, // green
	4: 0, // blue
});

Other color components are accessible through their respective names:

  • warmWhite
  • coldWhite
  • red
  • green
  • blue
  • amber
  • cyan
  • purple
  • index

Here's a complete example to annoy your colleagues by making the LED quickly change bright colors for a few seconds:

// define a set of colors
const colors = [
	{ red: 255, green: 0, blue: 0 },
	{ red: 0, green: 255, blue: 0 },
	{ red: 0, green: 0, blue: 255 },
	{ red: 255, green: 255, blue: 0 },
	{ red: 0, green: 255, blue: 255 },
	{ red: 255, green: 0, blue: 255 },
];

async function main() {
	const node = driver.controller.nodes.getOrThrow(2);

	// loop through the colors twice while waiting 250 ms between each color change
	for (let i = 0; i < colors.length * 2; i++) {
		await node.commandClasses["Color Switch"].set(
			colors[i % colors.length],
		);
		await setTimeout(250);
	}

	// for your own sanity, turn the LED off afterwards
	await node.commandClasses["Color Switch"].set({
		red: 0,
		green: 0,
		blue: 0,
	});
}