d_sellers1

Check if internet working FLOW

Nov 11th, 2021
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 12.39 KB | None | 0 0
  1. [
  2.     {
  3.         "id": "ad826ef04f4196a1",
  4.         "type": "group",
  5.         "z": "9037e766.edc3f8",
  6.         "name": "Check if internet is working",
  7.         "style": {
  8.             "label": true
  9.         },
  10.         "nodes": [
  11.             "9bb8729c.e8506",
  12.             "208efb8e.bf71c4",
  13.             "f3d50486.c7dd8",
  14.             "ab7e109d.b56d68",
  15.             "2abfb19ef0d9c15c"
  16.         ],
  17.         "x": 36,
  18.         "y": 739.5,
  19.         "w": 792,
  20.         "h": 149.5
  21.     },
  22.     {
  23.         "id": "0d235b6a3d90ffd2",
  24.         "type": "subflow",
  25.         "name": "Override",
  26.         "info": "",
  27.         "category": "",
  28.         "in": [
  29.             {
  30.                 "x": 160,
  31.                 "y": 100,
  32.                 "wires": [
  33.                     {
  34.                         "id": "06069b864a382fb3"
  35.                     }
  36.                 ]
  37.             }
  38.         ],
  39.         "out": [],
  40.         "env": [],
  41.         "meta": {},
  42.         "color": "#3FADB5",
  43.         "icon": "node-red-contrib-home-assistant-websocket/ha-entity-switch.svg"
  44.     },
  45.     {
  46.         "id": "48b611b2d5bdccc9",
  47.         "type": "api-call-service",
  48.         "z": "0d235b6a3d90ffd2",
  49.         "name": "Enable override",
  50.         "server": "5829325a.2a80bc",
  51.         "version": 3,
  52.         "debugenabled": false,
  53.         "service_domain": "input_boolean",
  54.         "service": "turn_on",
  55.         "entityId": "input_boolean.automation_override",
  56.         "data": "",
  57.         "dataType": "jsonata",
  58.         "mergecontext": "",
  59.         "mustacheAltTags": false,
  60.         "outputProperties": [],
  61.         "queue": "none",
  62.         "x": 500,
  63.         "y": 100,
  64.         "wires": [
  65.             [
  66.                 "5ef3cda4b43ed796"
  67.             ]
  68.         ]
  69.     },
  70.     {
  71.         "id": "5ef3cda4b43ed796",
  72.         "type": "delay",
  73.         "z": "0d235b6a3d90ffd2",
  74.         "name": "",
  75.         "pauseType": "delay",
  76.         "timeout": "5",
  77.         "timeoutUnits": "seconds",
  78.         "rate": "1",
  79.         "nbRateUnits": "1",
  80.         "rateUnits": "second",
  81.         "randomFirst": "1",
  82.         "randomLast": "5",
  83.         "randomUnits": "seconds",
  84.         "drop": false,
  85.         "outputs": 1,
  86.         "x": 680,
  87.         "y": 100,
  88.         "wires": [
  89.             [
  90.                 "4a2718f526f681b8"
  91.             ]
  92.         ]
  93.     },
  94.     {
  95.         "id": "4a2718f526f681b8",
  96.         "type": "api-call-service",
  97.         "z": "0d235b6a3d90ffd2",
  98.         "name": "Disable override",
  99.         "server": "5829325a.2a80bc",
  100.         "version": 3,
  101.         "debugenabled": false,
  102.         "service_domain": "input_boolean",
  103.         "service": "turn_off",
  104.         "entityId": "input_boolean.automation_override",
  105.         "data": "",
  106.         "dataType": "jsonata",
  107.         "mergecontext": "",
  108.         "mustacheAltTags": false,
  109.         "outputProperties": [],
  110.         "queue": "none",
  111.         "x": 860,
  112.         "y": 100,
  113.         "wires": [
  114.             []
  115.         ]
  116.     },
  117.     {
  118.         "id": "06069b864a382fb3",
  119.         "type": "change",
  120.         "z": "0d235b6a3d90ffd2",
  121.         "name": "Clear payload",
  122.         "rules": [
  123.             {
  124.                 "t": "set",
  125.                 "p": "payload",
  126.                 "pt": "msg",
  127.                 "to": "",
  128.                 "tot": "str"
  129.             }
  130.         ],
  131.         "action": "",
  132.         "property": "",
  133.         "from": "",
  134.         "to": "",
  135.         "reg": false,
  136.         "x": 300,
  137.         "y": 100,
  138.         "wires": [
  139.             [
  140.                 "48b611b2d5bdccc9"
  141.             ]
  142.         ]
  143.     },
  144.     {
  145.         "id": "9bb8729c.e8506",
  146.         "type": "ping",
  147.         "z": "9037e766.edc3f8",
  148.         "g": "ad826ef04f4196a1",
  149.         "protocol": "Automatic",
  150.         "mode": "timed",
  151.         "name": "Ping server",
  152.         "host": "8.8.8.8",
  153.         "timer": "15",
  154.         "inputs": 0,
  155.         "x": 132,
  156.         "y": 788,
  157.         "wires": [
  158.             [
  159.                 "f3d50486.c7dd8"
  160.             ]
  161.         ]
  162.     },
  163.     {
  164.         "id": "208efb8e.bf71c4",
  165.         "type": "api-call-service",
  166.         "z": "9037e766.edc3f8",
  167.         "g": "ad826ef04f4196a1",
  168.         "name": "Light Notification",
  169.         "server": "5829325a.2a80bc",
  170.         "version": 3,
  171.         "debugenabled": false,
  172.         "service_domain": "light",
  173.         "service": "",
  174.         "entityId": "",
  175.         "data": "",
  176.         "dataType": "json",
  177.         "mergecontext": "",
  178.         "mustacheAltTags": false,
  179.         "outputProperties": [],
  180.         "queue": "none",
  181.         "x": 712,
  182.         "y": 788,
  183.         "wires": [
  184.             []
  185.         ]
  186.     },
  187.     {
  188.         "id": "f3d50486.c7dd8",
  189.         "type": "function",
  190.         "z": "9037e766.edc3f8",
  191.         "g": "ad826ef04f4196a1",
  192.         "name": "Ping response handler",
  193.         "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",
  194.         "outputs": 3,
  195.         "noerr": 0,
  196.         "initialize": "",
  197.         "finalize": "// Code added here will be run when the\n// node is being stopped or re-deployed.\nlocal.ping_failure_count=0;",
  198.         "libs": [],
  199.         "x": 342,
  200.         "y": 788,
  201.         "wires": [
  202.             [],
  203.             [],
  204.             [
  205.                 "ab7e109d.b56d68",
  206.                 "2abfb19ef0d9c15c"
  207.             ]
  208.         ],
  209.         "outputLabels": [
  210.             "Failure",
  211.             "Operational",
  212.             "Call Service"
  213.         ]
  214.     },
  215.     {
  216.         "id": "ab7e109d.b56d68",
  217.         "type": "delay",
  218.         "z": "9037e766.edc3f8",
  219.         "g": "ad826ef04f4196a1",
  220.         "name": "",
  221.         "pauseType": "delay",
  222.         "timeout": "1",
  223.         "timeoutUnits": "seconds",
  224.         "rate": "1",
  225.         "nbRateUnits": "1",
  226.         "rateUnits": "second",
  227.         "randomFirst": "1",
  228.         "randomLast": "5",
  229.         "randomUnits": "seconds",
  230.         "drop": false,
  231.         "allowrate": false,
  232.         "outputs": 1,
  233.         "x": 542,
  234.         "y": 788,
  235.         "wires": [
  236.             [
  237.                 "208efb8e.bf71c4"
  238.             ]
  239.         ]
  240.     },
  241.     {
  242.         "id": "2abfb19ef0d9c15c",
  243.         "type": "subflow:0d235b6a3d90ffd2",
  244.         "z": "9037e766.edc3f8",
  245.         "g": "ad826ef04f4196a1",
  246.         "name": "",
  247.         "env": [],
  248.         "x": 542,
  249.         "y": 848,
  250.         "wires": []
  251.     },
  252.     {
  253.         "id": "5829325a.2a80bc",
  254.         "type": "server",
  255.         "name": "Home Assistant",
  256.         "version": 1,
  257.         "addon": true,
  258.         "rejectUnauthorizedCerts": true,
  259.         "ha_boolean": "y|yes|true|on|home|open",
  260.         "connectionDelay": true,
  261.         "cacheJson": true
  262.     }
  263. ]
Advertisement
Add Comment
Please, Sign In to add comment