Hold down to dim / brighten light in Switch Manager

Hi there, I have started using Switch Manager and so far really pleased at how easily it has allowed me to do things like double click to change scenes. I am however at a loss at how to enable hold to dim. I can program a hold function to lower or increase brightness but only by a single step. I need a repeat until is my guess, but because Switch Manager does not allow me to select the switch I cannot specify until hold_release or something similar. My guess is I need something like “Test if a template renders a value equal to true”. I am however not tech savy enough to write that template up against my Z2M dimmer switch. Anyone have this sorted and have some code they can share?

Hi,
yes you’re right you need a repeat and a variable to store the direction you are dimming.

Create the Variable under Variables in the Kebab Menu at the top right.

dim: 0

i use 0 for nothing, 1 for dimming up and -1 for dimming down

next you need to create the Hold script for up and down

- action: switch_manager.set_variables
  data:
    switch_id: "{{ data.switch_id }}"
    variables:
      dim: 1

First you need to set the dimming direction in this case 1 for up

- repeat:
    sequence:
      - action: light.turn_on
        metadata: {}
        data:
          transition: 0.2
          brightness_step: 13
        target:
          device_id: 937cc490ac2f21d71e8b2706d2fa5a7d
      - delay:
          hours: 0
          minutes: 0
          seconds: 0
          milliseconds: 200
    until:
      - condition: template
        value_template: "{{ data.variables.dim != 1 }}"

Then you create the repeat for the actual dimming.

The light.turn_on service is used to change the brightness with the brightness_step amount
i used 13 as it is roughly 5% brightness (5% of 255) per step, using the brightness_step_pct setting directly did not work for my lights. To smoothly dim set the transition to 0.2 s (200 ms). It is important to set a delay of at least the transition time so the lamp can transition to the new brightness first. Change the time to dim slower or faster.
Lastly we have the end conditon we are dimming until the variable is not 1 anymore.

Dimming down is the same just with a negative brightness_step and dim set to -1

- action: switch_manager.set_variables
  data:
    switch_id: "{{ data.switch_id }}"
    variables:
      dim: -1
- repeat:
    sequence:
      - action: light.turn_on
        metadata: {}
        data:
          transition: 0.2
          brightness_step: -133
        target:
          device_id: 937cc490ac2f21d71e8b2706d2fa5a7d
      - delay:
          hours: 0
          minutes: 0
          seconds: 0
          milliseconds: 200
    until:
      - condition: template
        value_template: "{{ data.variables.dim != -1 }}"

To stop the repeat when the button is released dim needs to be reset to 0

- action: switch_manager.set_variables
  data:
    switch_id: "{{ data.switch_id }}"
    variables:
      dim: 0

Use it with both hold(released)

@Domme thank you for taking the time to explain this. I did manage to solve it in a much simpler way.

For hold I use the following:

action: mqtt.publish
metadata: {}
data:
evaluate_payload: false
qos: 0
retain: false
topic: zigbee2mqtt/Bedroom light/set
payload: “{"brightness_move":40}”

And then for hold release I have:

action: mqtt.publish
metadata: {}
data:
evaluate_payload: false
qos: 0
retain: false
topic: zigbee2mqtt/Bedroom light/set
payload: “{"brightness_move":0}”

I was not aware of the direct mqtt.publish command brightness_move. I have now recoded all my remotes to use direct z2m commands with the exception of cycling through scenes where I use an input select helper.

2 Likes

Would you then use value -40 to dim down ?

Correct {“brightness_move”:-40}

I’m struggling at bit with this, and was wondering if anyone could point me in the right direction. Currently using the info above I can get my light (LT002) to increase or decrease brightness on holding the button (BT002), but what I want is to increase and decrease every second hold…

So my thinking was that I’d set the dim parameter to 0 to start with (using the kebab menu, as mentioned above):

dim: 0

and then use conditions to look if it is 0 or 1, use brightness_move +/- a value, and then change the dim parameter. But obviously, something goes wrong and I don’t think my yaml ever looks at the dim variable? (and neither sets it…)

Here’s my complete yaml code:

- choose:
    - conditions:
        - condition: template
          value_template: "{{ data.variables.dim == 0 }}"
      sequence:
        - action: mqtt.publish
          metadata: {}
          data:
            qos: "0"
            topic: zigbee2mqtt/LT002/set
            payload: "{\"brightness_move\":-40}"
          enabled: true
        - action: switch_manager.set_variables
          metadata: {}
          data:
            variables: data.variables.dim == 1
            switch_id: "0"
    - conditions:
        - condition: template
          value_template: "{{ data.variables.dim == 1 }}"
      sequence:
        - action: mqtt.publish
          metadata: {}
          data:
            qos: "0"
            topic: zigbee2mqtt/LT002/set
            payload: "{\"brightness_move\":40}"
          enabled: true
        - action: switch_manager.set_variables
          metadata: {}
          data:
            switch_id: "0"
            variables: data.variables.dim == 0

Hi, sorry, I’m very new to Home Assistant and am in the process of setting up my house with it.

I was just wondering if you could explain a little more about how you set this up?

I used the switch manager for my dimmer switch and it now switches the lights in my office on and off. This is of course not very complicated to set up. Now I want to dim the light and that’s why I’m here. I don’t know where to enter these codes?
I tried to go Dim up → Hold → 3 dots on the right → Yaml Editor → and then I added the code. I did this for up and down and also for the hold (released).

But it is not dimming, so what have I done wrong? Do I need to change something in the code?

Thank you very much for your help!

Did you find a solution for Dimming up and down with just one pushButton ?

Thanks, it worked nicely.
For beginners, here it is with the right formatting :

- action: mqtt.publish
  metadata: {}
  data:
    evaluate_payload: false
    qos: 0
    retain: false
    topic: zigbee2mqtt/Your_Lights/set
    payload: "{\"brightness_move\": 40}"

Adjust the payloads accordingly.

Sorry, no, I think I gave up. Might need to give it another go, it’d be nice to have that function.