Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [
- {
- "id": "ad826ef04f4196a1",
- "type": "group",
- "z": "9037e766.edc3f8",
- "name": "Check if internet is working",
- "style": {
- "label": true
- },
- "nodes": [
- "9bb8729c.e8506",
- "208efb8e.bf71c4",
- "f3d50486.c7dd8",
- "ab7e109d.b56d68",
- "2abfb19ef0d9c15c"
- ],
- "x": 36,
- "y": 739.5,
- "w": 792,
- "h": 149.5
- },
- {
- "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": 3,
- "debugenabled": false,
- "service_domain": "input_boolean",
- "service": "turn_on",
- "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": 3,
- "debugenabled": false,
- "service_domain": "input_boolean",
- "service": "turn_off",
- "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": "9bb8729c.e8506",
- "type": "ping",
- "z": "9037e766.edc3f8",
- "g": "ad826ef04f4196a1",
- "protocol": "Automatic",
- "mode": "timed",
- "name": "Ping server",
- "host": "8.8.8.8",
- "timer": "15",
- "inputs": 0,
- "x": 132,
- "y": 788,
- "wires": [
- [
- "f3d50486.c7dd8"
- ]
- ]
- },
- {
- "id": "208efb8e.bf71c4",
- "type": "api-call-service",
- "z": "9037e766.edc3f8",
- "g": "ad826ef04f4196a1",
- "name": "Light Notification",
- "server": "5829325a.2a80bc",
- "version": 3,
- "debugenabled": false,
- "service_domain": "light",
- "service": "",
- "entityId": "",
- "data": "",
- "dataType": "json",
- "mergecontext": "",
- "mustacheAltTags": false,
- "outputProperties": [],
- "queue": "none",
- "x": 712,
- "y": 788,
- "wires": [
- []
- ]
- },
- {
- "id": "f3d50486.c7dd8",
- "type": "function",
- "z": "9037e766.edc3f8",
- "g": "ad826ef04f4196a1",
- "name": "Ping response handler",
- "func": "// Description: This function is for use with Home Assistant and Node-Red. It will \n// turn on a specified light and set it to a specified color in the event of an \n// Internet outage. Upon re-connecting, the function will return the light to its \n// original state. Note that in order to operate properly, the specified light\n// must be locally controlled and not cloud controlled.\n\n// Input for this function is a Ping node. Tested with a target identified, automatic \n// protocol, timed mode, and an interval of 30 seconds. \n\n// For testing purposes, the target IP was set as a WiFi light bulb. To simulate an \n// outage, power was turned off to the light. When power was turned back on, the \n// re-connect portion would excute.\n\n// This function node requires three outputs. These outputs should allow for \n// expanded functionality outside the scope of this function node.\n// -the first output is for the failure path; payload = number of ping failures\n// -the second output is for the operational path; payload = ping response time\n// -the third output connects to an empty call service node for the notification.\n// payload sends the necessary variables in the event of an outage and re-connection.\n\n// Bug: function will sometimes throw a ReferenceError: local is not defined\n// Reason: not sure. seems random. mostly noticed when forcing scripts to restart.\n// Fix: unknown. tried a couple different ways to define local and they all did\n// the same thing. \n\n// Bug: During an outage, if the light's state or attributes are changed, the\n// function will still return it back to how it was before the outage was reported.\n// Reason: The light's state and attributes are stored prior to an outage. It does \n// not check if the state has changed during the outage.\n// Fix: None yet. Maybe checking during the outage to see if the current \n// state/attributes matches the outage state/attributes?\n\n// Note: The function does have a variable for the time of failure but it is not \n// used for anything yet. Maybe it will get added to the HA log in the future.\n\n//20211009 - Added node.status message\n\n// This is the entity that will be used for notification of an outage and \n// setting previous state.\nvar entity = \"light.hallway\";\n//This is the color the light will change to during an outage.\nvar rgb_out = [255,0,0];\n// This sets how many consecutive ping failures can occur before triggering the \n// outage notification.\nvar max_allowed_ping_failures = 3;\n\n// This function stores the current state of the entity (on/off). If the entity is \n// on, it will also stores the entity's attributes. These values will be used when \n// returning the entity to its previous state once the internet has re-connected.\nfunction set_entity_previous() {\n local.previous_state = ha.state;\n if (local.previous_state === \"on\") //Store entity's attributes if state is on\n {\n local.previous_attributes = ha.attributes;\n } else {\n local.previous_attributes = null\n }\n}\n\n// Initialize variables\nconst ha = global.get('homeassistant').homeAssistant.states[entity]\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\"};\nconst msg2 = {payload:\"\", data:\"\", topic:\"MSG2\"};\n\n// Establish variables if they do not exist and set default value\nif (typeof local !== undefined) { local = global.get('data') || {}; }\nif (local.ping_failure_count===undefined) { local.ping_failure_count = 0; }\nif (local.ping_failure_time===undefined) { local.ping_failure_time = 0; }\n\n// Establish standard payload to be passed to service call node\nmsg2.payload = {domain: \"light\", service: \"turn_on\",\n data: {\n entity_id: entity, }\n };\n\n// The Ping node will return false if it does not receive a response.\n// Otherwise, it will return a time in ms.\nif (msg.payload === false) // Ping response failed\n{\n node.status({fill:\"red\",shape:\"ring\",text:\"disconnected\"});\n local.ping_failure_count++;\n \n // Has max_allowed_ping_failures occured? Indicate outage.\n if (local.ping_failure_count === max_allowed_ping_failures) \n {\n if (local.previous_state === \"on\" || local.previous_state === \"off\") { set_entity_previous(); }\n local.ping_failure_time = Date.now(); //Creates timestamp variable in seconds; not used yet\n msg2.payload.data.rgb_color = rgb_out;\n messages[2] = msg2; // Send service call payload to output\n }\n \n msg0.payload = local.ping_failure_count; \n global.set('data',local);\n messages[0] = msg0; // Send ping failure count to output\n\n} else { // Ping response good\n node.status({fill:\"green\",shape:\"dot\",text:\"connected\"});\n // Has the internet re-connected following an outage?\n if (local.ping_failure_count >= max_allowed_ping_failures) \n {\n //Return light to its previous state \n if (local.previous_state === \"on\")\n {\n msg2.payload.data.brightness = local.previous_attributes.brightness;\n // Was light in color mode or RGB mode since both cannot be passed in the same payload? \n if (local.previous_attributes.rgb_color.toString() === '255,255,255') {\n msg2.payload.data.color_temp = local.previous_attributes.color_temp;\n } else {\n msg2.payload.data.rgb_color = local.previous_attributes.rgb_color;\n } \n } else { // Light's previous state was off\n msg2.payload.service = \"turn_off\"; \n }\n messages[2] = msg2; // Send service call payload to output\n }\n \n local.ping_failure_count = 0; // Reset/maintain ping failure count to zero\n set_entity_previous(); // Store entity's current state\n msg1.payload = msg.payload;\n global.set('data',local);\n messages[1] = msg1; // Send response time to output\n}\nreturn messages;\n",
- "outputs": 3,
- "noerr": 0,
- "initialize": "",
- "finalize": "// Code added here will be run when the\n// node is being stopped or re-deployed.\nlocal.ping_failure_count=0;",
- "libs": [],
- "x": 342,
- "y": 788,
- "wires": [
- [],
- [],
- [
- "ab7e109d.b56d68",
- "2abfb19ef0d9c15c"
- ]
- ],
- "outputLabels": [
- "Failure",
- "Operational",
- "Call Service"
- ]
- },
- {
- "id": "ab7e109d.b56d68",
- "type": "delay",
- "z": "9037e766.edc3f8",
- "g": "ad826ef04f4196a1",
- "name": "",
- "pauseType": "delay",
- "timeout": "1",
- "timeoutUnits": "seconds",
- "rate": "1",
- "nbRateUnits": "1",
- "rateUnits": "second",
- "randomFirst": "1",
- "randomLast": "5",
- "randomUnits": "seconds",
- "drop": false,
- "allowrate": false,
- "outputs": 1,
- "x": 542,
- "y": 788,
- "wires": [
- [
- "208efb8e.bf71c4"
- ]
- ]
- },
- {
- "id": "2abfb19ef0d9c15c",
- "type": "subflow:0d235b6a3d90ffd2",
- "z": "9037e766.edc3f8",
- "g": "ad826ef04f4196a1",
- "name": "",
- "env": [],
- "x": 542,
- "y": 848,
- "wires": []
- },
- {
- "id": "5829325a.2a80bc",
- "type": "server",
- "name": "Home Assistant",
- "version": 1,
- "addon": true,
- "rejectUnauthorizedCerts": true,
- "ha_boolean": "y|yes|true|on|home|open",
- "connectionDelay": true,
- "cacheJson": true
- }
- ]
Advertisement
Add Comment
Please, Sign In to add comment