Increase and Decrease Brightness with ONE button of a switch

Tags: #<Tag:0x00007f7399190488> #<Tag:0x00007f73991903c0> #<Tag:0x00007f73991902f8> #<Tag:0x00007f7399190230>

I want to use one button of an (Aqara Smart) switch to turn a lamp on and off (= toggle). This already works. Now I want to switch between increase and decrease brightness with a longer press of this one button. (I don’t know the technical wording for this. I call it toggle dimming.)
I could not realize this so far, because in the automation config only increase OR decrease is offered.

Does anyone know how I could implement my plan?

1 Like

What should the button do if the light is at let’s say 50% brightness, should it increase or decrease the brightness?

I know from other systems (f.e. many KNX devices) that in this case the brightness is increased first. If you want to decrease the brightness, you have to release the button and press again and hold.

Old touch lamps would just toggle, and always increase. 0 -> 50 -> 100 -> 0. That would be pretty easy to do, but you’d lose the toggle aspect of the switch. Or, you change the touch with this function, then have long press turn it off from any state…

Since Burninstone is currently typing something (probably an awesome automation for you lol), I’ll wait to see what they do before writing it all out.


I don’t think this is possible or at least it will become pretty complex to do this with Home Assistant automations. The difficulty is to find out whether the brighntess should be increased or decreased. You could probably do something like "if button has been long pressed in the last 5 seconds then decrease the brightness else increase the brightness.
Does your button send events continually when you long press the button or only on event at the beginning and one at the end?
Do you want the light to in-/decrease brightness smoothly while you hold the button and stop when you release the button?

I saw you typing and thought “maybe I’ll just stop it here, Jim will probably post a ready made solution” :rofl:
Now Taras is typing, we’ll probably see a solution for this soon :stuck_out_tongue_winking_eye:

1 Like

I agree with Burningstone. At first glance, it seems possible to achieve it but in a non-trivial way.

Personally, I’m dissuaded from participating in the development of a solution because, for the all effort, the end-result is a clunky UX (a “one-button lamely trying to do it all” user experience).

Having said that, if anyone has a clever solution that doesn’t lean heavily on a myriad of input_booleans, timers and/or date arithmetic, I’d love to see it.

1 Like

I have to agree with burning, Jim and Taras
I don’t see how this would work
Can you describe what levels you’d want and what you do with the button to take it from any state to the possible options ?

I do think the old touch lamps were somewhat intuitive. Heck, even the little turn knobs that killed your fingers, you’d turn it once, it would turn on dimly. You turn it again, it got brighter. Again, brighter. Again, off.

I would change long press to be toggle. This way, it would turn on with the last brightness you turned it off with. If you press it, it will just start at low brightness. This does get slightly annoying when you forget this and push when you meant to hold. Now you have to push a few more times…and hopefully you don’t overshoot lol (always happens with those damn touch lamps).

But here. Since the button seems to support single and double click, we’ll just make a double click do this. It’s just going to go up and wrap around. You can tweak the step size to your liking. Or, change it to
long_click_press if your switch supports that.

- alias: Dimmer touch
    platform: event
    event_type: click
      entity_id: binary_sensor.button_id
      click_type: double
    # Changing brightness is always a turn_on event
    - service: light.turn_on
      entity_id: light.lampy
        # Tweak the step size to liking. Note, using brightness_pct, its from 0-100, but state_attr is 
        # from 0-255, so need to convert.
        # Also, to handle the rounding, will just jam it to 100% if it's close (> 90%)
        brightness_pct: >
          {% set step_size = 25 %}
          {% set cb = (state_attr('light.lampy', 'brightness') | float / 255.0) * 100.0 %}
          {% set new_brightness = cb | int + step_size %}
          {% if 91 <= new_brightness < (90 + step_size) %}
          {% else %}
            {{ new_brightness if new_brightness < 100 else 0 }}
          {% endif %}

This will handle cases where the brightness was set manually by other means (check if > 100 and turn off).

It’s not exactly what you had in mind, but it should be at least functional. You can tweak the step_size so you find the values you would actually use. 25 would require 4 presses to get to max brightness from off.


By “clunky UX” do you mean the feature itself or the implementation with HA?
The feature has been offered by professional systems in building automation for years. Philips has now also implemented this feature with its Smart Buttons. If you test it, you will quickly see that this feature is absolutely easy to use.

Yes, thats what i want. Simply: I want to use the toggle/dimming function of a Philiphs Hue Smart Button also with an Aqara Smart Wireless Switch.

Yes, the aqara smart switch supports: click, double press, long press and ending of a long press.
Thanks for your suggestion. Unfortunately, I have worked almost exclusively with the UI and just a bit with code so far. So I have to try to get your code working first. I’m still having trouble with that.

After that I might be able to adapt it to my wishes. To implement my plan exactly, it would have to be something like “when long press starts, dim up in x% steps until long press ends”. Dimming down the same way afterwards would be really hard to do and needs a timer.

