Learn to code for Hubitat

@AutomatedThoughts (and @dman2306)

I think what @AutomatedThoughts wants to code is an app, but the example being used is for a driver.

If you are coding an app, you don't need to know any z-wave commands. Take a look at these two examples, especially the second, for a simple app:

2 Likes

@aaiyar, I actually do want to learn the driver side as well and all the zwave/zigbee and whatever else there is to learn here. I know I'm in for some frustration and a steep learning curve. Thanks for the app examples. I went through the building of the app as Adam walked us through in the video, and that worked fine to control the bulb. I want to be able to do it through the driver now. :wink:

1 Like

Ok - can I strongly recommend you start with a simple app, and not a zigbee/z-wave driver. The complexity of the latter is not suited for someone beginning with groovy.

If you're going to do a driver - do a driver for a virtual device.

1 Like

@aaiyar, I understand.... :exploding_head:

1 Like

I'm back with some success from more reading. Using the code below the bulb is turning off correctly but turning it on results in a dimming percentage of 20%. I can rest easy tonight knowing that I will learn this but it isn't easy by any means.


If anyone can help me to understand where that 20% is resulting from I'd appreciate it. I will stop here for now because I want to be able to sleep tonight and my fear is that if I continue I will be up all night.

I think I figured this out as well. It seems it was referring to the 0xFF in the on command which apparently is the factory default brightness setting?
Maybe someone can chime in here.
Thank you for the info and push in the right directions. I know some of you are probably laughing at me but I hope you will be laughing with me eventually.

/ ** A Dimmer **/ 

metadata{
    definition(
        name: "Dimmer type",
        namespace: "development",
        author: "Adam Kempenich",
        importURL: "") {
        
        capability "Actuator"
        capability "Initialize"
        capability "Switch"
        capability "SwitchLevel"
        
    }
}

def initialize(){
    log.debug "Device initialized."
    
    // Do nothing, since nothing needs to be intialized
}

def updated(){
    log.debug "Device update"
    
    initialize()   
}

//*============================================================================================================*//
def parse(String description) {
        def result = null
        def cmd = zwave.parse(description, [0x60: 3])
        if (cmd) {
                result = zwaveEvent(cmd)
                log.debug "Parsed ${cmd} to ${result.inspect()}"
        } else {
                log.debug "Non-parsed event: ${description}"
        }
        result
}
def zwaveEvent(hubitat.zwave.commands.basicv1.BasicReport cmd)
{
        def result = []
        result << createEvent(name:"switch1", value: cmd.value ? "on" : "off")

        // For a multilevel switch, cmd.value can be from 1-99 to represent
        // dimming levels
        result << createEvent(name:"level1", value: cmd.value, unit:"%",
                    descriptionText:"${device.displayName} dimmed ${cmd.value==225 ? 100 : cmd.value}%")

        result
}
def zwaveEvent(hubitat.zwave.commands.switchbinaryv1.SwitchBinaryReport cmd) {
        createEvent(name:"switch1", value: cmd.value ? "on" : "off")
}

def zwaveEvent(hubitat.zwave.commands.switchmultilevelv3.SwitchMultilevelReport cmd) {
        def result = []
        result << createEvent(name:"switch1", value: cmd.value ? "on" : "off")
        result << createEvent(name:"level1", value: cmd.value, unit:"%",
                     descriptionText:"${device.displayName} dimmed ${cmd.value==225 ? 100 : cmd.value}%")
        result
}

def zwaveEvent(hubitat.zwave.Command cmd) {
        createEvent(descriptionText: "${device.displayName}: ${cmd}")
}

def on() {
        delayBetween([
                zwave.basicV1.basicSet(value: 0xFF).format(),
                zwave.basicV1.basicGet().format()
        ], 5000)  // 5 second delay for dimmers that change gradually, can be left out for immediate switches
}

def off() {
        delayBetween([
                zwave.basicV1.basicSet(value: 0x00).format(),
                zwave.basicV1.basicGet().format()
        ], 5000)  // 5 second delay for dimmers that change gradually, can be left out for immediate switches
}
//*===============================================================================================================*//

def setLevel(level, duration) {
    
    sendEvent(name: "level", value: level.toInteger())
    sendEvent(name: "duration", value: duration.toInteger())
}
def toggle(){
    log.debug "Toggle Called"
    
    if(device.currentValue("switch") == "on") {
        off()
    } else {
        on()
    }

}