Using Hue Dimmer to brighten and dim light with Node-Red (via Zigbee2mqtt)

I’m pretty happy that I finaly figured out how to use Zigbee2mqtt and Node-Red. It’s like everyone is saying: I should of used Node-Red much sooner.

So i’ve gotten the basics working: motion sensors, regular switches and lights. But I feel like a lion looking at a zebra with the Hue Dimmer: I can’t make head or tail of it.

The Hue Dimmer is connected and it gets data in HA: ‘on-press’ ‘off-press’ and even stuff like the ‘hold’ and ‘hold duration (example 4.23 seconds)’. I can make it turn stuff on or off, but I don’t understand how to make it dim or brighten lights.

Does anyone have a flow for this ready to go? I’d like it to function more or less like it did when it was connected to the Hue Bridge. HueMagic Nodes won’t work: it needs the Hue Bridge.

1 Like

I got it working like this:

  • The switch separates the four button presses (on-press, off-press, up-press, down-press)
  • You can use a 2nd switch to switch based on count
    • Count is in the message at path msg.data.event.new_state.attributes.counter
  • I use a function to populate the JSON with brightness
    • Brightness is in the message path at msg.data.event.new_state.attributes.brightness

Here is the nodered JSON:

[{"id":"ff0bd4c6.5e6208","type":"trigger-state","z":"3da2c349.0ebdcc","name":"Hue Dimmer 2","server":"b8197728.8ac978","entityid":"sensor.0x001788010409379c_action","entityidfiltertype":"exact","debugenabled":false,"constraints":[],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":160,"y":1540,"wires":[["871af3aa.f2eef"],[]]},{"id":"29d53739.4015e8","type":"api-call-service","z":"3da2c349.0ebdcc","name":"Turn off bedside lights","server":"b8197728.8ac978","service_domain":"homeassistant","service":"turn_off","data":"{\"entity_id\":\"light.main_bedroom\"}","mergecontext":"","output_location":"","output_location_type":"none","x":980,"y":1600,"wires":[[]]},{"id":"871af3aa.f2eef","type":"switch","z":"3da2c349.0ebdcc","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on-press","vt":"str"},{"t":"eq","v":"off-press","vt":"str"},{"t":"eq","v":"up-press","vt":"str"},{"t":"eq","v":"down-press","vt":"str"}],"checkall":"true","repair":true,"outputs":4,"x":430,"y":1540,"wires":[["4ee0c9c1.0fd358"],["4448c709.21e6c8"],["f6063295.d2b4a"],["f6063295.d2b4a"]]},{"id":"4ee0c9c1.0fd358","type":"api-call-service","z":"3da2c349.0ebdcc","name":"Turn On James Bedside Light","server":"b8197728.8ac978","service_domain":"light","service":"turn_on","data":"{\"entity_id\":\"light.bedside_james\"}","mergecontext":"","output_location":"","output_location_type":"none","x":730,"y":1480,"wires":[[]]},{"id":"f202f877.ff4ae8","type":"api-call-service","z":"3da2c349.0ebdcc","name":"Turn Off James Bedside Lights","server":"b8197728.8ac978","service_domain":"light","service":"turn_off","data":"{\"entity_id\":\"light.bedside_james\"}","mergecontext":"","output_location":"","output_location_type":"none","x":1010,"y":1540,"wires":[[]]},{"id":"f6063295.d2b4a","type":"function","z":"3da2c349.0ebdcc","name":"Set brightness in payload","func":"msg.payload = {\n domain: 'light',\n service: 'turn_on',\n data: { entity_id: 'light.bedside_james', brightness: msg.data.event.new_state.attributes.brightness }\n};\nreturn msg;","outputs":1,"noerr":0,"x":710,"y":1660,"wires":[["367d8265.bc17ee"]]},{"id":"367d8265.bc17ee","type":"api-call-service","z":"3da2c349.0ebdcc","name":"Set James Bedside Brightness","server":"b8197728.8ac978","service_domain":"light","service":"turn_on","data":"{\"entity_id\":\"light.bedside_james\"}","mergecontext":"","output_location":"payload","output_location_type":"msg","x":1010,"y":1660,"wires":[[]]},{"id":"4448c709.21e6c8","type":"switch","z":"3da2c349.0ebdcc","name":"","property":"data.event.new_state.attributes.counter","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"},{"t":"gte","v":"2","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":650,"y":1560,"wires":[["f202f877.ff4ae8"],["29d53739.4015e8"]]},{"id":"b8197728.8ac978","type":"server","z":"","name":"Home Assistant instance","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]`

Hope it helps

2 Likes

Thanks! I got it working like this:

It pretty much loops this function untill the button is released:

newmsg = {};

    brightness = msg.data.attributes.brightness - 10


newmsg.payload = { data: {"brightness":brightness} };

return newmsg;
2 Likes

Hiya I’m also new to zigbee2mqtt and node red on hassio - so I’m trying to get my head around how you built the above i.e what entity you used in the 1st dimmer state and how the other functions work?

I currently use an automation that looks at the topic from mqtt platform:

  - platform: mqtt
    topic: 'zigbee2mqtt/0x0017880104f48b0f'

which then runs different scripts based on the button press “on-press” - off & on work great but the dimmer function is bad…

any help much appreciated

I have al devices integrated into Home Assistant. So any lights and switches Zigbee2mqtt detects, it’ll automatically add as a light, switch, sensor or whatever. If you don’t have that, maybe try to get that first (I can’t be of any help there, mine worked out of the box when I followed the install guide, I think discovery for mqtt must be true in config).

The flow starts when the button of the dimmer is pressed. Then it’ll get the state of the light it is supposed to dim. The function read the brightness and adds of detracts 10 points and in the next node it will tell the light use that brightness.

Looks great! Can you share the flow json?

Hi !
Just a quick message to thank you for the clear and awesome flows !
I’m just discovering node red, and having already incredible results with my new Philips Hue remote.
Thanks !

1 Like

I just started setting up my logic for the dimmer switch in NodeRED and this topic helps a lot!

I’ve got a question about the press count. I know that Hue recognizes double-click, but the dimmer switch seems to give out only the following states:

  • n_click
  • n_click_up
  • n_hold
  • n_hold_up

(n = button id)

Double-clicks are not recognized as a separate state and usually come in as one event (on rare occasions 2 pop up). Also, there is no msg.data.event.new_state.attributes.counter variable that I could find. Has this changed maybe?

I connected the remote via Zigbee2mqtt, and that had the count. Connecting via the Philips Hue Hub might not - I am not sure!

@MattTheBrofessor can you share the json for your flow? I am looking into the same topic right now and that would totally help me out :slight_smile:

1 Like

@flecmart
I used the example from MattTheBrofessor to build my own dimmerswitch.
On/Off set a fixed brightness and color_temp. With dim and long presses I can adjust brightness and color_temp.

[{"id":"9af1c19c.a6159","type":"mqtt in","z":"539a3525.6fc49c","name":"","topic":"zigbee2mqtt/slaapkamer_dimmer","qos":"2","datatype":"json","broker":"f9ecd49d.268f48","x":170,"y":700,"wires":[["99735d5c.eb1f1"]]},{"id":"99735d5c.eb1f1","type":"switch","z":"539a3525.6fc49c","name":"","property":"payload.action","propertyType":"msg","rules":[{"t":"eq","v":"up-press","vt":"str"},{"t":"eq","v":"down-press","vt":"str"},{"t":"eq","v":"on-press","vt":"str"},{"t":"eq","v":"off-press","vt":"str"},{"t":"eq","v":"on-hold","vt":"str"},{"t":"eq","v":"off-hold","vt":"str"}],"checkall":"true","repair":false,"outputs":6,"x":370,"y":700,"wires":[["eadf4e3a.e42e7"],["45a5598b.4dbd48"],["f9e64995.fa6e68"],["5a0f97b6.4d8e98"],["563f21dc.55754"],["233a9a14.b18886"]]},{"id":"5a0f97b6.4d8e98","type":"change","z":"539a3525.6fc49c","name":"off","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"state\":\"OFF\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":610,"y":740,"wires":[["6913edee.7943f4","c0dc12af.0ae03","f336c9e0.389948"]]},{"id":"6913edee.7943f4","type":"mqtt out","z":"539a3525.6fc49c","name":"","topic":"zigbee2mqtt/slaapkamer_spot3/set","qos":"","retain":"","broker":"f9ecd49d.268f48","x":1100,"y":800,"wires":[]},{"id":"f9e64995.fa6e68","type":"change","z":"539a3525.6fc49c","name":"on ","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"state\":\"ON\",\"brightness\":133,\"color_temp\":400}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":610,"y":700,"wires":[["6913edee.7943f4","f336c9e0.389948","c0dc12af.0ae03"]]},{"id":"c0dc12af.0ae03","type":"mqtt out","z":"539a3525.6fc49c","name":"","topic":"zigbee2mqtt/slaapkamer_spot2/set","qos":"","retain":"","broker":"f9ecd49d.268f48","x":1100,"y":720,"wires":[]},{"id":"f336c9e0.389948","type":"mqtt out","z":"539a3525.6fc49c","name":"","topic":"zigbee2mqtt/slaapkamer_spot1/set","qos":"","retain":"","broker":"f9ecd49d.268f48","x":1100,"y":640,"wires":[]},{"id":"eadf4e3a.e42e7","type":"api-current-state","z":"539a3525.6fc49c","name":"current slpkmr spot","server":"9476b506.f34b38","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.0x0017880102395e56_light","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":570,"y":580,"wires":[["6add284a.cea018"]]},{"id":"6add284a.cea018","type":"function","z":"539a3525.6fc49c","name":"+10","func":"newmsg = {};\n\n    brightness = msg.data.attributes.brightness + 10\n\n\nnewmsg.payload =  {\"state\":\"ON\",\"brightness\":brightness} ;\n\nreturn newmsg;","outputs":1,"noerr":0,"x":770,"y":580,"wires":[["6913edee.7943f4","f336c9e0.389948","c0dc12af.0ae03"]]},{"id":"45a5598b.4dbd48","type":"api-current-state","z":"539a3525.6fc49c","name":"current slpkmr spot","server":"9476b506.f34b38","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.0x0017880102395e56_light","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":570,"y":640,"wires":[["b939e4e7.d56a28"]]},{"id":"b939e4e7.d56a28","type":"function","z":"539a3525.6fc49c","name":"-10","func":"newmsg = {};\n\n    brightness = msg.data.attributes.brightness - 10\n\n\nnewmsg.payload =  {\"state\":\"ON\",\"brightness\":brightness} ;\n\nreturn newmsg;","outputs":1,"noerr":0,"x":770,"y":640,"wires":[["f336c9e0.389948","c0dc12af.0ae03","6913edee.7943f4"]]},{"id":"1a1ba.1d748e46e","type":"debug","z":"539a3525.6fc49c","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1010,"y":900,"wires":[]},{"id":"563f21dc.55754","type":"api-current-state","z":"539a3525.6fc49c","name":"current slpkmr spot","server":"9476b506.f34b38","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.0x0017880102395e56_light","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":570,"y":800,"wires":[["4602c143.30003"]]},{"id":"4602c143.30003","type":"function","z":"539a3525.6fc49c","name":"+50","func":"newmsg = {};\n\n    color_temp = msg.data.attributes.color_temp + 50\n\n\nnewmsg.payload = {\"state\":\"ON\",\"color_temp\":color_temp}\n\nreturn newmsg;","outputs":1,"noerr":0,"x":770,"y":800,"wires":[["1a1ba.1d748e46e","6913edee.7943f4","c0dc12af.0ae03","f336c9e0.389948"]]},{"id":"233a9a14.b18886","type":"api-current-state","z":"539a3525.6fc49c","name":"current slpkmr spot","server":"9476b506.f34b38","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.0x0017880102395e56_light","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":570,"y":860,"wires":[["16f149b6.e792a6"]]},{"id":"16f149b6.e792a6","type":"function","z":"539a3525.6fc49c","name":"-50","func":"newmsg = {};\n\n    color_temp = msg.data.attributes.color_temp - 50\n\n\nnewmsg.payload = {\"state\":\"ON\",\"color_temp\":color_temp}\n\nreturn newmsg;","outputs":1,"noerr":0,"x":770,"y":860,"wires":[["f336c9e0.389948","c0dc12af.0ae03","6913edee.7943f4"]]},{"id":"f9ecd49d.268f48","type":"mqtt-broker","z":"","name":"MQTT HA","broker":"172.16.1.123","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"9476b506.f34b38","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":false,"rejectUnauthorizedCerts":false,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]
2 Likes

I thought i might share my dimmer setup. This turns bedside lifx bulbs on and off and dims them too

[{"id":"242c7d72.2c9cb2","type":"comment","z":"52e6d6ca.2d467","name":"James Bedside Light","info":"","x":320,"y":2140,"wires":[]},{"id":"33f5c29.b14eb3e","type":"api-call-service","z":"52e6d6ca.2d467","name":"James On","server":"9ee46d87.3f71d","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.james_bedside_light","data":"{\"brightness_pct\":100,\"kelvin\":3200,\"transition\":0.5}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":950,"y":2060,"wires":[[]]},{"id":"6298a7ea.97d998","type":"api-call-service","z":"52e6d6ca.2d467","name":"James Off","server":"9ee46d87.3f71d","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.james_bedside_light","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":950,"y":2300,"wires":[[]]},{"id":"ccba635a.6e964","type":"api-call-service","z":"52e6d6ca.2d467","name":"James On","server":"9ee46d87.3f71d","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"{{payload.entity_id}}","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1370,"y":2200,"wires":[[]]},{"id":"deeceae1.be05f8","type":"api-current-state","z":"52e6d6ca.2d467","name":"James","server":"9ee46d87.3f71d","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.james_bedside_light","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":930,"y":2180,"wires":[["947e250c.bccd28"],[]]},{"id":"947e250c.bccd28","type":"function","z":"52e6d6ca.2d467","name":"increase brightness","func":"varBright = msg.data.attributes.brightness;\nvarEntity = msg.data.entity_id;\nif (varBright < 200) {varBrightness = varBright + 50}\nelse {varBrightness = 254}\nmsg.payload = {\"entity_id\":varEntity}\nmsg.payload.data = {\"brightness\":varBrightness}\nreturn msg;","outputs":1,"noerr":0,"x":1130,"y":2180,"wires":[["ccba635a.6e964"]]},{"id":"3f1d293.c27e4d6","type":"api-current-state","z":"52e6d6ca.2d467","name":"James","server":"9ee46d87.3f71d","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.james_bedside_light","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":930,"y":2220,"wires":[["78e34ddf.60ffb4"],[]]},{"id":"78e34ddf.60ffb4","type":"function","z":"52e6d6ca.2d467","name":"decrease brightness","func":"varBright = msg.data.attributes.brightness;\nvarEntity = msg.data.entity_id;\nif (varBright > 50) {varBrightness = varBright - 50}\nelse {varBrightness = 1}\nmsg.payload = {\"entity_id\":varEntity}\nmsg.payload.data = {\"brightness\":varBrightness}\nreturn msg;","outputs":1,"noerr":0,"x":1140,"y":2220,"wires":[["ccba635a.6e964"]]},{"id":"4e56a9.0e080958","type":"api-call-service","z":"52e6d6ca.2d467","name":"Cheryl On","server":"9ee46d87.3f71d","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.cheryl_bedside_light","data":"{\"brightness_pct\":100,\"kelvin\":3200,\"transition\":0.5}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":940,"y":2100,"wires":[[]]},{"id":"223d2dce.cb5922","type":"api-call-service","z":"52e6d6ca.2d467","name":"Cheryl Off","server":"9ee46d87.3f71d","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.cheryl_bedside_light","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":940,"y":2340,"wires":[[]]},{"id":"c36169c8.dd9e18","type":"hue-switch","z":"52e6d6ca.2d467","name":"James Dimmer Switch","bridge":"e81e9b.75df5168","sensorid":"159","skipevents":false,"x":320,"y":2180,"wires":[["1b809f9d.a10a8"]]},{"id":"1b809f9d.a10a8","type":"switch","z":"52e6d6ca.2d467","name":"button switch","property":"payload.name","propertyType":"msg","rules":[{"t":"eq","v":"On","vt":"str"},{"t":"eq","v":"Dim Up","vt":"str"},{"t":"eq","v":"Dim Down","vt":"str"},{"t":"eq","v":"Off","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":540,"y":2180,"wires":[["969eba87.d8024"],["266e6913.c176fe"],["3ea3eda7.f62cc2"],["d1b42a2f.dc11b"]]},{"id":"969eba87.d8024","type":"switch","z":"52e6d6ca.2d467","name":"push type","property":"payload.action","propertyType":"msg","rules":[{"t":"eq","v":"pressed","vt":"str"},{"t":"eq","v":"holded","vt":"str"},{"t":"eq","v":"short released","vt":"str"},{"t":"eq","v":"long released","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":740,"y":2100,"wires":[["33f5c29.b14eb3e"],[],["33f5c29.b14eb3e"],["4e56a9.0e080958"]]},{"id":"d1b42a2f.dc11b","type":"switch","z":"52e6d6ca.2d467","name":"push type","property":"payload.action","propertyType":"msg","rules":[{"t":"eq","v":"pressed","vt":"str"},{"t":"eq","v":"holded","vt":"str"},{"t":"eq","v":"short released","vt":"str"},{"t":"eq","v":"long released","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":740,"y":2280,"wires":[["6298a7ea.97d998"],[],["6298a7ea.97d998"],["223d2dce.cb5922"]]},{"id":"266e6913.c176fe","type":"switch","z":"52e6d6ca.2d467","name":"push type","property":"payload.action","propertyType":"msg","rules":[{"t":"eq","v":"pressed","vt":"str"},{"t":"eq","v":"holded","vt":"str"},{"t":"eq","v":"short released","vt":"str"},{"t":"eq","v":"long released","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":740,"y":2160,"wires":[[],[],["deeceae1.be05f8"],[]]},{"id":"3ea3eda7.f62cc2","type":"switch","z":"52e6d6ca.2d467","name":"push type","property":"payload.action","propertyType":"msg","rules":[{"t":"eq","v":"pressed","vt":"str"},{"t":"eq","v":"holded","vt":"str"},{"t":"eq","v":"short released","vt":"str"},{"t":"eq","v":"long released","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":740,"y":2220,"wires":[[],[],["3f1d293.c27e4d6"],[]]},{"id":"9ee46d87.3f71d","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true},{"id":"e81e9b.75df5168","type":"hue-bridge","z":"","name":"Philips hue","bridge":"192.168.1.5","key":"bzebqVHBEwYSOrxKTGL1Rk2rdHI3O3r3hsatCvJF","interval":"500","disableupdates":false}]
1 Like

Sorry for the late reply. This is the flow for the entire Hue Dimmer Switch

[{"id":"f5060296.38d55","type":"tab","label":"Slaapkamer redundant","disabled":false,"info":""},{"id":"feef0f5d.7aff5","type":"api-call-service","z":"f5060296.38d55","name":"nachtlampje aan","server":"ecc3b049.6889d","service_domain":"light","service":"turn_on","data":"{\"entity_id\":\"light.0x00158d0000cb52ce_light\",\"transition\":\"1.5\",\"brightness_pct\":\"5\"}","mergecontext":"","output_location":"","output_location_type":"none","x":1040,"y":20,"wires":[[]]},{"id":"52610c14.659794","type":"api-call-service","z":"f5060296.38d55","name":"nachtlampje aan","server":"ecc3b049.6889d","service_domain":"light","service":"turn_off","data":"{\"entity_id\":\"light.0x00158d0000cb52ce_light\",\"transition\":\"1.5\"}","mergecontext":"","output_location":"","output_location_type":"none","x":460,"y":660,"wires":[[]]},{"id":"ff6878a7.ccf2f8","type":"function","z":"f5060296.38d55","name":"functie","func":"newmsg = {};\n\n    brightness = msg.data.attributes.brightness + 30;\n    if (brightness < 30)\n    { msg.payload = \"off\";}\n\n\nnewmsg.payload = { data: {\"brightness\":brightness} };\n\nreturn newmsg;","outputs":1,"noerr":0,"x":790,"y":120,"wires":[["a7bd50d6.1ea68"]]},{"id":"32367f3f.4c83e","type":"api-current-state","z":"f5060296.38d55","name":"","server":"ecc3b049.6889d","outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.0x00158d0000cb52ce_light","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":520,"y":120,"wires":[["ff6878a7.ccf2f8"]]},{"id":"a7bd50d6.1ea68","type":"api-call-service","z":"f5060296.38d55","name":"nachtlampje iets feller","server":"ecc3b049.6889d","version":"1","debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.0x00158d0000cb52ce_light","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1020,"y":120,"wires":[[]]},{"id":"88c7a23b.10c6d","type":"server-state-changed","z":"f5060296.38d55","name":"dimmer","server":"ecc3b049.6889d","entityidfilter":"sensor.0x0017880103c88525_action","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is_not","outputs":1,"output_only_on_state_change":true,"x":70,"y":120,"wires":[["de103be2.f7ace8"]]},{"id":"de103be2.f7ace8","type":"switch","z":"f5060296.38d55","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on-press","vt":"str"},{"t":"eq","v":"up-press","vt":"str"},{"t":"eq","v":"up-hold","vt":"str"},{"t":"eq","v":"down-press","vt":"str"},{"t":"eq","v":"down-hold","vt":"str"},{"t":"eq","v":"off-press","vt":"str"}],"checkall":"true","repair":false,"outputs":6,"x":210,"y":120,"wires":[["62146de.a637b94","5b75ea0.566eb18"],["32367f3f.4c83e"],["b16ffc92.8a05f"],["f4c0afab.bad2a"],["2e15335e.745edc"],["52610c14.659794"]]},{"id":"ff074d53.a2f85","type":"function","z":"f5060296.38d55","name":"functie","func":"newmsg = {};\n\n    brightness = msg.data.attributes.brightness - 30;\n    if (brightness < 30)\n    { msg.payload = \"off\";}\n\n\nnewmsg.payload = { data: {\"brightness\":brightness} };\n\nreturn newmsg;","outputs":1,"noerr":0,"x":850,"y":560,"wires":[["872d8d1a.4da6e"]]},{"id":"f4c0afab.bad2a","type":"api-current-state","z":"f5060296.38d55","name":"","server":"ecc3b049.6889d","outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.0x00158d0000cb52ce_light","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":590,"y":560,"wires":[["ff074d53.a2f85"]]},{"id":"872d8d1a.4da6e","type":"api-call-service","z":"f5060296.38d55","name":"nachtlampje iets minder fel","server":"ecc3b049.6889d","version":"1","debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.0x00158d0000cb52ce_light","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1080,"y":560,"wires":[[]]},{"id":"7f845019.5312a","type":"function","z":"f5060296.38d55","name":"functie","func":"newmsg = {};\n\n    brightness = msg.data.attributes.brightness + 30;\n    if (brightness < 30)\n    { msg.payload = \"off\";}\n\n\nnewmsg.payload = { data: {\"transition\":\"0.5\",\"brightness\":brightness} };\n\nreturn newmsg;","outputs":1,"noerr":0,"x":810,"y":220,"wires":[["3d5d0710.302888"]]},{"id":"b16ffc92.8a05f","type":"api-current-state","z":"f5060296.38d55","name":"","server":"ecc3b049.6889d","outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.0x00158d0000cb52ce_light","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":590,"y":220,"wires":[["7f845019.5312a"]]},{"id":"3d5d0710.302888","type":"api-call-service","z":"f5060296.38d55","name":"nachtlampje feller","server":"ecc3b049.6889d","version":"1","debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.0x00158d0000cb52ce_light","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":990,"y":220,"wires":[["310c6290.16a62e"]]},{"id":"4bf28790.348178","type":"api-current-state","z":"f5060296.38d55","name":"release","server":"ecc3b049.6889d","outputs":2,"halt_if":"up-hold","halt_if_type":"str","halt_if_compare":"is_not","override_topic":false,"entity_id":"sensor.0x0017880103c88525_action","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":600,"y":300,"wires":[["b16ffc92.8a05f"],[]]},{"id":"310c6290.16a62e","type":"delay","z":"f5060296.38d55","name":"","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":810,"y":300,"wires":[["4bf28790.348178"]]},{"id":"b1c82b96.bc1f98","type":"function","z":"f5060296.38d55","name":"functie","func":"newmsg = {};\n\n    brightness = msg.data.attributes.brightness - 30\n\n\nnewmsg.payload = { data: {\"brightness\":brightness} };\n\nreturn newmsg;","outputs":1,"noerr":0,"x":850,"y":380,"wires":[["16e33313.65e53d"]]},{"id":"2e15335e.745edc","type":"api-current-state","z":"f5060296.38d55","name":"","server":"ecc3b049.6889d","outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.0x00158d0000cb52ce_light","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":590,"y":380,"wires":[["b1c82b96.bc1f98"]]},{"id":"16e33313.65e53d","type":"api-call-service","z":"f5060296.38d55","name":"nachtlampje dimmen","server":"ecc3b049.6889d","version":"1","debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.0x00158d0000cb52ce_light","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1060,"y":380,"wires":[["6d4000ba.87468"]]},{"id":"27596840.378ea8","type":"api-current-state","z":"f5060296.38d55","name":"release","server":"ecc3b049.6889d","outputs":2,"halt_if":"down-hold","halt_if_type":"str","halt_if_compare":"is_not","override_topic":false,"entity_id":"sensor.0x0017880103c88525_action","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":600,"y":460,"wires":[["2e15335e.745edc"],[]]},{"id":"6d4000ba.87468","type":"delay","z":"f5060296.38d55","name":"","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":810,"y":460,"wires":[["27596840.378ea8"]]},{"id":"5b75ea0.566eb18","type":"trigger-state","z":"f5060296.38d55","name":"count 1","server":"ecc3b049.6889d","entityid":"sensor.0x0017880103c88525_action","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"ppttm8q0bzc","targetType":"this_entity","targetValue":"","propertyType":"property","propertyValue":"new_state.attributes.counter","comparatorType":"is","comparatorValueDatatype":"num","comparatorValue":"1"},{"id":"08b8uci5d8s6","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on-press"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":560,"y":20,"wires":[["feef0f5d.7aff5"],[]]},{"id":"62146de.a637b94","type":"trigger-state","z":"f5060296.38d55","name":"count 2","server":"ecc3b049.6889d","entityid":"sensor.0x0017880103c88525_action","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"6v4salc10q4","targetType":"this_entity","targetValue":"","propertyType":"property","propertyValue":"new_state.attributes.counter","comparatorType":"is","comparatorValueDatatype":"num","comparatorValue":"2"},{"id":"hevp4ov57z4","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on-press"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":560,"y":80,"wires":[["6305acc1.c464e4"],[]]},{"id":"6305acc1.c464e4","type":"api-call-service","z":"f5060296.38d55","name":"nachtlampje aan","server":"ecc3b049.6889d","version":0,"service_domain":"light","service":"turn_on","data":"{\"entity_id\":\"light.0x00158d0000cb52ce_light\",\"transition\":\"1.5\",\"brightness_pct\":\"90\"}","mergecontext":"","output_location":"","output_location_type":"none","x":1040,"y":80,"wires":[[]]},{"id":"ecc3b049.6889d","type":"server","z":"","name":"Home Assistant"}]
1 Like

I have just installed Node Red and the Philips Dimmer. With the 4 buttons I turn 2 zones with lights on and off. So dim buttons turn the lights on and off. I would like to use the hold function to din or brighten the lights. So holding the on button will brighten and the off button will dim. I see the flows but no screenshot of how it looks inside the functions.

You can import the json that was posted into node red to get all the code.

If you’re interested I can share my solution to achieve your use case … I think I slightly modified the solutions posted here. I am not on my PC right now, though.

Woud be great!

I got the following up and running:

I implemented a naming convention so that when I name the switches like the lights that should be controlled by the switch it works automatically, e.g. the switch sensor.0x123..._action_gang controlls my light entity light.gang. The controls are “hue like” so on, off + hold to dim. For me it feels close to the vanilla hue experience.

The json is:

[{"id":"25f5502c.ea631","type":"api-call-service","z":"b43309c5.ca2ca8","name":"Turn On","server":"19b3c48b.589a6b","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":720,"y":40,"wires":[[]]},{"id":"6b6e29fa.428e68","type":"api-call-service","z":"b43309c5.ca2ca8","name":"Turn Off","server":"19b3c48b.589a6b","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":720,"y":120,"wires":[[]]},{"id":"a952d46c.72e408","type":"api-call-service","z":"b43309c5.ca2ca8","name":"Set brightness","server":"19b3c48b.589a6b","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":740,"y":180,"wires":[[]]},{"id":"b24dcc4f.acde","type":"switch","z":"b43309c5.ca2ca8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on-press","vt":"str"},{"t":"eq","v":"off-press","vt":"str"},{"t":"eq","v":"up-press","vt":"str"},{"t":"eq","v":"down-press","vt":"str"},{"t":"eq","v":"up-hold","vt":"str"},{"t":"eq","v":"down-hold","vt":"str"}],"checkall":"true","repair":true,"outputs":6,"x":290,"y":120,"wires":[["55c22b78.9657c4"],["cfe14fe6.923fa8"],["954bf5ab.cc2de8"],["954bf5ab.cc2de8"],["954bf5ab.cc2de8"],["954bf5ab.cc2de8"]]},{"id":"55c22b78.9657c4","type":"function","z":"b43309c5.ca2ca8","name":"Extract actor","func":"var regex = /sensor.*?_action_(.*)/;\nvar actor = msg.topic.match(regex)[1];\nnode.log(actor)\nmsg.payload = {\n data: { entity_id: 'light.' + actor }\n};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":450,"y":40,"wires":[["25f5502c.ea631"]]},{"id":"cfe14fe6.923fa8","type":"function","z":"b43309c5.ca2ca8","name":"Extract actor","func":"var regex = /sensor.*?_action_(.*)/;\nvar actor = msg.topic.match(regex)[1];\nnode.log(actor)\nmsg.payload = {\n data: { \n     entity_id: 'light.' + actor, \n     transition: 1\n }\n};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":450,"y":80,"wires":[["6b6e29fa.428e68"]]},{"id":"954bf5ab.cc2de8","type":"function","z":"b43309c5.ca2ca8","name":"Extract actor & brightness","func":"var regex = /sensor.*?_action_(.*)/;\nvar actor = msg.topic.match(regex)[1];\nmsg.payload = {\n data: { entity_id: 'light.' + actor, brightness: msg.data.new_state.attributes.brightness, transition: 4 }\n};\nreturn msg;","outputs":1,"noerr":0,"x":490,"y":140,"wires":[["a952d46c.72e408"]]},{"id":"adf335f3.9f2f7","type":"server-state-changed","z":"b43309c5.ca2ca8","name":"Dimmer","server":"19b3c48b.589a6b","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.*_action_*","entityidfiltertype":"regex","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":130,"y":120,"wires":[["b24dcc4f.acde"]]},{"id":"4ed9a5c6.c73fec","type":"comment","z":"b43309c5.ca2ca8","name":"sensor.*?_action_(.*)","info":"Regex to parse out actor for service calls\n\ne.g. sensor.0x123bla_actor_gang","x":130,"y":80,"wires":[]},{"id":"2178e3fe.25e93c","type":"comment","z":"b43309c5.ca2ca8","name":"Hue Dimmer","info":"Regex to parse out actor for service calls\n\ne.g. sensor.0x123bla_actor_gang","x":110,"y":40,"wires":[]},{"id":"19b3c48b.589a6b","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]
2 Likes

Maybe a noob question. But I have 1 Switch with 4 buttons that control 2 area’s.
The on and off button of the switch control area 1 (light kitchen) and the up and down press (dim buttons) control area 2 (lights dining table) This works. Now I want the "hold"function of the buttons dim respectively up and down. So the on-press should brighten and down press should dim.

The switch block distinguishes the different input types:

So if you want to map sth to hold of dim down you would connect it to the last output. I am not sure right now if “on-hold” and “off-hold” are supported, but just try it.

There is no need to check the brightness state of the lights because there are mqtt commands for moving/stepping brightness and color temperature. With this flow I control a ceiling lamp 'Deckenlampe' (which is an mqtt group of 6 hue bulbs 'Deckenlampe [1-6]') with a dimmer switch 'Deckenlampe Dimmer':

[{"id":"9bfc8ef6.375c6","type":"function","z":"9bf7fb6e.7aac88","name":"on-press","func":"const startup_scene = {brightness: 254, color_temp: 370};\nconst additional_scenes = [\n    {brightness: 144, color: {x: 0.5119, y: 0.4147}}, // relax\n    {brightness: 219, color: {x: 0.5119, y: 0.4147}}, // concentrate\n    {brightness: 203, color: {x: 0.368, y: 0.3686}}, // energize\n    {brightness: 240, color: {x: 0.4448, y: 0.4066}} // reading\n] || []; // && to exclude, || to include additional scenes\n\nconst scenes = [\n    startup_scene\n    , ...additional_scenes\n];\n\nvar payload = {state: \"ON\", transition: 0.5};\nif (msg.count > -1) {\n    Object.assign(\n        payload,\n        scenes[msg.count % scenes.length] // round robin\n    );\n}\n\nreturn {payload: payload};\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":820,"y":120,"wires":[["336a8b4d.d56504"]]}]

The only thing I need to query the state ('Deckenlampe 1' is the proxy for the group 'Deckenlampe') for is to make the on-press multifunctional. It uses a counter which counts on-press events and is reset to -2 whenever the bulb sends a {state: "OFF"}. This allows to use the first on-press (msg.count increases to -1) to send just {state: "ON"} and then (msg.count increases to 0 and above) does a round-robin over scenes msg.count % scenes.length with every other on-press.

1 Like