Dim/Brighten light on a single button long press?

I use an Ikea square switch, that has two buttons. Its connected to my HA via the ZHA integration. Currently. the 0 button is mapped to one group of lights, while the 1 button is mapped to another via toggle. This works fine. The group of lights on button 1 though i want to be able to dim/brighten. Since i have the switch added through ZHA, i have the 3 possible events: move_with_on_off (when 1 is held), move (when 0 is held), stop (when either is released)

Is it possible with the above events, dim / brighten the light group on button 1 via some automation? And if so, how ?

It’s easy to loop the brighten_pct service in node-red. And as of recently, you can easily perform a loop with an automation. Essentially, trigger on the held event and call the dim or brighten service followed by a relay. Continue this in a loop until the button release event is seen. I’m on mobile and can’t post examples, but hopefully I’ve inspired you to start googling! It can be done!

Yes it is possible because you have a hold and release event.

Trigger on the button hold. Then for actions, toggle an input boolean. Either dim or brighten (depending on the state of the input boolean) until the button release event.

I’m a total noob with these automations. However i think i got the trigger part. I’ve set a trigger on the button hold zha_event. I’m using the GUI editor, and when i attempt to set the action as you describe, i don’t know what to do. Theres nothing in the Action type option set to let me set a input boolean i think

Services are listed on the integration page: https://www.home-assistant.io/integrations/input_boolean/#services

You want the input_boolean.toggle service. There’s an example at the bottom of the page.

This is in node-red, but gives you an idea how to form the logic:

The node on the left listens to all ZHA events and feeds the event data through a couple nodes that filter out the specific remote and the specific button pressed (the yellow nodes). This just so happens to also be for an ikea 2 button dimmer.

