1.5 KiB
1.5 KiB
🎚️ Check the state of a switch
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.
To check the state of a switch, we can use the Binary Switch CC
API:
const state = await node.commandClasses["Binary Switch"].get();
This returns either
undefined
if the response from the node timed out- or an object with
currentValue
and optionallytargetValue
andduration
properties, which advertise the current state of the switch, which state it is transitioning to and how long that will take.
In this example we're only interested in the current state, which can be done as follows:
if (state != undefined) {
if (state.currentValue) {
console.log("Switch is ON");
} else {
console.log("Switch is OFF");
}
} else {
console.error("no response");
}
Full example:
import { Driver } from "zwave-js";
// ...driver initialization, see Quick Start guide
async function main() {
const node = driver.controller.nodes.getOrThrow(2);
// Get the current state from the node
const state = await node.commandClasses["Binary Switch"].get();
// ...and print it to the console
if (state != undefined) {
if (state.currentValue) {
console.log("Switch is ON");
} else {
console.log("Switch is OFF");
}
} else {
console.error("no response");
}
}