Leviton Decora Wi-Fi smart switch implementation

Excellent !! (Glad to help and looking forward to that beer !!)

My first rule for openHAB 3.x

decora-toggle
Sends commands to dimmers/switches/etc using decora_wifi-cli

triggers:
  - id: "1"
    configuration:
      itemName: Dimmer_1
    type: core.ItemCommandTrigger
  - id: "2"
    configuration:
      itemName: Dimmer_2
    type: core.ItemCommandTrigger
  - id: "3"
    configuration:
      itemName: Switch_3
    type: core.ItemCommandTrigger
  - id: "4"
    configuration:
      itemName: Switch_4
    type: core.ItemCommandTrigger
  - id: "5"
    configuration:
      itemName: Switch_5
    type: core.ItemCommandTrigger

conditions: []
actions:
  - inputs: {}
    id: "6"
    configuration:
      type: application/javascript
      script: >-
        // Setup Logging

        var logger = Java.type("org.slf4j.LoggerFactory").getLogger("decora-toggle");

        var Duration = Java.type("java.time.Duration");

        var Exec = Java.type("org.openhab.core.model.script.actions.Exec");


        // decora_timeout   : time to wait before killing command in case of failure

        // decora_script    : the script we run to get our status << make sure it's executable by openhab user

        // decora_email     : MyLeviton Email Address

        // decora_password  : MyLeviton Password

        // decora_command   : Command to send to switch/dimmer (ON/OFF/NN)

        // NOTE: event.itemCommand and event.itemName are only issued if the rule is triggered by an item event


        decora_timeout = Duration.ofSeconds(12)

        decora_script = "/etc/openhab/scripts/decora_wifi-cli.py";

        decora_email = "YOUR_EMAIL";

        decora_password = "YOUR_PASSWORD";

        decora_command = event.itemCommand;


        // Determine our switch/dimmer's device ID on

        switch(event.itemName){
          case "Dimmer_1":
            var decora_id="111111";
            break;
          case "Dimmer_2":
            var decora_id="222222";
            break;
          case "Switch_3":
            var decora_id="333333";
            break;
          case "Switch_4":
            var decora_id="444444";
            break;
          case "Switch_5":
            var decora_id="555555";
            break;
        }


        // It's log, it's log, it's better than bad it's good.

        logger.warn("Setting "+event.itemName+" ("+decora_id+") to "+decora_command+".");


        var decora_toggle = Exec.executeCommandLine(decora_timeout,decora_script,decora_email,decora_password,decora_id+":"+decora_command);


        // For debugging... comment out when stable

        //logger.warn(decora_toggle);
    type: script.ScriptAction

Second openHAB 3.x rule

decora-status:
Updates status of listed switches/dimmers/etc in case they were turned on manually
Run every 2 minutes by cron job

triggers:
  - id: "1"
    configuration:
      cronExpression: 0 0/2 * * * ? *
    type: timer.GenericCronTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript
      script: >-
        // Setup Logging

        var logger = Java.type("org.slf4j.LoggerFactory").getLogger("decora-status");

        var Duration = Java.type("java.time.Duration");

        // NOTE: event.itemCommand and event.itemName are only issued if the rule is triggered by an item event.

        //command = "/etc/openhab/scripts/"+event.itemName+"-"+event.itemCommand+".sh"
          
        // initialize exec

        var Exec = Java.type("org.openhab.core.model.script.actions.Exec");


        // Let's Define all our variables for this script.

        // decora_id        : Leviton Device ID #

        // decora_item      : Corresponding item Name in openHAB

        // decora_loop_wait : time in seconds to pause between runs of the script

        // decora_script    : the script we run to get our status

        // decora_email     : MyLeviton Email Address

        // decora_password  : MyLeviton Password


        // NOTE: to get your device ID's run the following command in a terminal:

        //       <path_to_script>/decora <leviton_email> <leviton_password> ?

        decora_id =   ["111111",     "222222",     "333333",    "444444",     "555555"];

        decora_item = ["Dimmer_1", "Dimmer_2", "Switch_3", "Switch_4", "Switch_5"]

        decora_script = "/etc/openhab/scripts/decora_wifi-cli";

        decora_email = "YOUR_EMAIL";

        decora_password = "YOUR_PASSWORD";

        // loop through decora id's to update status.

        for (i=0; i < decora_id.length; i++){
          // Run our status command for each device id.  the command on a terminal looks like this:
          // <path_to_script>/decora <leviton_email> <leviton_password> <decora_id>:?
          var decora_status = Exec.executeCommandLine(
            Duration.ofSeconds(12),
            [decora_script,                      // our script's path
              decora_email,                      // MyLeviton Email
              decora_password,                   // MyLeviton Password
              decora_id[i]+":?"                  // ID from loop & ? for status.
            ]);
          //Put the output in the log so we know what's going on ##COMMENT OUT WHEN DONE DEBUGGING!!!
          //logger.warn(decora_status);
          //check our output and set the item's status...
          if (decora_status.contains("0%") || decora_status.contains("OFF")){
            // Set our item to OFF
            events.sendCommand(decora_item[i], OFF);
            // Log it in...
            //logger.warn("Setting "+decora_item[i]+" to OFF");
          }
          else{
            // Set our item to ON
            events.sendCommand(decora_item[i], ON);
            // Log it in...
            //logger.warn("Setting "+decora_item[i]+" to ON");
          } 
        }
    type: script.ScriptAction

