How to dim Bedside lamp with Xiaomi button?

I am trying to set the brightness change of the xiaomi lamp with the wireless button.
I want to set continuously brightness change from actual state to max then jump to minimal brightness (if maximum is achieved) and so on…
Xiaomi button have “hold” state and “long_click_release” which can be used for this - “hold” trigger continuously change brightness and “long_click_release” stops.

Anyone can help me with this? Language syntax and scripting of HA is a little magic for me now. There is no documentation and support in my native language, either (Polish)

Already i have this code in automation.yaml

  • alias: Inc/Dec brightness of the gateway light
    trigger:
    platform: event
    event_type: click
    event_data:
    entity_id: binary_sensor.switch_158d0001dc44c4
    click_type: hold
    platform: event
    event_type: click
    event_data:
    entity_id: binary_sensor.switch_158d0001dc4519
    click_type: hold
    action:
    service: light.turn_on
    entity_id: light.yeelight_bedside_7811dc90cd74
    data_template:
    transition: ‘1’
    brightness: >
    {% set n = states.light.yeelight_bedside_7811dc90cd74.attributes.brightness + 25 %}
    {% if n > 255 %}
    10
    {% else %}
    {{ n | int }}
    {% endif %}

Works fine but brightness changes only one step at the time.

Hi,

You need to create:

-script with brightness control which calls itself during the time the button is being pressed.
-input_boolean to control wether to increase or decrease brightness

You can find more information and details in this forum post.

I’ve read whole thread about this, but it is very complicated to understand for me.

I’ve created some configuration:
(automations.yaml)

- alias: Zmiana jasnosci lampki
  id: '12345677'
  trigger:
  - platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001dc44c4
      click_type: long_click_press
  - platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001dc4519
      click_type: long_click_press
  action:
   - service: script.turn_on
     entity_id: script.zmien_jasnosc
    
- alias: Zmiana jasnosci lampki off
  id: '12345678'
  trigger:
  - platform: state
    entity_id: binary_sensor.switch_158d0001dc44c4
    to: 'off'
  - platform: state
    entity_id: binary_sensor.switch_158d0001dc4519
    to: 'off'
  action:
   - service: script.turn_off
     entity_id: script.zmien_jasnosc

Scripts.yaml

zmien_jasnosc:
    sequence:
    - condition: state
      entity_id: light.yeelight_bedside_7811dc90cd74
      state: 'on'
    - service: light.turn_on
      entity_id: light.yeelight_bedside_7811dc90cd74
      data_template:
       transition: '1'
       brightness: >-
        {% set n = states.light.yeelight_bedside_7811dc90cd74.attributes.brightness + 25 %}
        {% if n > 255 %}
          10
        {% else %}
         {{ n | int }}
        {% endif %}
    - service: script.turn_off
      entity_id: script.zmien_jasnosc
    - service: script.turn_on
      entity_id: script.zmien_jasnosc

Unfortunately it didn’t worked.

To keep things simple, I suggest that you try with following data _template inside your scripts:

Brighten :
brightness: '{{states.light.yeelight_bedside_7811dc90cd74.attributes.brightness + 10}

Dim:
brightness: '{{states.light.yeelight_bedside_7811dc90cd74.attributes.brightness - 10}}
and use a 2nd script to dim the light.

In total, I use 2 scripts , 4 automations and 1 input_boolean to dim/brighten 1 or more lights.

Here’s my configuration:

configuration.yaml:

input_boolean:
  light1_dim:
    name: Light1 Dim
    initial: off

automations.yaml:

- alias: Light1 brighten on
  id: '1511197874722'
  initial_state: 'on'
  trigger:
  - platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001a5d08e
      click_type: long_click_press
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: input_boolean.light1_dim
      state: 'off'
    - condition: state
      entity_id: light.light1
      state: 'on'
  action:
  - service: script.turn_on
    entity_id: script.1511118763251