That “switch” node routes the message to one of five places depending on what event it sees (button 1 press, button 1 hold, button 2 press or hold, and the last for either button being released after a hold. I have mine set up where a simple button 1 or 2 press turns a light on or off. Holding either button here dims the light up or down.

The ornage node is a “looptimer.” Upon being triggered by a hold, this node sends a message to the brighten or dim node to call the brightness_step_pct service. A positive value (say 5) will brighten the light by 5%, a negative value (say -5) will dim it by that amount. In my case, the loop timer node calls this service four times a second, each dimming the light by 5%:

Screen Shot 2020-08-13 at 5.48.32 PM

The looptimer node will stay in the loop until either it times out, or i release the button and send a “stop” message to the node.

I think all of this can now be easily recreated in the built in automation environment in HA. I just haven’t used the new features so you’re on your own if that’s the route you take. I personally find these types of things much easier to handle in node-red.

3 Likes

Came to a similar result : Capture d’écran, le 2020-10-23 à 08.33.37

I use Ikea button switch. On/off, dim up/down.

Is there a way to do something with a home assistant bouton : long press and release?

Thx a lot! It’s exactly what I looked for!

Hello @Francis_Caron and @andynbaker,

could one of you two be so kind and post the flow as an node red export here ?

Would help me a lot as I am quite new with that stuff.

Best regards,
distel

Not perfect, the function node could be better. Just adapt it to your setup.

[{"id":"fa7f4847.7012d8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"50c89d4.6b3f964","type":"switch","z":"fa7f4847.7012d8","name":"Messages","property":"payload.action","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"},{"t":"eq","v":"brightness_move_up","vt":"str"},{"t":"eq","v":"brightness_stop","vt":"str"},{"t":"eq","v":"brightness_move_down","vt":"str"}],"checkall":"true","repair":false,"outputs":5,"x":370,"y":300,"wires":[["653b5408.e0119c"],["31dc0a62.b24f96"],["8121f340.3310a"],["4345ece6.753f14"],["beec4d61.b1991"]]},{"id":"64154b37.69d4e4","type":"mqtt in","z":"fa7f4847.7012d8","name":"IKEA switch 02","topic":"zigbee2mqtt/ikeaswitch02","qos":"1","datatype":"json","broker":"3922a6cd.ff2dca","x":180,"y":300,"wires":[["50c89d4.6b3f964"]]},{"id":"653b5408.e0119c","type":"api-call-service","z":"fa7f4847.7012d8","name":"Lights ON","server":"117b1889.920667","version":1,"debugenabled":true,"service_domain":"homeassistant","service":"turn_on","entityId":"light.bureau","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":580,"y":200,"wires":[[]]},{"id":"31dc0a62.b24f96","type":"api-call-service","z":"fa7f4847.7012d8","name":"Lights OFF","server":"117b1889.920667","version":1,"debugenabled":true,"service_domain":"homeassistant","service":"turn_off","entityId":"light.bureau","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":590,"y":240,"wires":[[]]},{"id":"e0d64368.8198b","type":"function","z":"fa7f4847.7012d8","name":"Brightness up","func":"var step = 15;\nvar brightness = flow.get(\"light_brightness\");\n\nif(brightness > 0){\n    brightness = brightness + step;\n    flow.set(\"light_brightness\", brightness);\n    }\n\t\nif(brightness >= 255){\n    brightness = 254;\n    flow.set(\"light_brightness\", brightness);\n    }\n\nmsg.payload = {\"brightness\":brightness}\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":800,"y":280,"wires":[["8f75fbe1.63ac48"]]},{"id":"fe2da65e.50e228","type":"function","z":"fa7f4847.7012d8","name":"Brightness down","func":"var step = 15;\nvar brightness =  flow.get(\"light_brightness\");\n\nif(brightness <= 255){\n    brightness = brightness - step; \n    flow.set(\"light_brightness\", brightness);\n\t}\n\t\nif(brightness <= 0){\n    brightness = 1;\n    flow.set(\"light_brightness\", brightness);\n    }\n\nmsg.payload = {\"brightness\":brightness}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":810,"y":400,"wires":[["8f75fbe1.63ac48"]]},{"id":"8121f340.3310a","type":"looptimer","z":"fa7f4847.7012d8","duration":"0.4","units":"Second","maxloops":"100","maxtimeout":"1","maxtimeoutunits":"Hour","name":"","x":610,"y":280,"wires":[["e0d64368.8198b"],[]]},{"id":"8f75fbe1.63ac48","type":"api-call-service","z":"fa7f4847.7012d8","name":"Lights UP or DOWN","server":"117b1889.920667","version":1,"debugenabled":true,"service_domain":"homeassistant","service":"turn_on","entityId":"light.bureau","data":"{\"brightness\":\"{{payload.brightness}}\"}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":820,"y":340,"wires":[[]]},{"id":"beec4d61.b1991","type":"looptimer","z":"fa7f4847.7012d8","duration":"0.4","units":"Second","maxloops":"100","maxtimeout":"1","maxtimeoutunits":"Hour","name":"","x":610,"y":400,"wires":[["fe2da65e.50e228"],[]]},{"id":"4345ece6.753f14","type":"change","z":"fa7f4847.7012d8","name":"stop","rules":[{"t":"set","p":"payload","pt":"msg","to":"stop","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":340,"wires":[["8121f340.3310a","beec4d61.b1991"]]},{"id":"950f72d1.b84bb","type":"server-state-changed","z":"fa7f4847.7012d8","name":"Lumière bureau","server":"117b1889.920667","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.bureau, ","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":200,"y":400,"wires":[["73dd394b.bbbbb8"],[]]},{"id":"73dd394b.bbbbb8","type":"change","z":"fa7f4847.7012d8","name":"Brightness","rules":[{"t":"move","p":"data.new_state.attributes.brightness","pt":"msg","to":"light_brightness","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":440,"wires":[[]]},{"id":"3922a6cd.ff2dca","type":"mqtt-broker","name":"","broker":"192.168.2.136","port":"1883","tls":"","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"117b1889.920667","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]
1 Like

Thank you very much !

1 Like

How would be this be implemented with the new hass.io features?

replicated this in node red, thanks a lot!
Unfortunately it’s not smooth at all. it seems HA is stopping to accept service calls after some time as it might get “flooded” during dimming up and down? not sure might be my ancient lifx :slight_smile:

Hi tom_l, old post I know, but how do you end the loop on the button release event? Events is not Condition type listed as an Until condition. (if that makes sense?) cheers Andrew
image

Both loop-while and repeat-until are state based, not event based. So you will have to use another automation to turn on and off an input_boolean on the pushed and released events. Then use that input_boolean state to control your looping. Unless you have access to the state of the button, in which case use that directly.

Could I see a screen shot of how you have the (Increase The Level) node set up I’ve been trying and been unsuccessful

I dunt have that flow anymore. I’ve moved back to deconz for zigbee, and this functionality is baked in. Can’t native automations so this much easier now that they can loop?

Hi everyone, sorry for waking up this old thread, anyone found a way to solve toggle dim/bright without using node-red? I’m using the built in feature in deconz at the moment, this is the only reason I haven’t switched to ZHA.

1 Like

Could this automation be made into a blueprint? Would be great if you could map you lights en buttons in this blueprint.

Looking for a solution too! Let me know if you find anything please

1 Like

This works great with my switch:

alias: dim lights
description: ""
trigger:
  - platform: device
    domain: mqtt
    device_id: b3bc085725c505a4957b675cffcc6dcf
    type: action
    subtype: brightness_move_up_l2
    discovery_id: 0xccccccfffe6443d1 action_brightness_move_up_l2
    id: dim-up-l2
  - platform: device
    domain: mqtt
    device_id: b3bc085725c505a4957b675cffcc6dcf
    type: action
    subtype: brightness_move_down_l2
    discovery_id: 0xccccccfffe6443d1 action_brightness_move_down_l2
    id: dim-down-l2
  - platform: device
    domain: mqtt
    device_id: b3bc085725c505a4957b675cffcc6dcf
    type: action
    subtype: brightness_stop_l2
    discovery_id: 0xccccccfffe6443d1 action_brightness_stop_l2
    id: stop-l2
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: dim-up-l2
        sequence:
          - repeat:
              while:
                - condition: trigger
                  id: dim-up-l2
              sequence:
                - service: light.turn_on
                  data:
                    brightness_step: 5
                  target:
                    entity_id: light.dimmer_tak_stue
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 0
                    milliseconds: 200
      - conditions:
          - condition: trigger
            id: dim-down-l2
        sequence:
          - repeat:
              while:
                - condition: trigger
                  id: dim-down-l2
              sequence:
                - service: light.turn_on
                  data:
                    brightness_step: -5
                  target:
                    entity_id: light.dimmer_tak_stue
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 0
                    milliseconds: 200
mode: restart
1 Like