Philips Hue and Ikea switches with MQTT with time of day dimming
This flow allows you to mix the Ikea TRÅDFRI Wireless Dimmer as well as the Philips Hue Switch within your house.
It will turn the lights on to a particular brightness at night. Mine dim to the maximum at 9pm as I have a little one and we go to bed pretty early. You may want to change that. You'll need to install node-red-contrib-sun-position and set your location.
Your main changes you'll need to make are the topics. The incoming MQTT message from the switch and the the outgoing topic to the light. You can use an individual light or a group.
Check out the code in the on-press nodes. flow.set("thisTopic", "zigbee2mqtt/Bathroom/set"); flow.set("transition", 0.5); flow.set("prefix", "Bathroom"); Set these to your preference. The reason I set a flow value is so that when you go to dim the light it knows the value it's at. You can get nodes that will query the device but I have tried to build this with as little dependence on other nodes as possible.
[{"id":"8696f31a.dd9d8","type":"comment","z":"257d0b3a.fe10b4","name":"Lighting Automation","info":"Bathroom Hue","x":170,"y":3100,"wires":[]},{"id":"3b15bb79.c09fc4","type":"within-time-switch","z":"257d0b3a.fe10b4","name":"","nameInt":"","positionConfig":"1bc91131.b45e5f","startTime":"sunset","startTimeType":"pdsTime","startOffset":0,"startOffsetType":"none","startOffsetMultiplier":60000,"endTime":"sunrise","endTimeType":"pdsTime","endOffset":"60","endOffsetType":"num","endOffsetMultiplier":60000,"timeDays":"*","timeOnlyOddDays":false,"timeOnlyEvenDays":false,"timeMonths":"*","timedatestart":"","timedateend":"","propertyStart":"","propertyStartType":"none","propertyStartCompare":"true","propertyStartThreshold":"","propertyStartThresholdType":"num","startTimeAlt":"","startTimeAltType":"entered","startOffsetAlt":0,"startOffsetAltType":"none","startOffsetAltMultiplier":60000,"propertyEnd":"","propertyEndType":"none","propertyEndCompare":"true","propertyEndThreshold":"","propertyEndThresholdType":"num","endTimeAlt":"","endTimeAltType":"entered","endOffsetAlt":0,"endOffsetAltType":"none","endOffsetAltMultiplier":60000,"tsCompare":"0","x":1030,"y":3160,"wires":[["5ec3813.3a84a8"],["c1a657a5.48a918"]]},{"id":"98e28a4b.2a5738","type":"time-comp","z":"257d0b3a.fe10b4","outputs":1,"name":"Fix Timezone","positionConfig":"1bc91131.b45e5f","input":"","inputType":"date","inputFormat":"0","inputOffset":0,"inputOffsetType":"none","inputOffsetMultiplier":60000,"rules":[],"checkall":"true","result1":"","result1Type":"msgPayload","result1Value":"","result1ValueType":"input","result1Format":"3","result1Offset":0,"result1OffsetType":"none","result1OffsetMultiplier":60000,"x":850,"y":3180,"wires":[["3b15bb79.c09fc4"]]},{"id":"942e8c70.1faa7","type":"function","z":"257d0b3a.fe10b4","name":"","func":"var thisBrightness = flow.get( flow.get(\"prefix\") + \"brightness\" );\nthisBrightness = thisBrightness - 51;\nif (thisBrightness < 2) thisBrightness = 2\n\nvar payload = { \n transition: flow.get(\"transition\"), \n brightness: thisBrightness, \n state: \"ON\" \n };\n \n\nflow.set(flow.get(\"prefix\") + \"brightness\", thisBrightness);\n\nvar topic = flow.get(\"thisTopic\");\n\nreturn {payload: payload, topic: topic};","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1440,"y":3400,"wires":[["14021998.9bda86","b9eb1212.169fe"]]},{"id":"5ec3813.3a84a8","type":"within-time-switch","z":"257d0b3a.fe10b4","name":"","nameInt":"","positionConfig":"1bc91131.b45e5f","startTime":"21:00","startTimeType":"entered","startOffset":0,"startOffsetType":"none","startOffsetMultiplier":60000,"endTime":"sunrise","endTimeType":"pdsTime","endOffset":"60","endOffsetType":"num","endOffsetMultiplier":60000,"timeDays":"*","timeOnlyOddDays":false,"timeOnlyEvenDays":false,"timeMonths":"*","timedatestart":"","timedateend":"","propertyStart":"","propertyStartType":"none","propertyStartCompare":"true","propertyStartThreshold":"","propertyStartThresholdType":"num","startTimeAlt":"","startTimeAltType":"entered","startOffsetAlt":0,"startOffsetAltType":"none","startOffsetAltMultiplier":60000,"propertyEnd":"","propertyEndType":"none","propertyEndCompare":"true","propertyEndThreshold":"","propertyEndThresholdType":"num","endTimeAlt":"","endTimeAltType":"entered","endOffsetAlt":0,"endOffsetAltType":"none","endOffsetAltMultiplier":60000,"tsCompare":"0","x":1230,"y":3140,"wires":[["44fb7e61.6c167"],["57d807fb.607848"]]},{"id":"9adcc816.a18c48","type":"mqtt in","z":"257d0b3a.fe10b4","name":"Bathroom Dimmer","topic":"zigbee2mqtt/Philips-Dimmer-3/action","qos":"2","datatype":"auto","broker":"a49f11cc.a8a41","x":210,"y":3160,"wires":[["47916236.347cac"]]},{"id":"ee7f04e2.b0f7a8","type":"switch","z":"257d0b3a.fe10b4","name":"Hue Switch","property":"payload.input","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":false,"outputs":4,"x":630,"y":3280,"wires":[["98e28a4b.2a5738"],["96f8ca01.76a9d8"],["b16489c6.3e52e8"],["942e8c70.1faa7"]]},{"id":"44fb7e61.6c167","type":"function","z":"257d0b3a.fe10b4","name":"","func":"flow.set( flow.get(\"prefix\") + \"brightness\", 2);\n\nvar topic = flow.get(\"thisTopic\");\n\nif ( flow.get(\"prefix\") === \"Bathroom\" ) topic = \"zigbee2mqtt/Ikea-E27-4/set\"\nif ( flow.get(\"prefix\") === \"LivingRoom\" ) topic = \"zigbee2mqtt/LivingRoomMorning/set\"\n\n\nvar payload = { \n transition: flow.get(\"transition\"), \n brightness: 2, \n state: \"ON\" \n };\n \n\n\n\nreturn {payload: payload, topic: topic};","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1440,"y":3100,"wires":[["14021998.9bda86","b9eb1212.169fe"]]},{"id":"57d807fb.607848","type":"function","z":"257d0b3a.fe10b4","name":"","func":"flow.set( flow.get(\"prefix\") + \"brightness\", 80);\n\nvar payload = { \n transition: flow.get(\"transition\"), \n brightness: 80, \n state: \"ON\" \n };\n \nvar topic = flow.get(\"thisTopic\");\n\nreturn {payload: payload, topic: topic};","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1440,"y":3160,"wires":[["14021998.9bda86","b9eb1212.169fe"]]},{"id":"c1a657a5.48a918","type":"function","z":"257d0b3a.fe10b4","name":"","func":"flow.set( flow.get(\"prefix\") + \"brightness\", 200);\n\nvar payload = { \n transition: flow.get(\"transition\"), \n brightness: 200, \n state: \"ON\" \n };\n \nvar topic = flow.get(\"thisTopic\");\n\nreturn {payload: payload, topic: topic};\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1440,"y":3220,"wires":[["14021998.9bda86","b9eb1212.169fe"]]},{"id":"96f8ca01.76a9d8","type":"function","z":"257d0b3a.fe10b4","name":"","func":"flow.set( flow.get(\"prefix\") + \"brightness\", 0);\nvar msgReturn = [];\nvar payload0= { \n transition: flow.get(\"transition\"),\n brightness: 2\n };\n \nvar payload1= { \n state: \"OFF\"\n };\n \nvar topic = flow.get(\"thisTopic\");\n\nmsgReturn.push({payload: payload0, topic: topic});\nmsgReturn.push({payload: payload1, topic: topic});\n\nreturn [msgReturn];","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1440,"y":3280,"wires":[["14021998.9bda86","b9eb1212.169fe"]]},{"id":"492d523d.9e9acc","type":"mqtt in","z":"257d0b3a.fe10b4","name":"L Room Dimmer","topic":"zigbee2mqtt/Philips-Dimmer-1/action","qos":"2","datatype":"utf8","broker":"a49f11cc.a8a41","x":220,"y":3220,"wires":[["446691ab.01e9"]]},{"id":"14021998.9bda86","type":"mqtt out","z":"257d0b3a.fe10b4","name":"Send Light Command","topic":"","qos":"","retain":"","broker":"a49f11cc.a8a41","x":1680,"y":3260,"wires":[]},{"id":"47916236.347cac","type":"function","z":"257d0b3a.fe10b4","name":"on-press","func":"flow.set(\"thisTopic\", \"zigbee2mqtt/Bathroom/set\");\nflow.set(\"transition\", 0.5);\nflow.set(\"prefix\", \"Bathroom\");\n\nvar payload = { input: msg.payload };\n\nreturn {payload: payload};\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":420,"y":3160,"wires":[["ee7f04e2.b0f7a8"]]},{"id":"b16489c6.3e52e8","type":"function","z":"257d0b3a.fe10b4","name":"","func":"var thisBrightness = flow.get( flow.get(\"prefix\") + \"brightness\" );\nthisBrightness = thisBrightness + 51;\nif (thisBrightness > 255) thisBrightness = 254\n\nvar payload = { \n transition: flow.get(\"transition\"), \n brightness: thisBrightness, \n state: \"ON\" \n };\n \n\nflow.set(flow.get(\"prefix\") + \"brightness\", thisBrightness);\n\nvar topic = flow.get(\"thisTopic\");\n\nreturn {payload: payload, topic: topic};\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1440,"y":3340,"wires":[["14021998.9bda86","b9eb1212.169fe"]]},{"id":"a008b4ae.5a3f58","type":"mqtt in","z":"257d0b3a.fe10b4","name":"Lemon Dimmer","topic":"zigbee2mqtt/Philips-Dimmer-4/action","qos":"2","datatype":"auto","broker":"a49f11cc.a8a41","x":220,"y":3280,"wires":[["bb54e154.b6546"]]},{"id":"bb54e154.b6546","type":"function","z":"257d0b3a.fe10b4","name":"on-press","func":"flow.set(\"thisTopic\", \"zigbee2mqtt/Philips-E27-1/set\");\nflow.set(\"transition\", 0.5);\nflow.set(\"prefix\", \"Lemon\");\n\nvar payload = { input: msg.payload };\n\nreturn {payload: payload};\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":420,"y":3280,"wires":[["ee7f04e2.b0f7a8"]]},{"id":"483caaa7.79a904","type":"mqtt in","z":"257d0b3a.fe10b4","name":"Kitchen Dimmer","topic":"zigbee2mqtt/Philips-Dimmer-5/action","qos":"2","datatype":"auto","broker":"a49f11cc.a8a41","x":220,"y":3340,"wires":[["c6a700b7.aa70a"]]},{"id":"d9795454.4952a8","type":"mqtt in","z":"257d0b3a.fe10b4","name":"Ensuite Dimmer","topic":"zigbee2mqtt/Philips-Dimmer-2/action","qos":"2","datatype":"auto","broker":"a49f11cc.a8a41","x":220,"y":3400,"wires":[["2cae8f5d.b20f9"]]},{"id":"446691ab.01e9","type":"function","z":"257d0b3a.fe10b4","name":"on-press","func":"flow.set(\"thisTopic\", \"zigbee2mqtt/LivingRoom/set\");\nflow.set(\"transition\", 0.5);\nflow.set(\"prefix\", \"LivingRoom\");\n\nvar payload = { input: msg.payload };\n\nreturn {payload: payload};\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":420,"y":3220,"wires":[["ee7f04e2.b0f7a8"]]},{"id":"c6a700b7.aa70a","type":"function","z":"257d0b3a.fe10b4","name":"on-press","func":"flow.set(\"thisTopic\", \"zigbee2mqtt/Kitchen/set\");\nflow.set(\"transition\", 0.5);\nflow.set(\"prefix\", \"Kitchen\");\n\nvar payload = { input: msg.payload };\n\nreturn {payload: payload};\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":420,"y":3340,"wires":[["ee7f04e2.b0f7a8"]]},{"id":"2cae8f5d.b20f9","type":"function","z":"257d0b3a.fe10b4","name":"on-press","func":"flow.set(\"thisTopic\", \"zigbee2mqtt/Ensuite/set\");\nflow.set(\"ransition\", 0.5);\nflow.set(\"prefix\", \"Ensuite\");\n\nvar payload = { input: msg.payload };\n\nreturn {payload: payload};\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":420,"y":3400,"wires":[["ee7f04e2.b0f7a8"]]},{"id":"816746d7.5c1118","type":"switch","z":"257d0b3a.fe10b4","name":"Ikea Button Switch","property":"payload.input","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"},{"t":"eq","v":"brightness_up","vt":"str"},{"t":"eq","v":"brightness_down","vt":"str"},{"t":"eq","v":"brightness_stop","vt":"str"}],"checkall":"true","repair":false,"outputs":5,"x":630,"y":3460,"wires":[["98e28a4b.2a5738"],["96f8ca01.76a9d8"],["b16489c6.3e52e8"],["942e8c70.1faa7"],[]]},{"id":"245e8714.09d228","type":"mqtt in","z":"257d0b3a.fe10b4","name":"Alma's Lamp","topic":"zigbee2mqtt/Ikea-Button-4/click","qos":"2","datatype":"auto","broker":"a49f11cc.a8a41","x":230,"y":3460,"wires":[["4e3a33d6.b906cc"]]},{"id":"4e3a33d6.b906cc","type":"function","z":"257d0b3a.fe10b4","name":"on-press","func":"flow.set(\"thisTopic\", \"zigbee2mqtt/Philips-GU10-9/set\");\nflow.set(\"transition\", 0.5);\nflow.set(\"prefix\", \"AlmaLamp\");\n\nvar payload = { input: msg.payload };\n\nreturn {payload: payload};\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":420,"y":3460,"wires":[["816746d7.5c1118"]]},{"id":"545e467d.2130e8","type":"mqtt in","z":"257d0b3a.fe10b4","name":"Eva Fairy Lights (1)","topic":"zigbee2mqtt/Ikea-Button-1/click","qos":"2","datatype":"auto","broker":"a49f11cc.a8a41","x":210,"y":3520,"wires":[["ae73fbcb.0c6458"]]},{"id":"1f70f40b.09a80c","type":"mqtt in","z":"257d0b3a.fe10b4","name":"Alex Fairy Lights (3)","topic":"zigbee2mqtt/Ikea-Button-3/click","qos":"2","datatype":"auto","broker":"a49f11cc.a8a41","x":210,"y":3580,"wires":[["ae73fbcb.0c6458"]]},{"id":"ae73fbcb.0c6458","type":"function","z":"257d0b3a.fe10b4","name":"on-press","func":"flow.set(\"thisTopic\", \"zigbee2mqtt/LED-DIMMER-1/set\");\nflow.set(\"transition\", 0.5);\nflow.set(\"prefix\", \"BedroomFairy\");\n\nvar payload = { input: msg.payload };\n\nreturn {payload: payload};\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":420,"y":3520,"wires":[["816746d7.5c1118"]]},{"id":"b9eb1212.169fe","type":"debug","z":"257d0b3a.fe10b4","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1650,"y":3100,"wires":[]},{"id":"1bc91131.b45e5f","type":"position-config","name":"Home","isValide":"true","longitude":"0","latitude":"0","angleType":"deg","timeZoneOffset":"99","timeZoneDST":"0","stateTimeFormat":"3","stateDateFormat":"12"},{"id":"a49f11cc.a8a41","type":"mqtt-broker","name":"MQTT Server","broker":"10.0.0.2","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]