[
    {
        "id": "afe325b9.9fde28",
        "type": "tab",
        "label": "Send the running total difference for the last 30 seconds to oee.ai",
        "disabled": false,
        "info": "This example sends the difference of a randomly increasing running total to oee.ai every 30 seconds."
    },
    {
        "id": "a403dbe1.dd4db8",
        "type": "inject",
        "z": "afe325b9.9fde28",
        "name": "Timestamp every 30 seconds",
        "props": [
            {
                "p": "to",
                "v": "",
                "vt": "date"
            }
        ],
        "repeat": "30",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "",
        "x": 210,
        "y": 80,
        "wires": [
            [
                "10b24cf7.a556a3"
            ]
        ],
        "info": "This node is triggered every 30 seconds and sets `msg.to` to the current timestamp."
    },
    {
        "id": "8c8ad66f.629bd8",
        "type": "oee-ai-connector",
        "z": "afe325b9.9fde28",
        "name": "Send to oee.ai",
        "server": "preview",
        "x": 1020,
        "y": 80,
        "wires": [],
        "info": "This node sends the produced units (`msg.count`) during the last 30 seconds (`msg.to - 30 seconds`) to oee.ai ."
    },
    {
        "id": "b05010ac.aae65",
        "type": "comment",
        "z": "afe325b9.9fde28",
        "name": "^^ Configure this node",
        "info": "To connect the node above to oee.ai please create a new sensor at the desired location in the oee.ai web interface at `Machine -> Sensors -> Create sensor`.\n\nCopy and paste the sensor's ID and token into the configuration input fields of the oee.ai connector node.\n\nThe selected server (`preview` for testing and development, `production` for live machine data) must match the one where the sensor was created.",
        "x": 1020,
        "y": 140,
        "wires": []
    },
    {
        "id": "a0b717a.c7627e8",
        "type": "function",
        "z": "afe325b9.9fde28",
        "name": "Difference to previous msg",
        "func": "const lastValue = context.get('lastValue');\nconst currentValue = msg.count;\n\nif (isNaN(lastValue) || isNaN(currentValue)) {\n    node.status({\n        fill: 'yellow',\n        shape: 'ring',\n        text: 'waiting for data',\n    });\n\n    context.set('lastValue', currentValue);\n\n    return;\n} else {\n    node.status({});\n\n    if (currentValue < lastValue) {\n        msg.count = currentValue;\n    } else {\n        msg.count = currentValue - lastValue;\n    }\n    context.set('lastValue', currentValue);\n\n    return msg;\n}\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 780,
        "y": 80,
        "wires": [
            [
                "8c8ad66f.629bd8"
            ]
        ],
        "info": "This node stores the last received `msg.count` value in it's context and replaces it with the difference to the current `msg.count` value.\n\nIf `msg.count` or the last stored value is not a number it does not forward the message and shows a yellow status indicating it is currently waiting for data.\n\nIf the current `msg.count` is smaller than the previous value it is returned as-is to account for a reset in the incoming running total."
    },
    {
        "id": "10b24cf7.a556a3",
        "type": "function",
        "z": "afe325b9.9fde28",
        "name": "Randomly increasing number",
        "func": "const previousValue = context.get('previousValue') || 0;\nconst currentValue = Math.floor(Math.random() * 25) + 75;\nconst nextValue = previousValue + currentValue;\n\nmsg.count = nextValue;\ncontext.set('previousValue', nextValue);\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 500,
        "y": 80,
        "wires": [
            [
                "a0b717a.c7627e8"
            ]
        ],
        "info": "This node stores the a running total in it's context and adds a randomly generated number between 75 and 100 to it when called.\n\nIt writes the new running total value to `msg.count`, simulating the actual production output of a machine.\n\nReplace this node with a node that reads the running total of produced units from the actual production machine and writes it into `msg.count`."
    }
]
