Dimmer control with binary_sensor and NodeRED

With the new release of Homeassistant 0.106, they brought the brightness_step and brightness_step_pct parameters to light.turn_on service.

This gives us the possibilite to increase and decrease brightness with easy!

This code below is for nodered in which we can use a button (any binary_sensor wouldwork) and use to toggle the light with single press and dim the light by holding it.

[{"id":"2ee1bd8f.8ba3e2","type":"api-call-service","z":"821f0103.79925","name":"","server":"1d707c49.07a204","version":1,"debugenabled":false,"service_domain":"light","service":"toggle","entityId":"light.fita_led","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1060,"y":1160,"wires":[[]]},{"id":"3654187b.f4cb78","type":"server-state-changed","z":"821f0103.79925","name":"","server":"1d707c49.07a204","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.sala_b2","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":190,"y":1180,"wires":[["b8234bd5.80f2e8"],["fc400eb.27085f"]]},{"id":"7757b6c4.356e18","type":"looptimer","z":"821f0103.79925","duration":"0.2","units":"Second","maxloops":"10","maxtimeout":"10","maxtimeoutunits":"Second","name":"","x":410,"y":1240,"wires":[["453bc20c.95fb1c"],["e2b6be46.e2ba9"]]},{"id":"fc400eb.27085f","type":"change","z":"821f0103.79925","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"STOP","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":160,"y":1240,"wires":[["7757b6c4.356e18"]]},{"id":"d32e280d.cac588","type":"api-call-service","z":"821f0103.79925","name":"","server":"1d707c49.07a204","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.fita_led","data":"{\"brightness_step_pct\":\"{{dim}}\",\"transition\":0.5}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1090,"y":1240,"wires":[[]]},{"id":"e2b6be46.e2ba9","type":"function","z":"821f0103.79925","name":"","func":"var updown = global.get(\"updown\");\nif(updown){\n    msg.payload = \"ON\";\n} else { msg.payload = \"OFF\";}\nglobal.set(\"updown\", !updown);\nreturn msg;","outputs":1,"noerr":0,"x":130,"y":1280,"wires":[[]]},{"id":"453bc20c.95fb1c","type":"function","z":"821f0103.79925","name":"","func":"var updown = global.get(\"updown\");\nif(updown){\n    msg.dim = 10;\n} else { msg.dim = -10;}\nreturn msg;","outputs":1,"noerr":0,"x":590,"y":1240,"wires":[["d32e280d.cac588"]]},{"id":"b8234bd5.80f2e8","type":"trigger","z":"821f0103.79925","op1":"","op2":"0","op1type":"nul","op2type":"str","duration":"300","extend":false,"units":"ms","reset":"","bytopic":"all","name":"","x":460,"y":1160,"wires":[["31c7fe1f.0b3ea2"]]},{"id":"31c7fe1f.0b3ea2","type":"api-current-state","z":"821f0103.79925","name":"","server":"1d707c49.07a204","version":1,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"binary_sensor.sala_b2","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":730,"y":1160,"wires":[["2ee1bd8f.8ba3e2"],["7757b6c4.356e18"]]},{"id":"1d707c49.07a204","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]
1 Like

Hello , can you share your code using Xiaomi Button?
Thanks

All you have to do is replace the binary_sensor entity I used with the one associated with your Xiaomi Button

Please chceck this

You’ve got to use events. I’ll look into it and post an example

1 Like

There you go. On the first switch node change to yours buttons’ entitty_id and you can use as many buttons as you like, just associate the light you want to dimmer with the switch output related to you button’s entity_id

[{"id":"ce3355a3.2352b8","type":"switch","z":"821f0103.79925","name":"","property":"payload.event.click_type","propertyType":"msg","rules":[{"t":"eq","v":"long_click_press","vt":"str"},{"t":"eq","v":"long_click_release","vt":"str"}],"checkall":"false","repair":false,"outputs":2,"x":590,"y":3080,"wires":[["d33b4e94.99e4e"],["552a94c6.cbb7cc"]]},{"id":"14a2abbb.70fbf4","type":"switch","z":"821f0103.79925","name":"","property":"payload.entity_id","propertyType":"msg","rules":[{"t":"eq","v":"binary_sensor.my_switch","vt":"str"},{"t":"eq","v":"binary_sensor.my_switch_2","vt":"str"},{"t":"eq","v":"binary_sensor.my_switch_3","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":430,"y":3100,"wires":[["ce3355a3.2352b8"],[],[]]},{"id":"d33b4e94.99e4e","type":"looptimer","z":"821f0103.79925","duration":"0.2","units":"Second","maxloops":"20","maxtimeout":"10","maxtimeoutunits":"Second","name":"","x":970,"y":3080,"wires":[["437d308d.d4f01"],["f68c7ab7.cf3528"]]},{"id":"552a94c6.cbb7cc","type":"change","z":"821f0103.79925","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"STOP","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":3120,"wires":[["d33b4e94.99e4e"]]},{"id":"6ea1aa9b.8830b4","type":"server-events","z":"821f0103.79925","name":"","server":"1d707c49.07a204","event_type":"xiaomi_aqara.click","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"x":230,"y":3080,"wires":[["14a2abbb.70fbf4"]]},{"id":"437d308d.d4f01","type":"function","z":"821f0103.79925","name":"","func":"var updown = flow.get(\"updown\");\nif(updown){\n    msg.dim = 5;\n} else { msg.dim = -5;}\nreturn msg;","outputs":1,"noerr":0,"x":1150,"y":3060,"wires":[["ee13fc62.09cfe"]]},{"id":"f68c7ab7.cf3528","type":"function","z":"821f0103.79925","name":"","func":"var updown = flow.get(\"updown\");\nif(updown){\n    msg.payload = \"ON\";\n} else { msg.payload = \"OFF\";}\nflow.set(\"updown\", !updown);\nreturn msg;","outputs":1,"noerr":0,"x":1150,"y":3120,"wires":[[]]},{"id":"ee13fc62.09cfe","type":"api-call-service","z":"821f0103.79925","name":"","server":"1d707c49.07a204","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.escritorio","data":"{\"brightness_step_pct\":\"{{dim}}\",\"transition\":0.2}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1330,"y":3060,"wires":[[]]},{"id":"1d707c49.07a204","type":"server","z":"","name":"Home Assistant [mine]","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]
2 Likes

Something was wrong

This is your and my flow.

This is my Tasmota dimmer config

This is my output from Tasmota console when i hold the button

Does your light feedback works fine in HA? If you change the brightness in tasmota does show right in HA? It appears as if it doesn’t update and HA is sending always the same value

1 Like

I made config via Tasmota Wiki and they example have mistake in brightness_state_topic.
Your code works like a charm. Thanks you for help and patience.

1 Like

Hi

Just made a Dimmer function that works with a Hue dimmer as button and a Fibaro Dimmer as light
Its only 5 nodes and works like a charm.

Hope it helps

[{"id":"7c52eaf0.130834","type":"deconz-input","z":"50b9eb63.a8b7b4","name":"Gang 1e Dimmer","server":"38f1b7c6.db67d8","device":"00:17:88:01:04:f4:22:d2-02-fc00","device_name":"Dimmer 1e : ZHASwitch","topic":"","state":"buttonevent","output":"always","outputAtStartup":false,"x":160,"y":560,"wires":[["8de250cc.58ed5"],[]]},{"id":"1d9501e2.9b359e","type":"api-call-service","z":"50b9eb63.a8b7b4","name":"","server":"8a023b70.1946c8","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.multilevel_power_switch_instance_1_level_2","data":"{\"brightness_step_pct\":\"{{payload}}\",\"transition\":1}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1030,"y":560,"wires":[[]]},{"id":"b447605b.9f405","type":"looptimer","z":"50b9eb63.a8b7b4","duration":"0.1","units":"Second","maxloops":"10000","maxtimeout":"10","maxtimeoutunits":"Second","name":"","x":630,"y":560,"wires":[["c6d9f8eb.6025f8"],[]]},{"id":"8de250cc.58ed5","type":"change","z":"50b9eb63.a8b7b4","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"2003","fromt":"num","to":"STOP","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"3003","fromt":"num","to":"STOP","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"2001","fromt":"num","to":"5","tot":"num"},{"t":"change","p":"payload","pt":"msg","from":"3001","fromt":"num","to":"-5","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":560,"wires":[["b447605b.9f405"]]},{"id":"c6d9f8eb.6025f8","type":"switch","z":"50b9eb63.a8b7b4","name":"","property":"payload","propertyType":"msg","rules":[{"t":"btwn","v":"-5","vt":"num","v2":"5","v2t":"num"}],"checkall":"true","repair":false,"outputs":1,"x":830,"y":560,"wires":[["1d9501e2.9b359e"]]},{"id":"38f1b7c6.db67d8","type":"deconz-server","z":"","name":"ZigBee GW","ip":"10.128.0.47","port":"40850","apikey":"25A978282A","ws_port":"8081","secure":false,"polling":"15"},{"id":"8a023b70.1946c8","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

I know this is an old post, but I wonder, is there anyway to make the flow only increase OR decrease, not do both. For me it increase the light and after it reached max it starts to decrease.

I would like to make one flow for one binary_sensor to increase, and another binary_sensor to decrease.