That’s not doable with standard HA automations you need something more advanced like AppDaemon or Node-Red. There’s an app called Controller X foe AppDaemom that supports smooth dimming but not the way you want it. How is the Aqara button integrated into HA, through the aqara hub or deconz or something else?

1 Like

his code will not work for you, as you only have one event at the beginning and at the end of the long press. His code will increase the brighntess a bit on each press, however for you it will only fire once in the beginning of the long press, so to increase brightness to the max ypu would need to long press the butron, release it, long press again etc.

I installed nodered a few weeks ago, but have not really gotten used to it yet. But if I can’t realize my plans with it, then I don’t need to try it again.

I integrated the button with Deconz.

Yes, i know. I understand the code. But first i had problems to find the right event for my configuration.

Are the lights you want to control also integrated through deconz? I ask becasue there’s a service for deconz that allows smooth dimming, which I use in one of my AppDaemon apps.

Unfortunately no, not yet. Still using the Hue gateway for the lights.

As an interim solution I am currently trying to implement the automation from jocnnor. Therefore I have changed the trigger to:

    platform: event
    event_type: deconz_event
      entity_id: smart_switch
      event: 1001

The lamp turns on at 25% when I press and hold, but it doesn’t get any brighter when I press and hold again.

Yes. The fact it’s been offered by professional systems doesn’t change my opinion of it. In fact, I own several devices with buttons that can be programmed to operate in different ways including the “all-in-one” (push once for on, push twice for off, press and hold to brighten or dim). I believe it’s far more convenient, and intuitive, to use two separate buttons for on/off, dimming/brightening (or, better yet, the 4-button Philips Hue Dimmer). That’s why I have no desire to invest my time and effort creating something I dislike.

However, as I’ve already stated, that’s my opinion; you’re certainly free to your opinion and to emulate the “all-in-one” behavior with Home Assistant. Good luck.

I think @jocnnor ‘s approach is the only one that will work.
I also agree with Taras in that “as stated” the functionality would be’ clunky’ (I’d say worse than that TBH). My acid test would be “can you give this to a 4yr old and expect them to work it” ? (the answer is no ! )
So Jim’s approach (replicating the old touch lamps) is probably the best compromise. And avoiding the long press (is a short press followed by a release) issue. I’d go with : -

  1. Any press brings it on (at previous level)
  2. Double click switches it off
  3. Single press takes it to the next level and these wrap round at (10, 20, 25 whatever % steps - note: at 10% you will a quickly get bored of clicking the damn button). And it eventually takes it to ‘off’

The main reason I don’t like it is (say) in the late evening, it was last on at ‘bright’ (100%), it comes on, and blinds you OR it comes on at 50% and you want it on at 25% - you have to click it 4 times AND take it through the blinding 100% mark.

The most telling thing is that even the guy proposing its use, can’t tell you how it’s supposed to work. (it’s a “just work some magic” thing ! :rofl: )

If he would have his lights integrated through deconz as well, it should be possible to replicate the desired behaviour with smooth dimming. First long press brigntess up, second long press brightness down.

Nah, I do appreciate your optimism but this isn’t ‘you’ you are designing for, this is the ‘general user’ so : - ‘brighten’ - ‘assess’ (just right / no, needs to be brighter / no, toooo bright) - next action is…
There is no use case where this works for everybody where there are not 3 different actions to be performed to give 3 different results (preferably keeping the user manual down to 2 or 3 pages of A4)
As time goes by, I’m leaning more and more to Petro’s position on keeping user interaction to the minimum (but then I’ve always been cynical). :rofl:

I just wanted to state that the use case OP describes would be solvable if the lights are integrated through deconz. Deconz has a service that smoothly increases/decreases brightness up/down to the specified brightness and another service to stop this dimming. For this services you can also specify how long it should take to increase/decrease the brightness to the desired brightness. So with some math in the automations you can make sure that it always increases/decreases brightness at the same speed.
This means OP could have:
Long press of button -> increase brightness
Release long press -> stop increasing brightness
Long press of button and last long press less than e.g. 5 seconds ago -> decrease brightness
Release long press -> stop decreasing brightness
I don’t say it’s easy, but I think it would solve OPs case.

That’s my credo as well, I always try to reduce user input to a minimum. Ideally everything would work automatically in the background (that’s why I’m also not a huge fan of putting a lot of time in the frontend anymore). And I try to make the places where I still need user input (like pressing a button) as intuitive as possible as it gets harder and harder to rembember the different actions assigned to the buttons. That’s why I like the Hue Dimmer Switches so much, the buttons have an icon on it that makes it clear what the button should do. My dimmer switches work like this:

  • Single press ON button -> turn light on
  • Long press ON button -> turn light on at 100% brightness
  • Long press BRIGHTNESS UP -> smoothly increase brightness until button is released
  • Long press BRIGHTNESS DOWN -> smoothly decrease brightness until button is released
  • Single Press OFF button -> turn light off
  • Long Press OFF button -> turn all devices in the room off
    Which I think is pretty intuitive, except for the long press of the ON/OFF button.