- alias: Light1 brighten off - bool on
  id: '1511197874733'
  initial_state: 'on'
  trigger:
  - platform: state
    entity_id: binary_sensor.switch_158d0001a5d08e
    from: 'on'
    to: 'off'
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: input_boolean.light1_dim
      state: 'off'
    - condition: state
      entity_id: light.light1
      state: 'on'
  action:
  - service: input_boolean.turn_on
    entity_id: input_boolean.light1_dim
- alias: Light1 dim on
  id: '1511197874724'
  initial_state: 'on'
  trigger:
  - platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001a5d08e
      click_type: long_click_press
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: input_boolean.light1_dim
      state: 'on'
    - condition: state
      entity_id: light.light1
      state: 'on'
  action:
  - service: script.turn_on
    entity_id: script.1511119598248
- alias: Light1 brighten off - bool off
  id: '1511197876722'
  initial_state: 'on'
  trigger:
  - platform: state
    entity_id: binary_sensor.switch_158d0001a5d08e
    from: 'on'
    to: 'off'
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: input_boolean.light1_dim
      state: 'on'
    - condition: state
      entity_id: light.light1
      state: 'on'
  action:
  - service: input_boolean.turn_off
    entity_id: input_boolean.light1_dim

scripts.yaml:

'1511119598248':
  alias: dim_light1
  sequence:
  - condition: state
    entity_id: input_boolean.light1_dim
    state: 'on'
  - service: light.turn_on
    entity_id: light.light1
    data_template:
      brightness: '{{states.light.light1.attributes.brightness - 10}}'
  - service: script.turn_off
    entity_id: script.1511119598248
  - service: script.turn_on
    entity_id: script.1511119598248
'1511118763251':
  alias: brighten_light1
  sequence:
  - condition: state
    entity_id: input_boolean.light1_dim
    state: 'off'
  - service: light.turn_on
    entity_id: light.light1
    data_template:
      brightness: '{{states.light.light1.attributes.brightness + 10}}'
  - service: script.turn_off
    entity_id: script.1511118763251
  - service: script.turn_on
    entity_id: script.1511118763251

You can also apply this set of scripts/automations to rise or lower the Volume of a TV (with a Broadlink IR Emitter for example) with the Xiaomi Switches.

Above lines in my script was unnecessary.
Unfortunately with my script or yours scripts I’ve got “Qoute Exceed, flood protection” from bedside lamp in my logs And lamp stops responding. Then I added delay 00:01 at the end of script.
Now dimming ligth works like a charm but a little slow. Is it possible to delay 0.3 s instead of 1 s?
Frankly speaking creating recurrention in HA is really unfriendly.

Yes, 0.3s delay is possible :

- delay:
    # supports milliseconds, seconds, minutes, hours, days
    milliseconds: 300

Thank you. Now its perfect :slight_smile:

For those interested I’ve created a version that works with the Zigbee2MQTT version using the Xiaomi round button, using the above.

input_boolean:
  light1_dim:
    name: Light1 Dim
    initial: off

