Finally - a cheap WIRELESS switch that dims! Xiaomi Switch Gen1

OK, so I’ve been looking for a wireless switch that capable of dimming a WiFi enabled Bulb for some time. I’m using This WiFi Bulb which has been flashed and is running the wonderful AiLight firmware by @stelgenhof but you could use any bulb, switch or remote capable of dimming.

This assumes you have already added your Gen1 Button using the Xiaomi Component

NOTE: The square Aqara (Square Gen2) Version is not capable of this as it does not have the required “long_press_press” click type.

There’s probably a better way of doing this, but it works really well!

CODE:

Input Boolean
input_boolean:
  landing_dim:
    name: Landing Dim
    initial: off
Script - Dim Light
script:
  dim_light_landing:
    sequence:

      - condition: state
        entity_id: input_boolean.landing_dim
        state: 'on'
    
      - service: light.turn_on
        entity_id: light.landing_light
        data_template:
          brightness: '{{states.light.landing_light.attributes.brightness - 10}}'

      - service: script.turn_off
        entity_id: script.dim_light_landing

      - service: script.turn_on
        entity_id: script.dim_light_landing
Script - Brighten Light
script:
  brighten_light_landing:
    sequence:

  - condition: state
    entity_id: input_boolean.landing_dim
    state: 'off'

  - service: light.turn_on
    entity_id: light.landing_light
    data_template:
      brightness: '{{states.light.landing_light.attributes.brightness + 10}}'
  
  - service: script.turn_off
    entity_id: script.brighten_light_landing

  - service: script.turn_on
    entity_id: script.brighten_light_landing
Automations - Using TWO Xiaomi Switched to control one light
- alias: xiaomi landing brighten on
  initial_state: 'on'
  trigger:
    - platform: event
      event_type: click
      event_data:
        entity_id: binary_sensor.switch_158d00019df218
        click_type: long_click_press
    - platform: event
      event_type: click
      event_data:
        entity_id: binary_sensor.switch_158d00016c0bac
        click_type: long_click_press
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: input_boolean.landing_dim
      state: 'off'
    - condition: state
      entity_id: light.landing_light
      state: 'on'
  action:
    - service: script.turn_on
      entity_id: script.brighten_light_landing
      
- alias: xiaomi landing brighten off
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: binary_sensor.switch_158d00019df218
      from: 'on'
      to: 'off'
    - platform: state
      entity_id: binary_sensor.switch_158d00016c0bac
      from: 'on'
      to: 'off'
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: input_boolean.landing_dim
      state: 'off'
    - condition: state
      entity_id: light.landing_light
      state: 'on'
  action:
    - service: input_boolean.turn_on
      entity_id: input_boolean.landing_dim
      
- alias: xiaomi landing dim on
  initial_state: 'on'
  trigger:
    - platform: event
      event_type: click
      event_data:
        entity_id: binary_sensor.switch_158d00019df218
        click_type: long_click_press
    - platform: event
      event_type: click
      event_data:
        entity_id: binary_sensor.switch_158d00016c0bac
        click_type: long_click_press
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: input_boolean.landing_dim
      state: 'on'
    - condition: state
      entity_id: light.landing_light
      state: 'on'
  action:
    - service: script.turn_on
      entity_id: script.dim_light_landing

- alias: xiaomi landing brighten off
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: binary_sensor.switch_158d00019df218
      from: 'on'
      to: 'off'
    - platform: state
      entity_id: binary_sensor.switch_158d00016c0bac
      from: 'on'
      to: 'off'
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: input_boolean.landing_dim
      state: 'on'
    - condition: state
      entity_id: light.landing_light
      state: 'on'
  action:
    - service: input_boolean.turn_off
      entity_id: input_boolean.landing_dim

When you press and hold the switch, the light will brighten, 10 steps at a time, when you release the switch the light will stay at the brightness you release on. When you then press and hold the switch the the second time the light will do the opposite and start to dim, 10 steps at a time, when you release the button the light will stay as the desired brightness!

