Circadian/Adaptive Lighting NodeRed

Here is my new revised logic which now implements manual take-over of the brightness &/or color temp until light is switched off/on or circadian booleans are toggled:

[{"id":"03454e69d1fb38da","type":"subflow","name":"Color / Brightness","info":"Color / Brightness Control commands for q-gate node","category":"","in":[{"x":340,"y":120,"wires":[{"id":"e9076938078efe18"}]}],"out":[{"x":640,"y":60,"wires":[{"id":"e9076938078efe18","port":0}]},{"x":640,"y":120,"wires":[{"id":"e9076938078efe18","port":1}]},{"x":640,"y":180,"wires":[{"id":"e9076938078efe18","port":2}]}],"env":[],"meta":{},"color":"#DDAA99","status":{"x":640,"y":240,"wires":[]}},{"id":"e9076938078efe18","type":"function","z":"03454e69d1fb38da","name":"","func":"var circadian_mireds = global.get(\"nr_circadian_mireds\");\nvar circadian_brightness = global.get(\"nr_circadian_brightness\");\nvar calculated_brightness_state = Math.round(circadian_brightness * 0.996078431);\nif (calculated_brightness_state == 254) {calculated_brightness_state = 255}\nvar entity_id = msg.data.entity_id\n/*\nvar current_brightness = global.get('homeassistant.homeAssistant.states[\"' + entity_id + '\"].attributes.brightness.state');\nvar current_color_temp = global.get('homeassistant.homeAssistant.states[\"' + entity_id + '\"].attributes.color_temp.state');\n*/\nvar current_brightness = msg.data.new_state.attributes.brightness;\nvar current_color_temp = msg.data.new_state.attributes.color_temp;\n\n\nvar debug =  \n    {\n        \"Entity\": entity_id,\n        \"Old color\": msg.data.old_state.attributes.color_temp,\n        \"New color\": msg.data.new_state.attributes.color_temp,\n        \"Current color_temp\": current_color_temp,\n        \"Circadian mireds\": circadian_mireds,\n        \"Old brightness\": msg.data.old_state.attributes.brightness,\n        \"New brightness\": msg.data.new_state.attributes.brightness,\n        \"Current brightness\": current_brightness,\n        \"Calculated state brightness\": calculated_brightness_state,\n        \"Circadian brightness\": circadian_brightness\n    }\n\nif (msg.data.old_state.state == \"on\" && msg.data.new_state.state == \"on\"){\n\n        if (msg.data.new_state.attributes.color_temp == circadian_mireds &&\n            current_brightness == calculated_brightness_state){\n            return [{topic: \"control\", payload: \"open\"}, {topic: \"control\", payload: \"open\"}, debug]\n        }\n        if (msg.data.new_state.attributes.color_temp != circadian_mireds &&\n            current_brightness == calculated_brightness_state){\n            return [{topic: \"control\", payload: \"close\"}, {topic: \"control\", payload: \"open\"}, debug]\n        }\n        if (msg.data.new_state.attributes.color_temp == circadian_mireds &&\n            current_brightness != calculated_brightness_state){\n            return [{topic: \"control\", payload: \"open\"}, {topic: \"control\", payload: \"close\"}, debug]\n        }\n        if (msg.data.new_state.attributes.color_temp != circadian_mireds && \n            current_brightness != calculated_brightness_state){\n            return [{topic: \"control\", payload: \"close\"}, {topic: \"control\", payload: \"close\"}, debug]\n        }\n    }\n    \nif (msg.data.old_state.state == \"off\" && msg.data.new_state.state == \"on\"){\n    return [ { topic: \"control\", payload: \"open\" }, { topic: \"control\", payload: \"open\" }, debug ]\n    }\n\nif (msg.data.old_state.state == \"on\" && msg.data.new_state.state == \"off\"){\n        return [{ topic: \"control\", payload: \"queue\" }, { topic: \"control\", payload: \"queue\"}, debug ]\n    }\n\nreturn [null, null, debug]\n\n","outputs":3,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":120,"wires":[[],[],[]]},{"id":"ae8af5aa195ee960","type":"debug","z":"03454e69d1fb38da","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"$globalContext('nr_circadian_mireds')\t","targetType":"jsonata","statusVal":"","statusType":"auto","x":480,"y":180,"wires":[]},{"id":"15d3ba0a712a2f77","type":"debug","z":"03454e69d1fb38da","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"$globalContext('nr_circadian_brightness')\t","targetType":"jsonata","statusVal":"","statusType":"auto","x":480,"y":220,"wires":[]},{"id":"e812678d.651e28","type":"subflow","name":"Circadian Calculator","info":"","category":"","in":[{"x":40,"y":240,"wires":[{"id":"aa4b86d8.b17118"}]}],"out":[{"x":1130,"y":120,"wires":[{"id":"40de744.1e2ca8c","port":0}]},{"x":1130,"y":180,"wires":[{"id":"15b473ec45fa7fc4","port":0}]},{"x":1170,"y":280,"wires":[{"id":"6f365d7d05905955","port":0}]},{"x":1170,"y":340,"wires":[{"id":"c26bdcca.eb483","port":0}]}],"env":[],"meta":{},"color":"#DDAA99","inputLabels":["Trigger"],"outputLabels":["Brightness Value","Brightness Percent","Mireds","Kelvin"]},{"id":"5de89261.4fb97c","type":"switch","z":"e812678d.651e28","name":"curve switch","property":"payload.times_of_day.active.name","propertyType":"msg","rules":[{"t":"eq","v":"night","vt":"str"},{"t":"eq","v":"morning","vt":"str"},{"t":"eq","v":"day","vt":"str"},{"t":"eq","v":"evening","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":470,"y":240,"wires":[["ba17c1e6.e6f58","840431b7.eeefc"],["e5bb5a04.a33948","d4d99259.d0d7e"],["7c0f0774.90adf8","6ac1af7.dddb15"],["23b260f9.19f78","a907f5f4.0bf488"]]},{"id":"ba17c1e6.e6f58","type":"spline-curve","z":"e812678d.651e28","name":"night curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.3},{"x":0.333,"y":0.037},{"x":0.477,"y":0},{"x":0.783,"y":0},{"x":1,"y":0}],"x":710,"y":80,"wires":[["40de744.1e2ca8c","15b473ec45fa7fc4"]]},{"id":"e5bb5a04.a33948","type":"spline-curve","z":"e812678d.651e28","name":"morning curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.01},{"x":0.132,"y":0.01},{"x":0.269,"y":0.027},{"x":0.449,"y":0.124},{"x":0.676,"y":0.334},{"x":1,"y":0.75}],"x":720,"y":120,"wires":[["40de744.1e2ca8c","15b473ec45fa7fc4"]]},{"id":"23b260f9.19f78","type":"spline-curve","z":"e812678d.651e28","name":"evening curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.85},{"x":0.036,"y":0.78},{"x":0.176,"y":0.63},{"x":1,"y":0.3}],"x":720,"y":200,"wires":[["40de744.1e2ca8c","15b473ec45fa7fc4"]]},{"id":"7c0f0774.90adf8","type":"spline-curve","z":"e812678d.651e28","name":"daylight curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.744},{"x":0.03,"y":0.838},{"x":0.097,"y":0.941},{"x":0.183,"y":1},{"x":0.86,"y":0.995},{"x":0.943,"y":0.931},{"x":1,"y":0.85}],"x":720,"y":160,"wires":[["40de744.1e2ca8c","15b473ec45fa7fc4"]]},{"id":"840431b7.eeefc","type":"spline-curve","z":"e812678d.651e28","name":"night curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.5},{"x":0.067,"y":0.368},{"x":0.133,"y":0.265},{"x":0.27,"y":0.171},{"x":0.333,"y":0.16},{"x":0.506,"y":0.124},{"x":0.606,"y":0}],"x":710,"y":280,"wires":[["c26bdcca.eb483","6f365d7d05905955"]]},{"id":"d4d99259.d0d7e","type":"spline-curve","z":"e812678d.651e28","name":"morning curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0},{"x":0.42,"y":0.018},{"x":0.54,"y":0.051},{"x":0.587,"y":0.101},{"x":0.817,"y":0.675},{"x":1,"y":0.85}],"x":720,"y":320,"wires":[["c26bdcca.eb483","6f365d7d05905955"]]},{"id":"a907f5f4.0bf488","type":"spline-curve","z":"e812678d.651e28","name":"evening curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.85},{"x":0.787,"y":0.598},{"x":1,"y":0.5}],"x":720,"y":400,"wires":[["c26bdcca.eb483","6f365d7d05905955"]]},{"id":"6ac1af7.dddb15","type":"spline-curve","z":"e812678d.651e28","name":"daylight curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.85},{"x":0.123,"y":0.927},{"x":0.5,"y":1},{"x":0.92,"y":0.904},{"x":1,"y":0.85}],"x":720,"y":360,"wires":[["c26bdcca.eb483","6f365d7d05905955"]]},{"id":"a330b219.92e4e","type":"function","z":"e812678d.651e28","name":"Times of Day","func":"var newmsg = JSON.parse(JSON.stringify(msg));\nnewmsg.payload.times_of_day = {};\n\nprocessTimeOfDay = function(nme, obj, name_start, name_end) {\n    var ret = {};\n    ret.name  = nme;\n    ret.start = obj.times[name_start].ts;\n    ret.end   = obj.times[name_end].ts;\n    ret.perc  = (obj.ts - ret.start) / (ret.end - ret.start);\n    \n    \n    // to cover range starting before midnight and ending after it.\n    if (ret.start > ret.end)\n    {\n        var a;\n\n        // before midnight\n        if (ret.start < obj.ts) \n        {\n            a = new Date(ret.end);\n            a.setUTCDate(a.getUTCDate() + 1);\n            ret.end = a.getTime();\n             \n             \n        }\n        else if (ret.end > obj.ts) \n        {\n            a = new Date(ret.start);\n            a.setUTCDate(a.getUTCDate() - 1);\n            ret.start = a.getTime();\n\n        }\n\n    }\n    \n    ret.perc  = (obj.ts - ret.start) / (ret.end - ret.start);\n    \n    obj.times_of_day[nme] = ret;\n    if (ret.start <= obj.ts && obj.ts < ret.end ) obj.times_of_day.active = ret;\n}\n\n\nprocessTimeOfDay('night', newmsg.payload, \"astronomicalDusk\", \"astronomicalDawn\");\nprocessTimeOfDay('morning', newmsg.payload, \"astronomicalDawn\", \"goldenHourDawnEnd\");\nprocessTimeOfDay('day', newmsg.payload, \"goldenHourDawnEnd\", \"sunsetStart\");\nprocessTimeOfDay('evening', newmsg.payload, \"sunsetStart\", \"astronomicalDusk\");\n\n\nreturn newmsg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":240,"wires":[["5de89261.4fb97c"]]},{"id":"40de744.1e2ca8c","type":"range","z":"e812678d.651e28","minin":"0","maxin":"1","minout":"1","maxout":"254","action":"clamp","round":true,"property":"payload","name":"","x":940,"y":120,"wires":[[]]},{"id":"c26bdcca.eb483","type":"range","z":"e812678d.651e28","minin":"0","maxin":"1","minout":"2700","maxout":"6500","action":"clamp","round":true,"property":"payload","name":"","x":950,"y":340,"wires":[[]]},{"id":"aa4b86d8.b17118","type":"sun-position","z":"e812678d.651e28","name":"","positionConfig":"31826811.22a458","rules":[],"onlyOnChange":"true","topic":"","outputs":1,"start":"","startType":"none","startOffset":0,"startOffsetType":"num","startOffsetMultiplier":60000,"end":"","endType":"none","endOffset":0,"endOffsetType":"num","endOffsetMultiplier":60000,"x":150,"y":240,"wires":[["a330b219.92e4e"]]},{"id":"15b473ec45fa7fc4","type":"range","z":"e812678d.651e28","minin":"0","maxin":"1","minout":"1","maxout":"100","action":"clamp","round":true,"property":"payload","name":"","x":940,"y":180,"wires":[[]]},{"id":"6f365d7d05905955","type":"range","z":"e812678d.651e28","minin":"0","maxin":"1","minout":"370.37","maxout":"153.85","action":"clamp","round":true,"property":"payload","name":"","x":960,"y":280,"wires":[[]]},{"id":"31826811.22a458","type":"position-config","name":"Home Location","isValide":"true","longitude":"0","latitude":"0","angleType":"deg","timeZoneOffset":"99","timeZoneDST":"0","stateTimeFormat":"3","stateDateFormat":"12"},{"id":"f5e1d08b.a8cd58","type":"ha-entity","z":"7ea0620.a243ca","name":"kelvin","server":"c879ac61.99cd1","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"nr_circadian_kelvin"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:brightness-6"},{"property":"unit_of_measurement","value":"K"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"msg","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"","outputPayloadType":"str","x":890,"y":260,"wires":[[]]},{"id":"365094a5.72bb5c","type":"subflow:e812678d.651e28","z":"7ea0620.a243ca","name":"","env":[],"x":480,"y":170,"wires":[["5f4a2fbe.d6b89"],["2bd57efd76cfcedf"],["eb1275669172038f"],["947dde334221e6b9"]]},{"id":"e80bd956.f2e098","type":"ha-entity","z":"7ea0620.a243ca","name":"color_temp","server":"c879ac61.99cd1","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"nr_circadian_color_temp"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:brightness-6"},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":914,"y":200,"wires":[["53a933a438c1326b"]]},{"id":"476cda43.0f4c94","type":"ha-entity","z":"7ea0620.a243ca","name":"brightness","server":"c879ac61.99cd1","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"nr_circadian_brightness"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:brightness-auto"},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":false,"outputLocation":"","outputLocationType":"none","inputOverride":"block","outputOnStateChange":false,"outputPayload":"","outputPayloadType":"str","x":914,"y":82,"wires":[["ba7018086eca537b"]]},{"id":"a7bca137.c0d2f","type":"comment","z":"7ea0620.a243ca","name":"Update","info":"","x":70,"y":40,"wires":[]},{"id":"60a4dca9.c29d14","type":"api-call-service","z":"7ea0620.a243ca","name":"Light 1","server":"c879ac61.99cd1","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.office_light_1","data":"{\t   \"brightness\": msg.payload.brightness,\t   \"color_temp\": msg.payload.color_temp\t}","dataType":"jsonata","mergecontext":"lightOptions","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1250,"y":480,"wires":[[]]},{"id":"5f4a2fbe.d6b89","type":"function","z":"7ea0620.a243ca","name":"global brightness","func":"/*\nvar new_brightness = msg.payload;\nif (new_brightness == 254) {new_brightness=255;}\n*/\nvar new_brightness = (msg.payload == 254) ? 255 : msg.payload;\nvar old_brightness = global.get('nr_circadian_brightness');\nif (old_brightness == new_brightness) {return null}\nglobal.set('nr_circadian_brightness',new_brightness);\n/* \nmsg.payload = new_brightness;\nreturn msg\n*/\nreturn {payload: new_brightness}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":730,"y":82,"wires":[["476cda43.0f4c94"]]},{"id":"30bf6ebc.f4da52","type":"function","z":"7ea0620.a243ca","name":"global kelvin","func":"/*var new_kelvin = msg.payload;*/\nvar new_kelvin = Math.round(1000000/msg.payload,0);\nvar old_kelvin = global.get('nr_circadian_kelvin');\nif (new_kelvin == old_kelvin) return null;\nglobal.set('nr_circadian_kelvin',new_kelvin);\nreturn {payload: new_kelvin}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":710,"y":300,"wires":[[]]},{"id":"f7382c3f63fec06f","type":"api-current-state","z":"7ea0620.a243ca","name":"Circadian On?","server":"c879ac61.99cd1","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_boolean.circadian_lighting","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"date"}],"for":0,"forType":"num","forUnits":"minutes","x":280,"y":176,"wires":[["365094a5.72bb5c"],[]]},{"id":"934e0600896a7adb","type":"link out","z":"7ea0620.a243ca","name":"Kelvin","mode":"link","links":["4b80d3dcd0dcb9fa","4593a42aa239dadd","2a3d1ccf39330b28","f6f58ab7e35f5688","eb0c9865a1d29657"],"x":1209,"y":200,"wires":[]},{"id":"b1ee887a5f59e581","type":"link out","z":"7ea0620.a243ca","name":"Brightness","mode":"link","links":["bb5ea6235f3f465f","e376d76995dda48c","bfc5edc53296a8c4","4193dc61304afe65","4f8d44ef76bac586","fe41bfcf0ed50cf5","f469dccbd0fea627","4c8de271d9d32579","17fcb852b5d4df73"],"x":1209,"y":82,"wires":[]},{"id":"9dfb80638a8e3d73","type":"q-gate","z":"7ea0620.a243ca","name":"color_temp 1","controlTopic":"control","defaultState":"closed","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","queueCmd":"queue","defaultCmd":"default","triggerCmd":"trigger","flushCmd":"flush","resetCmd":"reset","peekCmd":"","dropCmd":"","statusCmd":"","maxQueueLength":"1","keepNewest":true,"qToggle":true,"persist":true,"storeName":"file","x":830,"y":460,"wires":[["ee6e3c5a8750dbd3"]]},{"id":"1c0b19f6eaee2844","type":"q-gate","z":"7ea0620.a243ca","name":"brightness 1","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","queueCmd":"queue","defaultCmd":"default","triggerCmd":"trigger","flushCmd":"flush","resetCmd":"reset","peekCmd":"","dropCmd":"","statusCmd":"","maxQueueLength":"1","keepNewest":true,"qToggle":true,"persist":true,"storeName":"file","x":830,"y":520,"wires":[["ee6e3c5a8750dbd3"]]},{"id":"448f5e6c691dccc4","type":"server-state-changed","z":"7ea0620.a243ca","name":"Light 1","server":"c879ac61.99cd1","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.office_light_1","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":"milliseconds","ignorePrevStateNull":true,"ignorePrevStateUnknown":true,"ignorePrevStateUnavailable":true,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"},{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"}],"x":370,"y":500,"wires":[["9843d42f60a63e30"]]},{"id":"5cd0d7153aaad741","type":"ha-entity","z":"7ea0620.a243ca","name":"brightness_pct","server":"c879ac61.99cd1","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"nr_circadian_brightness_pct"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:brightness-percent"},{"property":"unit_of_measurement","value":"%"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"msg","inputOverride":"block","outputOnStateChange":false,"outputPayload":"","outputPayloadType":"str","x":924,"y":140,"wires":[[]]},{"id":"2a3d1ccf39330b28","type":"link in","z":"7ea0620.a243ca","name":"Office Light 1 Color","links":["934e0600896a7adb","a8f21399bc702421","cd1e6d98e1856bda","5dfe7cf2b9de1bed"],"x":685,"y":458,"wires":[["9dfb80638a8e3d73","11dfa753a1a2cd05"]]},{"id":"4f8d44ef76bac586","type":"link in","z":"7ea0620.a243ca","name":"Office Light 1 Brightness","links":["a8f21399bc702421","b1ee887a5f59e581","f66d24bc98a1e3de","5dfe7cf2b9de1bed"],"x":685,"y":543,"wires":[["1c0b19f6eaee2844","94a02ed09a89725d"]]},{"id":"f75a6792daaeb0b4","type":"comment","z":"7ea0620.a243ca","name":"Office","info":"","x":70,"y":380,"wires":[]},{"id":"eb1275669172038f","type":"function","z":"7ea0620.a243ca","name":"global mireds","func":"var new_mireds = msg.payload\nvar old_mireds = global.get('nr_circadian_mireds')\nif (new_mireds == old_mireds) return null;\nglobal.set('nr_circadian_mireds',new_mireds);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":710,"y":200,"wires":[["e80bd956.f2e098"]]},{"id":"2bd57efd76cfcedf","type":"function","z":"7ea0620.a243ca","name":"global brightness %","func":"var new_brightness_pct = msg.payload;\nvar old_brightness_pct = global.get('nr_circadian_brightness_pct');\nif (new_brightness_pct == old_brightness_pct) return null;\nglobal.set('nr_circadian_brightness_pct',new_brightness_pct);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":730,"y":140,"wires":[["5cd0d7153aaad741"]]},{"id":"11dfa753a1a2cd05","type":"ha-entity","z":"7ea0620.a243ca","name":"Light 1","server":"c879ac61.99cd1","version":2,"debugenabled":false,"outputs":1,"entityType":"binary_sensor","config":[{"property":"name","value":"Office Light 1 Color Control"},{"property":"device_class","value":"running"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload = \"open\" ? \"on\" : \"off\"\t","stateType":"jsonata","attributes":[{"property":"command","value":"payload","valueType":"msg"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":810,"y":400,"wires":[[]]},{"id":"94a02ed09a89725d","type":"ha-entity","z":"7ea0620.a243ca","name":"Light 1","server":"c879ac61.99cd1","version":2,"debugenabled":false,"outputs":1,"entityType":"binary_sensor","config":[{"property":"name","value":"Office Light 1 Brightness Control"},{"property":"device_class","value":"running"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload = \"open\" ? \"on\" : \"off\"\t","stateType":"jsonata","attributes":[{"property":"command","value":"payload","valueType":"msg"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":810,"y":579,"wires":[[]]},{"id":"ee6e3c5a8750dbd3","type":"join","z":"7ea0620.a243ca","name":"","mode":"custom","build":"merged","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"2","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":990,"y":480,"wires":[["c6387631e23d9171"]]},{"id":"53a933a438c1326b","type":"change","z":"7ea0620.a243ca","name":"change payload","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.color_temp","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1084,"y":200,"wires":[["934e0600896a7adb"]]},{"id":"ba7018086eca537b","type":"change","z":"7ea0620.a243ca","name":"change payload","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.brightness","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1084,"y":82,"wires":[["b1ee887a5f59e581"]]},{"id":"f6a0b62593cc81c1","type":"interval-multiples-timer","z":"7ea0620.a243ca","interval":"60000","payload":"","topic":"","name":"Every minute","x":86,"y":160,"wires":[["f7382c3f63fec06f"]],"outputLabels":["timestamp"]},{"id":"b49e0a964008e7ba","type":"inject","z":"7ea0620.a243ca","name":"At startup","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":"5","topic":"","payload":"","payloadType":"date","x":96,"y":200,"wires":[["f7382c3f63fec06f"]]},{"id":"2758054b03a4112e","type":"subflow:03454e69d1fb38da","z":"7ea0620.a243ca","name":"Sub","x":650,"y":500,"wires":[["11dfa753a1a2cd05","9dfb80638a8e3d73"],["1c0b19f6eaee2844","94a02ed09a89725d"],[]]},{"id":"9843d42f60a63e30","type":"trigger","z":"7ea0620.a243ca","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"2","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":520,"y":500,"wires":[["2758054b03a4112e"]]},{"id":"947dde334221e6b9","type":"function","z":"7ea0620.a243ca","name":"global kelvin","func":"var new_kelvin = msg.payload;\n/*var new_kelvin = Math.round(1000000/msg.payload,0);*/\nvar old_kelvin = global.get('nr_circadian_kelvin');\nif (new_kelvin == old_kelvin) return null;\nglobal.set('nr_circadian_kelvin',new_kelvin);\nreturn {payload: new_kelvin}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":710,"y":260,"wires":[["f5e1d08b.a8cd58"]]},{"id":"c6387631e23d9171","type":"trigger","z":"7ea0620.a243ca","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"1","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":1120,"y":480,"wires":[["60a4dca9.c29d14"]]},{"id":"c879ac61.99cd1","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30"}]

Revision 3 (if anyone is interested)…

2 Likes

Very nice job with this!

Thought I’d share my version of this that I have working well with my Hue bulbs and LEDNED strips.

In the subflow, I went with exactly what you created but went with a temperature from 2200 to 6500 to match what my bulbs and strips support. And I like the more blue light during the day.

Next I went a slightly different route (and I apologize if this was already discussed in this or another thread but I didn’t see it) to keep things super clean as I use these calculated color temperature and brightness levels across the rest of my Node-RED project. I have a few hundred Hue bulbs and strips, so keeping things as tight as possible is super important.

Instead of calling the subflow at every place I might need these values, I’m updating a global variable that I can then use from within every service call to HA:

So every second, I’m having the “Circadian” subflow calculate and output the brightness and color temperature and then writing those to the global.brightness and global.colorTemp variables. You could name them whatever you wanted.

With these global variables being constantly updated, I can use them for any light anywhere as needed, like this:

Practically, here’s what that looks like in my system:

In my pantry, when the lights were off and then turn on, and also when on every 30 seconds, the “update color and brightness” updates the color temperature and brightness using the data you can see in that previous screenshot.

This whole thing is very effective and I have it working across hundreds of bulbs.

Thanks again for this!

2 Likes

In my version I now have 4 outputs:

I’m also using global variables but also creating HA entities as well:


global_mireds function:

var new_mireds = msg.payload
if (new_mireds != global.get('nr_circadian_mireds')) {
    global.set('nr_circadian_mireds',new_mireds);
    return msg;
}

global_brightness function:

var new_brightness = (msg.payload == 254) ? 255 : msg.payload;
if (new_brightness != global.get('nr_circadian_brightness')){
    global.set('nr_circadian_brightness',new_brightness);
    return {payload: new_brightness}
}

I needed 255 rather than 254 as that is what HA brightness value is at max setting even though the range is 1:254. It uses 1:253, 255! Also had to use a special multiplier in my subfunction to back-convert…

var circadian_brightness = global.get("nr_circadian_brightness");
var calculated_brightness_state = Math.round(circadian_brightness * 0.996078431);
if (calculated_brightness_state == 254) {calculated_brightness_state = 255}

That whole function controls q-gates to allow/block color_temp/brightness changes to the lights.
If I manually adjust either attribute of a light, the gate switches to queuing mode.

var circadian_mireds = global.get("nr_circadian_mireds");
var circadian_brightness = global.get("nr_circadian_brightness");
var calculated_brightness_state = Math.round(circadian_brightness * 0.996078431);
if (calculated_brightness_state == 254) {calculated_brightness_state = 255}
var entity_id = msg.data.entity_id
var current_brightness = msg.data.new_state.attributes.brightness;
var current_color_temp = msg.data.new_state.attributes.color_temp;
var debug =  
    {
        "Entity": entity_id,
        "Old color": msg.data.old_state.attributes.color_temp,
        "New color": msg.data.new_state.attributes.color_temp,
        "Current color_temp": current_color_temp,
        "Circadian mireds": circadian_mireds,
        "Old brightness": msg.data.old_state.attributes.brightness,
        "New brightness": msg.data.new_state.attributes.brightness,
        "Current brightness": current_brightness,
        "Calculated state brightness": calculated_brightness_state,
        "Circadian brightness": circadian_brightness
    }

if (msg.data.old_state.state == "on" && msg.data.new_state.state == "on"){
        if (msg.data.new_state.attributes.color_temp == circadian_mireds &&
            current_brightness == calculated_brightness_state){
            return [{topic: "control", payload: "open"}, {topic: "control", payload: "open"}, debug]
        }
        if (msg.data.new_state.attributes.color_temp != circadian_mireds &&
            current_brightness == calculated_brightness_state){
            return [{topic: "control", payload: "close"}, {topic: "control", payload: "open"}, debug]
        }
        if (msg.data.new_state.attributes.color_temp == circadian_mireds &&
            current_brightness != calculated_brightness_state){
            return [{topic: "control", payload: "open"}, {topic: "control", payload: "close"}, debug]
        }
        if (msg.data.new_state.attributes.color_temp != circadian_mireds && 
            current_brightness != calculated_brightness_state){
            return [{topic: "control", payload: "close"}, {topic: "control", payload: "close"}, debug]
        }
    }
    
if (msg.data.old_state.state == "off" && msg.data.new_state.state == "on"){
    return [ { topic: "control", payload: "open", data: {color_temp: circadian_mireds}},
             { topic: "control", payload: "open", data: {brightness: circadian_brightness}},
             debug ]}

if (msg.data.old_state.state == "on" && msg.data.new_state.state == "off"){
        return [{ topic: "control", payload: "queue" }, { topic: "control", payload: "queue"}, debug ]
    }

return [null, null, debug]

Great stuff guys! I am gonna give this a try…

Hi @maxym & @dbrunt, thank you for your awesome ideas and work!
I imported the last version you posted @dbrunt (post #41 I think) and am trying to get it to run.
First, I changed the location and adjustet the temp values in the subnode to me needs and I’m receiving correct values in HA sensors (sensor.nr_circadian…).
I then edited “Light 1” to a light.entity in my setup but for now nothing is happening.

Could you give me a brief explanation about every point I’m able (or have to) adjust something to my setup/needs?
Is it possible to adress more than one light at once (e.g. per room)?
Can I simply copy “Light 1” flow to add more lights?

And last can I get your latest version? And how can I update it if you post a new one without having to setup everything again?

I really appreciated your work on adaptive lighting and I love to see what you came up with here!

Regards,
Stephan

Short follow up: I got it to work once. I missed to create the input_boolean. After I did that and switched it on it adjusted my light once. Then I wanted to know whats happing if I change the color manually and know nothing happens again… what do I have to do?

Okay, so I’ve been troubleshooting my original logic. Here is where I am at now… I occasionally see a gate incorrectly go closed or queuing when it should have stayed open as I have not made any manual changes to color/brightness. I think I mentioned my office lights almost always are on from early morning to late evening and the hiccups I’m seeing are generally in the evening as color_temp/brightness is reducing. There was an issue with the join node being incorrect and I had to change that and the payloads being passed from the Update module.

[{"id":"e812678d.651e28","type":"subflow","name":"Circadian Calculator","info":"","category":"time and astro","in":[{"x":80,"y":80,"wires":[{"id":"aa4b86d8.b17118"}]}],"out":[{"x":890,"y":100,"wires":[{"id":"40de744.1e2ca8c","port":0}]},{"x":890,"y":160,"wires":[{"id":"15b473ec45fa7fc4","port":0}]},{"x":930,"y":260,"wires":[{"id":"6f365d7d05905955","port":0}]},{"x":930,"y":320,"wires":[{"id":"c26bdcca.eb483","port":0}]}],"env":[],"meta":{},"color":"#FFCC66","inputLabels":["Trigger"],"outputLabels":["Brightness Value","Brightness Percent","Mireds","Kelvin"]},{"id":"5de89261.4fb97c","type":"switch","z":"e812678d.651e28","name":"curve switch","property":"payload.times_of_day.active.name","propertyType":"msg","rules":[{"t":"eq","v":"night","vt":"str"},{"t":"eq","v":"morning","vt":"str"},{"t":"eq","v":"day","vt":"str"},{"t":"eq","v":"evening","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":250,"y":220,"wires":[["ba17c1e6.e6f58","840431b7.eeefc"],["e5bb5a04.a33948","d4d99259.d0d7e"],["7c0f0774.90adf8","6ac1af7.dddb15"],["23b260f9.19f78","a907f5f4.0bf488"]]},{"id":"ba17c1e6.e6f58","type":"spline-curve","z":"e812678d.651e28","name":"night curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.3},{"x":0.333,"y":0.037},{"x":0.477,"y":0},{"x":0.783,"y":0},{"x":1,"y":0}],"x":490,"y":80,"wires":[["40de744.1e2ca8c","15b473ec45fa7fc4"]]},{"id":"e5bb5a04.a33948","type":"spline-curve","z":"e812678d.651e28","name":"morning curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.01},{"x":0.132,"y":0.01},{"x":0.269,"y":0.027},{"x":0.449,"y":0.124},{"x":0.676,"y":0.334},{"x":1,"y":0.75}],"x":500,"y":120,"wires":[["40de744.1e2ca8c","15b473ec45fa7fc4"]]},{"id":"23b260f9.19f78","type":"spline-curve","z":"e812678d.651e28","name":"evening curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.85},{"x":0.036,"y":0.78},{"x":0.176,"y":0.63},{"x":1,"y":0.3}],"x":500,"y":200,"wires":[["40de744.1e2ca8c","15b473ec45fa7fc4"]]},{"id":"7c0f0774.90adf8","type":"spline-curve","z":"e812678d.651e28","name":"daylight curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.744},{"x":0.03,"y":0.838},{"x":0.097,"y":0.941},{"x":0.183,"y":1},{"x":0.86,"y":0.995},{"x":0.943,"y":0.931},{"x":1,"y":0.85}],"x":500,"y":160,"wires":[["40de744.1e2ca8c","15b473ec45fa7fc4"]]},{"id":"840431b7.eeefc","type":"spline-curve","z":"e812678d.651e28","name":"night curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.5},{"x":0.067,"y":0.368},{"x":0.133,"y":0.265},{"x":0.27,"y":0.171},{"x":0.333,"y":0.16},{"x":0.506,"y":0.124},{"x":0.606,"y":0}],"x":490,"y":240,"wires":[["c26bdcca.eb483","6f365d7d05905955"]]},{"id":"d4d99259.d0d7e","type":"spline-curve","z":"e812678d.651e28","name":"morning curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0},{"x":0.42,"y":0.018},{"x":0.54,"y":0.051},{"x":0.587,"y":0.101},{"x":0.817,"y":0.675},{"x":1,"y":0.85}],"x":500,"y":280,"wires":[["c26bdcca.eb483","6f365d7d05905955"]]},{"id":"a907f5f4.0bf488","type":"spline-curve","z":"e812678d.651e28","name":"evening curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.85},{"x":0.787,"y":0.598},{"x":1,"y":0.5}],"x":500,"y":360,"wires":[["c26bdcca.eb483","6f365d7d05905955"]]},{"id":"6ac1af7.dddb15","type":"spline-curve","z":"e812678d.651e28","name":"daylight curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.85},{"x":0.123,"y":0.927},{"x":0.5,"y":1},{"x":0.92,"y":0.904},{"x":1,"y":0.85}],"x":500,"y":320,"wires":[["c26bdcca.eb483","6f365d7d05905955"]]},{"id":"a330b219.92e4e","type":"function","z":"e812678d.651e28","name":"Times of Day","func":"var newmsg = JSON.parse(JSON.stringify(msg));\nnewmsg.payload.times_of_day = {};\n\nprocessTimeOfDay = function(nme, obj, name_start, name_end) {\n    var ret = {};\n    ret.name  = nme;\n    ret.start = obj.times[name_start].ts;\n    ret.end   = obj.times[name_end].ts;\n    ret.perc  = (obj.ts - ret.start) / (ret.end - ret.start);\n    \n    \n    // to cover range starting before midnight and ending after it.\n    if (ret.start > ret.end)\n    {\n        var a;\n\n        // before midnight\n        if (ret.start < obj.ts) \n        {\n            a = new Date(ret.end);\n            a.setUTCDate(a.getUTCDate() + 1);\n            ret.end = a.getTime();\n             \n             \n        }\n        else if (ret.end > obj.ts) \n        {\n            a = new Date(ret.start);\n            a.setUTCDate(a.getUTCDate() - 1);\n            ret.start = a.getTime();\n\n        }\n\n    }\n    \n    ret.perc  = (obj.ts - ret.start) / (ret.end - ret.start);\n    \n    obj.times_of_day[nme] = ret;\n    if (ret.start <= obj.ts && obj.ts < ret.end ) obj.times_of_day.active = ret;\n}\n\n\nprocessTimeOfDay('night', newmsg.payload, \"astronomicalDusk\", \"astronomicalDawn\");\nprocessTimeOfDay('morning', newmsg.payload, \"astronomicalDawn\", \"goldenHourDawnEnd\");\nprocessTimeOfDay('day', newmsg.payload, \"goldenHourDawnEnd\", \"sunsetStart\");\nprocessTimeOfDay('evening', newmsg.payload, \"sunsetStart\", \"astronomicalDusk\");\n\n\nreturn newmsg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":230,"y":140,"wires":[["5de89261.4fb97c"]]},{"id":"40de744.1e2ca8c","type":"range","z":"e812678d.651e28","minin":"0","maxin":"1","minout":"1","maxout":"254","action":"clamp","round":true,"property":"payload","name":"","x":710,"y":100,"wires":[[]]},{"id":"c26bdcca.eb483","type":"range","z":"e812678d.651e28","minin":"0","maxin":"1","minout":"2700","maxout":"6500","action":"clamp","round":true,"property":"payload","name":"","x":730,"y":320,"wires":[[]]},{"id":"aa4b86d8.b17118","type":"sun-position","z":"e812678d.651e28","name":"","positionConfig":"31826811.22a458","rules":[],"onlyOnChange":"true","topic":"","outputs":1,"start":"","startType":"none","startOffset":0,"startOffsetType":"num","startOffsetMultiplier":60000,"end":"","endType":"none","endOffset":0,"endOffsetType":"num","endOffsetMultiplier":60000,"x":210,"y":80,"wires":[["a330b219.92e4e"]]},{"id":"15b473ec45fa7fc4","type":"range","z":"e812678d.651e28","minin":"0","maxin":"1","minout":"1","maxout":"100","action":"clamp","round":true,"property":"payload","name":"","x":710,"y":160,"wires":[[]]},{"id":"6f365d7d05905955","type":"range","z":"e812678d.651e28","minin":"0","maxin":"1","minout":"370","maxout":"154","action":"clamp","round":true,"property":"payload","name":"","x":720,"y":260,"wires":[[]]},{"id":"31826811.22a458","type":"position-config","name":"Home Location","isValide":"true","longitude":"0","latitude":"0","angleType":"deg","timeZoneOffset":"99","timeZoneDST":"0","stateTimeFormat":"3","stateDateFormat":"12"},{"id":"f5e1d08b.a8cd58","type":"ha-entity","z":"7ea0620.a243ca","name":"kelvin","server":"c879ac61.99cd1","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"nr_circadian_kelvin"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:brightness-6"},{"property":"unit_of_measurement","value":"K"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"nr_circadian_kelvin","stateType":"global","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"msg","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"","outputPayloadType":"str","x":890,"y":260,"wires":[[]]},{"id":"365094a5.72bb5c","type":"subflow:e812678d.651e28","z":"7ea0620.a243ca","name":"","env":[],"x":480,"y":170,"wires":[["5f4a2fbe.d6b89"],["2bd57efd76cfcedf"],["eb1275669172038f","30bf6ebc.f4da52"],[]]},{"id":"e80bd956.f2e098","type":"ha-entity","z":"7ea0620.a243ca","name":"color_temp","server":"c879ac61.99cd1","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"nr_circadian_color_temp"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:brightness-6"},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"nr_circadian_mireds","stateType":"global","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":910,"y":200,"wires":[["53a933a438c1326b"]]},{"id":"476cda43.0f4c94","type":"ha-entity","z":"7ea0620.a243ca","name":"brightness","server":"c879ac61.99cd1","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"nr_circadian_brightness"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:brightness-auto"},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"nr_circadian_brightness","stateType":"global","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"block","outputOnStateChange":false,"outputPayload":"","outputPayloadType":"str","x":910,"y":80,"wires":[["ba7018086eca537b"]]},{"id":"5f4a2fbe.d6b89","type":"function","z":"7ea0620.a243ca","name":"global brightness","func":"var new_brightness = (msg.payload == 254) ? 255 : msg.payload;\nif (new_brightness != global.get('nr_circadian_brightness')){\n    global.set('nr_circadian_brightness',new_brightness);\n    return {payload: new_brightness}\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":730,"y":80,"wires":[["476cda43.0f4c94"]]},{"id":"30bf6ebc.f4da52","type":"function","z":"7ea0620.a243ca","name":"global kelvin","func":"/*var new_kelvin = msg.payload;*/\nvar new_kelvin = Math.round(1000000/msg.payload,0);\nvar old_kelvin = global.get('nr_circadian_kelvin');\nif (new_kelvin == old_kelvin) return null;\nglobal.set('nr_circadian_kelvin',new_kelvin);\nreturn {payload: new_kelvin}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":710,"y":260,"wires":[["f5e1d08b.a8cd58"]]},{"id":"934e0600896a7adb","type":"link out","z":"7ea0620.a243ca","name":"Mireds","mode":"link","links":["9d3d2931ff09f30e","43c2e1ab9d4c3a33","46442d133be6f963","a52970d03d1f30a5","a897dbf21ffb2b4c"],"x":1195,"y":200,"wires":[]},{"id":"b1ee887a5f59e581","type":"link out","z":"7ea0620.a243ca","name":"Brightness","mode":"link","links":["e376d76995dda48c","4c8de271d9d32579","df433767e85260dc","3055458e27486427","4ccd12c7e9fa28f9","d72d56acc2f72b41","b1137e5488a33dda","ffa6896cb76348d5","19af89da0660a8f1"],"x":1195,"y":80,"wires":[]},{"id":"5cd0d7153aaad741","type":"ha-entity","z":"7ea0620.a243ca","name":"brightness_pct","server":"c879ac61.99cd1","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"nr_circadian_brightness_pct"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:brightness-percent"},{"property":"unit_of_measurement","value":"%"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"nr_circadian_brightness_pct","stateType":"global","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"msg","inputOverride":"block","outputOnStateChange":false,"outputPayload":"","outputPayloadType":"str","x":920,"y":140,"wires":[[]]},{"id":"eb1275669172038f","type":"function","z":"7ea0620.a243ca","name":"global mireds","func":"var new_mireds = msg.payload\nif (new_mireds != global.get('nr_circadian_mireds')) {\n    global.set('nr_circadian_mireds',new_mireds);\n    return msg;\n}\n\n/*\nvar new_mireds = msg.payload\nvar old_mireds = global.get('nr_circadian_mireds')\nif (new_mireds == old_mireds) return null;\nglobal.set('nr_circadian_mireds',new_mireds);\nreturn msg;\n*/\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":710,"y":200,"wires":[["e80bd956.f2e098"]]},{"id":"2bd57efd76cfcedf","type":"function","z":"7ea0620.a243ca","name":"global brightness %","func":"var new_brightness_pct = msg.payload;\nvar old_brightness_pct = global.get('nr_circadian_brightness_pct');\nif (new_brightness_pct == old_brightness_pct) return null;\nglobal.set('nr_circadian_brightness_pct',new_brightness_pct);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":730,"y":140,"wires":[["5cd0d7153aaad741"]]},{"id":"53a933a438c1326b","type":"change","z":"7ea0620.a243ca","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"color_temp","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1070,"y":200,"wires":[["934e0600896a7adb"]]},{"id":"ba7018086eca537b","type":"change","z":"7ea0620.a243ca","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"brightness","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1070,"y":80,"wires":[["b1ee887a5f59e581"]]},{"id":"f6a0b62593cc81c1","type":"interval-multiples-timer","z":"7ea0620.a243ca","interval":"60000","payload":"","topic":"","name":"Every minute","x":86,"y":160,"wires":[["f7382c3f63fec06f"]],"outputLabels":["timestamp"]},{"id":"b49e0a964008e7ba","type":"inject","z":"7ea0620.a243ca","name":"At startup","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":"5","topic":"","payload":"","payloadType":"date","x":96,"y":200,"wires":[["f7382c3f63fec06f"]]},{"id":"f7382c3f63fec06f","type":"api-current-state","z":"7ea0620.a243ca","name":"Circadian On?","server":"c879ac61.99cd1","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_boolean.circadian_lighting","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"date"}],"for":0,"forType":"num","forUnits":"minutes","x":280,"y":176,"wires":[["365094a5.72bb5c"],[]]},{"id":"c879ac61.99cd1","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30"}]
1 Like

[{"id":"03454e69d1fb38da","type":"subflow","name":"Color / Brightness","info":"Color / Brightness Control commands for q-gate node","category":"","in":[{"x":340,"y":140,"wires":[{"id":"e9076938078efe18"}]}],"out":[{"x":640,"y":60,"wires":[{"id":"e9076938078efe18","port":0}]},{"x":640,"y":120,"wires":[{"id":"e9076938078efe18","port":1}]},{"x":640,"y":180,"wires":[{"id":"e9076938078efe18","port":2}]}],"env":[],"meta":{},"color":"#DDAA99","status":{"x":640,"y":240,"wires":[]}},{"id":"e9076938078efe18","type":"function","z":"03454e69d1fb38da","name":"","func":"var entity_id = msg.data.entity_id\nvar current_brightness = msg.data.new_state.attributes.brightness;\nvar current_color_temp = msg.data.new_state.attributes.color_temp;\nvar circadian_mireds = global.get(\"nr_circadian_mireds\");\nvar circadian_brightness = global.get(\"nr_circadian_brightness\");\n/*\nvar calculated_brightness_state = Math.round(circadian_brightness * 0.996078431);\nif (calculated_brightness_state == 254) {calculated_brightness_state = 255}\n*/\ncircadian_brightness = Math.round(circadian_brightness * 0.996078431) + Math.floor(circadian_brightness/255);\n\nvar debug =  \n    {\n        \"Entity\": entity_id,\n        \"Old color\": msg.data.old_state.attributes.color_temp,\n        \"New color\": msg.data.new_state.attributes.color_temp,\n        \"Current color_temp\": current_color_temp,\n        \"Circadian mireds\": circadian_mireds,\n        \"Old brightness\": msg.data.old_state.attributes.brightness,\n        \"New brightness\": msg.data.new_state.attributes.brightness,\n        \"Current brightness\": current_brightness,\n        \"Circadian brightness\": circadian_brightness\n    }\n\nif (msg.data.old_state.state == \"on\" && msg.data.new_state.state == \"on\"){\n\n        if (current_color_temp == circadian_mireds &&\n            current_brightness == circadian_brightness){\n            return [{topic: \"control\", payload: \"open\"}, {topic: \"control\", payload: \"open\"}, debug]\n        }\n        if (current_color_temp != circadian_mireds &&\n            current_brightness == circadian_brightness){\n            return [{topic: \"control\", payload: \"queue\"}, {topic: \"control\", payload: \"open\"}, debug]\n        }\n        if (current_color_temp == circadian_mireds &&\n            current_brightness != circadian_brightness){\n            return [{topic: \"control\", payload: \"open\"}, {topic: \"control\", payload: \"queue\"}, debug]\n        }\n        if (current_color_temp != circadian_mireds && \n            current_brightness != circadian_brightness){\n            return [{topic: \"control\", payload: \"queue\"}, {topic: \"control\", payload: \"queue\"}, debug]\n        }\n    }\n    \nif (msg.data.old_state.state == \"off\" && msg.data.new_state.state == \"on\"){\n    return [ { topic: \"control\", payload: \"open\", data: {color_temp: circadian_mireds}},\n             { topic: \"control\", payload: \"open\", data: {brightness: circadian_brightness}},\n             debug ]}\n\n/* payload.brightness */\n\nif (msg.data.old_state.state == \"on\" && msg.data.new_state.state == \"off\"){\n        return [{ topic: \"control\", payload: \"queue\" }, { topic: \"control\", payload: \"queue\"}, debug ]\n    }\n\nreturn [null, null, debug]\n\n","outputs":3,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":140,"wires":[[],[],[]]},{"id":"ae8af5aa195ee960","type":"debug","z":"03454e69d1fb38da","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"$globalContext('nr_circadian_mireds')\t","targetType":"jsonata","statusVal":"","statusType":"auto","x":420,"y":260,"wires":[]},{"id":"15d3ba0a712a2f77","type":"debug","z":"03454e69d1fb38da","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"$globalContext('nr_circadian_brightness')\t","targetType":"jsonata","statusVal":"","statusType":"auto","x":420,"y":220,"wires":[]},{"id":"71e41def.76e254","type":"server-state-changed","z":"7ea0620.a243ca","name":"Cicadian Office","server":"c879ac61.99cd1","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.circadian_lighting_office","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":true,"ignorePrevStateUnknown":true,"ignorePrevStateUnavailable":true,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"$entity().state = \"on\" ? \"open\" : \"queue\"","valueType":"jsonata"},{"property":"topic","propertyType":"msg","value":"control","valueType":"str"}],"x":205,"y":600,"wires":[["a8f21399bc702421"]]},{"id":"60a4dca9.c29d14","type":"api-call-service","z":"7ea0620.a243ca","name":"Light 1","server":"c879ac61.99cd1","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.office_light_1"],"data":"{\t   \"brightness\": payload.brightness,\t   \"color_temp\": payload.color_temp\t}","dataType":"jsonata","mergeContext":"lightOptions","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1330,"y":420,"wires":[[]]},{"id":"3863e49219f2c06c","type":"api-call-service","z":"7ea0620.a243ca","name":"Light 2","server":"c879ac61.99cd1","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.office_light_2"],"data":"{\t   \"brightness\": payload.brightness,\t   \"color_temp\": payload.color_temp}","dataType":"jsonata","mergeContext":"lightOptions","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1335,"y":680,"wires":[[]]},{"id":"c5c6137c20db841c","type":"api-call-service","z":"7ea0620.a243ca","name":"Light 3","server":"c879ac61.99cd1","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.office_light_3"],"data":"{\t   \"brightness\": payload.brightness,\t   \"color_temp\": payload.color_temp\t}","dataType":"jsonata","mergeContext":"lightOptions","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1340,"y":940,"wires":[[]]},{"id":"5953dc0a6a10c314","type":"server-state-changed","z":"7ea0620.a243ca","name":"Office Color","server":"c879ac61.99cd1","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.circadian_lighting_color_office","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":true,"ignorePrevStateUnknown":true,"ignorePrevStateUnavailable":true,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"topic","propertyType":"msg","value":"control","valueType":"str"},{"property":"payload","propertyType":"msg","value":"$entity().state = \"on\" ? \"open\" : \"queue\"","valueType":"jsonata"}],"x":215,"y":540,"wires":[["cd1e6d98e1856bda"]]},{"id":"3728361be31f9153","type":"server-state-changed","z":"7ea0620.a243ca","name":"Office Brightness","server":"c879ac61.99cd1","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.circadian_lighting_brightness_office","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":true,"ignorePrevStateUnknown":true,"ignorePrevStateUnavailable":true,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"$entity().state = \"on\" ? \"open\" : \"queue\"","valueType":"jsonata"},{"property":"topic","propertyType":"msg","value":"control","valueType":"str"}],"x":205,"y":660,"wires":[["f66d24bc98a1e3de"]]},{"id":"5fcdbdfcad60b453","type":"q-gate","z":"7ea0620.a243ca","name":"color_temp 2","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","queueCmd":"queue","defaultCmd":"default","triggerCmd":"trigger","flushCmd":"flush","resetCmd":"reset","peekCmd":"","dropCmd":"","statusCmd":"","maxQueueLength":"1","keepNewest":true,"qToggle":true,"persist":true,"storeName":"file","x":755,"y":700,"wires":[["b36ec2b3b096f8d6"]]},{"id":"4b80d3dcd0dcb9fa","type":"link in","z":"7ea0620.a243ca","name":"Office Light 3 Color ","links":["a8f21399bc702421","cd1e6d98e1856bda","babbee9d013605d2"],"x":620,"y":900,"wires":[["e847b97612b57cde"]]},{"id":"bb5ea6235f3f465f","type":"link in","z":"7ea0620.a243ca","name":"Office Light 2 Brightness ","links":["a8f21399bc702421","f66d24bc98a1e3de","fe02e119919f1fbf"],"x":620,"y":820,"wires":[["6e90b1ffee7f80b8"]]},{"id":"5731df33e0ef2e56","type":"q-gate","z":"7ea0620.a243ca","name":"brightness 2","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","queueCmd":"queue","defaultCmd":"default","triggerCmd":"trigger","flushCmd":"flush","resetCmd":"reset","peekCmd":"","dropCmd":"","statusCmd":"","maxQueueLength":"1","keepNewest":true,"qToggle":true,"persist":true,"storeName":"file","x":755,"y":760,"wires":[["b36ec2b3b096f8d6"]]},{"id":"f38eeefba2af67ce","type":"q-gate","z":"7ea0620.a243ca","name":"color_temp 3","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","queueCmd":"queue","defaultCmd":"default","triggerCmd":"trigger","flushCmd":"flush","resetCmd":"reset","peekCmd":"","dropCmd":"","statusCmd":"","maxQueueLength":"1","keepNewest":true,"qToggle":true,"persist":true,"storeName":"file","x":755,"y":960,"wires":[["1bd0cac097924a72"]]},{"id":"9dfb80638a8e3d73","type":"q-gate","z":"7ea0620.a243ca","name":"color_temp 1","controlTopic":"control","defaultState":"closed","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","queueCmd":"queue","defaultCmd":"default","triggerCmd":"trigger","flushCmd":"flush","resetCmd":"reset","peekCmd":"","dropCmd":"","statusCmd":"","maxQueueLength":"1","keepNewest":true,"qToggle":true,"persist":true,"storeName":"file","x":750,"y":440,"wires":[["ee6e3c5a8750dbd3"]]},{"id":"22570f7cadf11491","type":"q-gate","z":"7ea0620.a243ca","name":"brightness 3","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","queueCmd":"queue","defaultCmd":"default","triggerCmd":"trigger","flushCmd":"flush","resetCmd":"reset","peekCmd":"","dropCmd":"","statusCmd":"","maxQueueLength":"1","keepNewest":true,"qToggle":true,"persist":true,"storeName":"file","x":755,"y":1020,"wires":[["1bd0cac097924a72"]]},{"id":"1c0b19f6eaee2844","type":"q-gate","z":"7ea0620.a243ca","name":"brightness 1","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","queueCmd":"queue","defaultCmd":"default","triggerCmd":"trigger","flushCmd":"flush","resetCmd":"reset","peekCmd":"","dropCmd":"","statusCmd":"","maxQueueLength":"1","keepNewest":true,"qToggle":true,"persist":true,"storeName":"file","x":750,"y":500,"wires":[["ee6e3c5a8750dbd3"]]},{"id":"a8f21399bc702421","type":"link out","z":"7ea0620.a243ca","name":"Circadian Office","mode":"link","links":["2a3d1ccf39330b28","4193dc61304afe65","4b80d3dcd0dcb9fa","4f8d44ef76bac586","bb5ea6235f3f465f","f6f58ab7e35f5688","dcfa43aa5b436092","43c2e1ab9d4c3a33","3055458e27486427","9d3d2931ff09f30e","df433767e85260dc","46442d133be6f963","4ccd12c7e9fa28f9"],"x":330,"y":600,"wires":[]},{"id":"448f5e6c691dccc4","type":"server-state-changed","z":"7ea0620.a243ca","name":"Light 1","server":"c879ac61.99cd1","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.office_light_1","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"for":0,"forType":"num","forUnits":"milliseconds","ignorePrevStateNull":true,"ignorePrevStateUnknown":true,"ignorePrevStateUnavailable":true,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"},{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"}],"x":70,"y":440,"wires":[["9843d42f60a63e30"]]},{"id":"4193dc61304afe65","type":"link in","z":"7ea0620.a243ca","name":"Office Light 3 Brightness ","links":["a8f21399bc702421","f66d24bc98a1e3de","5da1ef5db9218f1e"],"x":620,"y":1080,"wires":[["1489ae336f7022ab"]]},{"id":"2a3d1ccf39330b28","type":"link in","z":"7ea0620.a243ca","name":"Office Light 1 Color","links":["a8f21399bc702421","b05b2fb283191597","cd1e6d98e1856bda"],"x":615,"y":380,"wires":[["11dfa753a1a2cd05"]]},{"id":"f6f58ab7e35f5688","type":"link in","z":"7ea0620.a243ca","name":"Office Light 2 Color","links":["166c734b13052fb7","a8f21399bc702421","cd1e6d98e1856bda"],"x":620,"y":640,"wires":[["7b9a79c482830d5b"]]},{"id":"4f8d44ef76bac586","type":"link in","z":"7ea0620.a243ca","name":"Office Light 1 Brightness","links":["a8f21399bc702421","a92499638df3f0ae","f66d24bc98a1e3de"],"x":615,"y":560,"wires":[["94a02ed09a89725d"]]},{"id":"ae45a60691ab54fc","type":"server-state-changed","z":"7ea0620.a243ca","name":"Light 2","server":"c879ac61.99cd1","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.office_light_2","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"for":0,"forType":"num","forUnits":"milliseconds","ignorePrevStateNull":true,"ignorePrevStateUnknown":true,"ignorePrevStateUnavailable":true,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"},{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"}],"x":55,"y":760,"wires":[["5bdc695b13ac08ce"]]},{"id":"900f6d3a6843c9d4","type":"server-state-changed","z":"7ea0620.a243ca","name":"Light 3","server":"c879ac61.99cd1","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.office_light_3","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"for":0,"forType":"num","forUnits":"milliseconds","ignorePrevStateNull":true,"ignorePrevStateUnknown":true,"ignorePrevStateUnavailable":true,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"data","propertyType":"msg","value":"","valueType":"eventData"}],"x":55,"y":980,"wires":[["360cde2520d4ef73"]]},{"id":"f66d24bc98a1e3de","type":"link out","z":"7ea0620.a243ca","name":"Office Brightness","mode":"link","links":["4193dc61304afe65","4f8d44ef76bac586","bb5ea6235f3f465f","3055458e27486427","df433767e85260dc","4ccd12c7e9fa28f9"],"x":330,"y":660,"wires":[]},{"id":"cd1e6d98e1856bda","type":"link out","z":"7ea0620.a243ca","name":"Office Color","mode":"link","links":["2a3d1ccf39330b28","4b80d3dcd0dcb9fa","f6f58ab7e35f5688","43c2e1ab9d4c3a33","9d3d2931ff09f30e","46442d133be6f963"],"x":330,"y":540,"wires":[]},{"id":"f75a6792daaeb0b4","type":"comment","z":"7ea0620.a243ca","name":"Office","info":"","x":70,"y":340,"wires":[]},{"id":"11dfa753a1a2cd05","type":"ha-entity","z":"7ea0620.a243ca","name":"Light 1 Color Control","server":"c879ac61.99cd1","version":2,"debugenabled":false,"outputs":1,"entityType":"binary_sensor","config":[{"property":"name","value":"Office Light 1 Color Control"},{"property":"device_class","value":"opening"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload = \"open\" ? \"on\" : \"off\"\t","stateType":"jsonata","attributes":[{"property":"command","value":"payload","valueType":"msg"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":780,"y":380,"wires":[[]]},{"id":"94a02ed09a89725d","type":"ha-entity","z":"7ea0620.a243ca","name":"Light 1 Brightness Control","server":"c879ac61.99cd1","version":2,"debugenabled":false,"outputs":1,"entityType":"binary_sensor","config":[{"property":"name","value":"Office Light 1 Brightness Control"},{"property":"device_class","value":"opening"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload = \"open\" ? \"on\" : \"off\"\t","stateType":"jsonata","attributes":[{"property":"command","value":"payload","valueType":"msg"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":790,"y":560,"wires":[[]]},{"id":"7b9a79c482830d5b","type":"ha-entity","z":"7ea0620.a243ca","name":"Light 2 Color Control","server":"c879ac61.99cd1","version":2,"debugenabled":false,"outputs":1,"entityType":"binary_sensor","config":[{"property":"name","value":"Office Light 2 Color Control"},{"property":"device_class","value":"opening"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload = \"open\" ? \"on\" : \"off\"\t","stateType":"jsonata","attributes":[{"property":"command","value":"payload","valueType":"msg"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":785,"y":640,"wires":[[]]},{"id":"6e90b1ffee7f80b8","type":"ha-entity","z":"7ea0620.a243ca","name":"Light 2 Brightness Control","server":"c879ac61.99cd1","version":2,"debugenabled":false,"outputs":1,"entityType":"binary_sensor","config":[{"property":"name","value":"Office Light 2 Brightness Control"},{"property":"device_class","value":"opening"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload = \"open\" ? \"on\" : \"off\"\t","stateType":"jsonata","attributes":[{"property":"command","value":"payload","valueType":"msg"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":795,"y":820,"wires":[[]]},{"id":"e847b97612b57cde","type":"ha-entity","z":"7ea0620.a243ca","name":"Light 3 Color Control","server":"c879ac61.99cd1","version":2,"debugenabled":false,"outputs":1,"entityType":"binary_sensor","config":[{"property":"name","value":"Office Light 3 Color Control"},{"property":"device_class","value":"opening"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload = \"open\" ? \"on\" : \"off\"\t","stateType":"jsonata","attributes":[{"property":"command","value":"payload","valueType":"msg"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":785,"y":900,"wires":[[]]},{"id":"1489ae336f7022ab","type":"ha-entity","z":"7ea0620.a243ca","name":"Light 3 Brightness Control","server":"c879ac61.99cd1","version":2,"debugenabled":false,"outputs":1,"entityType":"binary_sensor","config":[{"property":"name","value":"Office Light 3 Brightness Control"},{"property":"device_class","value":"opening"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload = \"open\" ? \"on\" : \"off\"\t","stateType":"jsonata","attributes":[{"property":"command","value":"payload","valueType":"msg"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":795,"y":1080,"wires":[[]]},{"id":"ee6e3c5a8750dbd3","type":"join","z":"7ea0620.a243ca","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"1","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":915,"y":460,"wires":[["ae9faf4f99f5df6b"]]},{"id":"b36ec2b3b096f8d6","type":"join","z":"7ea0620.a243ca","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"1","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":920,"y":720,"wires":[["4449a34d85b294de"]]},{"id":"1bd0cac097924a72","type":"join","z":"7ea0620.a243ca","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"1","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":920,"y":980,"wires":[["5e4f3934b95b1e9e"]]},{"id":"2758054b03a4112e","type":"subflow:03454e69d1fb38da","z":"7ea0620.a243ca","name":"Office Light 1 Control","x":380,"y":440,"wires":[["b05b2fb283191597"],["a92499638df3f0ae"],[]]},{"id":"6ab4a1291df30475","type":"subflow:03454e69d1fb38da","z":"7ea0620.a243ca","name":"Office Light 2 Control","x":365,"y":760,"wires":[["166c734b13052fb7"],["fe02e119919f1fbf"],[]]},{"id":"5e7dad3531a9ab92","type":"subflow:03454e69d1fb38da","z":"7ea0620.a243ca","name":"Office Light 3 Control","x":365,"y":980,"wires":[["babbee9d013605d2"],["5da1ef5db9218f1e"],[]]},{"id":"5bdc695b13ac08ce","type":"trigger","z":"7ea0620.a243ca","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"1","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":185,"y":760,"wires":[["6ab4a1291df30475"]]},{"id":"9843d42f60a63e30","type":"trigger","z":"7ea0620.a243ca","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"1","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":200,"y":440,"wires":[["2758054b03a4112e"]]},{"id":"360cde2520d4ef73","type":"trigger","z":"7ea0620.a243ca","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"1","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":185,"y":980,"wires":[["5e7dad3531a9ab92"]]},{"id":"668bfd5e97d64c23","type":"trigger","z":"7ea0620.a243ca","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"1","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":1205,"y":680,"wires":[["3863e49219f2c06c"]]},{"id":"6985e773c83c44f3","type":"trigger","z":"7ea0620.a243ca","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"1","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":1205,"y":940,"wires":[["c5c6137c20db841c"]]},{"id":"c6387631e23d9171","type":"trigger","z":"7ea0620.a243ca","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"1","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":1200,"y":420,"wires":[["60a4dca9.c29d14"]]},{"id":"43c2e1ab9d4c3a33","type":"link in","z":"7ea0620.a243ca","name":"Office Light 1 Color Temp q-gate","links":["934e0600896a7adb","a8f21399bc702421","b05b2fb283191597","cd1e6d98e1856bda","9c85a0510c0826d0"],"x":615,"y":440,"wires":[["9dfb80638a8e3d73"]]},{"id":"3055458e27486427","type":"link in","z":"7ea0620.a243ca","name":"Office Light 1 Brightness q-gate","links":["a8f21399bc702421","a92499638df3f0ae","b1ee887a5f59e581","f66d24bc98a1e3de","9c85a0510c0826d0"],"x":615,"y":500,"wires":[["1c0b19f6eaee2844"]]},{"id":"b05b2fb283191597","type":"link out","z":"7ea0620.a243ca","name":"Office Light 1 Color Control","mode":"link","links":["43c2e1ab9d4c3a33","2a3d1ccf39330b28"],"x":525,"y":420,"wires":[]},{"id":"a92499638df3f0ae","type":"link out","z":"7ea0620.a243ca","name":"Office Light 1 Brightness Control","mode":"link","links":["3055458e27486427","4f8d44ef76bac586"],"x":525,"y":460,"wires":[]},{"id":"166c734b13052fb7","type":"link out","z":"7ea0620.a243ca","name":"Office Light 2 Color Control","mode":"link","links":["f6f58ab7e35f5688","9d3d2931ff09f30e"],"x":510,"y":740,"wires":[]},{"id":"fe02e119919f1fbf","type":"link out","z":"7ea0620.a243ca","name":"Office Light 2 Brightness Control","mode":"link","links":["bb5ea6235f3f465f","df433767e85260dc"],"x":510,"y":780,"wires":[]},{"id":"9d3d2931ff09f30e","type":"link in","z":"7ea0620.a243ca","name":"Office Light 2 Color Temp q-gate","links":["166c734b13052fb7","934e0600896a7adb","a8f21399bc702421","cd1e6d98e1856bda","8f139b7a2e1e1072"],"x":620,"y":700,"wires":[["5fcdbdfcad60b453"]]},{"id":"df433767e85260dc","type":"link in","z":"7ea0620.a243ca","name":"Office Light 2 Brightness q-gate","links":["a8f21399bc702421","b1ee887a5f59e581","f66d24bc98a1e3de","fe02e119919f1fbf","8f139b7a2e1e1072"],"x":620,"y":760,"wires":[["5731df33e0ef2e56"]]},{"id":"babbee9d013605d2","type":"link out","z":"7ea0620.a243ca","name":"Office Light 2 Color Control","mode":"link","links":["46442d133be6f963","4b80d3dcd0dcb9fa"],"x":510,"y":960,"wires":[]},{"id":"5da1ef5db9218f1e","type":"link out","z":"7ea0620.a243ca","name":"Office Light 2 Brightness Control","mode":"link","links":["4193dc61304afe65","4ccd12c7e9fa28f9"],"x":510,"y":1000,"wires":[]},{"id":"46442d133be6f963","type":"link in","z":"7ea0620.a243ca","name":"Office Light 3 Color Temp q-gate","links":["934e0600896a7adb","a8f21399bc702421","cd1e6d98e1856bda","babbee9d013605d2","3c70d73a047e84c9"],"x":620,"y":960,"wires":[["f38eeefba2af67ce"]]},{"id":"4ccd12c7e9fa28f9","type":"link in","z":"7ea0620.a243ca","name":"Office Light 3 Brightness q-gate","links":["5da1ef5db9218f1e","a8f21399bc702421","b1ee887a5f59e581","f66d24bc98a1e3de","3c70d73a047e84c9"],"x":620,"y":1020,"wires":[["22570f7cadf11491"]]},{"id":"ae9faf4f99f5df6b","type":"api-current-state","z":"7ea0620.a243ca","name":"On?","server":"c879ac61.99cd1","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.office_light_1","state_type":"str","blockInputOverrides":false,"outputProperties":[],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":1035,"y":460,"wires":[["c6387631e23d9171"],["d96a7d6cd4f42087"]]},{"id":"4449a34d85b294de","type":"api-current-state","z":"7ea0620.a243ca","name":"On?","server":"c879ac61.99cd1","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.office_light_2","state_type":"str","blockInputOverrides":false,"outputProperties":[],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":1040,"y":720,"wires":[["668bfd5e97d64c23"],["d5a0d787dd0474e6"]]},{"id":"5e4f3934b95b1e9e","type":"api-current-state","z":"7ea0620.a243ca","name":"On?","server":"c879ac61.99cd1","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.office_light_3","state_type":"str","blockInputOverrides":false,"outputProperties":[],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":1055,"y":980,"wires":[["6985e773c83c44f3"],["d17fef61a294583d"]]},{"id":"d96a7d6cd4f42087","type":"change","z":"7ea0620.a243ca","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"queue","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1220,"y":500,"wires":[["9c85a0510c0826d0"]]},{"id":"9c85a0510c0826d0","type":"link out","z":"7ea0620.a243ca","name":"Office Light 1 is off","mode":"link","links":["3055458e27486427","43c2e1ab9d4c3a33"],"x":1335,"y":500,"wires":[]},{"id":"d5a0d787dd0474e6","type":"change","z":"7ea0620.a243ca","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"queue","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1225,"y":760,"wires":[["8f139b7a2e1e1072"]]},{"id":"8f139b7a2e1e1072","type":"link out","z":"7ea0620.a243ca","name":"Office Light 1 is off","mode":"link","links":["9d3d2931ff09f30e","df433767e85260dc"],"x":1340,"y":760,"wires":[]},{"id":"3c70d73a047e84c9","type":"link out","z":"7ea0620.a243ca","name":"Office Light 1 is off","mode":"link","links":["46442d133be6f963","4ccd12c7e9fa28f9"],"x":1340,"y":1020,"wires":[]},{"id":"d17fef61a294583d","type":"change","z":"7ea0620.a243ca","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"queue","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1225,"y":1020,"wires":[["3c70d73a047e84c9"]]},{"id":"911fe5374aa62c8f","type":"debug","z":"7ea0620.a243ca","name":"LIGHT 3 >>","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":475,"y":1040,"wires":[]},{"id":"a7af95da048df2ed","type":"debug","z":"7ea0620.a243ca","name":"JOIN","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1035,"y":940,"wires":[]},{"id":"9752a473fa01a315","type":"debug","z":"7ea0620.a243ca","name":">> LIGHT 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":275,"y":1040,"wires":[]},{"id":"c5e4a35bf6b097fc","type":"debug","z":"7ea0620.a243ca","name":"color control","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1015,"y":900,"wires":[]},{"id":"717f986548852cb7","type":"debug","z":"7ea0620.a243ca","name":"brightness control","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1035,"y":1040,"wires":[]},{"id":"c879ac61.99cd1","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30"}]
1 Like

Brightness only:

[{"id":"072b6cc83f33fb15","type":"subflow","name":"Brightness Control","info":"","category":"","in":[{"x":380,"y":120,"wires":[{"id":"987e20ee59d62bb0"}]}],"out":[{"x":600,"y":80,"wires":[{"id":"987e20ee59d62bb0","port":0}]},{"x":600,"y":120,"wires":[{"id":"987e20ee59d62bb0","port":1}]}],"env":[],"meta":{},"color":"#DDAA99","status":{"x":600,"y":160,"wires":[{"id":"987e20ee59d62bb0","port":2}]}},{"id":"987e20ee59d62bb0","type":"function","z":"072b6cc83f33fb15","name":"","func":"var circadian_brightness = global.get(\"nr_circadian_brightness\");\nvar calculated_brightness_state = Math.round(circadian_brightness * 0.996078431);\nif (calculated_brightness_state == 254) {calculated_brightness_state = 255}\nvar entity_id = msg.data.entity_id\nvar current_brightness = msg.data.new_state.attributes.brightness;\n\n\nvar debug =  \n    {\n        \"Entity\": entity_id,\n        \"Old brightness\": msg.data.old_state.attributes.brightness,\n        \"New brightness\": msg.data.new_state.attributes.brightness,\n        \"Current brightness\": current_brightness,\n        \"Calculated state brightness\": calculated_brightness_state,\n        \"Circadian brightness\": circadian_brightness\n    }\n\nif (msg.data.old_state.state == \"on\" && msg.data.new_state.state == \"on\"){\n        if (current_brightness == calculated_brightness_state){\n            return [{topic: \"control\", payload: \"open\", data: {brightness: circadian_brightness}}, debug]\n        }\n        if (current_brightness != calculated_brightness_state){\n            return [{topic: \"control\", payload: \"close\"}, debug]\n        }\n    }\n    \nif (msg.data.old_state.state == \"off\" && msg.data.new_state.state == \"on\"){\n    return [ { topic: \"control\", payload: \"open\", data: {brightness: circadian_brightness}}, debug ]}\n\n/* payload.brightness */\n\nif (msg.data.old_state.state == \"on\" && msg.data.new_state.state == \"off\"){\n        return [{ topic: \"control\", payload: \"queue\"}, debug ]\n    }\n\nreturn [null, debug]","outputs":3,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":120,"wires":[[],[],[]]},{"id":"d75369b2320f4428","type":"debug","z":"072b6cc83f33fb15","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"$globalContext('nr_circadian_mireds')\t","targetType":"jsonata","statusVal":"","statusType":"auto","x":760,"y":140,"wires":[]},{"id":"8327cd21ef95b941","type":"debug","z":"072b6cc83f33fb15","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"$globalContext('nr_circadian_brightness')\t","targetType":"jsonata","statusVal":"","statusType":"auto","x":760,"y":100,"wires":[]},{"id":"b957873e.b725c8","type":"server-state-changed","z":"7ea0620.a243ca","name":"Circadian Bathroom","server":"c879ac61.99cd1","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.circadian_lighting_bathroom","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":true,"ignorePrevStateUnknown":true,"ignorePrevStateUnavailable":true,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"$entity().state = \"on\" ? \"open\" : \"queue\"","valueType":"jsonata"},{"property":"topic","propertyType":"msg","value":"control","valueType":"str"}],"x":370,"y":1920,"wires":[["128303271cb1130d"]]},{"id":"edb74bee98fc49eb","type":"api-call-service","z":"7ea0620.a243ca","name":"Kitchen","server":"c879ac61.99cd1","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.kitchen_light"],"data":"{\t   \"brightness\": $max(\t       [\t           $number($globalContext('homeassistant.homeAssistant.states[\"input_number.kitchen_minimum\"].state')),\t           msg.payload\t       ]\t   )\t}","dataType":"jsonata","mergeContext":"lightOptions","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1220,"y":1720,"wires":[[]]},{"id":"fc77eb6f5d536827","type":"q-gate","z":"7ea0620.a243ca","name":"brightness","controlTopic":"control","defaultState":"closed","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","queueCmd":"queue","defaultCmd":"default","triggerCmd":"trigger","flushCmd":"flush","resetCmd":"reset","peekCmd":"","dropCmd":"","statusCmd":"","maxQueueLength":"1","keepNewest":true,"qToggle":true,"persist":true,"storeName":"file","x":750,"y":1760,"wires":[["b22ab8a694b12c60"]]},{"id":"314c2c70ebd4f39a","type":"server-state-changed","z":"7ea0620.a243ca","name":"Kitchen","server":"c879ac61.99cd1","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.kitchen_light","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":true,"ignorePrevStateUnknown":true,"ignorePrevStateUnavailable":true,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"},{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"}],"x":55,"y":1760,"wires":[["1322873ff168384d"]]},{"id":"fe41bfcf0ed50cf5","type":"link in","z":"7ea0620.a243ca","name":"Kitchen Brightness Control","links":["dada9119ebbe0596","ff43789985c67c08"],"x":615,"y":1820,"wires":[["0579c2f5b054abe8"]]},{"id":"75b33e99cafd8042","type":"comment","z":"7ea0620.a243ca","name":"Kitchen","info":"","x":70,"y":1700,"wires":[]},{"id":"0579c2f5b054abe8","type":"ha-entity","z":"7ea0620.a243ca","name":"Kitchen Brightness Control","server":"c879ac61.99cd1","version":2,"debugenabled":false,"outputs":1,"entityType":"binary_sensor","config":[{"property":"name","value":"Kitchen Brightness Control"},{"property":"device_class","value":"opening"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload = \"open\" ? \"on\" : \"off\"\t","stateType":"jsonata","attributes":[{"property":"command","value":"payload","valueType":"msg"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":800,"y":1820,"wires":[[]]},{"id":"6c9f2202a1fff2d5","type":"server-state-changed","z":"7ea0620.a243ca","name":"Bathroom","server":"c879ac61.99cd1","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.bathroom_light","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":true,"ignorePrevStateUnknown":true,"ignorePrevStateUnavailable":true,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"}],"x":65,"y":2100,"wires":[["94f3524f701742aa"]]},{"id":"2703a86ea0d3038e","type":"server-state-changed","z":"7ea0620.a243ca","name":"Bathroom Brightness","server":"c879ac61.99cd1","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.circadian_lighting_brightness_bathroom","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":true,"ignorePrevStateUnknown":true,"ignorePrevStateUnavailable":true,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"$entity().state = \"on\" ? \"open\" : \"queue\"","valueType":"jsonata"},{"property":"topic","propertyType":"msg","value":"control","valueType":"str"}],"x":360,"y":1980,"wires":[["128303271cb1130d"]]},{"id":"128303271cb1130d","type":"link out","z":"7ea0620.a243ca","name":"Circadian Bathroom ","mode":"link","links":["17fcb852b5d4df73","ffa6896cb76348d5"],"x":525,"y":1980,"wires":[]},{"id":"35ad34b8f96115c9","type":"q-gate","z":"7ea0620.a243ca","name":"brightness","controlTopic":"control","defaultState":"closed","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","queueCmd":"queue","defaultCmd":"default","triggerCmd":"trigger","flushCmd":"flush","resetCmd":"reset","peekCmd":"","dropCmd":"","statusCmd":"","maxQueueLength":"1","keepNewest":true,"qToggle":true,"persist":true,"storeName":"file","x":750,"y":1980,"wires":[["66276b24fa10ad45"]]},{"id":"17fcb852b5d4df73","type":"link in","z":"7ea0620.a243ca","name":"Bathroom Brightness Control","links":["128303271cb1130d","1d9a557d60b23ece","a95d943d637d7a92"],"x":615,"y":2040,"wires":[["5ba97e1504657679"]]},{"id":"5ba97e1504657679","type":"ha-entity","z":"7ea0620.a243ca","name":"Bathroom Brightness Control","server":"c879ac61.99cd1","version":2,"debugenabled":false,"outputs":1,"entityType":"binary_sensor","config":[{"property":"name","value":"Bathroom Brightness Control"},{"property":"device_class","value":"opening"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload = \"open\" ? \"on\" : \"off\"\t","stateType":"jsonata","attributes":[{"property":"command","value":"payload","valueType":"str"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":800,"y":2040,"wires":[[]]},{"id":"9f996a25e40fb144","type":"comment","z":"7ea0620.a243ca","name":"Bathroom","info":"","x":80,"y":1880,"wires":[]},{"id":"25693f6bb04c3b3a","type":"server-state-changed","z":"7ea0620.a243ca","name":"Sleep Mode","server":"c879ac61.99cd1","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.sleep_mode","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"ff","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":false,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":true,"ignorePrevStateUnknown":true,"ignorePrevStateUnavailable":true,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"$entity().state = \"on\" ? \"queue\" : \"default\"","valueType":"jsonata"},{"property":"topic","propertyType":"msg","value":"control","valueType":"str"}],"x":390,"y":2040,"wires":[["128303271cb1130d"],[]]},{"id":"5fb1a55d922a5654","type":"api-current-state","z":"7ea0620.a243ca","name":"Sleep Mode","server":"c879ac61.99cd1","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_boolean.sleep_mode","state_type":"str","blockInputOverrides":false,"outputProperties":[],"for":0,"forType":"num","forUnits":"minutes","x":1050,"y":1960,"wires":[[],["24502b8b2345bd39"]]},{"id":"1322873ff168384d","type":"trigger","z":"7ea0620.a243ca","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"1","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":185,"y":1760,"wires":[["76a4726f666b0555"]]},{"id":"94f3524f701742aa","type":"trigger","z":"7ea0620.a243ca","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"1","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":206,"y":2100,"wires":[["3a5334c9c04ac17f"]]},{"id":"2e87f3b04f851720","type":"trigger","z":"7ea0620.a243ca","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"1","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":1060,"y":1720,"wires":[["edb74bee98fc49eb"]]},{"id":"b22ab8a694b12c60","type":"api-current-state","z":"7ea0620.a243ca","name":"On?","server":"c879ac61.99cd1","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.kitchen_light","state_type":"str","blockInputOverrides":false,"outputProperties":[],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":910,"y":1760,"wires":[["2e87f3b04f851720"],["ac49f9c7c3f9feba"]]},{"id":"66276b24fa10ad45","type":"api-current-state","z":"7ea0620.a243ca","name":"On?","server":"c879ac61.99cd1","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.bathroom_light","state_type":"str","blockInputOverrides":false,"outputProperties":[],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":890,"y":1980,"wires":[["5fb1a55d922a5654"],["6b60e2dfc7448ee0"]]},{"id":"ac49f9c7c3f9feba","type":"change","z":"7ea0620.a243ca","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"queue","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1080,"y":1800,"wires":[["dada9119ebbe0596"]]},{"id":"dada9119ebbe0596","type":"link out","z":"7ea0620.a243ca","name":"Kitchen Light is off","mode":"link","links":["fe41bfcf0ed50cf5"],"x":1195,"y":1800,"wires":[]},{"id":"6b60e2dfc7448ee0","type":"change","z":"7ea0620.a243ca","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"queue","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1060,"y":2020,"wires":[["1d9a557d60b23ece"]]},{"id":"1d9a557d60b23ece","type":"link out","z":"7ea0620.a243ca","name":"Bathroom Light 1 is off","mode":"link","links":["17fcb852b5d4df73"],"x":1175,"y":2020,"wires":[]},{"id":"19af89da0660a8f1","type":"link in","z":"7ea0620.a243ca","name":"Kitchen Brightness","links":["b1ee887a5f59e581","ff43789985c67c08"],"x":615,"y":1760,"wires":[["fc77eb6f5d536827"]]},{"id":"ff43789985c67c08","type":"link out","z":"7ea0620.a243ca","name":"Kitchen Light","mode":"link","links":["fe41bfcf0ed50cf5","19af89da0660a8f1"],"x":525,"y":1760,"wires":[]},{"id":"ffa6896cb76348d5","type":"link in","z":"7ea0620.a243ca","name":"Bathroom Brightness","links":["128303271cb1130d","a95d943d637d7a92","b1ee887a5f59e581"],"x":615,"y":1980,"wires":[["35ad34b8f96115c9"]]},{"id":"a95d943d637d7a92","type":"link out","z":"7ea0620.a243ca","name":"Bathroom Light","mode":"link","links":["17fcb852b5d4df73","ffa6896cb76348d5"],"x":525,"y":2040,"wires":[]},{"id":"24502b8b2345bd39","type":"api-call-service","z":"7ea0620.a243ca","name":"Bathroom","server":"c879ac61.99cd1","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.bathroom_light"],"data":"{\t   \"brightness\": $max(\t       [\t           $number($globalContext('homeassistant.homeAssistant.states[\"input_number.bathroom_minimum\"].state')),\t           msg.payload\t       ]\t   )\t}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1220,"y":1960,"wires":[[]]},{"id":"76a4726f666b0555","type":"subflow:072b6cc83f33fb15","z":"7ea0620.a243ca","name":"Kitchen Control","x":380,"y":1760,"wires":[["ff43789985c67c08"],[]]},{"id":"3a5334c9c04ac17f","type":"subflow:072b6cc83f33fb15","z":"7ea0620.a243ca","name":"Bathrrom Control","x":380,"y":2100,"wires":[["a95d943d637d7a92"],[]]},{"id":"c879ac61.99cd1","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30"}]

I added the HA entities which reflect the state of the q-gates to this panel so I can quickly see the state of the q-gates without having to open Node-Red:

2 Likes

Hi, thank you for your work!

So I set one light up and try to understand whats going on. The color gate does indeed go closed every now and then.

My question ist: if I have every gate open, getting correct values for brightness/color from calculator and switch my light on with very different values, why does it take so long to adjust (had around 15 minutes)? What triggers it to change? The light.turn_on? But then it should change immediately, what it never does. What’s the interval behind it to adjust? Can you explain that to me?

My latest flow…

[{"id":"03454e69d1fb38da","type":"subflow","name":"Color / Brightness","info":"Color / Brightness Control commands for q-gate node","category":"","in":[{"x":340,"y":140,"wires":[{"id":"e9076938078efe18"}]}],"out":[{"x":640,"y":60,"wires":[{"id":"e9076938078efe18","port":0}]},{"x":640,"y":120,"wires":[{"id":"e9076938078efe18","port":1}]},{"x":640,"y":180,"wires":[{"id":"e9076938078efe18","port":2}]}],"env":[],"meta":{},"color":"#DDAA99","status":{"x":640,"y":240,"wires":[]}},{"id":"e9076938078efe18","type":"function","z":"03454e69d1fb38da","name":"","func":"var entity_id = msg.data.entity_id\nvar current_brightness = msg.data.new_state.attributes.brightness;\nvar current_color_temp = msg.data.new_state.attributes.color_temp;\nvar circadian_mireds = global.get(\"nr_circadian_mireds\");\nvar circadian_brightness = global.get(\"nr_circadian_brightness\");\n/*\nvar calculated_brightness_state = Math.round(circadian_brightness * 0.996078431);\nif (calculated_brightness_state == 254) {calculated_brightness_state = 255}\n*/\ncircadian_brightness = Math.round(circadian_brightness * 0.996078431) + Math.floor(circadian_brightness/255);\n\nvar debug =  \n    {\n        \"Entity\": entity_id,\n        \"Old color\": msg.data.old_state.attributes.color_temp,\n        \"New color\": msg.data.new_state.attributes.color_temp,\n        \"Current color_temp\": current_color_temp,\n        \"Circadian mireds\": circadian_mireds,\n        \"Old brightness\": msg.data.old_state.attributes.brightness,\n        \"New brightness\": msg.data.new_state.attributes.brightness,\n        \"Current brightness\": current_brightness,\n        \"Circadian brightness\": circadian_brightness\n    }\n\nif (msg.data.old_state.state == \"on\" && msg.data.new_state.state == \"on\"){\n\n        if (current_color_temp == circadian_mireds &&\n            current_brightness == circadian_brightness){\n            return [{topic: \"control\", payload: \"open\"}, {topic: \"control\", payload: \"open\"}, debug]\n        }\n        if (current_color_temp != circadian_mireds &&\n            current_brightness == circadian_brightness){\n            return [{topic: \"control\", payload: \"queue\"}, {topic: \"control\", payload: \"open\"}, debug]\n        }\n        if (current_color_temp == circadian_mireds &&\n            current_brightness != circadian_brightness){\n            return [{topic: \"control\", payload: \"open\"}, {topic: \"control\", payload: \"queue\"}, debug]\n        }\n        if (current_color_temp != circadian_mireds && \n            current_brightness != circadian_brightness){\n            return [{topic: \"control\", payload: \"queue\"}, {topic: \"control\", payload: \"queue\"}, debug]\n        }\n    }\n    \nif (msg.data.old_state.state == \"off\" && msg.data.new_state.state == \"on\"){\n    return [ { topic: \"control\", payload: \"open\", data: {color_temp: circadian_mireds}},\n             { topic: \"control\", payload: \"open\", data: {brightness: circadian_brightness}},\n             debug ]}\n\n/* payload.brightness */\n\nif (msg.data.old_state.state == \"on\" && msg.data.new_state.state == \"off\"){\n        return [{ topic: \"control\", payload: \"queue\" }, { topic: \"control\", payload: \"queue\"}, debug ]\n    }\n\nreturn [null, null, debug]\n\n","outputs":3,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":140,"wires":[[],[],[]]},{"id":"ae8af5aa195ee960","type":"debug","z":"03454e69d1fb38da","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"$globalContext('nr_circadian_mireds')\t","targetType":"jsonata","statusVal":"","statusType":"auto","x":420,"y":260,"wires":[]},{"id":"15d3ba0a712a2f77","type":"debug","z":"03454e69d1fb38da","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"$globalContext('nr_circadian_brightness')\t","targetType":"jsonata","statusVal":"","statusType":"auto","x":420,"y":220,"wires":[]},{"id":"e812678d.651e28","type":"subflow","name":"Circadian Calculator","info":"","category":"time and astro","in":[{"x":80,"y":80,"wires":[{"id":"aa4b86d8.b17118"}]}],"out":[{"x":890,"y":100,"wires":[{"id":"40de744.1e2ca8c","port":0}]},{"x":890,"y":160,"wires":[{"id":"15b473ec45fa7fc4","port":0}]},{"x":930,"y":260,"wires":[{"id":"6f365d7d05905955","port":0}]},{"x":930,"y":320,"wires":[{"id":"c26bdcca.eb483","port":0}]}],"env":[],"meta":{},"color":"#FFCC66","inputLabels":["Trigger"],"outputLabels":["Brightness Value","Brightness Percent","Mireds","Kelvin"]},{"id":"5de89261.4fb97c","type":"switch","z":"e812678d.651e28","name":"curve switch","property":"payload.times_of_day.active.name","propertyType":"msg","rules":[{"t":"eq","v":"night","vt":"str"},{"t":"eq","v":"morning","vt":"str"},{"t":"eq","v":"day","vt":"str"},{"t":"eq","v":"evening","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":250,"y":220,"wires":[["ba17c1e6.e6f58","840431b7.eeefc"],["e5bb5a04.a33948","d4d99259.d0d7e"],["7c0f0774.90adf8","6ac1af7.dddb15"],["23b260f9.19f78","a907f5f4.0bf488"]]},{"id":"ba17c1e6.e6f58","type":"spline-curve","z":"e812678d.651e28","name":"night curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.3},{"x":0.333,"y":0.037},{"x":0.477,"y":0},{"x":0.783,"y":0},{"x":1,"y":0}],"x":490,"y":80,"wires":[["40de744.1e2ca8c","15b473ec45fa7fc4"]]},{"id":"e5bb5a04.a33948","type":"spline-curve","z":"e812678d.651e28","name":"morning curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.01},{"x":0.132,"y":0.01},{"x":0.269,"y":0.027},{"x":0.449,"y":0.124},{"x":0.676,"y":0.334},{"x":1,"y":0.75}],"x":500,"y":120,"wires":[["40de744.1e2ca8c","15b473ec45fa7fc4"]]},{"id":"23b260f9.19f78","type":"spline-curve","z":"e812678d.651e28","name":"evening curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.85},{"x":0.036,"y":0.78},{"x":0.176,"y":0.63},{"x":1,"y":0.3}],"x":500,"y":200,"wires":[["40de744.1e2ca8c","15b473ec45fa7fc4"]]},{"id":"7c0f0774.90adf8","type":"spline-curve","z":"e812678d.651e28","name":"daylight curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.744},{"x":0.03,"y":0.838},{"x":0.097,"y":0.941},{"x":0.183,"y":1},{"x":0.86,"y":0.995},{"x":0.943,"y":0.931},{"x":1,"y":0.85}],"x":500,"y":160,"wires":[["40de744.1e2ca8c","15b473ec45fa7fc4"]]},{"id":"840431b7.eeefc","type":"spline-curve","z":"e812678d.651e28","name":"night curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.5},{"x":0.067,"y":0.368},{"x":0.133,"y":0.265},{"x":0.27,"y":0.171},{"x":0.333,"y":0.16},{"x":0.506,"y":0.124},{"x":0.606,"y":0}],"x":490,"y":240,"wires":[["c26bdcca.eb483","6f365d7d05905955"]]},{"id":"d4d99259.d0d7e","type":"spline-curve","z":"e812678d.651e28","name":"morning curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0},{"x":0.42,"y":0.018},{"x":0.54,"y":0.051},{"x":0.587,"y":0.101},{"x":0.817,"y":0.675},{"x":1,"y":0.85}],"x":500,"y":280,"wires":[["c26bdcca.eb483","6f365d7d05905955"]]},{"id":"a907f5f4.0bf488","type":"spline-curve","z":"e812678d.651e28","name":"evening curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.85},{"x":0.787,"y":0.598},{"x":1,"y":0.5}],"x":500,"y":360,"wires":[["c26bdcca.eb483","6f365d7d05905955"]]},{"id":"6ac1af7.dddb15","type":"spline-curve","z":"e812678d.651e28","name":"daylight curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.85},{"x":0.123,"y":0.927},{"x":0.5,"y":1},{"x":0.92,"y":0.904},{"x":1,"y":0.85}],"x":500,"y":320,"wires":[["c26bdcca.eb483","6f365d7d05905955"]]},{"id":"a330b219.92e4e","type":"function","z":"e812678d.651e28","name":"Times of Day","func":"var newmsg = JSON.parse(JSON.stringify(msg));\nnewmsg.payload.times_of_day = {};\n\nprocessTimeOfDay = function(nme, obj, name_start, name_end) {\n    var ret = {};\n    ret.name  = nme;\n    ret.start = obj.times[name_start].ts;\n    ret.end   = obj.times[name_end].ts;\n    ret.perc  = (obj.ts - ret.start) / (ret.end - ret.start);\n    \n    \n    // to cover range starting before midnight and ending after it.\n    if (ret.start > ret.end)\n    {\n        var a;\n\n        // before midnight\n        if (ret.start < obj.ts) \n        {\n            a = new Date(ret.end);\n            a.setUTCDate(a.getUTCDate() + 1);\n            ret.end = a.getTime();\n             \n             \n        }\n        else if (ret.end > obj.ts) \n        {\n            a = new Date(ret.start);\n            a.setUTCDate(a.getUTCDate() - 1);\n            ret.start = a.getTime();\n\n        }\n\n    }\n    \n    ret.perc  = (obj.ts - ret.start) / (ret.end - ret.start);\n    \n    obj.times_of_day[nme] = ret;\n    if (ret.start <= obj.ts && obj.ts < ret.end ) obj.times_of_day.active = ret;\n}\n\n\nprocessTimeOfDay('night', newmsg.payload, \"astronomicalDusk\", \"astronomicalDawn\");\nprocessTimeOfDay('morning', newmsg.payload, \"astronomicalDawn\", \"goldenHourDawnEnd\");\nprocessTimeOfDay('day', newmsg.payload, \"goldenHourDawnEnd\", \"sunsetStart\");\nprocessTimeOfDay('evening', newmsg.payload, \"sunsetStart\", \"astronomicalDusk\");\n\n\nreturn newmsg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":230,"y":140,"wires":[["5de89261.4fb97c"]]},{"id":"40de744.1e2ca8c","type":"range","z":"e812678d.651e28","minin":"0","maxin":"1","minout":"1","maxout":"254","action":"clamp","round":true,"property":"payload","name":"","x":710,"y":100,"wires":[[]]},{"id":"c26bdcca.eb483","type":"range","z":"e812678d.651e28","minin":"0","maxin":"1","minout":"2700","maxout":"6500","action":"clamp","round":true,"property":"payload","name":"","x":730,"y":320,"wires":[[]]},{"id":"aa4b86d8.b17118","type":"sun-position","z":"e812678d.651e28","name":"","positionConfig":"31826811.22a458","rules":[],"onlyOnChange":"true","topic":"","outputs":1,"start":"","startType":"none","startOffset":0,"startOffsetType":"num","startOffsetMultiplier":60000,"end":"","endType":"none","endOffset":0,"endOffsetType":"num","endOffsetMultiplier":60000,"x":210,"y":80,"wires":[["a330b219.92e4e"]]},{"id":"15b473ec45fa7fc4","type":"range","z":"e812678d.651e28","minin":"0","maxin":"1","minout":"1","maxout":"100","action":"clamp","round":true,"property":"payload","name":"","x":710,"y":160,"wires":[[]]},{"id":"6f365d7d05905955","type":"range","z":"e812678d.651e28","minin":"0","maxin":"1","minout":"370","maxout":"154","action":"clamp","round":true,"property":"payload","name":"","x":720,"y":260,"wires":[[]]},{"id":"31826811.22a458","type":"position-config","name":"Home Location","isValide":"true","longitude":"0","latitude":"0","angleType":"deg","timeZoneOffset":"99","timeZoneDST":"0","stateTimeFormat":"3","stateDateFormat":"12"},{"id":"f5e1d08b.a8cd58","type":"ha-entity","z":"7ea0620.a243ca","name":"kelvin","server":"c879ac61.99cd1","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"nr_circadian_kelvin"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:brightness-6"},{"property":"unit_of_measurement","value":"K"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"nr_circadian_kelvin","stateType":"global","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"msg","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"","outputPayloadType":"str","x":890,"y":220,"wires":[[]]},{"id":"365094a5.72bb5c","type":"subflow:e812678d.651e28","z":"7ea0620.a243ca","name":"","env":[],"x":480,"y":130,"wires":[["5f4a2fbe.d6b89"],["2bd57efd76cfcedf"],["eb1275669172038f","30bf6ebc.f4da52"],[]]},{"id":"e80bd956.f2e098","type":"ha-entity","z":"7ea0620.a243ca","name":"color_temp","server":"c879ac61.99cd1","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"nr_circadian_color_temp"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:brightness-6"},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"nr_circadian_mireds","stateType":"global","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":910,"y":160,"wires":[["53a933a438c1326b"]]},{"id":"476cda43.0f4c94","type":"ha-entity","z":"7ea0620.a243ca","name":"brightness","server":"c879ac61.99cd1","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"nr_circadian_brightness"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:brightness-auto"},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"nr_circadian_brightness","stateType":"global","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"block","outputOnStateChange":false,"outputPayload":"","outputPayloadType":"str","x":910,"y":40,"wires":[["ba7018086eca537b"]]},{"id":"71e41def.76e254","type":"server-state-changed","z":"7ea0620.a243ca","name":"Cicadian Office","server":"c879ac61.99cd1","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.circadian_lighting_office","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":true,"ignorePrevStateUnknown":true,"ignorePrevStateUnavailable":true,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"$entity().state = \"on\" ? \"open\" : \"queue\"","valueType":"jsonata"},{"property":"topic","propertyType":"msg","value":"control","valueType":"str"}],"x":120,"y":320,"wires":[["a8f21399bc702421"]]},{"id":"a7bca137.c0d2f","type":"comment","z":"7ea0620.a243ca","name":"Update","info":"","x":90,"y":40,"wires":[]},{"id":"60a4dca9.c29d14","type":"api-call-service","z":"7ea0620.a243ca","name":"Light 1","server":"c879ac61.99cd1","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.office_light_1"],"data":"{\t   \"brightness\": payload.brightness,\t   \"color_temp\": payload.color_temp\t}","dataType":"jsonata","mergeContext":"lightOptions","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1250,"y":460,"wires":[[]]},{"id":"5f4a2fbe.d6b89","type":"function","z":"7ea0620.a243ca","name":"global brightness","func":"var new_brightness = (msg.payload == 254) ? 255 : msg.payload;\nif (new_brightness != global.get('nr_circadian_brightness')){\n    global.set('nr_circadian_brightness',new_brightness);\n    return {payload: new_brightness}\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":730,"y":40,"wires":[["476cda43.0f4c94"]]},{"id":"30bf6ebc.f4da52","type":"function","z":"7ea0620.a243ca","name":"global kelvin","func":"var new_kelvin = Math.round(1000000/msg.payload,0);\nif (new_kelvin != global.get('nr_circadian_kelvin')) {\n    global.set('nr_circadian_kelvin',new_kelvin);\n    return {payload: new_kelvin}\n    }\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":710,"y":220,"wires":[["f5e1d08b.a8cd58"]]},{"id":"5953dc0a6a10c314","type":"server-state-changed","z":"7ea0620.a243ca","name":"Office Color","server":"c879ac61.99cd1","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.circadian_lighting_color_office","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":true,"ignorePrevStateUnknown":true,"ignorePrevStateUnavailable":true,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"topic","propertyType":"msg","value":"control","valueType":"str"},{"property":"payload","propertyType":"msg","value":"$entity().state = \"on\" ? \"open\" : \"queue\"","valueType":"jsonata"}],"x":110,"y":380,"wires":[["cd1e6d98e1856bda"]]},{"id":"3728361be31f9153","type":"server-state-changed","z":"7ea0620.a243ca","name":"Office Brightness","server":"c879ac61.99cd1","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.circadian_lighting_brightness_office","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":true,"ignorePrevStateUnknown":true,"ignorePrevStateUnavailable":true,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"$entity().state = \"on\" ? \"open\" : \"queue\"","valueType":"jsonata"},{"property":"topic","propertyType":"msg","value":"control","valueType":"str"}],"x":120,"y":440,"wires":[["f66d24bc98a1e3de"]]},{"id":"934e0600896a7adb","type":"link out","z":"7ea0620.a243ca","name":"Mireds","mode":"link","links":["9d3d2931ff09f30e","43c2e1ab9d4c3a33","46442d133be6f963","a52970d03d1f30a5","a897dbf21ffb2b4c"],"x":1195,"y":160,"wires":[]},{"id":"b1ee887a5f59e581","type":"link out","z":"7ea0620.a243ca","name":"Brightness","mode":"link","links":["e376d76995dda48c","4c8de271d9d32579","df433767e85260dc","3055458e27486427","4ccd12c7e9fa28f9","d72d56acc2f72b41","b1137e5488a33dda","ffa6896cb76348d5","19af89da0660a8f1"],"x":1195,"y":40,"wires":[]},{"id":"9dfb80638a8e3d73","type":"q-gate","z":"7ea0620.a243ca","name":"color_temp 1","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","queueCmd":"queue","defaultCmd":"default","triggerCmd":"trigger","flushCmd":"flush","resetCmd":"reset","peekCmd":"","dropCmd":"","statusCmd":"","maxQueueLength":"1","keepNewest":true,"qToggle":true,"persist":true,"storeName":"file","x":810,"y":460,"wires":[["ee6e3c5a8750dbd3"]]},{"id":"1c0b19f6eaee2844","type":"q-gate","z":"7ea0620.a243ca","name":"brightness 1","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","queueCmd":"queue","defaultCmd":"default","triggerCmd":"trigger","flushCmd":"flush","resetCmd":"reset","peekCmd":"","dropCmd":"","statusCmd":"","maxQueueLength":"1","keepNewest":true,"qToggle":true,"persist":true,"storeName":"file","x":810,"y":520,"wires":[["ee6e3c5a8750dbd3"]]},{"id":"a8f21399bc702421","type":"link out","z":"7ea0620.a243ca","name":"Circadian Office","mode":"link","links":["2a3d1ccf39330b28","4193dc61304afe65","4b80d3dcd0dcb9fa","4f8d44ef76bac586","bb5ea6235f3f465f","f6f58ab7e35f5688","dcfa43aa5b436092"],"x":255,"y":320,"wires":[]},{"id":"448f5e6c691dccc4","type":"server-state-changed","z":"7ea0620.a243ca","name":"Light 1","server":"c879ac61.99cd1","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.office_light_1","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"for":"","forType":"num","forUnits":"seconds","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"},{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"}],"x":90,"y":496,"wires":[["9843d42f60a63e30"]]},{"id":"5cd0d7153aaad741","type":"ha-entity","z":"7ea0620.a243ca","name":"brightness_pct","server":"c879ac61.99cd1","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"nr_circadian_brightness_pct"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:brightness-percent"},{"property":"unit_of_measurement","value":"%"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"nr_circadian_brightness_pct","stateType":"global","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"msg","inputOverride":"block","outputOnStateChange":false,"outputPayload":"","outputPayloadType":"str","x":920,"y":100,"wires":[[]]},{"id":"2a3d1ccf39330b28","type":"link in","z":"7ea0620.a243ca","name":"Office Light 1 Color","links":["9c85a0510c0826d0","a8f21399bc702421","b05b2fb283191597","cd1e6d98e1856bda"],"x":675,"y":400,"wires":[["afacf51113b59757"]]},{"id":"4f8d44ef76bac586","type":"link in","z":"7ea0620.a243ca","name":"Office Light 1 Brightness","links":["9c85a0510c0826d0","a8f21399bc702421","a92499638df3f0ae","f66d24bc98a1e3de"],"x":675,"y":580,"wires":[["6b84a041a8d0dacb"]]},{"id":"f66d24bc98a1e3de","type":"link out","z":"7ea0620.a243ca","name":"Office Brightness","mode":"link","links":["4193dc61304afe65","4f8d44ef76bac586","bb5ea6235f3f465f"],"x":255,"y":440,"wires":[]},{"id":"cd1e6d98e1856bda","type":"link out","z":"7ea0620.a243ca","name":"Office Color","mode":"link","links":["2a3d1ccf39330b28","4b80d3dcd0dcb9fa","f6f58ab7e35f5688"],"x":255,"y":380,"wires":[]},{"id":"f75a6792daaeb0b4","type":"comment","z":"7ea0620.a243ca","name":"Office","info":"","x":70,"y":280,"wires":[]},{"id":"eb1275669172038f","type":"function","z":"7ea0620.a243ca","name":"global mireds","func":"var new_mireds = msg.payload\nif (new_mireds != global.get('nr_circadian_mireds')) {\n    global.set('nr_circadian_mireds',new_mireds);\n    return {payload: new_mireds}\n    }\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":160,"wires":[["e80bd956.f2e098"]]},{"id":"2bd57efd76cfcedf","type":"function","z":"7ea0620.a243ca","name":"global brightness %","func":"var new_brightness_pct = msg.payload;\nif (new_brightness_pct != global.get('nr_circadian_brightness_pct')) {\n    global.set('nr_circadian_brightness_pct',new_brightness_pct);\n    return {payload: new_brightness_pct}\n    }","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":730,"y":100,"wires":[["5cd0d7153aaad741"]]},{"id":"11dfa753a1a2cd05","type":"ha-entity","z":"7ea0620.a243ca","name":"Light 1 Color Control","server":"c879ac61.99cd1","version":2,"debugenabled":false,"outputs":1,"entityType":"binary_sensor","config":[{"property":"name","value":"Office Light 1 Color Control"},{"property":"device_class","value":"opening"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload = \"open\" ? \"on\" : \"off\"\t","stateType":"jsonata","attributes":[{"property":"command","value":"payload","valueType":"msg"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":1020,"y":420,"wires":[[]]},{"id":"94a02ed09a89725d","type":"ha-entity","z":"7ea0620.a243ca","name":"Light 1 Brightness Control","server":"c879ac61.99cd1","version":2,"debugenabled":false,"outputs":1,"entityType":"binary_sensor","config":[{"property":"name","value":"Office Light 1 Brightness Control"},{"property":"device_class","value":"opening"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload = \"open\" ? \"on\" : \"off\"\t","stateType":"jsonata","attributes":[{"property":"command","value":"payload","valueType":"msg"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":1030,"y":540,"wires":[[]]},{"id":"ee6e3c5a8750dbd3","type":"join","z":"7ea0620.a243ca","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"2","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":970,"y":480,"wires":[["ae9faf4f99f5df6b"]]},{"id":"53a933a438c1326b","type":"change","z":"7ea0620.a243ca","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"color_temp","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1070,"y":160,"wires":[["934e0600896a7adb"]]},{"id":"ba7018086eca537b","type":"change","z":"7ea0620.a243ca","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"brightness","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1070,"y":40,"wires":[["b1ee887a5f59e581"]]},{"id":"f6a0b62593cc81c1","type":"interval-multiples-timer","z":"7ea0620.a243ca","interval":"60000","payload":"","topic":"","name":"Every minute","x":86,"y":120,"wires":[["f7382c3f63fec06f"]],"outputLabels":["timestamp"]},{"id":"b49e0a964008e7ba","type":"inject","z":"7ea0620.a243ca","name":"At startup","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":"5","topic":"","payload":"","payloadType":"date","x":96,"y":160,"wires":[["f7382c3f63fec06f"]]},{"id":"2758054b03a4112e","type":"subflow:03454e69d1fb38da","z":"7ea0620.a243ca","name":"Office Light 1 Control","x":400,"y":496,"wires":[["b05b2fb283191597"],["a92499638df3f0ae"],[]]},{"id":"9843d42f60a63e30","type":"trigger","z":"7ea0620.a243ca","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"2","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":220,"y":496,"wires":[["2758054b03a4112e"]]},{"id":"43c2e1ab9d4c3a33","type":"link in","z":"7ea0620.a243ca","name":"Office Light 1 Color Temp q-gate","links":["934e0600896a7adb","14e63aec201e0a1d"],"x":675,"y":460,"wires":[["9dfb80638a8e3d73"]]},{"id":"3055458e27486427","type":"link in","z":"7ea0620.a243ca","name":"Office Light 1 Brightness q-gate","links":["b1ee887a5f59e581","a05475178f653842"],"x":675,"y":520,"wires":[["1c0b19f6eaee2844"]]},{"id":"b05b2fb283191597","type":"link out","z":"7ea0620.a243ca","name":"Office Light 1 Color Control","mode":"link","links":["2a3d1ccf39330b28"],"x":575,"y":460,"wires":[]},{"id":"a92499638df3f0ae","type":"link out","z":"7ea0620.a243ca","name":"Office Light 1 Brightness Control","mode":"link","links":["4f8d44ef76bac586"],"x":575,"y":522,"wires":[]},{"id":"ae9faf4f99f5df6b","type":"api-current-state","z":"7ea0620.a243ca","name":"On?","server":"c879ac61.99cd1","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.office_light_1","state_type":"str","blockInputOverrides":false,"outputProperties":[],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":1110,"y":480,"wires":[["60a4dca9.c29d14"],["d96a7d6cd4f42087"]]},{"id":"f7382c3f63fec06f","type":"api-current-state","z":"7ea0620.a243ca","name":"Circadian On?","server":"c879ac61.99cd1","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_boolean.circadian_lighting","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"date"}],"for":0,"forType":"num","forUnits":"minutes","x":280,"y":136,"wires":[["365094a5.72bb5c"],[]]},{"id":"d96a7d6cd4f42087","type":"change","z":"7ea0620.a243ca","name":"close gate","rules":[{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"queue","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1270,"y":520,"wires":[["9c85a0510c0826d0"]]},{"id":"9c85a0510c0826d0","type":"link out","z":"7ea0620.a243ca","name":"Office Light 1 is off","mode":"link","links":["2a3d1ccf39330b28","4f8d44ef76bac586"],"x":1405,"y":520,"wires":[]},{"id":"e0f5e85efbbf544d","type":"ha-device","z":"7ea0620.a243ca","name":"Light 1 Brightness","server":"c879ac61.99cd1","version":0,"debugenabled":false,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"inputs":0,"deviceType":"trigger","device":"f0dde411d2a72e45f369f769d636826d","event":{"platform":"device","type":"turned_on","device_id":"f0dde411d2a72e45f369f769d636826d","entity_id":"light.office_light_1","domain":"light"},"capabilities":[{"name":"for","type":"positive_time_period_dict","value":1,"unit":"seconds"}],"outputProperties":[{"property":"topic","propertyType":"msg","value":"brightness","valueType":"str"},{"property":"payload","propertyType":"msg","value":"nr_circadian_brightness","valueType":"global"}],"x":410,"y":580,"wires":[["a05475178f653842"]]},{"id":"6a9242f61c13b1ba","type":"ha-device","z":"7ea0620.a243ca","name":"Light 1 Color Temp","server":"c879ac61.99cd1","version":0,"debugenabled":false,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"inputs":0,"deviceType":"trigger","device":"f0dde411d2a72e45f369f769d636826d","event":{"platform":"device","type":"turned_on","device_id":"f0dde411d2a72e45f369f769d636826d","entity_id":"light.office_light_1","domain":"light"},"capabilities":[{"name":"for","type":"positive_time_period_dict","value":1,"unit":"seconds"}],"outputProperties":[{"property":"topic","propertyType":"msg","value":"color_temp","valueType":"str"},{"property":"payload","propertyType":"msg","value":"nr_circadian_mireds","valueType":"global"}],"x":410,"y":400,"wires":[["14e63aec201e0a1d"]]},{"id":"afacf51113b59757","type":"rbe","z":"7ea0620.a243ca","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":790,"y":400,"wires":[["9dfb80638a8e3d73","11dfa753a1a2cd05"]]},{"id":"6b84a041a8d0dacb","type":"rbe","z":"7ea0620.a243ca","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":790,"y":580,"wires":[["1c0b19f6eaee2844","94a02ed09a89725d"]]},{"id":"14e63aec201e0a1d","type":"link out","z":"7ea0620.a243ca","name":"Office Light 1 Color Temp Payload","mode":"link","links":["43c2e1ab9d4c3a33"],"x":575,"y":400,"wires":[]},{"id":"a05475178f653842","type":"link out","z":"7ea0620.a243ca","name":"Office Light 1 Brightness Payload","mode":"link","links":["3055458e27486427"],"x":575,"y":580,"wires":[]},{"id":"c879ac61.99cd1","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30"}]
2 Likes

I’ve been reading with interest this thread and I found it amazing. That’s a great job on using the correct tools for doing the job right. Spline curves and day subdivision.

I’m also a lover for node-red, but In this case, shouldn’t be better to bring up the rest of the logic, from the OPs code ahead to the apply on the lights, in Home Assistant?

Having the brightness and temperature in HA it should be only a matter of picking it for selected lights. There’s a Circadian implementation using an automation (blueprint). It should only be a matter of replacing it’s internal calculation with the correct entities. Isn’t it?

I guess it should be possible to integrate all the logic as an integration, but I don’t know nothing about python.

@maxym Would you mind to share your “day simulator” flow?
I still haven’t touched the curves, but in my node-red the curves have spikes and steps.
imagen

here is my whole subflow:

[{"id":"26c6c4d5.3a01e4","type":"subflow","name":"Sub Circadian NR","info":"","category":"","in":[{"x":60,"y":160,"wires":[{"id":"7d4ca80d.e23468"}]}],"out":[{"x":1120,"y":140,"wires":[{"id":"e64dbfa.021204","port":0}]},{"x":1120,"y":340,"wires":[{"id":"d9f0035a.28956","port":0}]},{"x":500,"y":500,"wires":[{"id":"667d6dd.334db94","port":0}]}],"env":[],"color":"#DDAA99","inputLabels":["any message"],"outputLabels":["Brightness","Warmth","Debug"],"icon":"node-red-contrib-sun-position/sun-white.png"},{"id":"dc97f68a.35ea2","type":"switch","z":"26c6c4d5.3a01e4","name":"curve switch","property":"payload.times_of_day.active.name","propertyType":"msg","rules":[{"t":"eq","v":"night","vt":"str"},{"t":"eq","v":"morning","vt":"str"},{"t":"eq","v":"day","vt":"str"},{"t":"eq","v":"evening","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":510,"y":240,"wires":[["ce91189c.8bc7d8","bd8db343.5462f8"],["ad9966f3.3fced","4db18fbe.9ffb6"],["c2d43d53.d7ddf8","838233f4.7ffed"],["eef1dc11.39342","2e9ef8a9.d7fd88"]]},{"id":"ce91189c.8bc7d8","type":"spline-curve","z":"26c6c4d5.3a01e4","name":"night curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.3},{"x":0.333,"y":0.037},{"x":0.477,"y":0},{"x":0.783,"y":0},{"x":1,"y":0}],"x":750,"y":80,"wires":[["e64dbfa.021204"]]},{"id":"ad9966f3.3fced","type":"spline-curve","z":"26c6c4d5.3a01e4","name":"morning curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.01},{"x":0.132,"y":0.01},{"x":0.269,"y":0.027},{"x":0.449,"y":0.124},{"x":0.676,"y":0.334},{"x":1,"y":0.75}],"x":760,"y":120,"wires":[["e64dbfa.021204"]]},{"id":"eef1dc11.39342","type":"spline-curve","z":"26c6c4d5.3a01e4","name":"evening curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.85},{"x":0.036,"y":0.78},{"x":0.176,"y":0.63},{"x":1,"y":0.3}],"x":760,"y":200,"wires":[["e64dbfa.021204"]]},{"id":"c2d43d53.d7ddf8","type":"spline-curve","z":"26c6c4d5.3a01e4","name":"daylight curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.744},{"x":0.03,"y":0.838},{"x":0.097,"y":0.941},{"x":0.183,"y":1},{"x":0.86,"y":0.995},{"x":0.943,"y":0.931},{"x":1,"y":0.85}],"x":760,"y":160,"wires":[["e64dbfa.021204"]]},{"id":"bd8db343.5462f8","type":"spline-curve","z":"26c6c4d5.3a01e4","name":"night curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.5},{"x":0.067,"y":0.368},{"x":0.133,"y":0.265},{"x":0.27,"y":0.171},{"x":0.333,"y":0.16},{"x":0.506,"y":0.124},{"x":0.606,"y":0}],"x":750,"y":280,"wires":[["d9f0035a.28956"]]},{"id":"4db18fbe.9ffb6","type":"spline-curve","z":"26c6c4d5.3a01e4","name":"morning curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0},{"x":0.42,"y":0.018},{"x":0.54,"y":0.051},{"x":0.587,"y":0.101},{"x":0.817,"y":0.675},{"x":1,"y":0.85}],"x":760,"y":320,"wires":[["d9f0035a.28956"]]},{"id":"2e9ef8a9.d7fd88","type":"spline-curve","z":"26c6c4d5.3a01e4","name":"evening curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.85},{"x":0.787,"y":0.598},{"x":1,"y":0.5}],"x":760,"y":400,"wires":[["d9f0035a.28956"]]},{"id":"838233f4.7ffed","type":"spline-curve","z":"26c6c4d5.3a01e4","name":"daylight curve","output_key":"","input_key":"payload.times_of_day.active.perc","points":[{"x":0,"y":0.85},{"x":0.123,"y":0.927},{"x":0.5,"y":1},{"x":0.92,"y":0.904},{"x":1,"y":0.85}],"x":760,"y":360,"wires":[["d9f0035a.28956"]]},{"id":"667d6dd.334db94","type":"function","z":"26c6c4d5.3a01e4","name":"Times of Day","func":"var newmsg = JSON.parse(JSON.stringify(msg));\nnewmsg.payload.times_of_day = {};\n\nprocessTimeOfDay = function(nme, obj, name_start, name_end) {\n    var ret = {};\n    ret.name  = nme;\n    ret.start = obj.times[name_start].ts;\n    ret.end   = obj.times[name_end].ts;\n    ret.perc  = (obj.ts - ret.start) / (ret.end - ret.start);\n    \n    \n    // to cover range starting before midnight and ending after it.\n    if (ret.start > ret.end)\n    {\n        var a;\n\n        // before midnight\n        if (ret.start < obj.ts) \n        {\n            a = new Date(ret.end);\n            a.setUTCDate(a.getUTCDate() + 1);\n            ret.end = a.getTime();\n             \n             \n        }\n        else if (ret.end > obj.ts) \n        {\n            a = new Date(ret.start);\n            a.setUTCDate(a.getUTCDate() - 1);\n            ret.start = a.getTime();\n\n        }\n\n    }\n    \n    ret.perc  = (obj.ts - ret.start) / (ret.end - ret.start);\n    \n    obj.times_of_day[nme] = ret;\n    if (ret.start <= obj.ts && obj.ts < ret.end ) obj.times_of_day.active = ret;\n}\n\n\nprocessTimeOfDay('night', newmsg.payload, \"astronomicalDusk\", \"civilDawn\");\nprocessTimeOfDay('morning', newmsg.payload, \"civilDawn\", \"goldenHourDawnEnd\");\nprocessTimeOfDay('day', newmsg.payload, \"goldenHourDawnEnd\", \"sunsetStart\");\nprocessTimeOfDay('evening', newmsg.payload, \"sunsetStart\", \"astronomicalDusk\");\n\n\nreturn newmsg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":290,"y":240,"wires":[["dc97f68a.35ea2"]]},{"id":"e64dbfa.021204","type":"range","z":"26c6c4d5.3a01e4","minin":"0","maxin":"1","minout":"1","maxout":"100","action":"scale","round":true,"property":"payload","name":"","x":980,"y":140,"wires":[[]]},{"id":"d9f0035a.28956","type":"range","z":"26c6c4d5.3a01e4","minin":"0","maxin":"1","minout":"2000","maxout":"5500","action":"scale","round":true,"property":"payload","name":"","x":990,"y":340,"wires":[[]]},{"id":"7d4ca80d.e23468","type":"sun-position","z":"26c6c4d5.3a01e4","name":"","positionConfig":"26f98e1b.bd1812","rules":[],"onlyOnChange":"true","topic":"","outputs":1,"start":"","startType":"none","startOffset":0,"startOffsetType":"num","startOffsetMultiplier":60000,"end":"","endType":"none","endOffset":0,"endOffsetType":"num","endOffsetMultiplier":60000,"x":190,"y":160,"wires":[["667d6dd.334db94"]]},{"id":"26f98e1b.bd1812","type":"position-config","name":"","isValide":"true","longitude":"0","latitude":"0","angleType":"deg","timeZoneOffset":99,"timeZoneDST":0,"stateTimeFormat":"3","stateDateFormat":"12"},{"id":"7c36779e.82d778","type":"subflow:26c6c4d5.3a01e4","z":"98a83ef1.1c6e2","name":"","env":[],"x":350,"y":240,"wires":[["f5dfa977.44ef5"],["2dee4f11.beb6e"],["73ce5946.a872a"]],"outputLabels":["Bigthness [%]","Warmth [K]","Debug"],"icon":"node-red-node-suncalc/sun.png"}]
2 Likes

Thanks maxym. But this is the subflow node. I asked for the curve testing flow that you mentioned on your first post, if it’s still possible.

I’m on holidays. Will be back on wed evening. If I still have the generator I will publish the code.

Well follwowing this topic I decided to give it a go and while I only spent a day on this so far I think some might like some of my additions to name a few:

  • Reorganized message with extra info
  • Combined output to a single message
  • Added offset to time period
  • Master & Indivual room control
  • Auto deactivate when a user changes brightness or temperature
  • Auto reactivate when the light off
  • Removed Day/Night curve in favor of extended dawn/dusk period

[{"id":"09e8f85672b69e6e","type":"group","z":"76d268b99378117f","name":"Flux","style":{"label":true},"nodes":["7c36779e.82d778","c17fcdbf5c1601ca","96c46a37c4c1d229","b2de534c9548128e","1f338bb84eea05a6","87212c336e7a404e","e03358fbb0d58292","221d625268d9d855","4ad115d03bf1bcc7","5075f9dec136e667","d48847fecb73ea12","9ea0ef28bf1e7c2d","28b68907a6c1c3bd","9a8c4b9aa821571a","ad73ab52dfc9464c","fc891a29a6a02cf6","65e26660fdb518f7","68dc351f6676efdf","f50f5dda01a96a73"],"x":74,"y":439,"w":1012,"h":242},{"id":"b62e4b55.b6e888","type":"subflow","name":"Sub User","info":"","category":"","in":[{"x":280,"y":140,"wires":[{"id":"3e282b34a50bc378"}]}],"out":[{"x":1080,"y":140,"wires":[{"id":"86c52c65d7f47c12","port":0}]}],"env":[],"meta":{},"color":"#DDAA99","icon":"font-awesome/fa-user-circle-o"},{"id":"71513b70.d07824","type":"file in","z":"b62e4b55.b6e888","name":"auth","filename":"/config/.storage/auth","filenameType":"str","format":"utf8","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":550,"y":140,"wires":[["833cfa89.049518"]]},{"id":"833cfa89.049518","type":"json","z":"b62e4b55.b6e888","name":"","property":"payload","action":"obj","pretty":false,"x":670,"y":140,"wires":[["2bd011b4.4f829e"]]},{"id":"2bd011b4.4f829e","type":"function","z":"b62e4b55.b6e888","name":"","func":"var user_id = msg.data.context.user_id;\n//var user_id = data.event.new_state.context.user_id;\nvar user_name = \"\";\nvar user_system = null;\n\nfor (var i = 0; i < msg.payload.data.users.length; i++) {\n    \n    var user = msg.payload.data.users[i];\n    \n    console.log(user.id);\n    \n    if (user.id == user_id) {\n        user_name = user.name;\n        user_system = user.system_generated;\n        break;\n    }\n}\nmsg.data.context.user_name = user_name == \"Hass.io\" ? \"Home Assistant\" : user_name;\nmsg.data.context.user_system = user_system;\n\ndelete msg.filename; \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":800,"y":140,"wires":[["86c52c65d7f47c12"]]},{"id":"3e282b34a50bc378","type":"change","z":"b62e4b55.b6e888","name":"Move","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload","tot":"flow","dc":true}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":140,"wires":[["71513b70.d07824"]]},{"id":"86c52c65d7f47c12","type":"change","z":"b62e4b55.b6e888","name":"Move","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload","tot":"flow","dc":true}],"action":"","property":"","from":"","to":"","reg":false,"x":970,"y":140,"wires":[[]]},{"id":"26c6c4d5.3a01e4","type":"subflow","name":"Sub Flux","info":"","category":"","in":[{"x":40,"y":60,"wires":[{"id":"7d4ca80d.e23468"}]}],"out":[{"x":1440,"y":60,"wires":[{"id":"7af30c5b46b6cb83","port":0}]}],"env":[],"meta":{},"color":"#DDAA99","inputLabels":["any message"],"outputLabels":["Info","Brightness","Temperature"],"icon":"node-red-contrib-sun-position/sun-white.png"},{"id":"dc97f68a.35ea2","type":"switch","z":"26c6c4d5.3a01e4","name":"Times","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"night","vt":"str"},{"t":"eq","v":"dusk","vt":"str"},{"t":"eq","v":"dawn","vt":"str"},{"t":"eq","v":"day","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":470,"y":200,"wires":[["57eb916940622fad"],["eef1dc11.39342","2e9ef8a9.d7fd88"],["ad9966f3.3fced","4db18fbe.9ffb6"],["87ef978514c3b115"]]},{"id":"ad9966f3.3fced","type":"spline-curve","z":"26c6c4d5.3a01e4","name":"dawn brightness","output_key":"payload","input_key":"payload","points":[{"x":0,"y":0},{"x":0.071,"y":0.02},{"x":0.254,"y":0.297},{"x":0.654,"y":0.877},{"x":1,"y":1}],"x":700,"y":220,"wires":[["e64dbfa.021204"]]},{"id":"eef1dc11.39342","type":"spline-curve","z":"26c6c4d5.3a01e4","name":"dusk brightness","output_key":"payload","input_key":"payload","points":[{"x":0,"y":1},{"x":0.25,"y":0.8},{"x":0.75,"y":0.1},{"x":1,"y":0}],"x":700,"y":140,"wires":[["e64dbfa.021204"]]},{"id":"4db18fbe.9ffb6","type":"spline-curve","z":"26c6c4d5.3a01e4","name":"dawn temperature","output_key":"payload","input_key":"payload","points":[{"x":0,"y":0},{"x":0.1,"y":0.01},{"x":0.25,"y":0.05},{"x":0.4,"y":0.2},{"x":0.573,"y":0.6},{"x":1,"y":1}],"x":710,"y":260,"wires":[["d9f0035a.28956"]]},{"id":"2e9ef8a9.d7fd88","type":"spline-curve","z":"26c6c4d5.3a01e4","name":"dusk temperature","output_key":"payload","input_key":"payload","points":[{"x":0,"y":1},{"x":0.019,"y":0.918},{"x":0.162,"y":0.541},{"x":0.536,"y":0.121},{"x":1,"y":0}],"x":710,"y":180,"wires":[["d9f0035a.28956"]]},{"id":"667d6dd.334db94","type":"function","z":"26c6c4d5.3a01e4","name":"Times of Day","func":"var newmsg = { topic: \"flux\", current: {}, times: {} };\nnewmsg.sunpos = JSON.parse(JSON.stringify(msg)).payload;\n\n//https://github.com/rdmtc/node-red-contrib-sun-position/wiki/Base-Functions#sun-times\nevalPeriod('night', newmsg, \"astronomicalDusk\",   \"amateurDawn\",3600000);\nevalPeriod('dawn',  newmsg, \"amateurDawn\",        \"goldenHourDawnEnd\");\nevalPeriod('day', newmsg, \"goldenHourDawnEnd\", \"sunsetStart\", 0, -1800000);\nevalPeriod('dusk', newmsg, \"sunsetStart\", \"astronomicalDusk\", -1800000,3600000);\n\nreturn [newmsg,\n    { \n        topic:      newmsg.current.name,\n        payload:    newmsg.current.perc\n    }\n    ];\n\nfunction Epoch2Human(epoch) {\n    var myDate = new Date(epoch);\n    return myDate.toLocaleString(\"fr-FR\");\n}\nfunction Epoch2iso(epoch) {\n    var myDate = new Date(epoch);\n    return myDate.toISOString();\n}\nfunction Epoch2Duration(epoch) {\n    var sec_num = Math.floor(epoch / 1000)\n    var hours = Math.floor(sec_num / 3600);\n    var minutes = Math.floor((sec_num - (hours * 3600)) / 60);\n    var seconds = sec_num - (hours * 3600) - (minutes * 60);\n\n    if (hours < 10) { hours = \"0\" + hours; }\n    if (minutes < 10) { minutes = \"0\" + minutes; }\n    if (seconds < 10) { seconds = \"0\" + seconds; }\n    return hours + ':' + minutes + ':' + seconds;\n}\nfunction evalPeriod(nme, obj, name_start, name_end, offset_start=0, offset_end=0) {\n    var ret         = { \n        name: nme, \n        hm_delta: null, \n        hm_start: null, \n        hm_end: null,\n        ts_start: null,\n        ts_end: null\n        };\n    ret.start       = obj.sunpos.times[name_start].ts + offset_start;\n    ret.end         = obj.sunpos.times[name_end].ts + offset_end;\n\n    // to cover range starting before midnight and ending after it.\n    if (ret.start > ret.end) {\n        var a;\n\n        // before midnight\n        if (ret.start < obj.sunpos.ts) {\n            a = new Date(ret.end);\n            a.setUTCDate(a.getUTCDate() + 1);\n            ret.end = a.getTime();\n\n\n        }\n        else if (ret.end > obj.sunpos.ts) {\n            a = new Date(ret.start);\n            a.setUTCDate(a.getUTCDate() - 1);\n            ret.start = a.getTime();\n\n        }\n    }\n\n    ret.delta       = ret.end - ret.start;\n    ret.hm_delta    = Epoch2Duration(ret.delta);\n    ret.hm_start    = Epoch2Human(ret.start);\n    ret.hm_end      = Epoch2Human(ret.end);\n    ret.ts_start    = Epoch2iso(ret.start);\n    ret.ts_end      = Epoch2iso(ret.end);\n\n    obj.times[nme]  = ret;\n    if (ret.start <= obj.sunpos.ts && obj.sunpos.ts < ret.end) {\n        obj.current = ret;\n        obj.current.perc = (obj.sunpos.ts - ret.start) / ret.delta;\n    }\n}","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":60,"wires":[["7af30c5b46b6cb83"],["dc97f68a.35ea2"]]},{"id":"e64dbfa.021204","type":"range","z":"26c6c4d5.3a01e4","minin":"0","maxin":"1","minout":"15","maxout":"100","action":"clamp","round":true,"property":"payload","name":"","x":960,"y":100,"wires":[["97d34f99252ec79b"]]},{"id":"d9f0035a.28956","type":"range","z":"26c6c4d5.3a01e4","minin":"0","maxin":"1","minout":"2700","maxout":"6500","action":"clamp","round":true,"property":"payload","name":"","x":970,"y":300,"wires":[["794d74c2affe32dd"]]},{"id":"7d4ca80d.e23468","type":"sun-position","z":"26c6c4d5.3a01e4","name":"","positionConfig":"72018d9177e1240e","rules":[],"onlyOnChange":"true","topic":"","outputs":1,"start":"","startType":"none","startOffset":0,"startOffsetType":"num","startOffsetMultiplier":60000,"end":"","endType":"none","endOffset":0,"endOffsetType":"num","endOffsetMultiplier":60000,"x":150,"y":60,"wires":[["667d6dd.334db94"]]},{"id":"87ef978514c3b115","type":"change","z":"26c6c4d5.3a01e4","name":"Day","rules":[{"t":"set","p":"payload","pt":"msg","to":"1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":300,"wires":[["8b303ac065606ebf"]]},{"id":"57eb916940622fad","type":"change","z":"26c6c4d5.3a01e4","name":"Night","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":100,"wires":[["211969df4553409a"]]},{"id":"794d74c2affe32dd","type":"change","z":"26c6c4d5.3a01e4","name":"Move","rules":[{"t":"delete","p":"topic","pt":"msg"},{"t":"move","p":"payload","pt":"msg","to":"current.temperature","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1130,"y":300,"wires":[["7af30c5b46b6cb83"]]},{"id":"97d34f99252ec79b","type":"change","z":"26c6c4d5.3a01e4","name":"Move","rules":[{"t":"delete","p":"topic","pt":"msg"},{"t":"move","p":"payload","pt":"msg","to":"current.brightness","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1130,"y":100,"wires":[["7af30c5b46b6cb83"]]},{"id":"7af30c5b46b6cb83","type":"join","z":"26c6c4d5.3a01e4","name":"","mode":"custom","build":"merged","property":"current","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"1","count":"12","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":1310,"y":60,"wires":[[]]},{"id":"72018d9177e1240e","type":"position-config","name":"","isValide":"true","longitude":"0","latitude":"0","angleType":"deg","timeZoneOffset":99,"timeZoneDST":0,"stateTimeFormat":"3","stateDateFormat":"12","contextStore":""},{"id":"7c36779e.82d778","type":"subflow:26c6c4d5.3a01e4","z":"76d268b99378117f","g":"09e8f85672b69e6e","name":"Flux","env":[],"x":330,"y":480,"wires":[["e03358fbb0d58292","1f338bb84eea05a6"]],"icon":"node-red-node-suncalc/sun.png"},{"id":"c17fcdbf5c1601ca","type":"inject","z":"76d268b99378117f","g":"09e8f85672b69e6e","name":"","props":[],"repeat":"300","crontab":"","once":true,"onceDelay":0.1,"topic":"","x":170,"y":480,"wires":[["7c36779e.82d778"]]},{"id":"96c46a37c4c1d229","type":"ha-entity","z":"76d268b99378117f","g":"09e8f85672b69e6e","name":"Brightness","server":"8093ca4.46eb138","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"Flux Brightness"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:brightness-percent"},{"property":"unit_of_measurement","value":" %"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"current.brightness","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"","outputPayloadType":"str","x":650,"y":560,"wires":[["b2de534c9548128e"]]},{"id":"b2de534c9548128e","type":"ha-entity","z":"76d268b99378117f","g":"09e8f85672b69e6e","name":"Temperature","server":"8093ca4.46eb138","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"Flux Kelvin"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:temperature-kelvin"},{"property":"unit_of_measurement","value":" K"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"current.temperature","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"","outputPayloadType":"str","x":830,"y":560,"wires":[[]]},{"id":"1f338bb84eea05a6","type":"ha-entity","z":"76d268b99378117f","g":"09e8f85672b69e6e","name":"Period","server":"8093ca4.46eb138","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"Flux Period"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:chart-timeline"},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"current.name","stateType":"msg","attributes":[{"property":"start","value":"current.ts_start","valueType":"msg"},{"property":"end","value":"current.ts_end","valueType":"msg"},{"property":"duration","value":"current.hm_delta","valueType":"msg"},{"property":"perc","value":"current.perc","valueType":"msg"}],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"","outputPayloadType":"str","x":490,"y":560,"wires":[["96c46a37c4c1d229"]]},{"id":"87212c336e7a404e","type":"api-call-service","z":"76d268b99378117f","g":"09e8f85672b69e6e","name":"Light","server":"8093ca4.46eb138","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["{{payload.entity_id}}"],"data":"{\t   \"brightness_pct\": current.brightness,\t   \"kelvin\": current.temperature,\t   \"transition\": payload.timeSinceChangedMs>30000?15:1\t}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1010,"y":480,"wires":[[]]},{"id":"e03358fbb0d58292","type":"api-current-state","z":"76d268b99378117f","g":"09e8f85672b69e6e","name":"Master","server":"8093ca4.46eb138","version":3,"outputs":2,"halt_if":"true","halt_if_type":"bool","halt_if_compare":"is","entity_id":"input_boolean.flux_master","state_type":"habool","blockInputOverrides":true,"outputProperties":[],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":490,"y":480,"wires":[["4ad115d03bf1bcc7"],[]]},{"id":"221d625268d9d855","type":"server-state-changed","z":"76d268b99378117f","g":"09e8f85672b69e6e","name":"Lights","server":"8093ca4.46eb138","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":["light.bedroom","light.closet","light.guest","light.hallway","light.office"],"entityidfiltertype":"list","outputinitially":false,"state_type":"habool","haltifstate":"true","halt_if_type":"bool","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":170,"y":560,"wires":[["7c36779e.82d778"],["ad73ab52dfc9464c"]]},{"id":"4ad115d03bf1bcc7","type":"ha-get-entities","z":"76d268b99378117f","g":"09e8f85672b69e6e","name":"Auto","server":"8093ca4.46eb138","version":0,"rules":[{"property":"entity_id","logic":"includes","value":"light.office,light.bedroom,light.guest,light.hallway,light.closet","valueType":"str"},{"property":"state","logic":"is","value":"on","valueType":"str"}],"output_type":"split","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":630,"y":480,"wires":[["65e26660fdb518f7"]]},{"id":"5075f9dec136e667","type":"trigger-state","z":"76d268b99378117f","g":"09e8f85672b69e6e","name":"Evt","server":"8093ca4.46eb138","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":["light.bedroom","light.closet","light.guest","light.hallway","light.office"],"entityidfiltertype":"list","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"},{"targetType":"this_entity","targetValue":"","propertyType":"previous_state","propertyValue":"old_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"inputs":0,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","enableInput":false,"x":170,"y":640,"wires":[["f50f5dda01a96a73"],[]]},{"id":"d48847fecb73ea12","type":"subflow:b62e4b55.b6e888","z":"76d268b99378117f","g":"09e8f85672b69e6e","name":"Who","env":[],"x":490,"y":640,"wires":[["9ea0ef28bf1e7c2d"]]},{"id":"9ea0ef28bf1e7c2d","type":"switch","z":"76d268b99378117f","g":"09e8f85672b69e6e","name":"User","property":"data.context.user_system","propertyType":"msg","rules":[{"t":"false"},{"t":"null"}],"checkall":"false","repair":false,"outputs":2,"x":630,"y":640,"wires":[["28b68907a6c1c3bd"],["28b68907a6c1c3bd"]]},{"id":"28b68907a6c1c3bd","type":"change","z":"76d268b99378117f","g":"09e8f85672b69e6e","name":"Lkp","rules":[{"t":"change","p":"topic","pt":"msg","from":"light.","fromt":"str","to":"input_boolean.flux_","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":810,"y":640,"wires":[["9a8c4b9aa821571a"]]},{"id":"9a8c4b9aa821571a","type":"api-call-service","z":"76d268b99378117f","g":"09e8f85672b69e6e","name":"Status","server":"8093ca4.46eb138","version":5,"debugenabled":false,"domain":"input_boolean","service":"turn_off","areaId":[],"deviceId":[],"entityId":["{{topic}}"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1010,"y":640,"wires":[[]]},{"id":"ad73ab52dfc9464c","type":"change","z":"76d268b99378117f","g":"09e8f85672b69e6e","name":"Reset","rules":[{"t":"set","p":"payload.service","pt":"msg","to":"turn_on","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":580,"wires":[["28b68907a6c1c3bd"]]},{"id":"fc891a29a6a02cf6","type":"api-current-state","z":"76d268b99378117f","g":"09e8f85672b69e6e","name":"Enabled","server":"8093ca4.46eb138","version":3,"outputs":2,"halt_if":"true","halt_if_type":"bool","halt_if_compare":"is","entity_id":"{{id_flux}}","state_type":"habool","blockInputOverrides":true,"outputProperties":[],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":880,"y":480,"wires":[["87212c336e7a404e"],[]]},{"id":"65e26660fdb518f7","type":"change","z":"76d268b99378117f","g":"09e8f85672b69e6e","name":"Lkp","rules":[{"t":"set","p":"id_flux","pt":"msg","to":"payload.entity_id","tot":"msg"},{"t":"change","p":"id_flux","pt":"msg","from":"light.","fromt":"re","to":"input_boolean.flux_","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":750,"y":480,"wires":[["fc891a29a6a02cf6"]]},{"id":"68dc351f6676efdf","type":"server-state-changed","z":"76d268b99378117f","g":"09e8f85672b69e6e","name":"Enabled","server":"8093ca4.46eb138","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.flux_","entityidfiltertype":"substring","outputinitially":false,"state_type":"habool","haltifstate":"true","halt_if_type":"bool","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"},{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"x":180,"y":520,"wires":[["7c36779e.82d778"],[]]},{"id":"f50f5dda01a96a73","type":"switch","z":"76d268b99378117f","g":"09e8f85672b69e6e","name":"Changed","property":"data.event","propertyType":"msg","rules":[{"t":"jsonata_exp","v":"data.event.old_state.attributes.brightness != data.event.new_state.attributes.brightness","vt":"jsonata"},{"t":"jsonata_exp","v":"data.event.old_state.attributes.color_temp != data.event.new_state.attributes.color_temp","vt":"jsonata"}],"checkall":"false","repair":false,"outputs":2,"x":340,"y":640,"wires":[["d48847fecb73ea12"],["d48847fecb73ea12"]],"outputLabels":["Brightness","Temperature"]},{"id":"8093ca4.46eb138","type":"server","name":"Home Assistant","version":4,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"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"}]

The following nodes could also be added to update only once when lights turn on :

[{"id":"ad9d46948c536056","type":"switch","z":"76d268b99378117f","name":"Init","property":"msg.payload.timeSinceChangedMs","propertyType":"msg","rules":[{"t":"lt","v":"10000","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1030,"y":820,"wires":[[],[]]},{"id":"ea4ca17a545569ee","type":"ha-get-entities","z":"76d268b99378117f","name":"Manual","server":"8093ca4.46eb138","version":0,"rules":[{"property":"entity_id","logic":"includes","value":"light.bedroom,light.guest,light.hallway,light.closet","valueType":"str"},{"property":"state","logic":"is","value":"on","valueType":"str"}],"output_type":"split","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":880,"y":820,"wires":[["ad9d46948c536056"]]},{"id":"8093ca4.46eb138","type":"server","name":"Home Assistant","version":4,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"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"}] 

2 Likes

I am trying to set this up based on the latest flow from @TEF2one. I added location and input boolean flux and flux_master. But how is it then working on the lights? I do not get errors except for a ‘no connection’ in the most left status node. If use the inject the brightness and temperature are changed but the lights them self do not change.

edit: when using a couple of debug nodes I see that there is an error:

“Entity could not be found in cache for entity_id: input_boolean.flux_light”

I see that this error occurs more frequent and has to do with connection with HA. I checked Delay connection attempts in the HA server. But still same error. I also tried a delay of 5 seconds between nodes but did not help either
Anyone has an idea?

edit: for anyone else. You have to make input booleans for every light separately. I thought it was a cache entity

Whow - this is awesome.
Thank you @maximm for the initial idea and setup, and @dbrunt for the revised flows, d thank you @TEF2one for the nice addons.

I actually prefer @fishertimj’s idea on using global variables wherever you set the lights, as I really hate it when a light turns on very bright at night, and then dims down after a second. The downside is, that I have to move all light on/off logic to NR, and won’t be able to use Toggle anymore (probably have to add a toggle-subflow of my own.

There are some “bugs” in the Version of TEF2one, that might be of interest:
during day or night, there is no brightness/temperature output defined. This is probably not a problem once the setup is running, as there shouldn’t be a change during day, but if you try to debug it during the night/day you will get some error messages.

I’m currently trying to combine all three versions, and figure out a way where I don’t need to enter all my light id’s manually, and maybe move the input-toggles to “area” based instead of “light” based, as I have way to many lights to add them all.

Would you be OK, if I put this on a node-red-contrib github, so others can import it easily into there NR, once I’m done?

1 Like