Hubitat Driver Development - Question

I'm making a switch driver.

Basic question, how do I check the switch state? I need to write an if statement, but want to check if it's on or off.

I know under an app, I subscribed to a switch and I can watch for the state changes.

I'm not sure how to do this?

Drivers can't "see" other devices. That's an Apps job.. and you know how unionized these Apps and Drivers are :smiley:

You can see the device you're acting as a driver for, and you can send Zigbee/Zwave/LAN commands to get that status.

You can create a Parent Child relationship and have several of YOUR children provide data to the Parent.

Sorry, I want to see the driver's current switch state.

https://docs.hubitat.com/index.php?title=Device_Object#currentValue

The name of the Attributes can be found:

Screen Shot 2020-06-01 at 2.00.01 PM

But of course, since you're writing the driver, you already know how you are spelling the Attributes. :smiley:

I'm very new to programming. Honestly, I don't know the syntax I need to see the state.

def currstate = device.currentState("switch2").getValue()

Something along that line, perhaps? :slight_smile:

I found it being used in a lot of the Community's driver code.. one example:

@aaron NOAA-Severe-Weather.groovy:

def result = (!modesYes && restrictbySwitch !=null && restrictbySwitch.currentState("switch").value == "on") ? true : false

to give you a better 'flavor' of the usage.

2 Likes

This also works:

restrictbySwitch.currentSwitch

1 Like