How to create a virtual dimmer?

Hi can anyone tell me how to add a virtual dimmer. I don’t see “virtual dimmer” or “simulated dimmer” as an option for a device to add. Any advice would be greatly appreciated. Thanks!

There’s no prebuilt template, you have to create your own using capabilities. Here’s one example:

@bravenel has provided just the dimmer code in the post below this, so you just install that in the IDE as code as a custom device rather than from a template.

1 Like

Here is the code for a virtual dimmer device type:

metadata {
        definition (name: "Virtual Dimmer", namespace: "smartthings", author: "smartthings") {
        capability "Switch"
        capability "Refresh"
        capability "Switch Level"
    }

	// simulator metadata
	simulator {
	}

	// UI tile definitions
	tiles {
		standardTile("button", "device.switch", width: 2, height: 2, canChangeIcon: true) {
			state "off", label: 'Off', action: "switch.on", icon: "st.Kids.kid10", backgroundColor: "#ffffff", nextState: "on"
			state "on", label: 'On', action: "switch.off", icon: "st.Kids.kid10", backgroundColor: "#79b821", nextState: "off"
		}
		standardTile("refresh", "device.switch", inactiveLabel: false, decoration: "flat") {
			state "default", label:'', action:"refresh.refresh", icon:"st.secondary.refresh"
		}        
        controlTile("levelSliderControl", "device.level", "slider", height: 1, width: 2, inactiveLabel: false, backgroundColor:"#ffe71e") {
            state "level", action:"switch level.setLevel"
        }
        valueTile("lValue", "device.level", inactiveLabel: true, height:1, width:1, decoration: "flat") {
            state "levelValue", label:'${currentValue}%', unit:"", backgroundColor: "#53a7c0"
        }

		main(["button"])
		details(["button", "refresh","levelSliderControl","lValue"])
	}
}

def parse(String description) {
}

def on() {
	sendEvent(name: "switch", value: "on")
    log.info "Dimmer On"
}

def off() {
	sendEvent(name: "switch", value: "off")
    log.info "Dimmer Off"
}

def setLevel(val){
    log.info "setLevel $val"
    
    if (val < 0) val = 0
    else if( val > 100) val = 100
    
    if(val == 0) off() else {
 	on()
 	sendEvent(name: "level", value: val)
    }
}

def refresh() {
    log.info "refresh"
}
9 Likes

This is an interesting thing for me.

Can someone explain to me, what a virtual dimmer switch would be good for?

I use virtual switches to turn on my GE Light bulbs that do not use wall switches. I use them to be the master to control individual lights in groups. I have it turn on tower fans in several rooms that use power plug adpaters.
I use them in a few smart apps that have timer triggers.
I even use it to control various functions of my thermostat.

1 Like

Any device that scales up-and-down. People typically use them to represent a thermostat or a sound system if you’re just adjusting one “dial.”

The most popular use, as @DarcRanger mentions, is probably to give you a master control tile for a group of bulbs that you want to dim together.

1 Like

Would it be possible to have a smartApp that uses the virtual dimmer to create set points that can ultimately be used for a IFTT recipe. Like having 4 switches assigned to one dimmer, each switch can activate something through IFTT and would be controlled on the dimmer’s setting.

Yes, sure. You can use level anyway you want.

:grinning: Great…now. who do I have to talk to make this happen?

Bruce, Are you willing to take on this brave undertaking?:grin:

I used code you posted to create a virtual dimmer. I want to use it to control a group of ge link bulbs. It turns them on and off but they don’t dim, any thoughts?

GE smart bulbs do not dim well are you sure they are not really dimming? My GE bulbs don’t really show much dim until about 30% or lower and still not much difference from full bright.

No definitely not dimming. I have had them for a while and I know what you mean by not dimming much but I see no change from 100% to 1%. I can have alexa dim them and see the difference but not the virtual dimmer. Its not a big deal just wondering if I missed something. I Just wanted them grouped together for one smarttile vs 8 separate tiles.

I am getting the same thing with a set of CREE lights. ON/Off works but the dimming slider has no affect. Any suggestions?

I think this will solve the issue. I am researching the same issue as my Virtual Switch wont dim my Cree bulbs.

This has worked great for me when I linked a set of GE Lights. I think with the recent heavy loads, ST has changed a few things and how they update. I can no longer dim the lights with this switch (just on and off). I tried to figure it out myself but I’m stuck. I get this error but I there isn’t a line 212: groovy.lang.MissingMethodException: No signature of method: java.lang.String.div() is applicable for argument types: (java.lang.Integer) values: [100]
Possible solutions: is(java.lang.Object), drop(int), wait(), trim(), any(), size() @ line 212.

Tried using the provided code and got this error:
No signature of method: script1480120616967197107680.metadata() is applicable for argument types: (script1480120616967197107680$_run_closure1) values: [script1480120616967197107680$_run_closure1@11c35f04] Possible solutions: getMetadata(), getState(), setState(java.lang.Object), metaClass(groovy.lang.Closure)

Any help/updated code would be appreciated.

Thx.

okay…how do we add a device via the IDE using this code?

Add the code provided above as a new Device Handler then go to Devices and create a new device. Select “Virtual Dimmer” as the Type and fill in Name (Virtual Dimmer), Label (Whatever you want to call it), Version (Published), Location (most commonly just one in drop down), Hub (most commonly just one in drop down) and leave Group blank for now. I use a convention of VD01, VD02, etc for Virtual Dimmers as my Device Network Id - make sure it is unique. Save and then you can go back in to select the appropriate group (room) or you can do it within the mobile app.

3 Likes

Maybe I’m daft, but I can’t figure out how to add two of my Hue bulbs to it. I have the device handler created and I can see the Virtual Dimmer in my app.

I am new to all this. How does one use the code? Do I create a new Smart App with it? Do I somehow add it to the IDE? I am a little lost. My current code works against my real dimmers but fails with the simulator switch, hoping this code can help.