Dim/Brighten light on button Long Press

I am running Home assistant on an old Intel NUC via Virtual Box. Since it is using shared resources with other server applications i run on the NUC, such as PLEX server, FTP server, XAMPP (apache etc) i would like to keep it as light as possible.

This is the reason i wouldn’t go to the node red as i believe that will chew up recourses and ultimately slow down the entire set up.

I can back up my virtual machine to a file (that can be deployed again) and give it a try.

If an automation runs in HA or in Node red should not make much difference. The idle of Node red on my Pi4 is:

I assume, when it’s running an automation it will be as much of a load as HA.
Also, making an automation in HA that needs more variables, helpers and steps seems counter intuitive in my opinion if load/performance is the goal.
But I don’t know for sure.

I am backing up my set up and installing Note Red. There only one way to find out! :slight_smile:

Here is a flow you can try.
It’s untested since I’m not home and I don’t have a one button remote.

[{"id":"d1e80f24c1404c45","type":"api-call-service","z":"a787a77.9c16458","name":"","server":"4bbca37b.1700ec","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.kokslampa","data":"{\t    \"brightness\": msg.data.attributes.brightness+15\t}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2430,"y":1200,"wires":[["262a5c1a0ab5d636"]]},{"id":"670fb406fbd06439","type":"api-current-state","z":"a787a77.9c16458","name":"","server":"4bbca37b.1700ec","version":2,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.kokslampa","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":2190,"y":1200,"wires":[["d1e80f24c1404c45"]]},{"id":"262a5c1a0ab5d636","type":"delay","z":"a787a77.9c16458","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":2630,"y":1200,"wires":[["8bda7d028cc01905"]]},{"id":"31edfd71c5043ac7","type":"change","z":"a787a77.9c16458","name":"","rules":[{"t":"set","p":"hold_btn","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1690,"y":1240,"wires":[["7e486ad0a5600a61"]]},{"id":"8bda7d028cc01905","type":"switch","z":"a787a77.9c16458","name":"","property":"hold_btn","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":2390,"y":1120,"wires":[["670fb406fbd06439"]]},{"id":"a0b29eada06560bd","type":"change","z":"a787a77.9c16458","name":"","rules":[{"t":"set","p":"hold_btn","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1730,"y":1180,"wires":[[]]},{"id":"570b6729d783eb93","type":"ha-device","z":"a787a77.9c16458","name":"Hold down","server":"4bbca37b.1700ec","version":0,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"inputs":0,"deviceType":"trigger","device":"770b0eca92df31064acad1ab9a76ec51","event":{"device_id":"770b0eca92df31064acad1ab9a76ec51","domain":"zha","platform":"device","type":"remote_button_long_press","subtype":"dim_down"},"capabilities":[],"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":1500,"y":1240,"wires":[["31edfd71c5043ac7"]]},{"id":"898b9f228a3779cf","type":"ha-device","z":"a787a77.9c16458","name":"Hold down release","server":"4bbca37b.1700ec","version":0,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"inputs":0,"deviceType":"trigger","device":"770b0eca92df31064acad1ab9a76ec51","event":{"device_id":"770b0eca92df31064acad1ab9a76ec51","domain":"zha","platform":"device","type":"remote_button_long_release","subtype":"dim_down"},"capabilities":[],"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":1530,"y":1180,"wires":[["a0b29eada06560bd"]]},{"id":"7e486ad0a5600a61","type":"switch","z":"a787a77.9c16458","name":"","property":"dim_up_down","propertyType":"flow","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1890,"y":1240,"wires":[["670fb406fbd06439","ea2d0a30f1cc1a6f"],["a5b4c8483fefb419","d2400292384d0f84"]]},{"id":"ea2d0a30f1cc1a6f","type":"change","z":"a787a77.9c16458","name":"","rules":[{"t":"set","p":"dim_up_down","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":2060,"y":1100,"wires":[[]]},{"id":"a5b4c8483fefb419","type":"change","z":"a787a77.9c16458","name":"","rules":[{"t":"set","p":"dim_up_down","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":2060,"y":1400,"wires":[[]]},{"id":"d2400292384d0f84","type":"api-current-state","z":"a787a77.9c16458","name":"","server":"4bbca37b.1700ec","version":2,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.kokslampa","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":2190,"y":1280,"wires":[["cb6b78e7e1319641"]]},{"id":"43d638cefe4dd9bc","type":"switch","z":"a787a77.9c16458","name":"","property":"hold_btn","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":2390,"y":1360,"wires":[["d2400292384d0f84"]]},{"id":"cb6b78e7e1319641","type":"api-call-service","z":"a787a77.9c16458","name":"","server":"4bbca37b.1700ec","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.kokslampa","data":"{\t    \"brightness\": msg.data.attributes.brightness-15\t}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2430,"y":1280,"wires":[["dbb6714f9cf7f5f1"]]},{"id":"dbb6714f9cf7f5f1","type":"delay","z":"a787a77.9c16458","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":2630,"y":1280,"wires":[["43d638cefe4dd9bc"]]},{"id":"4bbca37b.1700ec","type":"server","name":"Home Assistant","version":1,"legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

It could probably have been made simpler/smaller but this is what I came up with remotly.

The nodes that is marked with red is the ones you need to put some attention to.
You need to replace the device with your device long press/release command and the light entity

This dims up/down every other time you press it.

3 Likes

Just using a tweak of your logic above to try and get a button dimming and brightening my blind and got a bit of an odd thing happening… it works, but only twice…

[{"id":"e8e6290c0de09c90","type":"server-state-changed","z":"59430083fba16f20","name":"Study Light Switch","server":"3aa13fed.bf745","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.study_light_switch_action","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":150,"y":660,"wires":[["6cc9216855f24d53","1371276ecd430ab4"]]},{"id":"6cc9216855f24d53","type":"switch","z":"59430083fba16f20","name":"Hold Actions","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"left_top_hold","vt":"str"},{"t":"eq","v":"left_bottom_hold","vt":"str"},{"t":"eq","v":"right_top_hold","vt":"str"},{"t":"eq","v":"right_bottom_hold","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":350,"y":660,"wires":[["df44b2fd95736b35"],["1ecb47afdecdaddf"],["d8315b8b3acdde08"],["6a8878273cf61135"]]},{"id":"df44b2fd95736b35","type":"change","z":"59430083fba16f20","name":"","rules":[{"t":"set","p":"hold_btn_lt","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":600,"wires":[[]]},{"id":"1ecb47afdecdaddf","type":"change","z":"59430083fba16f20","name":"","rules":[{"t":"set","p":"hold_btn_lb","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":640,"wires":[[]]},{"id":"d8315b8b3acdde08","type":"change","z":"59430083fba16f20","name":"","rules":[{"t":"set","p":"hold_btn_rt","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":680,"wires":[["d7b539ce91a0c835"]]},{"id":"6a8878273cf61135","type":"change","z":"59430083fba16f20","name":"","rules":[{"t":"set","p":"hold_btn_rb","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":720,"wires":[["8d0654457eadd171"]]},{"id":"5d4cd4124797a743","type":"api-call-service","z":"59430083fba16f20","name":"","server":"3aa13fed.bf745","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.study_bulb"],"data":"{     \"brightness\": msg.data.attributes.brightness+15 }","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":890,"y":720,"wires":[["95595c2a7ac6add5"]]},{"id":"acb0e4c1adef50af","type":"switch","z":"59430083fba16f20","name":"","property":"hold_btn_rt","propertyType":"flow","rules":[{"t":"eq","v":"true","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":910,"y":640,"wires":[["d7b539ce91a0c835"]]},{"id":"95595c2a7ac6add5","type":"delay","z":"59430083fba16f20","name":"","pauseType":"delay","timeout":"50","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":1070,"y":720,"wires":[["acb0e4c1adef50af"]]},{"id":"d7b539ce91a0c835","type":"api-current-state","z":"59430083fba16f20","name":"","server":"3aa13fed.bf745","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.study_bulb","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":785,"y":720,"wires":[["5d4cd4124797a743"]],"l":false},{"id":"1371276ecd430ab4","type":"debug","z":"59430083fba16f20","name":"debug 24","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":340,"y":580,"wires":[]},{"id":"8d0654457eadd171","type":"api-current-state","z":"59430083fba16f20","name":"","server":"3aa13fed.bf745","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.study_bulb","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":785,"y":780,"wires":[["4bb53e34ae3dfbf0"]],"l":false},{"id":"4bb53e34ae3dfbf0","type":"api-call-service","z":"59430083fba16f20","name":"","server":"3aa13fed.bf745","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.study_bulb"],"data":"{     \"brightness\": msg.data.attributes.brightness-15 }","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":890,"y":780,"wires":[["d001348625689d84"]]},{"id":"d001348625689d84","type":"delay","z":"59430083fba16f20","name":"","pauseType":"delay","timeout":"50","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":1070,"y":780,"wires":[["59b6a2eea7648bd0"]]},{"id":"59b6a2eea7648bd0","type":"switch","z":"59430083fba16f20","name":"","property":"hold_btn_rb","propertyType":"flow","rules":[{"t":"eq","v":"true","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":970,"y":860,"wires":[["8d0654457eadd171"]]},{"id":"3aa13fed.bf745","type":"server","name":"Home Assistant","version":4,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m"}]

I suspect it might be because on my button, for some reason holding it (or any action actually) give a blank state response immediately after the actual state change
image

Is there any way to ignore this?
Is this flow still the best way of doing this?

I guess you can add a switch node to filter out any that is empty.
Not sure what that will do to the sequence though.

Sadly didn’t work… the state node still picks up the state change so it murders that ongoing flow…
As a long shot I wondered if the below would work
image
But even though it’s blank it’s still known / available

Wondering if there is a way to manually ignore some other state…

I also tried using a trigger node instead but the same problem that the trigger then fails the check

[{"id":"9a4d8882d23268a7","type":"trigger-state","z":"59430083fba16f20","name":"Right Bottom Hold","server":"3aa13fed.bf745","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"sensor.study_light_switch_action","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"right_bottom_hold"}],"inputs":0,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","enableInput":false,"x":130,"y":540,"wires":[["d1758408ae0a41e0"],[]]},{"id":"d1758408ae0a41e0","type":"change","z":"59430083fba16f20","name":"","rules":[{"t":"set","p":"hold_btn_rb","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":540,"wires":[["3b3bdef2436bfc7c"]]},{"id":"3b3bdef2436bfc7c","type":"api-current-state","z":"59430083fba16f20","name":"","server":"3aa13fed.bf745","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.study_bulb","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":605,"y":600,"wires":[["0294170206cc2f11"]],"l":false},{"id":"c4d113215f9048d1","type":"switch","z":"59430083fba16f20","name":"","property":"hold_btn_rb","propertyType":"flow","rules":[{"t":"eq","v":"true","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":810,"y":660,"wires":[["3b3bdef2436bfc7c"]]},{"id":"0294170206cc2f11","type":"api-call-service","z":"59430083fba16f20","name":"","server":"3aa13fed.bf745","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.study_bulb"],"data":"{     \"brightness\": msg.data.attributes.brightness-15 }","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":810,"y":600,"wires":[["218031f10ac994c9"]]},{"id":"aa4369a23d3d4fcd","type":"trigger-state","z":"59430083fba16f20","name":"Right Top Hold","server":"3aa13fed.bf745","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"sensor.study_light_switch_action","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"right_top_hold"}],"inputs":0,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","enableInput":false,"x":140,"y":500,"wires":[["d622527c69016cd5"],[]]},{"id":"d622527c69016cd5","type":"change","z":"59430083fba16f20","name":"","rules":[{"t":"set","p":"hold_btn_rt","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":500,"wires":[["986974456c92a99d"]]},{"id":"986974456c92a99d","type":"api-current-state","z":"59430083fba16f20","name":"","server":"3aa13fed.bf745","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.study_bulb","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":605,"y":540,"wires":[["581bdc874bf62849"]],"l":false},{"id":"581bdc874bf62849","type":"api-call-service","z":"59430083fba16f20","name":"","server":"3aa13fed.bf745","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.study_bulb"],"data":"{     \"brightness\": msg.data.attributes.brightness+15 }","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":810,"y":540,"wires":[["8c71e0fa990452f4"]]},{"id":"bcae84ec994df04f","type":"switch","z":"59430083fba16f20","name":"","property":"hold_btn_rt","propertyType":"flow","rules":[{"t":"eq","v":"true","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":810,"y":480,"wires":[["986974456c92a99d"]]},{"id":"218031f10ac994c9","type":"delay","z":"59430083fba16f20","name":"","pauseType":"delay","timeout":"50","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":1070,"y":600,"wires":[["c4d113215f9048d1"]]},{"id":"8c71e0fa990452f4","type":"delay","z":"59430083fba16f20","name":"","pauseType":"delay","timeout":"50","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":1070,"y":540,"wires":[["bcae84ec994df04f"]]},{"id":"3aa13fed.bf745","type":"server","name":"Home Assistant","version":4,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m"}]

Where is the switch node I suggested?

Not there because I already tested that and it didn’t work (as mentioned)
I was showing the alternate path I’m exploring

The switch test was in the other flow :wink:

Irrespective, something very strange is going on tho - debugging further it seems that sometimes (but not always), the hold action triggers twice on this particular switch

So these 4 actions are a single hold - not just a stray empty string, but actually a stray repeat as well
image

This explains the “twice only” thing I was seeing - it was triggering the flow twice - it doesn’t seem to be traversing the loop for some reason however…

I also tried using a complete node to separate out the bits but again it triggers but doesn’t seem to traverse the loop properly for some reason :confused:

[{"id":"581bdc874bf62849","type":"api-call-service","z":"59430083fba16f20","name":"","server":"3aa13fed.bf745","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.study_bulb"],"data":"{     \"brightness\": msg.data.attributes.brightness+15 }","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":970,"y":500,"wires":[["8c71e0fa990452f4"]]},{"id":"0294170206cc2f11","type":"api-call-service","z":"59430083fba16f20","name":"","server":"3aa13fed.bf745","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.study_bulb"],"data":"{     \"brightness\": msg.data.attributes.brightness-15 }","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1050,"y":540,"wires":[["218031f10ac994c9"]]},{"id":"218031f10ac994c9","type":"delay","z":"59430083fba16f20","name":"","pauseType":"delay","timeout":"50","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":1250,"y":540,"wires":[["c4d113215f9048d1"]]},{"id":"c4d113215f9048d1","type":"switch","z":"59430083fba16f20","name":"","property":"hold_btn_right_bottom","propertyType":"flow","rules":[{"t":"eq","v":"true","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":970,"y":620,"wires":[["3b3bdef2436bfc7c","ee3ac0dd8a80efe9"]]},{"id":"bcae84ec994df04f","type":"switch","z":"59430083fba16f20","name":"","property":"hold_btn_right_top","propertyType":"flow","rules":[{"t":"eq","v":"true","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":970,"y":440,"wires":[["986974456c92a99d","ee950c34e83e71db"]]},{"id":"8c71e0fa990452f4","type":"delay","z":"59430083fba16f20","name":"","pauseType":"delay","timeout":"50","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":1230,"y":500,"wires":[["bcae84ec994df04f"]]},{"id":"986974456c92a99d","type":"api-current-state","z":"59430083fba16f20","name":"","server":"3aa13fed.bf745","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.study_bulb","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":765,"y":500,"wires":[["581bdc874bf62849"]],"l":false},{"id":"3b3bdef2436bfc7c","type":"api-current-state","z":"59430083fba16f20","name":"","server":"3aa13fed.bf745","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.study_bulb","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":865,"y":540,"wires":[["0294170206cc2f11"]],"l":false},{"id":"06274c7b846f7f0b","type":"complete","z":"59430083fba16f20","name":"Complete: hold_btn_right_top","scope":["d8315b8b3acdde08"],"uncaught":false,"x":580,"y":500,"wires":[["986974456c92a99d","6b7ebc4170efdeca"]]},{"id":"e250fd4ce1b37371","type":"complete","z":"59430083fba16f20","name":"Complete: hold_btn_right_bottom","scope":["6a8878273cf61135"],"uncaught":false,"x":590,"y":540,"wires":[["3b3bdef2436bfc7c","6b7ebc4170efdeca"]]},{"id":"4027532693ecdaca","type":"debug","z":"59430083fba16f20","name":"Bottom Switch","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1100,"y":780,"wires":[]},{"id":"6b7ebc4170efdeca","type":"debug","z":"59430083fba16f20","name":"Is Complete Object being Called","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":660,"wires":[]},{"id":"ee3ac0dd8a80efe9","type":"change","z":"59430083fba16f20","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"hold_btn_right_bottom","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1020,"y":720,"wires":[["4027532693ecdaca"]]},{"id":"3624ae76341cf1e5","type":"debug","z":"59430083fba16f20","name":"Top Switch","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1170,"y":380,"wires":[]},{"id":"ee950c34e83e71db","type":"change","z":"59430083fba16f20","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"hold_btn_right_top","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1100,"y":320,"wires":[["3624ae76341cf1e5"]]},{"id":"df44b2fd95736b35","type":"change","z":"59430083fba16f20","name":"","rules":[{"t":"set","p":"hold_btn_lt","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":780,"wires":[[]]},{"id":"1ecb47afdecdaddf","type":"change","z":"59430083fba16f20","name":"","rules":[{"t":"set","p":"hold_btn_lb","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":820,"wires":[[]]},{"id":"d8315b8b3acdde08","type":"change","z":"59430083fba16f20","name":"","rules":[{"t":"set","p":"hold_btn_right_top","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":860,"wires":[[]]},{"id":"6a8878273cf61135","type":"change","z":"59430083fba16f20","name":"","rules":[{"t":"set","p":"hold_btn_right_bottom","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":900,"wires":[[]]},{"id":"6cc9216855f24d53","type":"switch","z":"59430083fba16f20","name":"Hold Actions","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"left_top_hold","vt":"str"},{"t":"eq","v":"left_bottom_hold","vt":"str"},{"t":"eq","v":"right_top_hold","vt":"str"},{"t":"eq","v":"right_bottom_hold","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":290,"y":840,"wires":[["df44b2fd95736b35"],["1ecb47afdecdaddf"],["d8315b8b3acdde08"],["6a8878273cf61135"]]},{"id":"e8e6290c0de09c90","type":"server-state-changed","z":"59430083fba16f20","name":"Study Light Switch","server":"3aa13fed.bf745","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.study_light_switch_action","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":110,"y":840,"wires":[["6cc9216855f24d53"]]},{"id":"3aa13fed.bf745","type":"server","name":"Home Assistant","version":4,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m"}]

OMG… I’m such a muppet

NONE of the above matters… the loop was failing as I had string true as opposed to ‘is true’ in the switch :sob:

I came across the same issue when setting up my ShellyI4 and i may have found the issue.

The automation “mode” is standard set to single, when the trigger is met the automation just starts the action and that’s it.

What i needed was for the automation mode to be set to “restart” so the automation will restart the action based on the new trigger (e.g. release, or “btn_up” for shelly). Combined with repeat/until this kills the actions when the release trigger is met.

Example:

alias: Shellybtndown
description: ""
trigger:
  - platform: device
    device_id: ShellyI4ID
    domain: shelly
    type: btn_down
    subtype: button1
    id: hold
  - platform: device
    device_id: ShellyI4ID
    domain: shelly
    type: btn_up
    subtype: button1
    id: release
condition: []
action:
  - repeat:
      until:
        - condition: trigger
          id: release
      sequence:
        - device_id: LightID
          domain: light
          entity_id: light.woonkamer_stalamp
          type: brightness_increase
mode: restart

Hope this helps

1 Like

I would really like to steal your code of this for my Aqara 3-button device, but HA is not having it.

I managed to make it work as single-shot using the graphical editor:

alias: Study2 decrease hold
description: ""
trigger:
  - device_id: 5c436d9650bd53e7232c0733905f2acb
    domain: zha
    platform: device
    type: remote_button_long_press
    subtype: button_1
condition: []
action:
  - device_id: 37b49fbe56bc2b59b08df81ab6c89adf
    domain: light
    entity_id: light.gledopto_gl_c_007_light
    type: brightness_decrease
mode: restart

But If I modify that to wat you have, I get this when I save:

Message malformed: Invalid trigger configuration

This is what I tried to set up:

alias: Study2 decrease hold
description: ""
trigger:
  - device_id: 5c436d9650bd53e7232c0733905f2acb
    domain: zha
    platform: device
    type: remote_button_long_press
    subtype: button_1
condition: []
action:
  - device_id: 37b49fbe56bc2b59b08df81ab6c89adf
    domain: light
    entity_id: light.gledopto_gl_c_007_light
    type: brightness_decrease
mode: restart

I am also using Aqara scene Controllers with HA to dim my lights that are connected via zigbee2mqtt (since your light device is called gledopto_gl_c_007 I assume you’re using zigbee too?!). Most Zigbee Lights (actually all I have here, Osram, GLEDOPTO, Ledvance) implement a “brightness_move” and “color_move” function that’s quite handy in these situations since it does not flood your zigbee network with thousands of “set brightness to X” messages but only one message to start the brightness/color move and another one to stop it. My current automation for these light looks like this:

Dim down:

alias: TEST_SC_OFFICE_DIM_DOWN
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.sc_test_01_action
    to: button_3_hold
condition: []
action:
  - service: mqtt.publish
    data:
      topic: zigbee2mqtt/office_lights/set
      payload: "{\"brightness_move\":-50}"
  - wait_for_trigger:
      - platform: state
        entity_id:
          - sensor.sc_test_01_action
        to: button_3_release
  - service: mqtt.publish
    data:
      topic: zigbee2mqtt/office_lights/set
      payload: "{\"brightness_move\":0}"
mode: single

Dim up:

alias: TEST_SC_OFFICE_DIM_UP
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.sc_test_01_action
    to: button_4_hold
condition: []
action:
  - service: mqtt.publish
    data:
      topic: zigbee2mqtt/office_lights/set
      payload: "{\"brightness_move\":50}"
  - wait_for_trigger:
      - platform: state
        entity_id:
          - sensor.sc_test_01_action
        to: button_4_release
  - service: mqtt.publish
    data:
      topic: zigbee2mqtt/office_lights/set
      payload: "{\"brightness_move\":0}"
mode: single

Since I couldn’t find a way to fire a brightness_move event (I’m quite new to HA - migrated from ioBroker two weeks ago) I just send the required MQTT messages “manually” by calling the mqtt.publish service.

Hope that solves your problem.

1 Like

I know this topic is silent for a while but when I was looking for a way to dim my ledstrip I found this thread.
I fixed it by adding a boolean helper

input_boolean.twinkly_slaapkamer_dimming

This is my automation for my Hue Smart button (1 pressable button with 5 events)
Simplefied the script since I use this with 2 buttons, left and a right one

description: "Hue Smart Button Slaapkamer Long Pressed"
mode: single
trigger:
  - platform: state
    entity_id:
      - event.hue_smart_button_slaapkamer_left
    attribute: event_type
    to: repeat
condition: []
action:
  - if:
      - condition: state
        entity_id: input_boolean.twinkly_slaapkamer_dimming
        state: "on"
    then:
      - repeat:
          sequence:
            - device_id: 54291e227b8ef6e114eaf2155995dcff
              domain: light
              entity_id: 6826e3a7b0360c251683e8e18db34ded
              type: brightness_increase
          until:
            - condition: state
              entity_id: event.hue_smart_button_slaapkamer_left
              attribute: event_type
              state: long_release
    else:
      - repeat:
          sequence:
            - device_id: 54291e227b8ef6e114eaf2155995dcff
              domain: light
              entity_id: 6826e3a7b0360c251683e8e18db34ded
              type: brightness_decrease
          until:
            - condition: state
              entity_id: event.hue_smart_button_slaapkamer_left
              attribute: event_type
              state: long_release
  - service: input_boolean.toggle
    data: {}
    target:
      entity_id: input_boolean.twinkly_slaapkamer_dimming

Hopefully this is usefull for someone

3 Likes

I just ran across this challenge myself and wanted to share what’s working for me. Thanks to Hotfield and others for their solutions, which I started with. The main trick is setting the mode to “Restart” and triggering on both the button being pressed and the button being released. This means that when the button is released, the automation run from the button being pressed gets cancelled. The next trick is to have the action do nothing when the trigger id is not button-pressed. Third trick is to terminate the loop when it has reached too many iterations, so it doesn’t run forever in case the release event is not received - thanks to this post for how to do that. The final trick is to add a delay to the loop to rate-limit the dim commands sent - adjust for your preferred dimming speed. For my particular button (IKEA Rodret) the final trick is triggering on “Dim up released after long press” even for the dim down command, because the Rodret appears to send a “Dim up released” event when the dim down button is released (perhaps this is a bug?). For good measure I cancel the automation if either dim up or dim down are released, in case the behavior changes in the future.

Here’s my YAML for the dimming automation:

alias: Rodret => Master Bedroom Dimmer
description: ""
trigger:
  - device_id: 09cb83cd15c47ad4c213f0082f5f5f2e
    domain: zha
    platform: device
    type: remote_button_long_press
    subtype: dim_down
    id: pressed
  - device_id: 09cb83cd15c47ad4c213f0082f5f5f2e
    domain: zha
    platform: device
    type: remote_button_long_release
    subtype: dim_up
  - device_id: 09cb83cd15c47ad4c213f0082f5f5f2e
    domain: zha
    platform: device
    type: remote_button_long_release
    subtype: dim_down
condition: []
action:
  - repeat:
      sequence:
        - device_id: 190d09647566c764d4ef3416ebd7de16
          domain: light
          entity_id: 5b441d76bd3feaa8ad316a8244839860
          type: brightness_decrease
        - delay:
            hours: 0
            minutes: 0
            seconds: 0
            milliseconds: 350
      while:
        - condition: and
          conditions:
            - condition: trigger
              id:
                - pressed
            - condition: template
              value_template: "{{ repeat.index < 20 }}"
mode: restart

In the GUI it looks like this:

2 Likes

Smooth dimming!

Here is slightly improved version of the above thanks for inspiring me! The main change is targeting the device using light.turn_on action, in order to use the nice API where we include a brightness change in percentage-points and a transition time. This also means just targeting the device_id and not also the entity_id.

This works with my Ikea trådfri switch and Ikea Ormanas LED strip. For dimming up, just replace -20 with 20 and ..._down with ..._up.

alias: Dim this down
description: ""
trigger:
  # Either long pressing or releasing will trigger this action
  - device_id: d6eff8819416684ba3ac23f9b4a16346
    domain: zha
    platform: device
    type: remote_button_long_press
    subtype: dim_down
    id: pressed
  - device_id: d6eff8819416684ba3ac23f9b4a16346
    domain: zha
    platform: device
    type: remote_button_long_release
    subtype: dim_down
condition: []
action:
  - repeat:
      sequence:
        - action: light.turn_on
          target:
            device_id: 6a3368aacc82ae15e7b5f7bd87d390f5
          data:
            # For 5 steps, we use -20%
            brightness_step_pct: -20
            transition: 0.350 # seconds, matches 350 below for smoothness
        - delay:
            hours: 0
            minutes: 0
            seconds: 0
            milliseconds: 350
      while:
        - condition: and
          conditions:
            - condition: trigger
              id:
                - pressed
            - condition: template
              value_template: "{{ repeat.index < 20 }}"
mode: restart
2 Likes

I recently started diving into the world of smart homes and wanted to use the Aqara Mini Button (via Zigbee) to dim my office lights by holding it. After some trial and error, I figured it out. I’ll admit, it can be frustrating trying to describe such a simple task instead of just writing code, but here’s what worked for me. Hopefully, this will help anyone else looking to do the same.

My Setup:

  • Aqara Mini Button with Zigbee2MQTT.
  • The key is being able to capture the “hold” event from the button.

Step 1: Create a Helper

  1. Go to Settings > Devices & Services.
  2. At the top, select Helpers.
  3. In the bottom-right, click CREATE HELPER and choose Text.
  4. Name the helper (I used “Minibutton Last State”).

Step 2: Create an Automation to Store the Button’s Last Event

  1. Go to Settings > Automations & Scenes.
  2. In the bottom-right, click CREATE AUTOMATION > Create new automation.
  3. Click the three dots at the top-right and select Edit in YAML.
  4. Paste the code below, but make sure to:
  • Change the entity_id to match your button.
  • If you named the helper something other than “minibutton_last_state,” update the entity_id of the helper as well.
alias: Update Mini Button Last State
trigger:
  - platform: state
    entity_id: sensor.0x54ef441000a859fe_action
action:
  - choose:
      - conditions:
          # important. the state might return empty string(idle?) if you held the button too long. So only store active states.
          - condition: template
            value_template: "{{ trigger.to_state.state != '' }}"
        sequence:
          - target:
              entity_id: input_text.minibutton_last_state
            data:
              value: "{{ trigger.to_state.state }}"
            action: input_text.set_value

Step 3: Create Another Automation for Dimming the Lights

The following automation will:

  1. Increase brightness if it’s below 50% or if the light is off.
  2. Dim the light if the brightness is above 50%, until the light turns off.
  3. The brightness step is set to 5% every 0.1 second, but you can adjust this.

Make sure to change the entity_id to match your devices.

alias: Office Light Dimmer
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.0x54ef441000a859fe_action
    to: hold
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: light.office_all
            state: "on"
          - condition: template
            value_template: "{{ state_attr('light.office_all', 'brightness') | int > 127 }}"
        sequence:
          - repeat:
              while:
                - condition: template
                  value_template: "{{ states('input_text.minibutton_last_state') == 'hold' }}"
              sequence:
                - target:
                    entity_id: light.office_all
                  data:
                    brightness_step: -8
                  action: light.turn_on
                - delay: 0.1
      - conditions:
          - condition: state
            entity_id: light.office_all
            state: "on"
          - condition: template
            value_template: "{{ state_attr('light.office_all', 'brightness') | int <= 127 }}"
        sequence:
          - repeat:
              while:
                - condition: template
                  value_template: "{{ states('input_text.minibutton_last_state') == 'hold' }}"
              sequence:
                - target:
                    entity_id: light.office_all
                  data:
                    brightness_step: 8
                  action: light.turn_on
                - delay: 0.1
      - conditions:
          - condition: state
            entity_id: light.office_all
            state: "off"
        sequence:
          - target:
              entity_id: light.office_all
            data:
              brightness: 0
            action: light.turn_on
          - repeat:
              while:
                - condition: template
                  value_template: "{{ states('input_text.minibutton_last_state') == 'hold' }}"
              sequence:
                - target:
                    entity_id: light.office_all
                  data:
                    brightness_step: 8
                  action: light.turn_on
                - delay: 0.1


A Note on Automations

If you try using a single automation with a “while” loop and checking conditions inside it, new events might not trigger the same code properly, which can cause issues. For me, separating this into two automations fixed the problem.

I’m new to this ecosystem, so this might not be a universal pitfall, but it worked for me. I hope this helps!

2 Likes

Hello,

I also used the valuable information from this thread (and some others, thank you guys!) to make two automations for the Ikea Rodret 2-button dimmer (ZHA). My goal was to have continuous dimming on long press but I also wanted to use double-click for e.g. max and min brightness. As far as I understood all these automation/trigger things need different mode for these two different use-cases. For long press and continuously doing something but also stop on button up (a different trigger) we need mode = restart as this terminates running automations if new trigger arrives (“button up”). For double-click recognition we need to stay in the running automation even if new trigger arrives, so mode = single.

Extra tip
Check with Developer Tools/Events/Listen (event type zha_event in my case) which events are actually sent by your remote buttons and in which order for button down/button up.

Here the two automations (maybe I will later try to make some blueprints from these):

Rodret_dimmer_template

  • see variables section to configure (var_remote = device id of your Rodret dimmer, open the device and copy from URL)
  • description
    ** hold <on> = dim up
    ** hold <off> = dim down
alias: Rodret_dimmer_template
description: |
  "Ikea Rodret 2 button dimmer:  <hold on> = dim up, <hold off> = dim down
trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: "{{ var_remote }}"
      command: move_with_on_off
    id: dim_up
  - platform: event
    event_type: zha_event
    event_data:
      device_id: "{{ var_remote }}"
      command: move
    id: dim_down
  - platform: event
    event_type: zha_event
    event_data:
      device_id: "{{ var_remote }}"
      command: stop_with_on_off
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - dim_up
              - dim_down
        sequence:
          - repeat:
              sequence:
                - choose:
                    - conditions:
                        - condition: trigger
                          id: dim_up
                      sequence:
                        - action: light.turn_on
                          target:
                            entity_id: "{{ var_light }}"
                          data:
                            brightness_step_pct: "{{ var_step_pct }}"
                            transition: "{{ var_delay }}"
                    - conditions:
                        - condition: trigger
                          id: dim_down
                      sequence:
                        - action: light.turn_on
                          target:
                            entity_id: "{{ var_light }}"
                          data:
                            brightness_step_pct: "-{{ var_step_pct }}"
                            transition: "{{ var_delay }}"
                - delay: "{{ var_delay }}"
              while:
                - condition: template
                  value_template: "{{ repeat.index < 20 }}"
variables:
  var_light: light.testlampe1_licht
  var_delay: 0.3
  var_step_pct: 15
trigger_variables:
  var_remote: 343189cd10b641d95d86bf56162cfa37
mode: restart

Rodret_on_off_template

  • see variables section to configure (var_remote = device id of your Rodret dimmer, open the device and copy from URL)
  • description
    ** <on> = on
    ** <off> = off
    ** <on> while on = toggles through color temps
    ** double-click <on> = max brightness
    ** double-click <off> = min brightness
alias: Rodret_on_off_template
description: >
  "Ikea Rodret 2 button dimmer:  <on> = on,  <off> = off, <on> while on =
  toggles through color temps", <double-click on> = max brightness,
  <double-click off> = min brightness
trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: "{{ var_remote }}"
      command: "on"
    id: "on"
  - platform: event
    event_type: zha_event
    event_data:
      device_id: "{{ var_remote }}"
      command: "off"
    id: "off"
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: "on"
        sequence:
          - wait_for_trigger:
              - platform: event
                event_type: zha_event
                event_data:
                  device_id: "{{ var_remote }}"
                  command: "on"
            timeout: "{{var_interval}}"
          - if:
              - condition: template
                value_template: "{{(as_timestamp(now()) - var_time ) < var_interval }}"
            then:
              - action: light.turn_on
                target:
                  entity_id: "{{ var_light }}"
                data:
                  brightness_pct: 100
            else:
              - if:
                  - alias: if already on toggle through color temps
                    condition: template
                    value_template: "{{is_state(var_light, 'on')}}"
                then:
                  - action: light.turn_on
                    target:
                      entity_id: "{{ var_light }}"
                    data:
                      color_temp_kelvin: >
                        {{iif(state_attr(var_light,'color_temp_kelvin') ==
                        var_max_color_temp, var_min_color_temp, 
                        iif(state_attr(var_light,'color_temp_kelvin')+var_color_temp_step
                        > var_max_color_temp, var_max_color_temp,
                        state_attr(var_light,'color_temp_kelvin')+var_color_temp_step))}}
                else:
                  - action: light.turn_on
                    target:
                      entity_id: "{{ var_light }}"
      - conditions:
          - condition: trigger
            id: "off"
        sequence:
          - wait_for_trigger:
              - platform: event
                event_type: zha_event
                event_data:
                  device_id: "{{ var_remote }}"
                  command: "off"
            timeout: "{{var_interval}}"
          - if:
              - condition: template
                value_template: "{{(as_timestamp(now()) - var_time ) < var_interval }}"
            then:
              - action: light.turn_on
                target:
                  entity_id: "{{ var_light }}"
                data:
                  brightness_pct: 10
            else:
              - action: light.turn_off
                target:
                  entity_id: "{{ var_light }}"
variables:
  var_light: light.testlampe1_licht
  var_min_color_temp: 2200
  var_max_color_temp: 4000
  var_color_temp_step: 600
  var_time: " {{ as_timestamp(now()) }} "
  var_interval: 0.25
mode: single
max_exceeded: silent
trigger_variables:
  var_remote: 343189cd10b641d95d86bf56162cfa37

Wow… I found this topic while trying to figure out how to use “long_press” on some buttons to be able to just dim or brighten a light… BUT…

(1) I tried one of these solutions and couldn’t get it to work – heck, couldn’t get it to “compile” as the YAML editor kept giving errors.

(2) There must be some easy straightforward way to do this since I’m certain it’s got to be a very common thing that folks will want to do. Is there? (if yes, where can I find it please?)

(3) Looking at a couple of the most recent solutions I see lots of ways to do this, but I haven’t been able to find any RTFM to find out how these YAML solutions “work”. Is there some “doc” about this? e.g. simple testing showed that a couple of different buttons I’ve tried give a long_press trigger only when you release the button – so how do any of these solutions do the dimming / brightening while you’re pressing the button???

Any pointers to info on my questions would be most appreciated. Thanks.

This is awesome! I tried using a Xiaomi mini button and it worked!

Glad it helped!

I’ve created an improved version of the mini button automation that:

  1. Support Single, double, triple, … many-press
  2. Dims the last controlled light by holding
  3. Uses only 1 automation and 2 text helper entities for simplicity

Please note that this blueprint is ideal if you’re using your button exclusively for light controls. For other actions, you can “own” the automation and then edit for your needs.

Features

  • Single, double, triple, … many-press: Toggle lights on/off
  • Hold: dimming the last controlled light (alternates between increasing and decreasing brightness)

Installation

  1. Click the link below to import the blueprint directly into Home Assistant:

    Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

  2. Follow the blueprint setup instructions to configure your button and lights

Enjoy!