I have submitted a pull request to decora_wifi-cli on github.

It adds ID:?? command which shows just the status
This checks if it is a DW6HD dimmer switch and outputs brightness and if it is not, just ON/OFF.

For example, if your DW6HD is a dimmer switch set at 40%, it will output: 40. not a DW6HD, and your switch is on, it outputs ON.

This way we can use the output of the command as a variable to set our itemā€™s status.

Using this new version we can change the decora-status rule to this:

triggers:
  - id: "1"
    configuration:
      cronExpression: 0 0/2 * * * ? *
    type: timer.GenericCronTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript
      script: >-
        // Setup Logging

        var logger = Java.type("org.slf4j.LoggerFactory").getLogger("decora-status");

        var Duration = Java.type("java.time.Duration");

        // NOTE: event.itemCommand and event.itemName are only issued if the rule is triggered by an item event.

        //command = "/etc/openhab/scripts/"+event.itemName+"-"+event.itemCommand+".sh"
          
        // initialize exec

        var Exec = Java.type("org.openhab.core.model.script.actions.Exec");


        // Let's Define all our variables for this script.

        // decora_id        : Leviton Device ID #

        // decora_item      : Corresponding item Name in openHAB

        // decora_loop_wait : time in seconds to pause between runs of the script

        // decora_script    : the script we run to get our status

        // decora_email     : MyLeviton Email Address

        // decora_password  : MyLeviton Password


        // NOTE: to get your device ID's run the following command in a terminal:

        //       <path_to_script>/decora <leviton_email> <leviton_password> ?

        decora_id =   ["111111",     "222222",     "333333",    "444444",     "555555"];

        decora_item = ["Dimmer_1", "Dimmer_2", "Switch_3", "Switch_4", "Switch_5"]

        decora_script = "/etc/openhab/scripts/decora_wifi-cli";

        decora_email = "YOUR_EMAIL";

        decora_password = "YOUR_PASSWORD";

        // loop through decora id's to update status.

        for (i=0; i < decora_id.length; i++){
          // Run our status command for each device id.  the command on a terminal looks like this:
          // <path_to_script>/decora <leviton_email> <leviton_password> <decora_id>:?
          var decora_status = Exec.executeCommandLine(
            Duration.ofSeconds(12),
            [decora_script,                      // our script's path
              decora_email,                      // MyLeviton Email
              decora_password,                   // MyLeviton Password
              decora_id[i]+":?"                  // ID from loop & ? for status.
            ]);
          //Put the output in the log so we know what's going on ##COMMENT OUT WHEN DONE DEBUGGING!!!
          //logger.warn(decora_status);
          //check our output and set the item's status...
           if (String(decora_status).contains("ON")){
    events.sendCommand(decora_item[i], ON);
  }
  else if (String(decora_status).contains("OFF")){
    events.sendCommand(decora_item[i], OFF);
  }
  else if (isNumeric = true){
    events.sendCommand(decora_item[i],Number(decora_status));
  }
  else{
    logger.warn("decora script not giving proper output");
    logger.warn("decora script output: "+decora_status);
  }
}

Hi, a cheerful noob here, who recently installed several of these pricey dimmers and was hoping to use OpenHab until I found out thereā€™s no binding yet. I donā€™t yet know anything about openHab 3.x rules, but Iā€™ll catch on.

I just have one question going in: does this actually work? Is it able to control the ā€œ2nd genā€ Decora dimmers (D26HD)?

I canā€™t say for sure since I donā€™t have that model of dimmer switch. But Iā€™d suggest that if you can control the dimmer using the My Leviton app, then the Python script as described above will likely work as it controls devices using that app.

As luck would have it - I found out by accident that this model can be controlled by HomeKit.

I tried Home Assistant, even though Iā€™d read that its MyLeviton support had stopped working reliably. And on setup it detected my dimmers and told me they were controllable via the HomeKit integration. I got the codes off the switch labels, and it worked. No password, no logon.

I was totally stunned.

I think openHAB has HomeKit support as well, although I havenā€™t tried it.