Fibaro Dimmer 2 scenes

Hello!

I just finished writing and testing my first smartapp. :slight_smile:

I wanted to be able to dim my Trådfri bulbs from a normal light-switch using by the S2 input on the Fibaro 2 Dimmers. I also wanted to have a switch to trigger “Goodbye” when we leave home.

So 1 click = Toggle light on/off, 2 clicks = run routine, and hold = dim level up/down.

I would like your opinion on my code. Especially the dimming part at the end of the code. Maybe is there a better/easier way to do this?

Blockquote

/**

  • Fibaro Dimmer 2 scenes
  • Copyright 2018 Tommy Saaek
  • Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except
  • in compliance with the License. You may obtain a copy of the License at:
  •  http://www.apache.org/licenses/LICENSE-2.0
    
  • Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
  • on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
  • for the specific language governing permissions and limitations under the License.

*/
definition(
name: "Fibaro Dimmer 2 scenes ",
namespace: “Fibaro Dimmer 2 scenes”,
author: “Tommy Saaek”,
description: “Smartapp for using your Fibaro dimmers scene ID’s to control other devices like for example Tr\u00E5dfri bulbs. Single click to turn on/off. Hold to dim level.\r\n\r\nRun a routine by doubleclicking.\r\n\r\n”,
category: “My Apps”,
iconUrl: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png”,
iconX2Url: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png”,
iconX3Url: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png”)

preferences {
section(“Choose your Fibaro dimmer”) {
input “masterDevice”, “capability.switch”, title: “Master device”, multiple: true, required: true
}

section("Scene for singleclick. (Toggle on/off)") {
    input "sceneSingle", "enum", title: "Scene id", multiple: false, required: true,
          options: ["16", "26"]
}

 section("Scene for doubleclick. Run scene (optional)") {
    input "sceneDouble", "enum", title: "Scene id", multiple: false, required: false,
          options: ["14", "24"]
}

/* section(“Scene for trippleclick”) {
input “sceneTripple”, “enum”, title: “Scene id”, multiple: false, required: false,
options: [“15”, “25”]
}*/

section("Scene for Holdning down. Dimming (optional)") {
	input "sceneHold", "enum", title: "Scene id", multiple: false, required: false,
    	options: ["12","22"]
}

 section("Scene for Release (Requiered if Hold is used)") {
	input "sceneRelease", "enum", title: "Scene id", multiple: false, required: false,
    	options: ["13","23"]
}

section("Device to control") {
  input "slave", "capability.switch", title: "Slave device", multiple: true, required: true

}

section("Dim step size") {
  input "stepSize", "number", title: "Step size in percent", multiple: false, required: true

}

section("Select Routine for double-click (optional)") {
	input "Routine", "text", title: "Name of Routine", multiple: false, required: false
}

}

def installed()
{
subscribe(masterDevice, “scene”, sceneHandler, [filterEvents: false])
subscribe(slave, “level”, dimLevel)
log.debug “Installed with settings: ${settings}”
state.dimDirection = “up”
}

def updated()
{
unsubscribe()
subscribe(masterDevice, “scene”, sceneHandler, [filterEvents: false])
log.debug “Updated with settings: ${settings}”
subscribe(slave, “level”, dimLevel)
state.dimDirection = “up”
}

def sceneHandler(evt) {
if(sceneSingle && sceneSingle.contains(evt.value)) {
toggle(slave)
}

else if(sceneDouble && sceneDouble.contains(evt.value)) {
	location.helloHome?.execute(Routine)
}


else if(sceneTripple && sceneTripple.contains(evt.value)) {
/* Not used */
}

else if(sceneHold &&  sceneHold.contains(evt.value)) {

state.doDim = “yes”
if (state.dimDirection == “up”) {
dimUp(slave)
}

	else if (state.dimDirection == "down") {
    dimDown(slave)
	}
}

else if(sceneRelease && sceneRelease.contains(evt.value)) {
state.doDim = “no”
if (state.dimDirection == “up”){
state.dimDirection = “down”
}

    else if (state.dimDirection == "down") {
    state.dimDirection = "up"
    }
}

}

def toggle(devices) {
if (devices*.currentValue(‘switch’).contains(‘off’)) {
devices.on()
}
else if (devices*.currentValue(‘switch’).contains(‘on’)) {
devices.off()
}
else {
devices.on()
}
}

def dimUp(devices) {

if (state.doDim == “yes”){
state.currentLevel = “${devices*.currentLevel[0].toInteger()}”
def nextLevel = state.currentLevel.toInteger() + stepSize
state.newLevel = nextLevel.value
devices*.setLevel(nextLevel)
}
}

def dimDown(devices) {
if (state.doDim == “yes”){
state.currentLevel = “${devices*.currentLevel[0].toInteger()}”
def nextLevel = state.currentLevel.toInteger() - stepSize
state.newLevel = nextLevel.value
devices*.setLevel(nextLevel)
}
}

def dimLevel(evt) {
if (state.doDim == “yes”){
if (state.newLevel && evt.value) {
if (state.dimDirection == “up”) {
if (evt.value == “100”){
state.doDim = “no”
}
dimUp(slave)
}

        else if (state.dimDirection == "down") {
            if (evt.value == "0"){
        	state.doDim = "no"
       		}
  	dimDown(slave)
  	}
  }              
} 

}