Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [{"id":"210626d9905241cc","type":"group","z":"9037e766.edc3f8","name":"Lighting","style":{"label":true},"nodes":["ab30a321b00b8531","f382913234be1df9","b0789420e3ecdfb8","d1f82979da84c480","a09a976f7587e8aa","958dc8f4ff133912","6fc858561eeab6af","d9bbc159b0473e05","7e7f37e135ae1f8c","b91d81aa313f06ff","dc31e37dd45c9db0","5f76c5d7f41b2fc2","31a5f88ec63593a3","48d6c4079c320e66","a043619889bec59d"],"x":34,"y":199,"w":1452,"h":282},{"id":"0d235b6a3d90ffd2","type":"subflow","name":"Override","info":"","category":"","in":[{"x":160,"y":100,"wires":[{"id":"06069b864a382fb3"}]}],"out":[],"env":[],"meta":{},"color":"#3FADB5","icon":"node-red-contrib-home-assistant-websocket/ha-entity-switch.svg"},{"id":"48b611b2d5bdccc9","type":"api-call-service","z":"0d235b6a3d90ffd2","name":"Enable override","server":"5829325a.2a80bc","version":5,"debugenabled":false,"domain":"input_boolean","service":"turn_on","areaId":[],"deviceId":[],"entityId":["input_boolean.automation_override"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":500,"y":100,"wires":[["5ef3cda4b43ed796"]]},{"id":"5ef3cda4b43ed796","type":"delay","z":"0d235b6a3d90ffd2","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"outputs":1,"x":680,"y":100,"wires":[["4a2718f526f681b8"]]},{"id":"4a2718f526f681b8","type":"api-call-service","z":"0d235b6a3d90ffd2","name":"Disable override","server":"5829325a.2a80bc","version":5,"debugenabled":false,"domain":"input_boolean","service":"turn_off","areaId":[],"deviceId":[],"entityId":["input_boolean.automation_override"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":860,"y":100,"wires":[[]]},{"id":"06069b864a382fb3","type":"change","z":"0d235b6a3d90ffd2","name":"Clear payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":100,"wires":[["48b611b2d5bdccc9"]]},{"id":"ab30a321b00b8531","type":"server-state-changed","z":"9037e766.edc3f8","g":"210626d9905241cc","name":"When a light is turned on","server":"5829325a.2a80bc","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"^light\\.*","entityidfiltertype":"regex","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"seconds","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"turn_on","valueType":"str"},{"property":"payload.entity_id","propertyType":"msg","value":"","valueType":"triggerId"}],"x":530,"y":440,"wires":[["31a5f88ec63593a3"],[]]},{"id":"f382913234be1df9","type":"chronos-switch","z":"9037e766.edc3f8","g":"210626d9905241cc","name":"What time of day?","config":"267834d7.270114","baseTime":"","baseTimeType":"msgIngress","conditions":[{"operator":"between","label":"between 07:00 and 07:01","operands":[{"type":"time","value":"07:00","offset":0,"random":false},{"type":"time","value":"07:01","offset":0,"random":false}]},{"operator":"between","label":"between 07:01 and 23:59","operands":[{"type":"time","value":"07:01","offset":0,"random":false},{"type":"time","value":"23:59","offset":0,"random":false}]},{"operator":"between","label":"between 00:00 and 06:59","operands":[{"type":"time","value":"00:00","offset":0,"random":false},{"type":"time","value":"06:59","offset":0,"random":false}]}],"stopOnFirstMatch":true,"outputs":3,"x":930,"y":380,"wires":[["a043619889bec59d"],["7e7f37e135ae1f8c"],["48d6c4079c320e66"]]},{"id":"b0789420e3ecdfb8","type":"debug","z":"9037e766.edc3f8","g":"210626d9905241cc","name":"Light On (array)","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":640,"y":240,"wires":[]},{"id":"d1f82979da84c480","type":"api-call-service","z":"9037e766.edc3f8","g":"210626d9905241cc","name":"Payload","server":"5829325a.2a80bc","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["{{payload.entity_id}}"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1400,"y":300,"wires":[[]]},{"id":"a09a976f7587e8aa","type":"cronplus","z":"9037e766.edc3f8","g":"210626d9905241cc","name":"Every minute between 0700 & 0000","outputField":"payload","timeZone":"","persistDynamic":false,"commandResponseMsgOutput":"output1","outputs":1,"options":[{"name":"Minute","topic":"Minute","payloadType":"date","payload":"","expressionType":"cron","expression":"0 */1 7-23 * * *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"},{"name":"Midnight","topic":"Midnight","payloadType":"date","payload":"","expressionType":"cron","expression":"0 0 0 * * *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"}],"x":230,"y":380,"wires":[["958dc8f4ff133912"]]},{"id":"958dc8f4ff133912","type":"ha-get-entities","z":"9037e766.edc3f8","g":"210626d9905241cc","name":"What lights are on?","server":"5829325a.2a80bc","version":0,"rules":[{"property":"entity_id","logic":"starts_with","value":"light","valueType":"str"},{"property":"state","logic":"is","value":"on","valueType":"str"},{"property":"attributes.supported_features","logic":"gt","value":"1","valueType":"num"},{"property":"entity_id","logic":"is_not","value":"_lights","valueType":"re"}],"output_type":"split","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":550,"y":380,"wires":[["31a5f88ec63593a3"]]},{"id":"6fc858561eeab6af","type":"ha-get-entities","z":"9037e766.edc3f8","g":"210626d9905241cc","name":"What lights are on? (array)","server":"5829325a.2a80bc","version":0,"rules":[{"property":"entity_id","logic":"starts_with","value":"light","valueType":"str"},{"property":"state","logic":"is","value":"on","valueType":"str"},{"property":"attributes.supported_features","logic":"gt","value":"1","valueType":"num"}],"output_type":"array","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":380,"y":240,"wires":[["b0789420e3ecdfb8"]]},{"id":"d9bbc159b0473e05","type":"inject","z":"9037e766.edc3f8","g":"210626d9905241cc","name":"Inject","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":240,"wires":[["6fc858561eeab6af"]]},{"id":"7e7f37e135ae1f8c","type":"function","z":"9037e766.edc3f8","g":"210626d9905241cc","name":"Calculate Circadian sine","func":"\nvar sunrise = \"06:00\"; //Hard-coded sunrise\nvar sunset = \"18:00\"; //Hard-coded sunset\n\nvar color_min = 2700; // Color temp at sunrise and sunset\nvar color_max = 6500; // Color temp at noon\n\nfunction map(x, in_min, in_max, out_min, out_max)\n{\n return Math.round((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min);\n}\n\n//If light is operating in RGB, do not set the color_temp except if the the light\n//has just been turned on.\nif (msg.topic != \"turn_on\") {\n if (msg.payload.attributes.color_mode != \"color_temp\") {\n return;\n }\n}\n\nvar today = new Date();\nvar today_date = today.getFullYear() + '-' + (today.getMonth()+1) + '-' + today.getDate();\nvar today_time = today.getHours() + \":\" + String(today.getMinutes()).padStart(2, '0');\n\nvar unix_sunrise = new Date(today_date + ' ' + sunrise).getTime() / 1000;\nvar unix_sunset = new Date(today_date + ' ' + sunset).getTime() / 1000;\nvar unix_now = new Date().getTime() / 1000;\nvar brightness = \"\"\n\nvar color_temp = color_min;\n\nvar day_progress = (unix_now-unix_sunrise) / (unix_sunset-unix_sunrise);\n\nif (unix_now >= unix_sunrise && unix_now <= unix_sunset)\n{\n var color_sine = Math.sin(((360 * day_progress)-90) * (Math.PI/180));\n \n color_temp = map(color_sine, -1.0, 1.0, color_min, color_max);\n}\n\nday_progress = Math.round(day_progress * 100); //Round to a whole number\nnode.status({text: color_temp + \"K @ \" + today_time + \" (\" + day_progress + \"%)\" });\n\n/*msg.payload = {domain: \"light\", service: \"turn_on\",\n data: {\n entity_id: msg.payload.entity_id,\n kelvin: color_temp, }\n };\n*/\n //msg.payload.domain = \"light\";\n //msg.payload.service = \"turn_on\";\n \n //msg.payload.data = msg.payload.data || {}\n msg.payload.data.kelvin = color_temp\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1170,"y":380,"wires":[["d1f82979da84c480"]]},{"id":"b91d81aa313f06ff","type":"function","z":"9037e766.edc3f8","g":"210626d9905241cc","name":"Ramp up brightness before alarm","func":"var next_alarm_entity = \"sensor.derek_s20_next_alarm\"\nvar person_or_device_entity = \"person.derek\"\n//20-30 minutes for real-world; 600 minutes for testing\nvar how_many_minutes_before_alarm = 30\nvar entity_to_adjust_based_on_alarm = \"light.bedroom1\"\n\n\nmsg.payload = null;\nvar d = global.get('homeassistant').homeAssistant\nvar next_alarm = d.states[next_alarm_entity].state\nvar who = d.states[person_or_device_entity].state\nfunction map(x, in_min, in_max, out_min, out_max)\n{\n return Math.round((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min);\n}\n\nvar entity_current_brightness = d.states[entity_to_adjust_based_on_alarm].attributes.brightness\nvar entity_current_state = d.states[\"light.bedroom1\"].state\nvar time_now_unix = new Date().getTime();\nvar next_alarm_unix = new Date(next_alarm).getTime();\nvar how_long_until_next_alarm_in_minutes = ((next_alarm_unix - time_now_unix) / 60000).toFixed()\n\n// Check if home and within number of minutes before alarm\nif (who === \"Home\" && how_long_until_next_alarm_in_minutes <= how_many_minutes_before_alarm) {\n \n //convert the percentage of minutes until alarm compared to variable how_many_minutes_before_alarm\n //and then set to scale of 0 to 255 for brightness attribute\n var brightness = ((1 - (how_long_until_next_alarm_in_minutes / how_many_minutes_before_alarm)) * 100).toFixed()\n brightness = map(brightness, 0, 100, 0, 255);\n\n\n //Check if current brightness is less than brightness variable or undefined (off)\n if (entity_current_brightness < brightness || entity_current_brightness === undefined) {\n msg.payload = {domain: \"light\", service: \"turn_on\", data: {\n entity_id: entity_to_adjust_based_on_alarm,\n brightness: brightness,\n //kelvin: null }\n }\n };\n msg.payload.entity_id = entity_to_adjust_based_on_alarm;\n \n node.status({text: \"Within range; adjusting brightness to \" + brightness}); \n return msg;\n \n } else {\n \n //The brightness is already brighter than what script is wanting to adjust it to\n node.status({text: \"Within range but not adjusting brightness\"});\n \n }\n \n} else {\n \n //.status({text: \"Not time yet; flow halted\"});\n node.status({text: how_long_until_next_alarm_in_minutes + \" minutes until alarm\"});\n //node.status({text: how_long_until_next_alarm_in_minutes});\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":880,"y":300,"wires":[["5f76c5d7f41b2fc2","d1f82979da84c480"]]},{"id":"dc31e37dd45c9db0","type":"cronplus","z":"9037e766.edc3f8","g":"210626d9905241cc","name":"Every minute","outputField":"payload","timeZone":"","persistDynamic":false,"commandResponseMsgOutput":"output1","outputs":1,"options":[{"name":"Alarm_checker","topic":"Alarm_checker","payloadType":"date","payload":"","expressionType":"cron","expression":"0 */1 * * * *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"}],"x":530,"y":300,"wires":[["b91d81aa313f06ff"]]},{"id":"5f76c5d7f41b2fc2","type":"subflow:0d235b6a3d90ffd2","z":"9037e766.edc3f8","g":"210626d9905241cc","name":"","env":[],"x":1120,"y":260,"wires":[]},{"id":"31a5f88ec63593a3","type":"function","z":"9037e766.edc3f8","g":"210626d9905241cc","name":"","func":"var transition_to_day = 7\nvar transition_to_night = 17\n\nconst t = new Date();\nvar hour = t.getHours();\nvar min = t.getMinutes();\nconst d = global.get('homeassistant').homeAssistant\nvar override = d.states[\"input_boolean.automation_override\"].state\n\nconst messages = new Array(node.outputCount) // Create an array same length as there are outputs\nconst msg0 = {payload:\"\", topic:\"MSG0\"};\nconst msg1 = {payload:\"\", topic:\"MSG1\"};\n\n/* \nA group of lights, such as light.all_lights, will be considered off if \nall lights are off and will be on if one or more lights are on. While \nthe light group is off, if any light within the group is turned on, all \nof the lights within the group will be turned on as a means of this \nfunction detecting the change from off to on. This IF statement catches \nthe entity names that conform to \"light.<name of room/group>_lights\" and \nprevents it from being passed. \n*/\nif( /((^light.[0-9a-z_]+_lights))/gi.test(msg.payload.entity_id) ){\n node.status({text: \"Caught light group; halted\"}); \n return;\n}\n\nif (override === \"on\") {\n node.status({text: \"Override on; flow halted\"}); \n return;\n}\n\n\nif (msg.topic === \"turn_on\") {\n msg.payload.data = { \"brightness_pct\": 100, kelvin: null }\n node.status({text: \"Turn on; continuing flow\"}); \n return msg;\n}\n\n\n/*\nif (msg.topic != \"turn_on\") {\n msg.payload.data = msg.payload.data || {}\n msg.payload.data.kelvin = \"\"\n node.status({text: \"Circadian; continuing flow\"}); \n return msg;\n}\n*/ \n\nif (msg.topic != \"turn_on\") {\n msg.payload.data = msg.payload.data || {}\n msg.payload.data.kelvin = \"\"\n node.status({text: \"Circadian; continuing flow\"}); \n //messages[0] = msg\n return msg;\n}\n \n\n\n/*\n\n\nif (hour >= transition_to_night) {\n delete msg.payload.data.kelvin;\n msg.payload.data.color_name = \"blue\";\n msg.payload.data.brightness_pct = 75;\n //return msg;\n messages[1] = msg\n}\n\n/*\n\nif (hour === transition_to_day && min === 5) {\n \n msg.payload.data.color_name = \"white\";\n}\n\ndelete msg.payload.data.kelvin;\n\nnode.status({text: hour}); \nreturn msg;\n//return\n\n\n\n*/\n\nreturn messages ","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":740,"y":380,"wires":[["f382913234be1df9"],[]]},{"id":"48d6c4079c320e66","type":"function","z":"9037e766.edc3f8","g":"210626d9905241cc","name":"Transition to nighttime","func":"const m = msg.payload.data;\nconst entities = global.get('homeassistant').homeAssistant.states;\nvar enabled = entities['input_boolean.automation_auto_nighttime_lights'].state\nvar color = entities['input_select.night_time_light_color_selector'].state\n\nif(enabled === \"on\") {\n delete m.kelvin;\n m.color_name = color;\n m.brightness_pct = 75;\n node.status({fill:\"green\",shape:\"dot\",text:\"Enabled - \" + color});\n return msg;\n} else {\n node.status({fill:\"red\",shape:\"dot\",text:\"Disabled\"});\n return\n}\n\n\n\n\n\n\n\n\n\n\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1160,"y":440,"wires":[["d1f82979da84c480"]]},{"id":"a043619889bec59d","type":"function","z":"9037e766.edc3f8","g":"210626d9905241cc","name":"Transition to daytime","func":"delete msg.payload.data.kelvin;\n//msg.payload.data.color_name = \"white\";\nmsg.payload.data.color_temp = \"2700\";\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1160,"y":320,"wires":[["d1f82979da84c480"]]},{"id":"5829325a.2a80bc","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":true,"heartbeatInterval":"30"},{"id":"267834d7.270114","type":"chronos-config","name":"Home","sunPositions":[]}]
Advertisement
Add Comment
Please, Sign In to add comment