Hope this is useful to some people and please do let me know if this can be simplified! :slight_smile:

Here’s a demo:

34 Likes

cool! i have a few of these switches that i haven’t put to use yet, so i’ll give a try sometime soon.

2 Likes

Let me know how you get on! :slight_smile:

I think its working in the same way as the script @Danielhiversen posted here:

I have adopted that script but never got it working as well as yours. I assumed it was a limitation of the HA script processing, but now I suspect that the brightness is changed in too small increments.

Thanks for posting automation and scripts yam. I need to get it working properly now :slight_smile:

1 Like

Also I like how you “hacked” your light switch :slight_smile:
I’m guessing you removed a dimmer unit + knob from the faceplate and connected the live to switched live in the box.
Then stuck the xiaomi switch over the resulting hole where a dimmer knob used to be?

I have never seen Daniel’s code before! It’s similar yes. not sure if it provides dimming and brightening though.

Yes, I have joined the switched live in the back box, put a face place over it and stuck the button on top. it looks ugly… I’d like to replace it with this https://www.thingiverse.com/thing:2286498 but I haven’t figured out how to fix it to the wall without gluing, plus the screws on the standard place hold the back box in, I’ll figure something out but for now this works fine :slight_smile:

Well yours is nicer than my solution of just blu-taking the xiaomi button to the wall next to the existing switches :slight_smile:
For me it’s complex though as I have multi-gang dimmers the I can’t easily swap out like this… but I’m lucky enough that when I had the house rewired about 8-9 years ago that I ended up with L + N behind the switches :slight_smile: (not through planning… just luck).
I love these gen1 xiaomi switches… its a real shame that they changed the behaviour for the new ones.
BTW I’m not sure I 100% agree on 3D printed faceplates though - the quality is (IMO) not really good enough… the surface will get dirty really quickly.

IIRC it cycled down then up, so you press and hold once until you get the desired brightness.
Didnt seem any where near as responsive though.

I actually have N behind all my switches, but not using it. As my bulbs are wifi bulbs the dimming is done on the bulb it’s self so a dimming switch didn’t help me :slight_smile:

This way is way cheaper (about £15 total for bulb and switch)

Glad you like it, would love to know if you have any improvements!

Amazing - I had click as toggle double press as dim long press as bright but this is much better

1 Like

I very nearly did that until I sat down for hours to figure out the long press.

My double clicks are generally used for shut down away or bedtime scripts now. Might have a play with them to so they toggle between “morning” and “night time” scripts and scenes.

Sounds like we have similar automations. I love my automation to turn off all the lights.

If anyone else is as dim[1] as me, the code is revealed when you click the right arrow/triangles in the first post, on the left of “Input Boolean” etc.

[1] see what I did there?

4 Likes

@badgerhome Are you liking the ARILUX bulbs? Do they have a nice warm white too?

I am. You can achieve a warmer white. I will share a video of my bed side lamps with are warmer. Not as warm as a Hue or a traditional halogen though but I feel they are acceptable. I prefer the white cold lighting in the hallways, just makes seeing thing easier!

I’ve made a mistake somewhere that I am trying to work out but shouldn’t the second script be?

script:
  brighten_light_landing:

instead of

Or are they both meant to be called dim_light_landing ?

And thank you very much for posting this, it’s too big for me to hold in my head all at once and I never would have been able to do this on my own. Much appreciated.

Hmm nice I guess this would also work for ‘dimming’ the volume of the iTunes component.

1 Like

You’re right, it looks wrong. I will check and update when I’m at a laptop. On mobile at the moment. Thanks!

I have updated now, you were right, should have been brighten. Thanks for pointing out.

I have tried the method with LimitlessLED component, but it doesn’t seem to be working. I can see the brightness increasing and then decreasing from the frontend. But the limitlessLED component is not fast enough. It’s sending commands every half a second or so (from the looks of it). Then the light gradually dims or brightens. I’ve looked into the limitlessled.py component code, but weren’t able to find anything related to repetitions or delays.

1 Like