Learn to code for Hubitat

I don't remember the last time I was on IRC and I started my programming career building IRC server software!

3 Likes

IRC is so old skool. :smiley:

5 Likes

@aaiyar I have been reading and have watched Adam's video as well. I guess I would just like a scenario where we can all work on a project where we set our IDE up the way the leader takes us for the lesson. For me, I can read until my eyes bleed. But there are so many questions.
Maybe even a MS Team's setup where we can get a few of us who are interested and have one of you guys walk us through basics.

1 Like

That's a SmartThings thing... Hubitat does not have it as a separate service.

Coding is all done via the left menu under the respective Apps Code or Drivers Code

2 Likes

@Evilborg I was talking about itellij idea, visualstudio, notepad++, etc.

I don't use any of those. I code everything via the hubitat UI and test on my dev hub

3 Likes

Understood, I have been reading and thought ItelliJ IDEA had some autocorrect mechanisms built in for groovy.

I use linux here and the app I use is Kate which does groovy formatting

2 Likes

I have that set up with the groovy plugin. I am trying to use the public driver/app examples but not sure where to start.

Ok here is what I have so far. I am trying to work from the code Adam shared in his video.
I am simply trying to turn on/off an inovelli lzw42 directly. What else do I need to make that happen? I know I am missing something, probably a bunch...

/ ** A Dimmer **/ 

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

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

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

def on(){
    log.debug "Device was turned on"
    
    sendEvent(name: "switch", value: "on")
}

def off(){
    log.debug "Device was turned off"
    
    sendEvent(name: "switch", value: "off")
    
}

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()
    }

}
1 Like

Not sure what your ultimate goal is but that won’t work. You’d need to implement zwave commands to communicate with the device.

2 Likes

@dman2306, for the moment all I want to do is learn to code the bulb to turn on/off. I am trying to learn to code.

1 Like

your ahead of me :wink: I got about 1 hr in. Following, and trying to learning too :+1:

1 Like

Gotcha. To do that for such a device you’d need to send a zwave command for on and off so the device knows what to do. The sendCommand will just update the on/off status in HE but it won’t send anything to the actual device.

2 Likes

@dman2306, can you point me to where there may be some examples of this?
Also, I assume this information will be necessary to use the z-wave commands? Or no?

fingerprint  mfr:"031E", prod:"0005", deviceId:"0001", inClusters:"0x5E,0x85,0x59,0x86,0x72,0x5A,0x33,0x26,0x70,0x27,0x98,0x73,0x7A", deviceJoinName: "Inovelli Bulb Multi-Color" 

@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()
    }

}