K.I.S.S.

Hue Wall Switch Module Dimming Automation for Home Assistant

Cover Image for Hue Wall Switch Module Dimming Automation for Home Assistant
@tkhome
@tkhome

Just wanted to share my setup for making a Philips Hue Wall Switch Module behind a single push button work for both turning a light on or off, as well as dimming up and down when the button is being held.

The desired behavior

The following behavior is how I wanted the light switch to have:

A short press should toggle the light on and off.

Long presses should dim either up or down. When you have dimmed all the way up, the direction changes to down. When you have dimmed all the way down (to ~1%) the direction again changes to dim up.

In addition, to avoid having to dim all the way up or down to change direction, each time you let go of a long press the dim direction also changes. So if you just held the button to dim up from 30% to 60%, if you hold it again it will start dimming down.

Finally, if you start dimming from the off state, it starts dimming upwards.

If this does not match your preferences, you can use my automation as a starting point and make the required changes.

Pre-requisites

  1. Set the correct mode for the Wall Switch Module (default is single rocker switch, needs to be push button). Detailed instructions: https://rudd-o.com/linux-and-free-software/how-to-use-the-philips-hue-wall-switch-module-with-push-button-wall-switches-in-home-assistant

  2. Create an input_boolean helper in Settings > Devices & Services > Helpers to keep track of the dimming direction


The automation(s)

I found that the dimming worked better and more consistently when split in two automations.

Here's the main automation:

alias: Bedroom Light Control with Dimming (dim direction separate)
description: Toggle and dim light with alternating up/down on hold and on limits
trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: e97e4ff2394da667cd21536c7317931a
      command: left_press_release
  - platform: event
    event_type: zha_event
    event_data:
      device_id: e97e4ff2394da667cd21536c7317931a
      command: left_hold
  - platform: event
    event_type: zha_event
    event_data:
      device_id: e97e4ff2394da667cd21536c7317931a
      command: left_hold_release
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.event.data.command == 'left_press_release' }}"
        sequence:
          - service: light.toggle
            target:
              entity_id: light.bedroom_light_light
            data:
              transition: 1
      - conditions:
          - condition: template
            value_template: "{{ trigger.event.data.command == 'left_hold' }}"
        sequence:
          - repeat:
              until:
                - condition: template
                  value_template: >-
                    {{ wait_for_trigger([{'platform': 'event', 'event_type':
                    'zha_event', 'event_data': {'device_id':
                    'e97e4ff2394da667cd21536c7317931a', 'command':
                    'left_hold_release'}}]) }}
              sequence:
                - service: light.turn_on
                  target:
                    entity_id: light.bedroom_light_light
                  data_template:
                    brightness_step: |
                      {% if is_state('input_boolean.dim_direction', 'on') %}
                        {% set current_brightness = state_attr('light.bedroom_light_light', 'brightness') | default(0) %}
                        {% if current_brightness <= 55 %}
                          -{{ current_brightness - 4 }}
                        {% else %}
                          -50
                        {% endif %}
                      {% else %}
                        50
                      {% endif %}
                    transition: 1
                - delay:
                    milliseconds: 500
mode: single

Here's the supporting automation keeping tabs on dimming direction:

alias: "Bedroom Light: Toggle Dim Direction"
description: >-
  Toggle the dimming direction for the light switch hold action. Swaps direction
  when reaching 100% or ~1% (with a 2 sec delay), as well as when turned off or
  the dimming button is released.
trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: e97e4ff2394da667cd21536c7317931a
      command: left_hold_release
    id: dim_release
  - platform: numeric_state
    entity_id:
      - light.bedroom_light_light
    attribute: brightness
    above: 253
    id: max_brightness
  - platform: numeric_state
    entity_id:
      - light.bedroom_light_light
    attribute: brightness
    id: min_brightness
    below: 5
  - platform: device
    type: turned_off
    device_id: d041cafd8ec0d80656ca3a5f41b259dc
    entity_id: 39ad36d4e959c6b7e1331f9a731a2676
    domain: light
    id: lamp_off
condition: []
action:
  - if:
      - condition: trigger
        id:
          - dim_release
      - condition: numeric_state
        entity_id: light.bedroom_light_light
        attribute: brightness
        above: 4
    then:
      - service: input_boolean.toggle
        target:
          entity_id:
            - input_boolean.dim_direction
        data: {}
    alias: Dimming button released
  - if:
      - condition: trigger
        id:
          - max_brightness
    then:
      - delay:
          hours: 0
          minutes: 0
          seconds: 2
          milliseconds: 0
      - service: input_boolean.turn_on
        target:
          entity_id: input_boolean.dim_direction
        data: {}
    alias: Max brightness reached
  - if:
      - condition: trigger
        id:
          - min_brightness
    then:
      - delay:
          hours: 0
          minutes: 0
          seconds: 2
          milliseconds: 0
      - service: input_boolean.turn_off
        target:
          entity_id:
            - input_boolean.dim_direction
        data: {}
    alias: Min brightness reached
  - if:
      - condition: trigger
        id:
          - lamp_off
    then:
      - service: input_boolean.turn_off
        target:
          entity_id:
            - input_boolean.dim_direction
        data: {}
    alias: Lamp turned off
mode: single

You'll need to update the yaml to reference your relevant device ID and entity names.

Hope you find this helpful!