Light control with xiaomi buttons long press

I was thinking about using a xiaomi button to change the light brightness and color temperature on one or more hue lights, by using the long press function. Did anybody try this?

1 Like

So you want to use “long press” like a slider to turn brightness or color up?
Next question what about turn down the brightness or color?

Yes I was thinking about that, so during pres it would gradually change. When stopping I would reverse the effect to the opposite, or change it with double click or single click.

I did it!
Gosh so glad and happy it works :smile:
My rule increases light by 10% each 400ms. If it reaches 100 I do a reset and put it to 0. Of course you can change that bottom to 10 if you dont want to switch it off.

You can take out the logInfos. They were good to see where my faults were, e.g. I did not define the state as DecimalType at first.

Only real problem is the timelag and once it seems the release didnt go through, but nice light show :wink:

rule "XiaomiSwitch2"
    Channel "mihome:sensor_switch:158d00015f2ae8:button" triggered
    var actionName = receivedEvent.getEvent()
    switch(actionName) {
        case "SHORT_PRESSED": {
					logInfo("test","1 state" + tradfri_0220_gwb072bf27be41_65545_brightness.state )

			if (  tradfri_0220_gwb072bf27be41_65545_brightness.state  == 0 ) {
			logInfo("test","1" + tradfri_0220_gwb072bf27be41_65545_brightness.state)
				sendCommand(TodesStern_Brightness , ON)
			else if (  tradfri_0220_gwb072bf27be41_65545_brightness.state > 0) {
			logInfo("test","2" + tradfri_0220_gwb072bf27be41_65545_brightness.state)
				sendCommand(TodesStern_Brightness , OFF)
        case "DOUBLE_PRESSED": {
        case "LONG_PRESSED": {
			SwitchLRPressedLong = true
				while (SwitchLRPressedLong){
				brightnessLR = (tradfri_0220_gwb072bf27be41_65545_brightness.state as DecimalType).intValue
				logInfo("test","wz long " + tradfri_0220_gwb072bf27be41_65545_brightness.state + " " + brightnessLR)  

				brightnessLRTarget = brightnessLR + 10
				logInfo("test","wz long " + tradfri_0220_gwb072bf27be41_65545_brightness.state + " " + brightnessLR)  

					if (brightnessLRTarget > 99) {
						brightnessLRTarget = 0
				logInfo("test","wz long 2 " + tradfri_0220_gwb072bf27be41_65545_brightness.state + " " + brightnessLRTarget)  

				sendCommand(TodesStern_Brightness , brightnessLRTarget)
				Thread::sleep(400) // experiment with the value to control dimming speed
        case "LONG_RELEASED": {
		SwitchLRPressedLong = false
		logInfo("test","5 long released")        

Oh dont forget to initialize the variables at the beginning of the rulefile

var boolean SwitchLRPressedLong = false
var int brightnessLR = 0
var int brightnessLRTarget = 0
1 Like

Well done! :+1:

How do you trigger LONG_PRESSED or LONG_RELEASED?

Pres the for an extended time :wink:

I have aqara Switches, and no matter how long I press them I wont get this trigger …