automation:
  - id: button_round_single
    initial_state: 'on'
    alias: Toggle lamp with button click
    trigger:
      platform: mqtt
      topic: 'zigbee2mqtt/Xiaomi round button'
    condition:
      condition: state
      entity_id: sensor.xiaomi_round
      state: 'single'
    action:
      - service_template: >
           {%- if is_state('light.tvlampmqtt', 'off') -%}
             light.turn_on
           {%- else -%}
             light.turn_off
           {%- endif %}
        entity_id: light.tvlampmqtt  

  - alias: Light1 brighten on
    id: '1511197874722'
    initial_state: 'on'
    trigger:
      platform: mqtt
      topic: 'zigbee2mqtt/Xiaomi round button'
    condition:
      condition: and
      conditions:
      - condition: state
        entity_id: sensor.xiaomi_round
        state: 'long'
      - condition: state
        entity_id: input_boolean.light1_dim
        state: 'off'
      - condition: state
        entity_id: light.tvlampmqtt
        state: 'on'
    action:
    - service: script.turn_on
      entity_id: script.1511118763251


  - alias: Light1 brighten off - bool on
    id: '1511197874733'
    initial_state: 'on'
    trigger:
      platform: mqtt
      topic: 'zigbee2mqtt/Xiaomi round button'
    condition:
      condition: and
      conditions:
      - condition: state
        entity_id: sensor.xiaomi_round
        state: 'long_release'
      - condition: state
        entity_id: input_boolean.light1_dim
        state: 'off'
      - condition: state
        entity_id: light.tvlampmqtt
        state: 'on'
    action:
    - service: input_boolean.turn_on
      entity_id: input_boolean.light1_dim


  - alias: Light1 dim on
    id: '1511197874724'
    initial_state: 'on'
    trigger:
      platform: mqtt
      topic: 'zigbee2mqtt/Xiaomi round button'
    condition:
      condition: and
      conditions:
      - condition: state
        entity_id: sensor.xiaomi_round
        state: 'long'
      - condition: state
        entity_id: input_boolean.light1_dim
        state: 'on'
      - condition: state
        entity_id: light.tvlampmqtt
        state: 'on'
    action:
    - service: script.turn_on
      entity_id: script.1511119598248

  - alias: Light1 brighten off - bool off
    id: '1511197876722'
    initial_state: 'on'
    trigger:
      platform: mqtt
      topic: 'zigbee2mqtt/Xiaomi round button'
    condition:
      condition: and
      conditions:
      - condition: state
        entity_id: sensor.xiaomi_round
        state: 'long_release'
      - condition: state
        entity_id: input_boolean.light1_dim
        state: 'on'
      - condition: state
        entity_id: light.tvlampmqtt
        state: 'on'
    action:
    - service: input_boolean.turn_off
      entity_id: input_boolean.light1_dim

script: 
  '1511119598248':
    alias: dim_light1
    sequence:
    - condition: state
      entity_id: input_boolean.light1_dim
      state: 'on'
    - service: light.turn_on
      entity_id: light.tvlampmqtt
      data_template:
        brightness: '{{states.light.tvlampmqtt.attributes.brightness - 10}}'
    - service: script.turn_off
      entity_id: script.1511119598248
    - service: script.turn_on
      entity_id: script.1511119598248
    - delay:
       milliseconds: 300

  '1511118763251':
    alias: brighten_light1
    sequence:
    - condition: state
      entity_id: input_boolean.light1_dim
      state: 'off'
    - service: light.turn_on
      entity_id: light.tvlampmqtt
      data_template:
        brightness: '{{states.light.tvlampmqtt.attributes.brightness + 10}}'
    - service: script.turn_off
      entity_id: script.1511118763251
    - service: script.turn_on
      entity_id: script.1511118763251
    - delay:
        milliseconds: 300
1 Like

For anyone interested in here. I have created an AppDaemon app to overcome this issue. You can easily configure a controller with a light or media player (through zigbee2mqtt, deconz and zha) and have all the needed functionality and more. You can check the documentation in here and the topic in the community forum in here.

If you have any question, you can just ask me :slight_smile:

1 Like

Here’s one I’m using at the moment.

# Lighting Automation - Kid bedside light brightness toggle
   - alias: 'Kid bedside brightness toggle'
     trigger:
       platform: state
       entity_id: sensor.0x00158d00027cd9c5_click
       to: 'double'
     action:
       service: light.turn_on
       entity_id: light.kid_bedside_light
       data_template:
         transition: 1
         brightness: >
             {% set suggested = states.light.kid_bedside_light.attributes.brightness | int + 30 %}
             {% if suggested < 240 %}
             {{ suggested }}
             {% else %}
             30
             {% endif %}
1 Like